1 7 package com.inversoft.beans.test; 8 9 10 import junit.framework.TestCase; 11 12 import com.inversoft.beans.BeanException; 13 import com.inversoft.beans.NestedBeanProperty; 14 import com.inversoft.util.typeconverter.TypeConversionException; 15 16 17 22 public class NestedBeanPropertyTest extends TestCase { 23 24 25 public NestedBeanPropertyTest(String name) { 26 super(name); 27 } 28 29 32 public void testDeepPropertyNoCreate() { 33 34 Bean1 bean = new Bean1(); 35 try { 36 NestedBeanProperty first = create("property1.property2.property3", bean.getClass()); 37 first.setStrict(true); 38 assertTrue(first.isStrict()); 39 first.setPropertyValue(bean, "foo", false); 40 fail("Should have failed because the property1 is null"); 41 } catch (BeanException be) { 42 assertTrue("Should have no root cause or target", 44 be.getCause() == null && be.getTarget() == null); 45 } catch (TypeConversionException tce) { 46 fail(tce.toString()); 47 } 48 49 try { 50 NestedBeanProperty first = create("property1.property2.property3", 51 bean.getClass(), true); 52 assertTrue(first.isStrict()); 53 first.setPropertyValue(bean, "foo", false); 54 fail("Should have failed because the property1 is null"); 55 } catch (BeanException be) { 56 assertTrue("Should have no root cause or target", 58 be.getCause() == null && be.getTarget() == null); 59 } catch (TypeConversionException tce) { 60 fail(tce.toString()); 61 } 62 } 63 64 67 public void testDeepPropertyCreate() { 68 69 Bean1 bean = new Bean1(); 70 try { 71 NestedBeanProperty first = create("property1.property2.property3", bean.getClass()); 72 assertFalse(first.isStrict()); 73 first.setPropertyValue(bean, "foo", false); 74 assertTrue("Should be completely filled in", 75 bean.getProperty1().getProperty2().getProperty3().equals("foo")); 76 assertTrue("Should be foo", first.getPropertyValue(bean).equals("foo")); 77 assertEquals(String .class, first.getPropertyType()); 78 } catch (BeanException be) { 79 fail(be.toString()); 80 } catch (TypeConversionException tce) { 81 fail(tce.toString()); 82 } 83 } 84 85 88 public void testLocalProperty() { 89 90 try { 91 Bean3 bean3 = new Bean3(); 92 NestedBeanProperty local = create("property3", bean3.getClass()); 93 local.setPropertyValue(bean3, "foo", false); 94 assertTrue("Shoold be set to foo", bean3.getProperty3().equals("foo")); 95 assertTrue("should be foo again", local.getPropertyValue(bean3).equals("foo")); 96 } catch (BeanException be) { 97 fail(be.toString()); 98 } catch (TypeConversionException tce) { 99 fail(tce.toString()); 100 } 101 } 102 103 106 public void testIndexedDeepPropertyGet() { 107 108 try { 109 Bean1 bean = new Bean1(); 110 Bean2 bean2 = new Bean2(); 111 Bean3 bean3 = new Bean3(); 112 113 bean.setIndexed(0, bean2); 114 bean2.setProperty2(bean3); 115 bean3.setProperty3("foo"); 116 NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class); 117 prop.setStrict(true); 118 String value = (String ) prop.getPropertyValue(bean); 119 120 assertTrue("Should have a value of foo", value != null && value.equals("foo")); 121 } catch (BeanException be) { 122 fail(be.toString()); 123 } 124 } 125 126 129 public void testIndexedDeepPropertyNoCreate() { 130 131 try { 132 Bean1 bean = new Bean1(); 133 NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class); 134 prop.setStrict(true); 135 prop.setPropertyValue(bean, "foo", false); 136 fail("Should have thrown an exception because the indexed property is null"); 137 } catch (BeanException be) { 138 assertTrue("Should have no root cause or target", 140 be.getCause() == null && be.getTarget() == null); 141 } catch (TypeConversionException tce) { 142 fail(tce.toString()); 143 } 144 } 145 146 149 public void testIndexedDeepPropertyCreate() { 150 151 try { 152 Bean1 bean = new Bean1(); 153 NestedBeanProperty prop = create("indexed[0].property2.property3", Bean1.class); 154 prop.setPropertyValue(bean, "foo", false); 155 assertTrue("Should be completely filled in", 156 bean.getIndexed(0).getProperty2().getProperty3().equals("foo")); 157 assertTrue("Should be foo", prop.getPropertyValue(bean).equals("foo")); 158 } catch (BeanException be) { 159 be.printStackTrace(); 160 fail("Should NOT have thrown an exception " + be.toString()); 161 } catch (TypeConversionException tce) { 162 fail(tce.toString()); 163 } 164 } 165 166 169 public void testSecondConstructor() { 170 171 try { 172 Bean1 bean = new Bean1(); 173 NestedBeanProperty prop = create("indexed[0].property2.property3", "com.inversoft.beans.test.Bean1"); 174 prop.setPropertyValue(bean, "foo", false); 175 assertTrue("Should be completely filled in", 176 bean.getIndexed(0).getProperty2().getProperty3().equals("foo")); 177 assertTrue("Should be foo", prop.getPropertyValue(bean).equals("foo")); 178 } catch (BeanException be) { 179 be.printStackTrace(); 180 fail("Should NOT have thrown an exception " + be.toString()); 181 } catch (TypeConversionException tce) { 182 fail(tce.toString()); 183 } 184 } 185 186 189 public void testAutoGenerationFailure() { 190 191 try { 192 Bean1 bean = new Bean1(); 193 NestedBeanProperty prop = create("property4.string1", Bean1.class); 194 195 prop.setPropertyValue(bean, "foo"); 196 fail("Should have failed because Bean4 doesn't have a default constructor"); 197 } catch (BeanException be) { 198 assertTrue("Should have a root cause of InstantiationException", 199 be.getCause() != null && be.getCause() instanceof InstantiationException ); 200 } 201 } 202 203 207 protected NestedBeanProperty create(String property, Class bean) 208 throws BeanException { 209 return new NestedBeanProperty(property, bean); 210 } 211 212 216 protected NestedBeanProperty create(String property, Class bean, boolean strict) 217 throws BeanException { 218 return new NestedBeanProperty(property, bean, strict); 219 } 220 221 225 protected NestedBeanProperty create(String property, String bean) 226 throws BeanException { 227 return new NestedBeanProperty(property, bean); 228 } 229 } | Popular Tags |