KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > test > PropertyEditorsUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * 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
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26 import java.io.File JavaDoc;
27 import java.net.InetAddress JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Calendar JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.TimeZone JavaDoc;
37
38 import javax.management.ObjectName JavaDoc;
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 JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46
47 /**
48  * Unit tests for the custom JBoss property editors
49  *
50  * @see org.jboss.util.propertyeditor.PropertyEditors
51  *
52  * @author Scott.Stark@jboss.org
53  * @author Dimitris.Andreadis@jboss.org
54  * @version $Revision: 43534 $
55  */

56 public class PropertyEditorsUnitTestCase extends JBossTestCase
57 {
58    Calendar JavaDoc calendar = Calendar.getInstance();
59    
60    /** Augment the PropertyEditorManager search path to incorporate the JBoss
61     specific editors. This simply references the PropertyEditors.class to
62     invoke its static initialization block.
63     */

64    static
65    {
66       Class JavaDoc c = org.jboss.util.propertyeditor.PropertyEditors.class;
67    }
68
69    static class StringArrayComparator implements Comparator JavaDoc
70    {
71       public int compare(Object JavaDoc o1, Object JavaDoc o2)
72       {
73          String JavaDoc[] a1 = (String JavaDoc[]) o1;
74          String JavaDoc[] a2 = (String JavaDoc[]) 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 JavaDoc
82    {
83       public int compare(Object JavaDoc o1, Object JavaDoc o2)
84       {
85          Class JavaDoc[] a1 = (Class JavaDoc[]) o1;
86          Class JavaDoc[] a2 = (Class JavaDoc[]) 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 JavaDoc
98    {
99       public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc name)
111    {
112       super(name);
113    }
114
115    public void testEditorSearchPath()
116       throws Exception JavaDoc
117    {
118       getLog().debug("+++ testEditorSearchPath");
119       String JavaDoc[] searchPath = PropertyEditorManager.getEditorSearchPath();
120       boolean foundJBossPath = false;
121       for(int p = 0; p < searchPath.length; p ++)
122       {
123          String JavaDoc 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    /** The mechanism for mapping java.lang.* variants of the primative types
131     misses editors for java.lang.Boolean and java.lang.Integer. Here we test
132     the java.lang.* variants we expect editors for.
133     **/

134    public void testJavaLangEditors()
135       throws Exception JavaDoc
136    {
137       getLog().debug("+++ testJavaLangEditors");
138       // The supported java.lang.* types
139
Class JavaDoc[] types = {
140          Boolean JavaDoc.class,
141          Byte JavaDoc.class,
142          Short JavaDoc.class,
143          Integer JavaDoc.class,
144          Long JavaDoc.class,
145          Float JavaDoc.class,
146          Double JavaDoc.class,
147          Byte JavaDoc.class,
148          Character JavaDoc.class,
149       };
150       // The input string data for each type
151
String JavaDoc[][] 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       // The expected java.lang.* instance for each inputData value
163
Object JavaDoc[][] 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 JavaDoc('A'), new Character JavaDoc('a'), new Character JavaDoc('Z'), new Character JavaDoc('z')},
173       };
174       // The expected string output from getAsText()
175
String JavaDoc[][] 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 JavaDoc[] comparators = new Comparator JavaDoc[types.length];
187
188       doTests(types, inputData, expectedData, expectedStringData, comparators);
189    }
190
191    /** Test custom JBoss property editors.
192     **/

193    public void testJBossEditors()
194       throws Exception JavaDoc
195    {
196       getLog().debug("+++ testJBossEditors");
197       Class JavaDoc[] types = {
198          javax.management.ObjectName JavaDoc.class,
199          java.util.Properties JavaDoc.class,
200          java.io.File JavaDoc.class,
201          java.net.URL JavaDoc.class,
202          java.lang.String JavaDoc.class,
203          java.lang.Class JavaDoc.class,
204          InetAddress JavaDoc.class,
205          String JavaDoc[].class,
206          Class JavaDoc[].class,
207          int[].class,
208          Date JavaDoc.class
209       };
210       // The input string data for each type
211
String JavaDoc[][] inputData = {
212          // javax.management.ObjectName.class
213
{"jboss.test:test=1"},
214          // java.util.Properties.class
215
{"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"},
216          // java.io.File.class
217
{"/tmp/test1", "/tmp/subdir/../test2"},
218          // java.net.URL.class
219
{"http://www.jboss.org"},
220          // java.lang.String.class
221
{"JBoss, Home of Professional Open Source"},
222          // java.lang.Class.class
223
{"java.util.Arrays"},
224          // InetAddress.class, localhost must be defined for this to work
225
{"127.0.0.1", "localhost"},
226          // String[].class
227
{"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,\\,v"},
228          // Class[].class
229
{"java.lang.Integer,java.lang.Float"},
230          // int[].class
231
{"0,#123,-123"},
232          // Date.class
233
{"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"}
234       };
235       // The expected instance for each inputData value
236
calendar.set(2005, 0, 4, 0, 0, 0);
237       calendar.set(Calendar.MILLISECOND, 0);
238       Date JavaDoc date1 = calendar.getTime();
239       calendar.setTimeZone(TimeZone.getTimeZone("PST"));
240       calendar.set(2005, 0, 4, 23, 38, 21);
241       Date JavaDoc date2 = calendar.getTime();
242       calendar.set(2005, 0, 4, 23, 38, 48);
243       Date JavaDoc date3 = calendar.getTime();
244       Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc[][] expectedData = {
253          {new ObjectName JavaDoc("jboss.test:test=1")},
254          {props},
255          {new File JavaDoc("/tmp/test1").getCanonicalFile(), new File JavaDoc("/tmp/test2").getCanonicalFile()},
256          {new URL JavaDoc("http://www.jboss.org")},
257          {new String JavaDoc("JBoss, Home of Professional Open Source")},
258          {java.util.Arrays JavaDoc.class},
259          {InetAddress.getByName("127.0.0.1"), InetAddress.getByName("localhost")},
260          {new String JavaDoc[]{"1", "2", "3"}, new String JavaDoc[] {"a", "b", "c"},
261             new String JavaDoc[]{}, new String JavaDoc[]{"#","%",",",".","_$", ",v"}},
262          {new Class JavaDoc[]{Integer JavaDoc.class, Float JavaDoc.class}},
263          {new int[]{0, 0x123, -123}},
264          {date1, date2, date3}
265       };
266       // The expected string output from getAsText()
267
String JavaDoc[][] expectedStringData = {
268          // javax.management.ObjectName.class
269
{"jboss.test:test=1"},
270          // java.util.Properties.class
271
{"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"},
272          // java.io.File.class
273
{"/tmp/test1", "/tmp/subdir/../test2"},
274          // java.net.URL.class
275
{"http://www.jboss.org"},
276          // java.lang.String.class
277
{"JBoss, Home of Professional Open Source"},
278          // java.lang.Class.class
279
{"java.util.Arrays"},
280          // InetAddress.class, localhost must be defined for this to work
281
{"127.0.0.1", "localhost"},
282          // String[].class
283
{"1,2,3", "a,b,c", "", "#,%,\\,,.,_$,,v"},
284          // Class[].class
285
{"java.lang.Integer,java.lang.Float"},
286          // int[].class
287
{"0,291,-123"},
288          // Date.class
289
{"Jan 4, 2005", "Tue Jan 4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"}
290       };
291       // The Comparator for non-trival types
292
Comparator JavaDoc[] comparators = {
293          null, // ObjectName
294
null, // Properties
295
null, // File
296
null, // URL
297
null, // String
298
null, // Class
299
null, // InetAddress
300
new StringArrayComparator(), // String[]
301
new ClassArrayComparator(), // Class[]
302
new IntArrayComparator(), // int[]
303
null // Date
304
};
305
306       doTests(types, inputData, expectedData, expectedStringData, comparators);
307    }
308    
309    public void testDateEditor() throws Exception JavaDoc
310    {
311       getLog().debug("+++ testDateEditor");
312       
313       Locale JavaDoc locale = Locale.getDefault();
314       
315       try
316       {
317          // Use the default locale
318
getLog().debug("Current Locale: " + Locale.getDefault());
319       
320          // An important date
321
String JavaDoc text = "Fri, 25 Jun 1971 00:30:00 +0200";
322          DateFormat JavaDoc format = new SimpleDateFormat JavaDoc("EEE, d MMM yyyy HH:mm:ss Z");
323          Date JavaDoc date = format.parse(text);
324          
325          PropertyEditor JavaDoc 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 JavaDoc)editor.getValue()) == 0);
330          
331          editor.setValue(date);
332          getLog().debug("setValue('" + date + "') --> getAsText() - '" + editor.getAsText() + "'");
333          Date JavaDoc date2 = format.parse(editor.getAsText());
334          assertTrue("Compare date1: " + date + ", date2: " + date2,
335                date.compareTo(date2) == 0);
336          
337          // Try in French
338
Locale.setDefault(Locale.FRENCH);
339          getLog().debug("Current Locale: " + Locale.getDefault());
340          DateEditor.initialize();
341          
342          // An important date
343
text = "ven., 25 juin 1971 00:30:00 +0200";
344          format = new SimpleDateFormat JavaDoc("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 JavaDoc)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          // reset locale
362
Locale.setDefault(locale);
363          DateEditor.initialize();
364       }
365    }
366    
367    /**
368     * Tests the DOM Document and Element editors.
369     */

370    public void testDocumentElementEditors()
371    {
372       getLog().debug("+++ testDocumentElementEditors");
373       DocumentEditor de = new DocumentEditor();
374       // Comments can appear outside of a document
375
String JavaDoc 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 JavaDoc)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 JavaDoc);
383       // Test whitespace preservation
384
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 JavaDoc);
393    }
394    
395    private void doTests(Class JavaDoc[] types, String JavaDoc[][] inputData, Object JavaDoc[][] expectedData,
396          String JavaDoc[][] expectedStringData, Comparator JavaDoc[] comparators)
397    {
398       for(int t = 0; t < types.length; t ++)
399       {
400          Class JavaDoc type = types[t];
401          getLog().debug("Checking property editor for: "+type);
402          PropertyEditor JavaDoc 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 JavaDoc input = inputData[t][i];
409             editor.setAsText(input);
410             Object JavaDoc expected = expectedData[t][i];
411             Object JavaDoc output = editor.getValue();
412             Comparator JavaDoc 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 JavaDoc expectedStringOutput = expectedStringData[t][i];
425             String JavaDoc 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    /**
438     * Log a Node hierarchy
439     */

440    private void log(Node JavaDoc node, String JavaDoc indent)
441    {
442       String JavaDoc name = node.getNodeName();
443       String JavaDoc value = node.getNodeValue();
444       getLog().debug(indent + "Name=" + name + ", Value=" + value);
445       NodeList JavaDoc 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 JavaDoc logString(String JavaDoc s)
451    {
452       return s != null ? s : "<null>";
453    }
454 }
455
Popular Tags