@Test public void notWritablePropertyExceptionContainsAlternativeMatch() { IntelliBean target = new IntelliBean(); BeanWrapper bw = createAccessor(target); try { bw.setPropertyValue("names", "Alef"); } catch (NotWritablePropertyException ex) { assertNotNull("Possible matches not determined", ex.getPossibleMatches()); assertEquals("Invalid amount of alternatives", 1, ex.getPossibleMatches().length); } }
@Test public void notWritablePropertyExceptionContainsAlternativeMatches() { IntelliBean target = new IntelliBean(); BeanWrapper bw = createAccessor(target); try { bw.setPropertyValue("mystring", "Arjen"); } catch (NotWritablePropertyException ex) { assertNotNull("Possible matches not determined", ex.getPossibleMatches()); assertEquals("Invalid amount of alternatives", 3, ex.getPossibleMatches().length); } }
@Test public void checkNotWritablePropertyHoldPossibleMatches() { TestBean target = new TestBean(); try { BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("ag", "foobar"); fail("Should throw exception on invalid property"); } catch (NotWritablePropertyException ex) { // expected assertEquals(1, ex.getPossibleMatches().length); assertEquals("age", ex.getPossibleMatches()[0]); } }
@Test public void incompletelyQuotedKeyLeadsToPropertyException() { TestBean target = new TestBean(); try { BeanWrapper accessor = createAccessor(target); accessor.setPropertyValue("[']", "foobar"); fail("Should throw exception on invalid property"); } catch (NotWritablePropertyException ex) { assertNull(ex.getPossibleMatches()); } }
@Test public void setUnknownProperty() { Simple target = new Simple("John", 2); AbstractPropertyAccessor accessor = createAccessor(target); try { accessor.setPropertyValue("name1", "value"); fail("Should have failed to set an unknown property."); } catch (NotWritablePropertyException e) { assertEquals(Simple.class, e.getBeanClass()); assertEquals("name1", e.getPropertyName()); assertEquals("Invalid number of possible matches", 1, e.getPossibleMatches().length); assertEquals("name", e.getPossibleMatches()[0]); } }
@Test public void testPossibleMatches() { try { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("ag", "foobar"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("tb", bd); lbf.getBean("tb"); fail("Should throw exception on invalid property"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof NotWritablePropertyException); NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); // expected assertEquals(1, cause.getPossibleMatches().length); assertEquals("age", cause.getPossibleMatches()[0]); } }