1 22 package org.jboss.test.util.test; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.io.File ; 27 import java.net.InetAddress ; 28 import java.net.URL ; 29 import java.text.DateFormat ; 30 import java.text.SimpleDateFormat ; 31 import java.util.Calendar ; 32 import java.util.Comparator ; 33 import java.util.Date ; 34 import java.util.Locale ; 35 import java.util.Properties ; 36 import java.util.TimeZone ; 37 38 import javax.management.ObjectName ; 39 40 import org.jboss.test.JBossTestCase; 41 import org.jboss.util.propertyeditor.DateEditor; 42 import org.jboss.util.propertyeditor.DocumentEditor; 43 import org.jboss.util.propertyeditor.ElementEditor; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 47 56 public class PropertyEditorsUnitTestCase extends JBossTestCase 57 { 58 Calendar calendar = Calendar.getInstance(); 59 60 64 static 65 { 66 Class c = org.jboss.util.propertyeditor.PropertyEditors.class; 67 } 68 69 static class StringArrayComparator implements Comparator 70 { 71 public int compare(Object o1, Object o2) 72 { 73 String [] a1 = (String []) o1; 74 String [] a2 = (String []) o2; 75 int compare = a1.length - a2.length; 76 for(int n = 0; n < a1.length; n ++) 77 compare += a1[n].compareTo(a2[n]); 78 return compare; 79 } 80 } 81 static class ClassArrayComparator implements Comparator 82 { 83 public int compare(Object o1, Object o2) 84 { 85 Class [] a1 = (Class []) o1; 86 Class [] a2 = (Class []) o2; 87 int compare = a1.length - a2.length; 88 for(int n = 0; n < a1.length; n ++) 89 { 90 int hash1 = a1[n].hashCode(); 91 int hash2 = a2[n].hashCode(); 92 compare += hash1 - hash2; 93 } 94 return compare; 95 } 96 } 97 static class IntArrayComparator implements Comparator 98 { 99 public int compare(Object o1, Object o2) 100 { 101 int[] a1 = (int[]) o1; 102 int[] a2 = (int[]) o2; 103 int compare = a1.length - a2.length; 104 for(int n = 0; n < a1.length; n ++) 105 compare += a1[n] - a2[n]; 106 return compare; 107 } 108 } 109 110 public PropertyEditorsUnitTestCase(String name) 111 { 112 super(name); 113 } 114 115 public void testEditorSearchPath() 116 throws Exception 117 { 118 getLog().debug("+++ testEditorSearchPath"); 119 String [] searchPath = PropertyEditorManager.getEditorSearchPath(); 120 boolean foundJBossPath = false; 121 for(int p = 0; p < searchPath.length; p ++) 122 { 123 String path = searchPath[p]; 124 getLog().debug("path["+p+"]="+path); 125 foundJBossPath |= path.equals("org.jboss.util.propertyeditor"); 126 } 127 assertTrue("Found org.jboss.util.propertyeditor in search path", foundJBossPath); 128 } 129 130 134 public void testJavaLangEditors() 135 throws Exception 136 { 137 getLog().debug("+++ testJavaLangEditors"); 138 Class [] types = { 140 Boolean .class, 141 Byte .class, 142 Short .class, 143 Integer .class, 144 Long .class, 145 Float .class, 146 Double .class, 147 Byte .class, 148 Character .class, 149 }; 150 String [][] inputData = { 152 {"true", "false", "TRUE", "FALSE", "tRuE", "FaLsE", null}, 153 {"1", "-1", "0", "0x1A"}, 154 {"1", "-1", "0", "0xA0"}, 155 {"1", "-1", "0", "0xA0"}, 156 {"1", "-1", "0", "1000"}, 157 {"1", "-1", "0", "1000.1"}, 158 {"1", "-1", "0", "1000.1"}, 159 {"0x1", "-#1", "0"}, 160 {"A", "a", "Z", "z"}, 161 }; 162 Object [][] expectedData = { 164 {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, null}, 165 {Byte.valueOf("1"), Byte.valueOf("-1"), Byte.valueOf("0"), Byte.decode("0x1A")}, 166 {Short.valueOf("1"), Short.valueOf("-1"), Short.valueOf("0"), Short.decode("0xA0")}, 167 {Integer.valueOf("1"), Integer.valueOf("-1"), Integer.valueOf("0"), Integer.decode("0xA0")}, 168 {Long.valueOf("1"), Long.valueOf("-1"), Long.valueOf("0"), Long.valueOf("1000")}, 169 {Float.valueOf("1"), Float.valueOf("-1"), Float.valueOf("0"), Float.valueOf("1000.1")}, 170 {Double.valueOf("1"), Double.valueOf("-1"), Double.valueOf("0"), Double.valueOf("1000.1")}, 171 {Byte.valueOf("1"), Byte.valueOf("-1"), Byte.valueOf("0")}, 172 {new Character ('A'), new Character ('a'), new Character ('Z'), new Character ('z')}, 173 }; 174 String [][] expectedStringData = { 176 {"true", "false", "true", "false", "true", "false", "null"}, 177 {"1", "-1", "0", "26"}, 178 {"1", "-1", "0", "160"}, 179 {"1", "-1", "0", "160"}, 180 {"1", "-1", "0", "1000"}, 181 {"1.0", "-1.0", "0.0", "1000.1"}, 182 {"1.0", "-1.0", "0.0", "1000.1"}, 183 {"1", "-1", "0"}, 184 {"A", "a", "Z", "z"}, 185 }; 186 Comparator [] comparators = new Comparator [types.length]; 187 188 doTests(types, inputData, expectedData, expectedStringData, comparators); 189 } 190 191 193 public void testJBossEditors() 194 throws Exception 195 { 196 getLog().debug("+++ testJBossEditors"); 197 Class [] types = { 198 javax.management.ObjectName .class, 199 java.util.Properties .class, 200 java.io.File .class, 201 java.net.URL .class, 202 java.lang.String .class, 203 java.lang.Class .class, 204 InetAddress .class, 205 String [].class, 206 Class [].class, 207 int[].class, 208 Date .class 209 }; 210 String [][] inputData = { 212 {"jboss.test:test=1"}, 214 {"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"}, 216 {"/tmp/test1", "/tmp/subdir/../test2"}, 218 {"http://www.jboss.org"}, 220 {"JBoss, Home of Professional Open Source"}, 222 {"java.util.Arrays"}, 224 {"127.0.0.1", "localhost"}, 226 {"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,\\,v"}, 228 {"java.lang.Integer,java.lang.Float"}, 230 {"0,#123,-123"}, 232 {"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"} 234 }; 235 calendar.set(2005, 0, 4, 0, 0, 0); 237 calendar.set(Calendar.MILLISECOND, 0); 238 Date date1 = calendar.getTime(); 239 calendar.setTimeZone(TimeZone.getTimeZone("PST")); 240 calendar.set(2005, 0, 4, 23, 38, 21); 241 Date date2 = calendar.getTime(); 242 calendar.set(2005, 0, 4, 23, 38, 48); 243 Date date3 = calendar.getTime(); 244 Properties props = new Properties (); 245 props.setProperty("prop1", "value1"); 246 props.setProperty("prop2", "value2"); 247 props.setProperty("prop3", "value3"); 248 props.setProperty("prop32", "value3"); 249 props.setProperty("prop4", System.getProperty("user.home")); 250 System.setProperty("some.win32.path", "C:\\disk1\\root\\"); 251 props.setProperty("prop5", "C:\\disk1\\root\\"); 252 Object [][] expectedData = { 253 {new ObjectName ("jboss.test:test=1")}, 254 {props}, 255 {new File ("/tmp/test1").getCanonicalFile(), new File ("/tmp/test2").getCanonicalFile()}, 256 {new URL ("http://www.jboss.org")}, 257 {new String ("JBoss, Home of Professional Open Source")}, 258 {java.util.Arrays .class}, 259 {InetAddress.getByName("127.0.0.1"), InetAddress.getByName("localhost")}, 260 {new String []{"1", "2", "3"}, new String [] {"a", "b", "c"}, 261 new String []{}, new String []{"#","%",",",".","_$", ",v"}}, 262 {new Class []{Integer .class, Float .class}}, 263 {new int[]{0, 0x123, -123}}, 264 {date1, date2, date3} 265 }; 266 String [][] expectedStringData = { 268 {"jboss.test:test=1"}, 270 {"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"}, 272 {"/tmp/test1", "/tmp/subdir/../test2"}, 274 {"http://www.jboss.org"}, 276 {"JBoss, Home of Professional Open Source"}, 278 {"java.util.Arrays"}, 280 {"127.0.0.1", "localhost"}, 282 {"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,,v"}, 284 {"java.lang.Integer,java.lang.Float"}, 286 {"0,291,-123"}, 288 {"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"} 290 }; 291 Comparator [] comparators = { 293 null, null, null, null, null, null, null, new StringArrayComparator(), new ClassArrayComparator(), new IntArrayComparator(), null }; 305 306 doTests(types, inputData, expectedData, expectedStringData, comparators); 307 } 308 309 public void testDateEditor() throws Exception 310 { 311 getLog().debug("+++ testDateEditor"); 312 313 Locale locale = Locale.getDefault(); 314 315 try 316 { 317 getLog().debug("Current Locale: " + Locale.getDefault()); 319 320 String text = "Fri, 25 Jun 1971 00:30:00 +0200"; 322 DateFormat format = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss Z"); 323 Date date = format.parse(text); 324 325 PropertyEditor editor = new DateEditor(); 326 editor.setAsText(text); 327 getLog().debug("setAsText('" + text + "') --> getValue() = '" + editor.getValue() + "'"); 328 assertTrue("Compare date1: " + date + ", date2: " + editor.getValue(), 329 date.compareTo((Date )editor.getValue()) == 0); 330 331 editor.setValue(date); 332 getLog().debug("setValue('" + date + "') --> getAsText() - '" + editor.getAsText() + "'"); 333 Date date2 = format.parse(editor.getAsText()); 334 assertTrue("Compare date1: " + date + ", date2: " + date2, 335 date.compareTo(date2) == 0); 336 337 Locale.setDefault(Locale.FRENCH); 339 getLog().debug("Current Locale: " + Locale.getDefault()); 340 DateEditor.initialize(); 341 342 text = "ven., 25 juin 1971 00:30:00 +0200"; 344 format = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss Z"); 345 date = format.parse(text); 346 347 editor = new DateEditor(); 348 editor.setAsText(text); 349 getLog().debug("setAsText('" + text + "') --> getValue() = '" + editor.getValue() + "'"); 350 assertTrue("Compare date1: " + date + ", date2: " + editor.getValue(), 351 date.compareTo((Date )editor.getValue()) == 0); 352 353 editor.setValue(date); 354 getLog().debug("setValue('" + date + "') --> getAsText() = '" + editor.getAsText() + "'"); 355 date2 = format.parse(editor.getAsText()); 356 assertTrue("Compare date1: " + date + ", date2: " + date2, 357 date.compareTo(date2) == 0); 358 } 359 finally 360 { 361 Locale.setDefault(locale); 363 DateEditor.initialize(); 364 } 365 } 366 367 370 public void testDocumentElementEditors() 371 { 372 getLog().debug("+++ testDocumentElementEditors"); 373 DocumentEditor de = new DocumentEditor(); 374 String s = "<!-- header comment --><doc name='whatever'/><!-- footer comment -->"; 376 getLog().debug("setAsText '" + s + "'"); 377 de.setAsText(s); 378 getLog().debug("Parsed XML document:"); 379 log((Node )de.getValue(), " "); 380 getLog().debug("getAsText '" + de.getAsText() + "'"); 381 assertTrue("Document :\n" + de.getAsText(), de.getAsText().trim().endsWith(s)); 382 assertTrue(de.getValue() instanceof org.w3c.dom.Document ); 383 s = "<element>\n\n<e2/> testing\n\n</element>"; 385 de.setAsText(s); 386 assertTrue("Document :\n" + de.getAsText() + "\nvs\n" + s, de.getAsText().trim().endsWith(s)); 387 388 ElementEditor ee = new ElementEditor(); 389 s = "<element>text</element>"; 390 ee.setAsText(s); 391 assertEquals(s, ee.getAsText()); 392 assertTrue(ee.getValue() instanceof org.w3c.dom.Element ); 393 } 394 395 private void doTests(Class [] types, String [][] inputData, Object [][] expectedData, 396 String [][] expectedStringData, Comparator [] comparators) 397 { 398 for(int t = 0; t < types.length; t ++) 399 { 400 Class type = types[t]; 401 getLog().debug("Checking property editor for: "+type); 402 PropertyEditor editor = PropertyEditorManager.findEditor(type); 403 assertTrue("Found property editor for: "+type, editor != null); 404 getLog().debug("Found property editor for: "+type+", editor="+editor.getClass().getName()); 405 assertTrue("inputData.length == expectedData.length", inputData[t].length == expectedData[t].length); 406 for(int i = 0; i < inputData[t].length; i ++) 407 { 408 String input = inputData[t][i]; 409 editor.setAsText(input); 410 Object expected = expectedData[t][i]; 411 Object output = editor.getValue(); 412 Comparator c = comparators[t]; 413 boolean equals = false; 414 if (c == null) 415 { 416 equals = output != null ? output.equals(expected) : expected == null; 417 } 418 else 419 { 420 equals = c.compare(output, expected) == 0; 421 } 422 assertTrue("Transform of "+input+" equals "+expected+", output="+output, equals); 423 424 String expectedStringOutput = expectedStringData[t][i]; 425 String stringOutput = editor.getAsText(); 426 getLog().debug("setAsText '" + logString(input) + "'"); 427 getLog().debug("getAsText '" + logString(stringOutput) + "'"); 428 429 boolean stringOutputEquals = stringOutput != null ? 430 stringOutput.equals(expectedStringOutput) : expectedStringOutput == null; 431 assertTrue("PropertyEditor: " + editor.getClass().getName() + ", getAsText() == expectedStringOutput '" + 432 logString(expectedStringOutput) + "'", stringOutputEquals); 433 } 434 } 435 } 436 437 440 private void log(Node node, String indent) 441 { 442 String name = node.getNodeName(); 443 String value = node.getNodeValue(); 444 getLog().debug(indent + "Name=" + name + ", Value=" + value); 445 NodeList list = node.getChildNodes(); 446 for (int i = 0; i < list.getLength(); i++) 447 log(list.item(i), indent + indent); 448 } 449 450 private static String logString(String s) 451 { 452 return s != null ? s : "<null>"; 453 } 454 } 455 | Popular Tags |