KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > utils > TestJavaUtils


1 package test.utils;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.axis.utils.JavaUtils;
7 import org.apache.axis.wsdl.toJava.Utils;
8
9 import javax.xml.rpc.holders.ByteHolder JavaDoc;
10 import javax.xml.rpc.holders.LongHolder JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Calendar JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 public class TestJavaUtils extends TestCase
21 {
22
23     public TestJavaUtils (String JavaDoc name) {
24         super(name);
25     }
26     public static Test suite() {
27         return new TestSuite(TestJavaUtils.class);
28     }
29
30     public void setup() {
31     }
32
33     /**
34      * See JSR-101: JAX-RPC, Appendix: Mapping of XML Names
35      */

36     public void testXmlNameToJava() {
37
38         /* Begin TABLE 20-2 Illustrative Examples from JAXRPC Spec */
39         assertEquals("mixedCaseName", JavaUtils.xmlNameToJava("mixedCaseName"));
40
41         assertEquals("nameWithDashes", JavaUtils.xmlNameToJava("name-with-dashes"));
42         
43         assertEquals("name_with_underscore", JavaUtils.xmlNameToJava("name_with_underscore"));
44         
45         assertEquals("other_punctChars", JavaUtils.xmlNameToJava("other_punct.chars"));
46         
47         assertEquals("answer42", JavaUtils.xmlNameToJava("Answer42"));
48         /* End TABLE 20-2 Illustrative Examples from JAXRPC Spec */
49
50         assertEquals("nameWithDashes",
51                 JavaUtils.xmlNameToJava("name-with-dashes"));
52
53         assertEquals("otherPunctChars",
54                 JavaUtils.xmlNameToJava("other.punct\u00B7chars"));
55
56         assertEquals("answer42", JavaUtils.xmlNameToJava("Answer42"));
57
58         assertEquals("\u2160Foo", JavaUtils.xmlNameToJava("\u2160foo"));
59
60         assertEquals("foo", JavaUtils.xmlNameToJava("2foo"));
61
62         //assertEquals("_Foo_", JavaUtils.xmlNameToJava("_foo_"));
63
assertEquals("_foo_", JavaUtils.xmlNameToJava("_foo_"));
64
65         assertEquals("foobar", JavaUtils.xmlNameToJava("--foobar--"));
66
67         assertEquals("foo22Bar", JavaUtils.xmlNameToJava("foo22bar"));
68
69         assertEquals("foo\u2160Bar", JavaUtils.xmlNameToJava("foo\u2160bar"));
70
71         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo-bar"));
72
73         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo.bar"));
74
75         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo:bar"));
76
77         //assertEquals("foo_Bar", JavaUtils.xmlNameToJava("foo_bar"));
78
assertEquals("foo_bar", JavaUtils.xmlNameToJava("foo_bar"));
79       
80         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo\u00B7bar"));
81
82         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo\u0387bar"));
83
84         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo\u06DDbar"));
85
86         assertEquals("fooBar", JavaUtils.xmlNameToJava("foo\u06DEbar"));
87
88         assertEquals("fooBar", JavaUtils.xmlNameToJava("FooBar"));
89
90         assertEquals("FOOBar", JavaUtils.xmlNameToJava("FOOBar"));
91
92         assertEquals("a1BBB", JavaUtils.xmlNameToJava("A1-BBB"));
93
94         assertEquals("ABBB", JavaUtils.xmlNameToJava("A-BBB"));
95
96         assertEquals("ACCC", JavaUtils.xmlNameToJava("ACCC"));
97         
98
99         // the following cases are ambiguous in JSR-101
100
assertEquals("fooBar", JavaUtils.xmlNameToJava("foo bar"));
101         assertEquals("_1", JavaUtils.xmlNameToJava("-"));
102     }
103     
104     /**
105      * Test for Bug 17994 - wsdl2java generates code with reserved words as variable names
106      */

107     public void testXmlNameToJava2() {
108         assertEquals("_abstract", JavaUtils.xmlNameToJava("abstract"));
109     }
110     
111     /**
112      * Test for Bug 17995 - wsdl2java converts namespace to uncompilable packages
113      */

114     public void testPackageNaming() {
115         assertEquals("_0._1.util.liberty", Utils.makePackageName("urn:liberty:util:1.0"));
116     }
117     
118     /**
119      * test the convert() function
120      * verify that we can convert to the Collection, List, and Set interfaces
121      */

122     public void testConvert() {
123         Integer JavaDoc[] array = new Integer JavaDoc[4];
124         array[0] = new Integer JavaDoc(5); array[1] = new Integer JavaDoc(4);
125         array[2] = new Integer JavaDoc(3); array[3] = new Integer JavaDoc(2);
126         
127         Object JavaDoc ret = JavaUtils.convert(array, List JavaDoc.class);
128         assertTrue("Converted array not a List", (ret instanceof List JavaDoc));
129         List JavaDoc list = (List JavaDoc)ret;
130         for (int i = 0; i < array.length; i++) {
131             assertEquals(array[i], list.get(i));
132         }
133         
134         ret = JavaUtils.convert(array, Collection JavaDoc.class);
135         assertTrue("Converted array is not a Collection", (ret instanceof Collection JavaDoc));
136         
137         ret = JavaUtils.convert(array, Set JavaDoc.class);
138         assertTrue("Converted array not a Set", (ret instanceof Set JavaDoc));
139         
140         ret = JavaUtils.convert(array, Vector JavaDoc.class);
141         assertTrue("Converted array not a Vector", (ret instanceof Vector JavaDoc));
142         
143         HashMap JavaDoc m = new HashMap JavaDoc();
144         m.put("abcKey", "abcVal");
145         m.put("defKey", "defVal");
146         ret = JavaUtils.convert(m, Hashtable JavaDoc.class);
147         assertTrue("Converted HashMap not a Hashtable", (ret instanceof Hashtable JavaDoc));
148
149         LongHolder JavaDoc holder = new LongHolder JavaDoc(1);
150         ret = JavaUtils.convert(holder, Object JavaDoc.class);
151         assertTrue(ret != null);
152         assertTrue(Long JavaDoc.class.isInstance(ret));
153
154         ByteHolder JavaDoc holder2 = new ByteHolder JavaDoc((byte)0);
155         ret = JavaUtils.convert(holder2, Object JavaDoc.class);
156         assertTrue(ret != null);
157         assertTrue(Byte JavaDoc.class.isInstance(ret));
158         
159         // Make sure we convert ArrayList to array in 2D cases
160
Object JavaDoc[] arrayin = new Object JavaDoc[1];
161         ArrayList JavaDoc data = new ArrayList JavaDoc(5);
162         data.add("one"); data.add(new Integer JavaDoc(2)); data.add(new Float JavaDoc(4.0));
163         data.add(new Double JavaDoc(5.0)); data.add("five");
164         arrayin[0] = data;
165         ret = JavaUtils.convert(arrayin, Object JavaDoc[][].class);
166         assertTrue("Converted 2D array/ArrayList wrong", ret.getClass().equals(Object JavaDoc[][].class));
167         Object JavaDoc[][] outer = (Object JavaDoc[][]) ret;
168         assertEquals("Outer array of 2D array/ArrayList is wrong length", 1, outer.length);
169         Object JavaDoc[] inner = ((Object JavaDoc[][])ret)[0];
170         assertEquals("Inner array of 2D array/ArrayLis is wrong length", 5, inner.length);
171         
172         // check 2D ArrayList of ArrayList
173
ArrayList JavaDoc data2D = new ArrayList JavaDoc(2);
174         data2D.add(data); data2D.add(data);
175         ret = JavaUtils.convert(data2D, Object JavaDoc[][].class);
176         assertTrue("Converted 2D ArrayList wrong", ret.getClass().equals(Object JavaDoc[][].class));
177         Object JavaDoc[][] outer2 = (Object JavaDoc[][]) ret;
178         assertEquals("Outer array of 2D ArrayList is wrong length", 2, outer2.length);
179         Object JavaDoc[] inner2 = ((Object JavaDoc[][]) ret)[0];
180         assertEquals("Inner array of 2D ArrayList is wrong length", 5, inner2.length);
181         
182     }
183
184     /**
185      * test the isConvertable() function
186      */

187     public void testIsConvert() {
188         assertTrue(JavaUtils.isConvertable(new Long JavaDoc(1),Long JavaDoc.class));
189         assertTrue(JavaUtils.isConvertable(new Long JavaDoc(1),long.class));
190         assertTrue(JavaUtils.isConvertable(new Long JavaDoc(1),Object JavaDoc.class));
191         assertTrue(!JavaUtils.isConvertable(new Long JavaDoc(1),Float JavaDoc.class));
192         Class JavaDoc clazz = long.class;
193         assertTrue(JavaUtils.isConvertable(clazz,Long JavaDoc.class));
194         assertTrue(JavaUtils.isConvertable(clazz,Object JavaDoc.class));
195         clazz = byte.class;
196         assertTrue(JavaUtils.isConvertable(clazz,Byte JavaDoc.class));
197         assertTrue(JavaUtils.isConvertable(clazz,Object JavaDoc.class));
198     }
199     
200     /**
201      * Make sure we can't say convert from string[] to Calendar[]
202      */

203     public void testIsConvert2()
204     {
205         String JavaDoc[] strings = new String JavaDoc[]{"hello"};
206         Calendar JavaDoc[] calendars = new Calendar JavaDoc[1];
207         assertTrue(!JavaUtils.isConvertable(strings, calendars.getClass()));
208     }
209
210     public static void main(String JavaDoc args[]){
211         TestJavaUtils tester = new TestJavaUtils("TestJavaUtils");
212         tester.testIsConvert();
213         tester.testConvert();
214     }
215 }
216
Popular Tags