1 23 24 27 28 package com.sun.enterprise.tools.common.validation.util; 29 30 import junit.framework.*; 31 import java.lang.reflect.Constructor ; 32 import java.lang.reflect.Method ; 33 34 39 40 public class UtilsTest extends TestCase{ 41 42 43 private Utils utils = null; 44 45 public UtilsTest(String name){ 46 super(name); 47 utils = new Utils(); 48 } 49 50 51 public static void main(String args[]){ 52 junit.textui.TestRunner.run(suite()); 53 } 54 55 56 public void testGetIndexedName(){ 57 String element = "element"; int index = 5; 59 assertTrue("element(5)".equals( utils.getIndexedName(element,index))); 61 assertTrue(null == utils.getIndexedName(null,index)); 62 63 } 64 65 66 public void testGetElement(){ 77 Object object = new CustomObject(); 78 assertTrue("foo".equals( (String )utils.getElement("foo", object))); assertTrue(null == utils.getElement("", object)); assertTrue(null == utils.getElement(null, object)); 82 assertTrue(null == utils.getElement("foo", null)); assertTrue(null == utils.getElement(null, null)); 85 Integer integer = (Integer ) utils.getElement("integer", object); assertTrue(5 == integer.intValue()); 87 String [] colours = 88 (String []) utils.getElements("primaryColours", object); assertTrue("green".equals(colours[1])); 91 String colour = 92 (String ) utils.getElement("primaryColours", 2, object); assertTrue("blue".equals(colour)); assertTrue(null == utils.getElement("", 2, object)); assertTrue(null == utils.getElement(null, 2, object)); 96 assertTrue(null == utils.getElement("primaryColours", 2, null)); assertTrue(null == utils.getElement("primaryColours", -1, object)); 99 assertTrue(null == utils.getElement(null, -3, null)); 100 101 assertTrue(null == utils.getElement("", object, "")); assertTrue(null == utils.getElement("primaryColours", null, "size")); integer = (Integer ) utils.getElement( 105 "primaryColours", object, ""); assertTrue(3 == integer.intValue()); 107 integer = (Integer ) utils.getElement( 108 "primaryColours", object, null); assertTrue(3 == integer.intValue()); 110 integer = (Integer ) utils.getElement( 111 "primaryColours", object, "size"); assertTrue(4 == integer.intValue()); 113 } 114 115 116 public void testMethodNameFromDtdName(){ 117 String name = "ejb-name"; String prefix = "set"; assertTrue("setEjbName".equals( utils.methodNameFromDtdName(name, prefix))); 121 } 122 123 124 public void testMethodNameFromBeanName(){ 125 String beanName = "name"; String prefix = "get"; assertTrue("getName".equals( utils.methodNameFromBeanName(beanName, prefix))); 129 130 prefix = null; 131 assertTrue("name".equals( utils.methodNameFromBeanName(beanName, prefix))); 133 134 prefix = ""; assertTrue("name".equals( utils.methodNameFromBeanName(beanName, prefix))); 137 138 beanName = ""; prefix = "set"; assertTrue("set".equals( utils.methodNameFromBeanName(beanName, prefix))); 142 143 beanName = null; 144 prefix = "set"; assertTrue(null == utils.methodNameFromBeanName(beanName, prefix)); 146 } 147 148 149 public void testEleminateHypen(){ 150 String str = "hello"; assertTrue(null == utils.eleminateHypen(null)); 152 assertTrue("".equals(utils.eleminateHypen(""))); assertTrue("hello".equals(utils.eleminateHypen(str))); str = "-hello"; assertTrue("hello".equals(utils.eleminateHypen(str))); str = "hello-"; assertTrue("hello".equals(utils.eleminateHypen(str))); str = "hello-world"; assertTrue("helloWorld".equals(utils.eleminateHypen(str))); str = "hello-whole-world"; assertTrue("helloWholeWorld".equals(utils.eleminateHypen(str))); str = "-hellO-WhOle-wOrld-"; assertTrue("hellOWhOleWOrld".equals(utils.eleminateHypen(str))); } 165 166 public void testGetMethod_One(){ 171 String str = "hello"; Method method = utils.getMethod("java.lang.String", "length"); int length = ((Integer )utils.invoke(str, method)).intValue(); 174 assertTrue(5 == length); 175 176 method = utils.getMethod(String .class, "length"); length = ((Integer )utils.invoke(str, method)).intValue(); 178 assertTrue(5 == length); 179 } 180 181 182 public void testGetMethod(){ 190 String str = "hello"; Class [] argumentClass = new Class [] {char.class, char.class}; 192 Method method = 193 utils.getMethod("java.lang.String", "replace", argumentClass); 195 Character oldChar = new Character ('h'); 196 Character newChar = new Character ('H'); 197 Object [] parameters = new Object [] { oldChar, newChar }; 198 assertTrue("Hello".equals(utils.invoke(str, method, parameters))); 200 201 method = utils.getMethod(String .class, "replace", argumentClass); 203 assertTrue("Hello".equals(utils.invoke(str, method, parameters))); 205 } 206 207 208 public void testGetClass(){ 212 assertTrue(String .class == utils.getClass("java.lang.String")); Integer integer = new Integer (1234); 214 assertTrue(Integer .class == utils.getClass(integer)); 215 } 216 217 218 public void testCreatObject_Two(){ 226 Class [] argumentTypes = new Class [] {String .class}; 227 Constructor constructor = 228 utils.getConstructor("java.lang.Integer", argumentTypes); Object [] argumentValues = new Object [] {"1234"}; Object object = utils.createObject(constructor, argumentValues); 231 Class objectClass = object.getClass(); 232 String objectName = objectClass.getName(); 233 assertTrue("java.lang.Integer".equals(objectName)); object = (Integer )object; 235 assertTrue("1234".equals(object.toString())); 237 Class [] argTypes = new Class [] {int.class}; 238 Constructor cons = utils.getConstructor(object.getClass(), argTypes); 239 Integer parameter = new Integer (4567); 240 Object [] arguments = new Object [] {parameter}; 241 Object obj = utils.createObject(cons, arguments); 242 assertTrue("java.lang.Integer".equals(objectName)); obj = (Integer )obj; 244 assertTrue("4567".equals(obj.toString())); } 246 247 248 public void testCreateObject_One(){ 250 Object object = utils.createObject("java.lang.String"); String str = "hello"; Class objectClass = str.getClass(); 253 String name = objectClass.getName(); 254 assertTrue("java.lang.String".equals(name)); } 256 257 258 public void testCreateObject(){ 260 Object object = utils.createObject("java.lang.String"); Class objectClass = object.getClass(); 262 String name = objectClass.getName(); 263 assertTrue("java.lang.String".equals(name)); } 265 266 267 public void testGetUpperCaseFirstLetter() { 268 assertTrue("Hello".equals( utils.upperCaseFirstLetter("hello"))); assertTrue("Hello".equals( utils.upperCaseFirstLetter("Hello"))); assertTrue("H".equals( utils.upperCaseFirstLetter("h"))); assertTrue(null == utils.upperCaseFirstLetter(null)); assertTrue("".equals( utils.upperCaseFirstLetter(""))); } 278 279 280 public void testGetParentName() { 281 assertTrue("/root/parent".equals( utils.getParentName("/root/parent/child", '/'))); assertTrue(null == utils.getParentName("child", '/')); assertTrue(null == utils.getParentName("/root", '/')); assertTrue(null == utils.getParentName(null, '/')); 286 } 287 288 289 public void testGetName() { 290 assertTrue("child".equals( 291 utils.getName("/root/parent/child", '/'))); assertTrue("child".equals(utils.getName("child", '/'))); assertTrue("root".equals(utils.getName("/root", '/'))); assertTrue(null == utils.getName(null, '/')); 295 } 296 297 298 301 public static Test suite(){ 302 TestSuite suite = new TestSuite(UtilsTest.class); 303 return suite; 304 } 305 306 307 310 protected void setUp() { 311 } 312 313 316 protected void tearDown() { 317 } 318 319 private void nyi() { 320 fail("Not yet implemented"); } 322 323 class CustomObject extends Object 324 { 325 String name = "foo"; Integer integer = new Integer (5); 327 String [] primaryColours = 328 new String [] {"red", "green", "blue", "yellow"}; 330 public String getFoo(){ 331 return name; 332 } 333 334 public int getInteger(){ 335 return integer.intValue(); 336 } 337 338 public String [] getPrimaryColours(){ 339 return primaryColours; 340 } 341 342 public String getPrimaryColours(int index){ 343 return primaryColours[index]; 344 } 345 346 public Integer sizePrimaryColours(){ 347 return new Integer (4); 348 } 349 350 public Integer primaryColours(){ 351 return new Integer (3); 352 } 353 } 354 } 355 | Popular Tags |