1 19 20 package org.netbeans.modules.j2ee.sun.validation.util; 21 22 import junit.framework.*; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.Method ; 25 26 31 32 public class UtilsTest extends TestCase{ 33 34 35 private Utils utils = null; 36 37 public UtilsTest(String name){ 38 super(name); 39 utils = new Utils(); 40 } 41 42 43 public static void main(String args[]){ 44 junit.textui.TestRunner.run(suite()); 45 } 46 47 48 public void testGetIndexedName(){ 49 String element = "element"; int index = 5; 51 assertTrue("element(5)".equals( utils.getIndexedName(element,index))); 53 assertTrue(null == utils.getIndexedName(null,index)); 54 55 } 56 57 58 public void testGetElement(){ 69 Object object = new CustomObject(); 70 assertTrue("foo".equals( (String )utils.getElement("foo", object))); assertTrue(null == utils.getElement("", object)); assertTrue(null == utils.getElement(null, object)); 74 assertTrue(null == utils.getElement("foo", null)); assertTrue(null == utils.getElement(null, null)); 77 Integer integer = (Integer ) utils.getElement("integer", object); assertTrue(5 == integer.intValue()); 79 String [] colours = 80 (String []) utils.getElements("primaryColours", object); assertTrue("green".equals(colours[1])); 83 String colour = 84 (String ) utils.getElement("primaryColours", 2, object); assertTrue("blue".equals(colour)); assertTrue(null == utils.getElement("", 2, object)); assertTrue(null == utils.getElement(null, 2, object)); 88 assertTrue(null == utils.getElement("primaryColours", 2, null)); assertTrue(null == utils.getElement("primaryColours", -1, object)); 91 assertTrue(null == utils.getElement(null, -3, null)); 92 93 assertTrue(null == utils.getElement("", object, "")); assertTrue(null == utils.getElement("primaryColours", null, "size")); integer = (Integer ) utils.getElement( 97 "primaryColours", object, ""); assertTrue(3 == integer.intValue()); 99 integer = (Integer ) utils.getElement( 100 "primaryColours", object, null); assertTrue(3 == integer.intValue()); 102 integer = (Integer ) utils.getElement( 103 "primaryColours", object, "size"); assertTrue(4 == integer.intValue()); 105 } 106 107 108 public void testMethodNameFromDtdName(){ 109 String name = "ejb-name"; String prefix = "set"; assertTrue("setEjbName".equals( utils.methodNameFromDtdName(name, prefix))); 113 } 114 115 116 public void testMethodNameFromBeanName(){ 117 String beanName = "name"; String prefix = "get"; assertTrue("getName".equals( utils.methodNameFromBeanName(beanName, prefix))); 121 122 prefix = null; 123 assertTrue("name".equals( utils.methodNameFromBeanName(beanName, prefix))); 125 126 prefix = ""; assertTrue("name".equals( utils.methodNameFromBeanName(beanName, prefix))); 129 130 beanName = ""; prefix = "set"; assertTrue("set".equals( utils.methodNameFromBeanName(beanName, prefix))); 134 135 beanName = null; 136 prefix = "set"; assertTrue(null == utils.methodNameFromBeanName(beanName, prefix)); 138 } 139 140 141 public void testEleminateHypen(){ 142 String str = "hello"; assertTrue(null == utils.eleminateHypen(null)); 144 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))); } 157 158 public void testGetMethod_One(){ 163 String str = "hello"; Method method = utils.getMethod("java.lang.String", "length"); int length = ((Integer )utils.invoke(str, method)).intValue(); 166 assertTrue(5 == length); 167 168 method = utils.getMethod(String .class, "length"); length = ((Integer )utils.invoke(str, method)).intValue(); 170 assertTrue(5 == length); 171 } 172 173 174 public void testGetMethod(){ 182 String str = "hello"; Class [] argumentClass = new Class [] {char.class, char.class}; 184 Method method = 185 utils.getMethod("java.lang.String", "replace", argumentClass); 187 Character oldChar = new Character ('h'); 188 Character newChar = new Character ('H'); 189 Object [] parameters = new Object [] { oldChar, newChar }; 190 assertTrue("Hello".equals(utils.invoke(str, method, parameters))); 192 193 method = utils.getMethod(String .class, "replace", argumentClass); 195 assertTrue("Hello".equals(utils.invoke(str, method, parameters))); 197 } 198 199 200 public void testGetClass(){ 204 assertTrue(String .class == utils.getClass("java.lang.String")); Integer integer = new Integer (1234); 206 assertTrue(Integer .class == utils.getClass(integer)); 207 } 208 209 210 public void testCreatObject_Two(){ 218 Class [] argumentTypes = new Class [] {String .class}; 219 Constructor constructor = 220 utils.getConstructor("java.lang.Integer", argumentTypes); Object [] argumentValues = new Object [] {"1234"}; Object object = utils.createObject(constructor, argumentValues); 223 Class objectClass = object.getClass(); 224 String objectName = objectClass.getName(); 225 assertTrue("java.lang.Integer".equals(objectName)); object = (Integer )object; 227 assertTrue("1234".equals(object.toString())); 229 Class [] argTypes = new Class [] {int.class}; 230 Constructor cons = utils.getConstructor(object.getClass(), argTypes); 231 Integer parameter = new Integer (4567); 232 Object [] arguments = new Object [] {parameter}; 233 Object obj = utils.createObject(cons, arguments); 234 assertTrue("java.lang.Integer".equals(objectName)); obj = (Integer )obj; 236 assertTrue("4567".equals(obj.toString())); } 238 239 240 public void testCreateObject_One(){ 242 Object object = utils.createObject("java.lang.String"); String str = "hello"; Class objectClass = str.getClass(); 245 String name = objectClass.getName(); 246 assertTrue("java.lang.String".equals(name)); } 248 249 250 public void testCreateObject(){ 252 Object object = utils.createObject("java.lang.String"); Class objectClass = object.getClass(); 254 String name = objectClass.getName(); 255 assertTrue("java.lang.String".equals(name)); } 257 258 259 public void testGetUpperCaseFirstLetter() { 260 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(""))); } 270 271 272 public void testGetParentName() { 273 assertTrue("/root/parent".equals( utils.getParentName("/root/parent/child", '/'))); assertTrue(null == utils.getParentName("child", '/')); assertTrue(null == utils.getParentName("/root", '/')); assertTrue(null == utils.getParentName(null, '/')); 278 } 279 280 281 public void testGetName() { 282 assertTrue("child".equals( 283 utils.getName("/root/parent/child", '/'))); assertTrue("child".equals(utils.getName("child", '/'))); assertTrue("root".equals(utils.getName("/root", '/'))); assertTrue(null == utils.getName(null, '/')); 287 } 288 289 290 293 public static Test suite(){ 294 TestSuite suite = new TestSuite(UtilsTest.class); 295 return suite; 296 } 297 298 299 302 protected void setUp() { 303 } 304 305 308 protected void tearDown() { 309 } 310 311 private void nyi() { 312 } 314 315 class CustomObject extends Object 316 { 317 String name = "foo"; Integer integer = new Integer (5); 319 String [] primaryColours = 320 new String [] {"red", "green", "blue", "yellow"}; 322 public String getFoo(){ 323 return name; 324 } 325 326 public int getInteger(){ 327 return integer.intValue(); 328 } 329 330 public String [] getPrimaryColours(){ 331 return primaryColours; 332 } 333 334 public String getPrimaryColours(int index){ 335 return primaryColours[index]; 336 } 337 338 public Integer sizePrimaryColours(){ 339 return new Integer (4); 340 } 341 342 public Integer primaryColours(){ 343 return new Integer (3); 344 } 345 } 346 } 347 | Popular Tags |