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 ; 10 import javax.xml.rpc.holders.LongHolder ; 11 import java.util.ArrayList ; 12 import java.util.Calendar ; 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Hashtable ; 16 import java.util.List ; 17 import java.util.Set ; 18 import java.util.Vector ; 19 20 public class TestJavaUtils extends TestCase 21 { 22 23 public TestJavaUtils (String 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 36 public void testXmlNameToJava() { 37 38 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 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_")); 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")); 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 assertEquals("fooBar", JavaUtils.xmlNameToJava("foo bar")); 101 assertEquals("_1", JavaUtils.xmlNameToJava("-")); 102 } 103 104 107 public void testXmlNameToJava2() { 108 assertEquals("_abstract", JavaUtils.xmlNameToJava("abstract")); 109 } 110 111 114 public void testPackageNaming() { 115 assertEquals("_0._1.util.liberty", Utils.makePackageName("urn:liberty:util:1.0")); 116 } 117 118 122 public void testConvert() { 123 Integer [] array = new Integer [4]; 124 array[0] = new Integer (5); array[1] = new Integer (4); 125 array[2] = new Integer (3); array[3] = new Integer (2); 126 127 Object ret = JavaUtils.convert(array, List .class); 128 assertTrue("Converted array not a List", (ret instanceof List )); 129 List list = (List )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 .class); 135 assertTrue("Converted array is not a Collection", (ret instanceof Collection )); 136 137 ret = JavaUtils.convert(array, Set .class); 138 assertTrue("Converted array not a Set", (ret instanceof Set )); 139 140 ret = JavaUtils.convert(array, Vector .class); 141 assertTrue("Converted array not a Vector", (ret instanceof Vector )); 142 143 HashMap m = new HashMap (); 144 m.put("abcKey", "abcVal"); 145 m.put("defKey", "defVal"); 146 ret = JavaUtils.convert(m, Hashtable .class); 147 assertTrue("Converted HashMap not a Hashtable", (ret instanceof Hashtable )); 148 149 LongHolder holder = new LongHolder (1); 150 ret = JavaUtils.convert(holder, Object .class); 151 assertTrue(ret != null); 152 assertTrue(Long .class.isInstance(ret)); 153 154 ByteHolder holder2 = new ByteHolder ((byte)0); 155 ret = JavaUtils.convert(holder2, Object .class); 156 assertTrue(ret != null); 157 assertTrue(Byte .class.isInstance(ret)); 158 159 Object [] arrayin = new Object [1]; 161 ArrayList data = new ArrayList (5); 162 data.add("one"); data.add(new Integer (2)); data.add(new Float (4.0)); 163 data.add(new Double (5.0)); data.add("five"); 164 arrayin[0] = data; 165 ret = JavaUtils.convert(arrayin, Object [][].class); 166 assertTrue("Converted 2D array/ArrayList wrong", ret.getClass().equals(Object [][].class)); 167 Object [][] outer = (Object [][]) ret; 168 assertEquals("Outer array of 2D array/ArrayList is wrong length", 1, outer.length); 169 Object [] inner = ((Object [][])ret)[0]; 170 assertEquals("Inner array of 2D array/ArrayLis is wrong length", 5, inner.length); 171 172 ArrayList data2D = new ArrayList (2); 174 data2D.add(data); data2D.add(data); 175 ret = JavaUtils.convert(data2D, Object [][].class); 176 assertTrue("Converted 2D ArrayList wrong", ret.getClass().equals(Object [][].class)); 177 Object [][] outer2 = (Object [][]) ret; 178 assertEquals("Outer array of 2D ArrayList is wrong length", 2, outer2.length); 179 Object [] inner2 = ((Object [][]) ret)[0]; 180 assertEquals("Inner array of 2D ArrayList is wrong length", 5, inner2.length); 181 182 } 183 184 187 public void testIsConvert() { 188 assertTrue(JavaUtils.isConvertable(new Long (1),Long .class)); 189 assertTrue(JavaUtils.isConvertable(new Long (1),long.class)); 190 assertTrue(JavaUtils.isConvertable(new Long (1),Object .class)); 191 assertTrue(!JavaUtils.isConvertable(new Long (1),Float .class)); 192 Class clazz = long.class; 193 assertTrue(JavaUtils.isConvertable(clazz,Long .class)); 194 assertTrue(JavaUtils.isConvertable(clazz,Object .class)); 195 clazz = byte.class; 196 assertTrue(JavaUtils.isConvertable(clazz,Byte .class)); 197 assertTrue(JavaUtils.isConvertable(clazz,Object .class)); 198 } 199 200 203 public void testIsConvert2() 204 { 205 String [] strings = new String []{"hello"}; 206 Calendar [] calendars = new Calendar [1]; 207 assertTrue(!JavaUtils.isConvertable(strings, calendars.getClass())); 208 } 209 210 public static void main(String args[]){ 211 TestJavaUtils tester = new TestJavaUtils("TestJavaUtils"); 212 tester.testIsConvert(); 213 tester.testConvert(); 214 } 215 } 216 | Popular Tags |