1 18 package org.apache.struts.action; 19 20 import junit.framework.Test; 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.beanutils.DynaProperty; 25 import org.apache.struts.config.FormBeanConfig; 26 import org.apache.struts.config.FormPropertyConfig; 27 import org.apache.struts.config.impl.ModuleConfigImpl; 28 29 30 34 public class TestDynaActionFormClass extends TestCase 35 { 36 41 public TestDynaActionFormClass(String theName) 42 { 43 super(theName); 44 } 45 46 51 public static void main(String [] theArgs) 52 { 53 junit.awtui.TestRunner.main 54 (new String [] {TestDynaActionFormClass.class.getName()}); 55 } 56 57 61 public static Test suite() 62 { 63 return new TestSuite(TestDynaActionFormClass.class); 65 } 66 67 68 70 71 75 protected FormBeanConfig beanConfig = null; 76 77 78 81 protected DynaActionFormClass dynaClass = null; 82 83 84 88 protected static final FormPropertyConfig[] dynaProperties = { 89 new FormPropertyConfig("booleanProperty", "boolean", "true"), 90 new FormPropertyConfig("booleanSecond", "boolean", "true"), 91 new FormPropertyConfig("doubleProperty", "double", "321.0"), 92 new FormPropertyConfig("floatProperty", "float", "123.0"), 93 new FormPropertyConfig("intArray", "int[]", 94 "{ 0, 10,20, \"30\" '40' }"), 95 new FormPropertyConfig("intIndexed", "int[]", 96 " 0 100, 200, 300, 400 "), 97 new FormPropertyConfig("intProperty", "int", "123"), 98 new FormPropertyConfig("listIndexed", "java.util.List", null), 99 new FormPropertyConfig("longProperty", "long", "321"), 100 new FormPropertyConfig("mappedProperty", "java.util.Map", null), 101 new FormPropertyConfig("mappedIntProperty", "java.util.Map", null), 102 new FormPropertyConfig("shortProperty", "short", "987"), 104 new FormPropertyConfig("stringArray", "java.lang.String[]", 105 "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"), 106 new FormPropertyConfig("stringIndexed", "java.lang.String[]", 107 "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"), 108 new FormPropertyConfig("stringProperty", "java.lang.String", 109 "This is a string"), 110 }; 111 112 113 115 116 public void setUp() { 117 118 beanConfig = new FormBeanConfig(); 120 beanConfig.setName("dynaForm"); 121 beanConfig.setType("org.apache.struts.action.DynaActionForm"); 122 123 for (int i = 0; i < dynaProperties.length; i++) { 125 beanConfig.addFormPropertyConfig(dynaProperties[i]); 126 } 127 128 ModuleConfigImpl moduleConfig = new ModuleConfigImpl(""); 130 131 dynaClass = new DynaActionFormClass(beanConfig); 133 134 } 135 136 137 public void tearDown() { 138 139 DynaActionFormClass.clear(); 140 dynaClass = null; 141 beanConfig = null; 142 DynaActionFormClass.clear(); 143 144 } 145 146 147 149 150 public void testConfigAdd() { 152 153 FormPropertyConfig prop = null; 154 155 prop = beanConfig.findFormPropertyConfig("fooProperty"); 157 assertNull("fooProperty not found", prop); 158 beanConfig.addFormPropertyConfig 159 (new FormPropertyConfig("fooProperty", "java.lang.String", "")); 160 prop = beanConfig.findFormPropertyConfig("fooProperty"); 161 assertNotNull("fooProperty found", prop); 162 163 beanConfig.freeze(); 165 prop = beanConfig.findFormPropertyConfig("barProperty"); 166 assertNull("barProperty not found", prop); 167 try { 168 beanConfig.addFormPropertyConfig 169 (new FormPropertyConfig("barProperty", "java.lang.String", "")); 170 fail("barProperty add not prevented"); 171 } catch (IllegalStateException e) { 172 ; } 174 175 } 176 177 178 public void testConfigCreate() { 180 181 assertTrue("dynamic is correct", beanConfig.getDynamic()); 182 assertEquals("name is correct", "dynaForm", beanConfig.getName()); 183 assertEquals("type is correct", 184 "org.apache.struts.action.DynaActionForm", 185 beanConfig.getType()); 186 187 } 188 189 190 public void testConfigDuplicate() { 192 193 FormPropertyConfig prop = null; 194 assertNull("booleanProperty is found", prop); 195 try { 196 beanConfig.addFormPropertyConfig 197 (new FormPropertyConfig("booleanProperty", "java.lang.String", 198 "")); 199 fail("Adding duplicate property not prevented"); 200 } catch (IllegalArgumentException e) { 201 ; } 203 204 } 205 206 207 public void testConfigInitialValues() { 209 210 assertEquals("booleanProperty value", 211 Boolean.TRUE, 212 beanConfig.findFormPropertyConfig("booleanProperty").initial()); 213 assertEquals("booleanSecond value", 214 Boolean.TRUE, 215 beanConfig.findFormPropertyConfig("booleanSecond").initial()); 216 assertEquals("doubleProperty value", 217 new Double (321.0), 218 beanConfig.findFormPropertyConfig("doubleProperty").initial()); 219 assertEquals("floatProperty value", 220 new Float ((float) 123.0), 221 beanConfig.findFormPropertyConfig("floatProperty").initial()); 222 assertEquals("intProperty value", 223 new Integer (123), 224 beanConfig.findFormPropertyConfig("intProperty").initial()); 225 assertEquals("longProperty value", 227 new Long (321), 228 beanConfig.findFormPropertyConfig("longProperty").initial()); 229 assertEquals("shortProperty value", 234 new Short ((short) 987), 235 beanConfig.findFormPropertyConfig("shortProperty").initial()); 236 assertEquals("stringProperty value", 239 "This is a string", 240 beanConfig.findFormPropertyConfig("stringProperty").initial()); 241 242 } 243 244 245 public void testConfigProperties() { 247 248 for (int i = 0; i < dynaProperties.length; i++) { 249 FormPropertyConfig dynaProperty = 250 beanConfig.findFormPropertyConfig(dynaProperties[i].getName()); 251 assertNotNull("Found dynaProperty " + 252 dynaProperties[i].getName(), dynaProperty); 253 assertEquals("dynaProperty name for " + 254 dynaProperties[i].getName(), 255 dynaProperties[i].getName(), 256 dynaProperty.getName()); 257 assertEquals("dynaProperty type for " + 258 dynaProperties[i].getName(), 259 dynaProperties[i].getType(), 260 dynaProperty.getType()); 261 assertEquals("dynaProperty initial for " + 262 dynaProperties[i].getName(), 263 dynaProperties[i].getInitial(), 264 dynaProperty.getInitial()); 265 } 266 267 } 268 269 270 public void testConfigRemove() { 272 273 FormPropertyConfig prop = null; 274 275 prop = beanConfig.findFormPropertyConfig("booleanProperty"); 277 assertNotNull("booleanProperty found", prop); 278 beanConfig.removeFormPropertyConfig(prop); 279 prop = beanConfig.findFormPropertyConfig("booleanProperty"); 280 assertNull("booleanProperty not deleted", prop); 281 282 beanConfig.freeze(); 284 prop = beanConfig.findFormPropertyConfig("booleanSecond"); 285 assertNotNull("booleanSecond found", prop); 286 try { 287 beanConfig.removeFormPropertyConfig(prop); 288 fail("booleanSecond remove not prevented"); 289 } catch (IllegalStateException e) { 290 ; } 292 293 } 294 295 296 298 299 public void testClassCreate() { 301 302 assertEquals("name", "dynaForm", dynaClass.getName()); 303 for (int i = 0; i < dynaProperties.length; i++) { 304 DynaProperty prop = 305 dynaClass.getDynaProperty(dynaProperties[i].getName()); 306 assertNotNull("Found property " + 307 dynaProperties[i].getName()); 308 assertEquals("Class for property " + 309 dynaProperties[i].getName(), 310 dynaProperties[i].getTypeClass(), 311 prop.getType()); 312 } 313 314 } 315 316 317 318 } 319 | Popular Tags |