1 16 package org.apache.commons.beanutils; 17 18 import junit.framework.TestCase; 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 22 27 public class LazyDynaClassTestCase extends TestCase { 28 29 protected LazyDynaClass dynaClass = null; 30 protected String testProperty = "myProperty"; 31 32 34 39 public LazyDynaClassTestCase(String name) { 40 super(name); 41 } 42 43 45 48 public static void main(String [] args) { 49 junit.textui.TestRunner.run(suite()); 50 } 51 52 55 public void setUp() throws Exception { 56 dynaClass = new LazyDynaClass(); 57 } 58 59 62 public static Test suite() { 63 return (new TestSuite(LazyDynaClassTestCase.class)); 64 } 65 66 69 public void tearDown() { 70 dynaClass = null; 71 } 72 73 75 78 public void testAddProperty1() { 79 dynaClass.add(testProperty); 80 DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty); 81 assertEquals("name is correct", testProperty, dynaProperty.getName()); 82 assertEquals("type is correct", Object .class, dynaProperty.getType()); 83 } 84 85 88 public void testAddProperty2() { 89 dynaClass.add(testProperty, String .class); 90 DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty); 91 assertEquals("name is correct", testProperty, dynaProperty.getName()); 92 assertEquals("type is correct", String .class, dynaProperty.getType()); 93 } 94 95 98 public void testAddProperty3() { 99 try { 100 dynaClass.add(testProperty, String .class, true, true); 101 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); 102 } catch (UnsupportedOperationException expected) { 103 } 105 } 106 107 110 public void testAddPropertyNullName1() { 111 try { 112 dynaClass.add((String )null); 113 fail("null property name not prevented"); 114 } catch (IllegalArgumentException expected) { 115 } 117 } 118 119 122 public void testAddPropertyNullName2() { 123 try { 124 dynaClass.add(null, String .class); 125 fail("null property name not prevented"); 126 } catch (IllegalArgumentException expected) { 127 } 129 } 130 131 134 public void testAddPropertyNullName3() { 135 try { 136 dynaClass.add(null, String .class, true, true); 137 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); 138 } catch (UnsupportedOperationException expected) { 139 } 141 } 142 143 146 public void testAddPropertyRestricted1() { 147 dynaClass.setRestricted(true); 148 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted()); 149 try { 150 dynaClass.add(testProperty); 151 fail("add(name) did not throw IllegalStateException"); 152 } catch (IllegalStateException expected) { 153 } 155 } 156 157 160 public void testAddPropertyRestricted2() { 161 dynaClass.setRestricted(true); 162 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted()); 163 try { 164 dynaClass.add(testProperty, String .class); 165 fail("add(name, type) did not throw IllegalStateException"); 166 } catch (IllegalStateException expected) { 167 } 169 } 170 171 174 public void testAddPropertyRestricted3() { 175 dynaClass.setRestricted(true); 176 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted()); 177 try { 178 dynaClass.add(testProperty, String .class, true, true); 179 fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); 180 } catch (UnsupportedOperationException t) { 181 } 183 } 184 185 188 public void testGetPropertyDoesntExist1() { 189 dynaClass.setReturnNull(false); 190 assertFalse("returnNull is 'false'", dynaClass.isReturnNull()); 191 DynaProperty dynaProperty = dynaClass.getDynaProperty(testProperty); 192 assertEquals("name is correct", testProperty, dynaProperty.getName()); 193 assertEquals("type is correct", Object .class, dynaProperty.getType()); 194 assertFalse("property doesnt exist", dynaClass.isDynaProperty(testProperty)); 195 } 196 197 198 201 public void testGetPropertyDoesntExist2() { 202 dynaClass.setReturnNull(true); 203 assertTrue("returnNull is 'true'", dynaClass.isReturnNull()); 204 assertNull("property is null", dynaClass.getDynaProperty(testProperty)); 205 } 206 207 210 public void testRemoveProperty() { 211 dynaClass.setReturnNull(true); 212 dynaClass.add(testProperty); 213 assertTrue("Property exists", dynaClass.isDynaProperty(testProperty)); 214 assertNotNull("property is Not null", dynaClass.getDynaProperty(testProperty)); 215 dynaClass.remove(testProperty); 216 assertFalse("Property doesn't exist", dynaClass.isDynaProperty(testProperty)); 217 assertNull("property is null", dynaClass.getDynaProperty(testProperty)); 218 } 219 220 223 public void testRemovePropertyNullName() { 224 try { 225 dynaClass.remove(null); 226 fail("remove(null) did not throw IllegalArgumentException"); 227 } catch (IllegalArgumentException expected) { 228 } 230 } 231 232 235 public void testRemovePropertyRestricted() { 236 dynaClass.add(testProperty); 237 assertTrue("Property exists", dynaClass.isDynaProperty(testProperty)); 238 dynaClass.setRestricted(true); 239 assertTrue("MutableDynaClass is restricted", dynaClass.isRestricted()); 240 try { 241 dynaClass.remove(testProperty); 242 fail("remove property when MutableDynaClassis restricted did not throw IllegalStateException"); 243 } catch (IllegalStateException expected) { 244 } 246 } 247 248 251 public void testRemovePropertyDoesntExist() { 252 assertFalse("property doesn't exist", dynaClass.isDynaProperty(testProperty)); 253 dynaClass.remove(testProperty); 254 assertFalse("property still doesn't exist", dynaClass.isDynaProperty(testProperty)); 255 } 256 } | Popular Tags |