PageRenderTime 20ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jboss-5.1.0/testsuite/src/main/org/jboss/test/util/test/PropertyEditorsUnitTestCase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 473 lines | 354 code | 32 blank | 87 comment | 21 complexity | 6f3b1d4b8914d2445d3e887836eac266 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2006, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.test.util.test;
  23. import java.beans.PropertyEditor;
  24. import java.beans.PropertyEditorManager;
  25. import java.io.File;
  26. import java.net.InetAddress;
  27. import java.net.URL;
  28. import java.text.DateFormat;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Calendar;
  31. import java.util.Comparator;
  32. import java.util.Date;
  33. import java.util.Locale;
  34. import java.util.Properties;
  35. import java.util.TimeZone;
  36. import javax.management.ObjectName;
  37. import junit.framework.Test;
  38. import junit.framework.TestSuite;
  39. import org.jboss.test.JBossTestCase;
  40. import org.jboss.util.propertyeditor.DateEditor;
  41. import org.jboss.util.propertyeditor.DocumentEditor;
  42. import org.jboss.util.propertyeditor.ElementEditor;
  43. import org.w3c.dom.Node;
  44. import org.w3c.dom.NodeList;
  45. /**
  46. * Unit tests for the custom JBoss property editors
  47. *
  48. * @see org.jboss.util.propertyeditor.PropertyEditors
  49. *
  50. * @author Scott.Stark@jboss.org
  51. * @author Dimitris.Andreadis@jboss.org
  52. * @version $Revision: 59598 $
  53. */
  54. public class PropertyEditorsUnitTestCase extends JBossTestCase
  55. {
  56. Calendar calendar = Calendar.getInstance();
  57. /** Augment the PropertyEditorManager search path to incorporate the JBoss
  58. specific editors. This simply references the PropertyEditors.class to
  59. invoke its static initialization block.
  60. */
  61. static
  62. {
  63. // Class c = org.jboss.util.propertyeditor.PropertyEditors.class;
  64. // JBAS-3617 - on linux the above initializer doesn't work!
  65. org.jboss.util.propertyeditor.PropertyEditors.init();
  66. }
  67. static class StringArrayComparator implements Comparator
  68. {
  69. public int compare(Object o1, Object o2)
  70. {
  71. String[] a1 = (String[]) o1;
  72. String[] a2 = (String[]) o2;
  73. int compare = a1.length - a2.length;
  74. for(int n = 0; n < a1.length; n ++)
  75. compare += a1[n].compareTo(a2[n]);
  76. return compare;
  77. }
  78. }
  79. static class ClassArrayComparator implements Comparator
  80. {
  81. public int compare(Object o1, Object o2)
  82. {
  83. Class[] a1 = (Class[]) o1;
  84. Class[] a2 = (Class[]) o2;
  85. int compare = a1.length - a2.length;
  86. for(int n = 0; n < a1.length; n ++)
  87. {
  88. int hash1 = a1[n].hashCode();
  89. int hash2 = a2[n].hashCode();
  90. compare += hash1 - hash2;
  91. }
  92. return compare;
  93. }
  94. }
  95. static class IntArrayComparator implements Comparator
  96. {
  97. public int compare(Object o1, Object o2)
  98. {
  99. int[] a1 = (int[]) o1;
  100. int[] a2 = (int[]) o2;
  101. int compare = a1.length - a2.length;
  102. for(int n = 0; n < a1.length; n ++)
  103. compare += a1[n] - a2[n];
  104. return compare;
  105. }
  106. }
  107. public static Test suite() throws Exception
  108. {
  109. // JBAS-3617 - the execution order of tests in this test case is important
  110. // so it must be defined explicitly when running under some JVMs
  111. TestSuite suite = new TestSuite();
  112. suite.addTest(new PropertyEditorsUnitTestCase("testEditorSearchPath"));
  113. suite.addTest(new PropertyEditorsUnitTestCase("testJavaLangEditors"));
  114. suite.addTest(new PropertyEditorsUnitTestCase("testJBossEditors"));
  115. suite.addTest(new PropertyEditorsUnitTestCase("testDateEditor"));
  116. suite.addTest(new PropertyEditorsUnitTestCase("testDocumentElementEditors"));
  117. return suite;
  118. }
  119. public PropertyEditorsUnitTestCase(String name)
  120. {
  121. super(name);
  122. }
  123. public void testEditorSearchPath()
  124. throws Exception
  125. {
  126. getLog().debug("+++ testEditorSearchPath");
  127. String[] searchPath = PropertyEditorManager.getEditorSearchPath();
  128. boolean foundJBossPath = false;
  129. for(int p = 0; p < searchPath.length; p ++)
  130. {
  131. String path = searchPath[p];
  132. getLog().debug("path["+p+"]="+path);
  133. foundJBossPath |= path.equals("org.jboss.util.propertyeditor");
  134. }
  135. assertTrue("Found org.jboss.util.propertyeditor in search path", foundJBossPath);
  136. }
  137. /** The mechanism for mapping java.lang.* variants of the primative types
  138. misses editors for java.lang.Boolean and java.lang.Integer. Here we test
  139. the java.lang.* variants we expect editors for.
  140. **/
  141. public void testJavaLangEditors()
  142. throws Exception
  143. {
  144. getLog().debug("+++ testJavaLangEditors");
  145. // The supported java.lang.* types
  146. Class[] types = {
  147. Boolean.class,
  148. Byte.class,
  149. Short.class,
  150. Integer.class,
  151. Long.class,
  152. Float.class,
  153. Double.class,
  154. Byte.class,
  155. Character.class,
  156. };
  157. // The input string data for each type
  158. String[][] inputData = {
  159. {"true", "false", "TRUE", "FALSE", "tRuE", "FaLsE", null},
  160. {"1", "-1", "0", "0x1A"},
  161. {"1", "-1", "0", "0xA0"},
  162. {"1", "-1", "0", "0xA0"},
  163. {"1", "-1", "0", "1000"},
  164. {"1", "-1", "0", "1000.1"},
  165. {"1", "-1", "0", "1000.1"},
  166. {"0x1", "-#1", "0"},
  167. {"A", "a", "Z", "z"},
  168. };
  169. // The expected java.lang.* instance for each inputData value
  170. Object[][] expectedData = {
  171. {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, null},
  172. {Byte.valueOf("1"), Byte.valueOf("-1"), Byte.valueOf("0"), Byte.decode("0x1A")},
  173. {Short.valueOf("1"), Short.valueOf("-1"), Short.valueOf("0"), Short.decode("0xA0")},
  174. {Integer.valueOf("1"), Integer.valueOf("-1"), Integer.valueOf("0"), Integer.decode("0xA0")},
  175. {Long.valueOf("1"), Long.valueOf("-1"), Long.valueOf("0"), Long.valueOf("1000")},
  176. {Float.valueOf("1"), Float.valueOf("-1"), Float.valueOf("0"), Float.valueOf("1000.1")},
  177. {Double.valueOf("1"), Double.valueOf("-1"), Double.valueOf("0"), Double.valueOf("1000.1")},
  178. {Byte.valueOf("1"), Byte.valueOf("-1"), Byte.valueOf("0")},
  179. {new Character('A'), new Character('a'), new Character('Z'), new Character('z')},
  180. };
  181. // The expected string output from getAsText()
  182. String[][] expectedStringData = {
  183. {"true", "false", "true", "false", "true", "false", "null"},
  184. {"1", "-1", "0", "26"},
  185. {"1", "-1", "0", "160"},
  186. {"1", "-1", "0", "160"},
  187. {"1", "-1", "0", "1000"},
  188. {"1.0", "-1.0", "0.0", "1000.1"},
  189. {"1.0", "-1.0", "0.0", "1000.1"},
  190. {"1", "-1", "0"},
  191. {"A", "a", "Z", "z"},
  192. };
  193. Comparator[] comparators = new Comparator[types.length];
  194. doTests(types, inputData, expectedData, expectedStringData, comparators);
  195. }
  196. /** Test custom JBoss property editors.
  197. **/
  198. public void testJBossEditors()
  199. throws Exception
  200. {
  201. getLog().debug("+++ testJBossEditors");
  202. Class[] types = {
  203. javax.management.ObjectName.class,
  204. java.util.Properties.class,
  205. java.io.File.class,
  206. java.net.URL.class,
  207. java.lang.String.class,
  208. java.lang.Class.class,
  209. InetAddress.class,
  210. String[].class,
  211. Class[].class,
  212. int[].class,
  213. Date.class
  214. };
  215. // The input string data for each type
  216. String[][] inputData = {
  217. // javax.management.ObjectName.class
  218. {"jboss.test:test=1"},
  219. // java.util.Properties.class
  220. {"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"},
  221. // java.io.File.class
  222. {"/tmp/test1", "/tmp/subdir/../test2"},
  223. // java.net.URL.class
  224. {"http://www.jboss.org"},
  225. // java.lang.String.class
  226. {"JBoss, Home of Professional Open Source"},
  227. // java.lang.Class.class
  228. {"java.util.Arrays"},
  229. // InetAddress.class, localhost must be defined for this to work
  230. {"127.0.0.1", "localhost"},
  231. // String[].class
  232. {"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,\\,v"},
  233. // Class[].class
  234. {"java.lang.Integer,java.lang.Float"},
  235. // int[].class
  236. {"0,#123,-123"},
  237. // Date.class
  238. {"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"}
  239. };
  240. // The expected instance for each inputData value
  241. calendar.set(2005, 0, 4, 0, 0, 0);
  242. calendar.set(Calendar.MILLISECOND, 0);
  243. Date date1 = calendar.getTime();
  244. calendar.setTimeZone(TimeZone.getTimeZone("PST"));
  245. calendar.set(2005, 0, 4, 23, 38, 21);
  246. Date date2 = calendar.getTime();
  247. calendar.set(2005, 0, 4, 23, 38, 48);
  248. Date date3 = calendar.getTime();
  249. Properties props = new Properties();
  250. props.setProperty("prop1", "value1");
  251. props.setProperty("prop2", "value2");
  252. props.setProperty("prop3", "value3");
  253. props.setProperty("prop32", "value3");
  254. props.setProperty("prop4", System.getProperty("user.home"));
  255. System.setProperty("some.win32.path", "C:\\disk1\\root\\");
  256. props.setProperty("prop5", "C:\\disk1\\root\\");
  257. Object[][] expectedData = {
  258. {new ObjectName("jboss.test:test=1")},
  259. {props},
  260. {new File("/tmp/test1").getCanonicalFile(), new File("/tmp/test2").getCanonicalFile()},
  261. {new URL("http://www.jboss.org")},
  262. {new String("JBoss, Home of Professional Open Source")},
  263. {java.util.Arrays.class},
  264. {InetAddress.getByName("127.0.0.1"), InetAddress.getByName("localhost")},
  265. {new String[]{"1", "2", "3"}, new String[] {"a", "b", "c"},
  266. new String[]{}, new String[]{"#","%",",",".","_$", ",v"}},
  267. {new Class[]{Integer.class, Float.class}},
  268. {new int[]{0, 0x123, -123}},
  269. {date1, date2, date3}
  270. };
  271. // The expected string output from getAsText()
  272. String[][] expectedStringData = {
  273. // javax.management.ObjectName.class
  274. {"jboss.test:test=1"},
  275. // java.util.Properties.class
  276. {"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"},
  277. // java.io.File.class
  278. {"/tmp/test1", "/tmp/subdir/../test2"},
  279. // java.net.URL.class
  280. {"http://www.jboss.org"},
  281. // java.lang.String.class
  282. {"JBoss, Home of Professional Open Source"},
  283. // java.lang.Class.class
  284. {"java.util.Arrays"},
  285. // InetAddress.class, localhost must be defined for this to work
  286. {"127.0.0.1", "localhost"},
  287. // String[].class
  288. {"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,,v"},
  289. // Class[].class
  290. {"java.lang.Integer,java.lang.Float"},
  291. // int[].class
  292. {"0,291,-123"},
  293. // Date.class
  294. {"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"}
  295. };
  296. // The Comparator for non-trival types
  297. Comparator[] comparators = {
  298. null, // ObjectName
  299. null, // Properties
  300. null, // File
  301. null, // URL
  302. null, // String
  303. null, // Class
  304. null, // InetAddress
  305. new StringArrayComparator(), // String[]
  306. new ClassArrayComparator(), // Class[]
  307. new IntArrayComparator(), // int[]
  308. null // Date
  309. };
  310. doTests(types, inputData, expectedData, expectedStringData, comparators);
  311. }
  312. public void testDateEditor() throws Exception
  313. {
  314. getLog().debug("+++ testDateEditor");
  315. Locale locale = Locale.getDefault();
  316. try
  317. {
  318. // Use the default locale
  319. getLog().debug("Current Locale: " + Locale.getDefault());
  320. // An important date
  321. 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. PropertyEditor editor = new DateEditor();
  325. editor.setAsText(text);
  326. getLog().debug("setAsText('" + text + "') --> getValue() = '" + editor.getValue() + "'");
  327. assertTrue("Compare date1: " + date + ", date2: " + editor.getValue(),
  328. date.compareTo((Date)editor.getValue()) == 0);
  329. editor.setValue(date);
  330. getLog().debug("setValue('" + date + "') --> getAsText() - '" + editor.getAsText() + "'");
  331. Date date2 = format.parse(editor.getAsText());
  332. assertTrue("Compare date1: " + date + ", date2: " + date2,
  333. date.compareTo(date2) == 0);
  334. // Try in French
  335. Locale.setDefault(Locale.FRENCH);
  336. getLog().debug("Current Locale: " + Locale.getDefault());
  337. DateEditor.initialize();
  338. // An important date
  339. text = "ven., 25 juin 1971 00:30:00 +0200";
  340. format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
  341. date = format.parse(text);
  342. editor = new DateEditor();
  343. editor.setAsText(text);
  344. getLog().debug("setAsText('" + text + "') --> getValue() = '" + editor.getValue() + "'");
  345. assertTrue("Compare date1: " + date + ", date2: " + editor.getValue(),
  346. date.compareTo((Date)editor.getValue()) == 0);
  347. editor.setValue(date);
  348. getLog().debug("setValue('" + date + "') --> getAsText() = '" + editor.getAsText() + "'");
  349. date2 = format.parse(editor.getAsText());
  350. assertTrue("Compare date1: " + date + ", date2: " + date2,
  351. date.compareTo(date2) == 0);
  352. }
  353. finally
  354. {
  355. // reset locale
  356. Locale.setDefault(locale);
  357. DateEditor.initialize();
  358. }
  359. }
  360. /**
  361. * Tests the DOM Document and Element editors.
  362. */
  363. public void testDocumentElementEditors()
  364. {
  365. getLog().debug("+++ testDocumentElementEditors");
  366. DocumentEditor de = new DocumentEditor();
  367. // Comments can appear outside of a document
  368. String s = "<!-- header comment --><doc name='whatever'/><!-- footer comment -->";
  369. getLog().debug("setAsText '" + s + "'");
  370. de.setAsText(s);
  371. getLog().debug("Parsed XML document:");
  372. log((Node)de.getValue(), " ");
  373. getLog().debug("getAsText '" + de.getAsText() + "'");
  374. assertTrue("Document :\n" + de.getAsText(), de.getAsText().trim().endsWith(s));
  375. assertTrue(de.getValue() instanceof org.w3c.dom.Document);
  376. // Test whitespace preservation
  377. s = "<element>\n\n<e2/> testing\n\n</element>";
  378. de.setAsText(s);
  379. assertTrue("Document :\n" + de.getAsText() + "\nvs\n" + s, de.getAsText().trim().endsWith(s));
  380. ElementEditor ee = new ElementEditor();
  381. s = "<element>text</element>";
  382. ee.setAsText(s);
  383. assertEquals(s, ee.getAsText());
  384. assertTrue(ee.getValue() instanceof org.w3c.dom.Element);
  385. }
  386. private void doTests(Class[] types, String[][] inputData, Object[][] expectedData,
  387. String[][] expectedStringData, Comparator[] comparators)
  388. {
  389. for(int t = 0; t < types.length; t ++)
  390. {
  391. Class type = types[t];
  392. getLog().debug("Checking property editor for: "+type);
  393. PropertyEditor editor = PropertyEditorManager.findEditor(type);
  394. assertTrue("Found property editor for: "+type, editor != null);
  395. getLog().debug("Found property editor for: "+type+", editor="+editor.getClass().getName());
  396. assertTrue("inputData.length == expectedData.length", inputData[t].length == expectedData[t].length);
  397. for(int i = 0; i < inputData[t].length; i ++)
  398. {
  399. String input = inputData[t][i];
  400. editor.setAsText(input);
  401. Object expected = expectedData[t][i];
  402. Object output = editor.getValue();
  403. Comparator c = comparators[t];
  404. boolean equals = false;
  405. if (c == null)
  406. {
  407. equals = output != null ? output.equals(expected) : expected == null;
  408. }
  409. else
  410. {
  411. equals = c.compare(output, expected) == 0;
  412. }
  413. assertTrue("Transform of "+input+" equals "+expected+", output="+output, equals);
  414. String expectedStringOutput = expectedStringData[t][i];
  415. String stringOutput = editor.getAsText();
  416. getLog().debug("setAsText '" + logString(input) + "'");
  417. getLog().debug("getAsText '" + logString(stringOutput) + "'");
  418. boolean stringOutputEquals = stringOutput != null ?
  419. stringOutput.equals(expectedStringOutput) : expectedStringOutput == null;
  420. assertTrue("PropertyEditor: " + editor.getClass().getName() + ", getAsText() == expectedStringOutput '" +
  421. logString(expectedStringOutput) + "'", stringOutputEquals);
  422. }
  423. }
  424. }
  425. /**
  426. * Log a Node hierarchy
  427. */
  428. private void log(Node node, String indent)
  429. {
  430. String name = node.getNodeName();
  431. String value = node.getNodeValue();
  432. getLog().debug(indent + "Name=" + name + ", Value=" + value);
  433. NodeList list = node.getChildNodes();
  434. for (int i = 0; i < list.getLength(); i++)
  435. log(list.item(i), indent + indent);
  436. }
  437. private static String logString(String s)
  438. {
  439. return s != null ? s : "<null>";
  440. }
  441. }