1 19 package org.enhydra.zeus.util; 20 21 import java.util.HashMap ; 22 import java.util.Vector ; 23 24 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.enhydra.zeus.InvalidCollectionTypeException; 31 32 40 public class ClassUtilsTest extends TestCase { 41 42 49 public ClassUtilsTest(String name) { 50 super(name); 51 } 52 53 62 public static Test suite(){ 63 return new TestSuite(ClassUtilsTest.class); 64 } 65 66 71 public void testIsCollectionClass() { 72 String [] collectionClasses = 73 { "java.util.Collection", "java.util.List", "java.util.LinkedList", 74 "java.util.ArrayList", "java.util.Vector", 75 "java.util.Stack", "java.util.Set", "java.util.SortedSet", 76 "java.util.HashSet", "java.util.TreeSet" }; 77 78 for (int i=0; i<collectionClasses.length; i++) { 79 assertTrue(ClassUtils.isCollectionClass(collectionClasses[i])); 80 } 81 } 82 83 89 public void testGetCollectionImplClass() { 90 String [] collectionClasses = 91 { "java.util.Collection", "java.util.List", "java.util.LinkedList", 92 "java.util.ArrayList", "java.util.Vector", 93 "java.util.Stack", "java.util.Set", "java.util.SortedSet", 94 "java.util.HashSet", "java.util.TreeSet" }; 95 96 String [] expectedClasses = 97 { "java.util.LinkedList", "java.util.LinkedList", 98 "java.util.LinkedList", "java.util.ArrayList", 99 "java.util.Vector", "java.util.Stack", "java.util.HashSet", 100 "java.util.SortedSet", "java.util.HashSet", 101 "java.util.TreeSet" }; 102 103 for (int i = 0; i < expectedClasses.length; i++) { 104 assertEquals(expectedClasses[i], 105 ClassUtils.getCollectionImplClass(collectionClasses[i])); 106 } 107 } 108 109 115 public void testGetParameter() { 116 Class [] paramTypes = 117 { String .class, int.class, Integer .class, long.class, Long .class, 118 float.class, Float .class, double.class, Double .class, 119 boolean.class, Boolean .class }; 120 121 String [] values = 122 { "string", "0", "0", "0", "0", "0.0f", "0.0f", "0.0d", "0.0d", 123 "false", "false" }; 124 125 Object [] expected = 126 { new String ("string"), new Integer (0), new Integer (0), 127 new Long (0), new Long (0), new Float (0.0f), new Float (0.0f), 128 new Double (0.0d), new Double (0.0d), new Boolean (false), 129 new Boolean (false) }; 130 131 for (int i=0; i<paramTypes.length; i++) { 132 assertEquals(expected[i], 133 ClassUtils.getParameter(values[i], paramTypes[i])); 134 } 135 } 136 137 143 public void testGetCollectionTypeAsInt() { 144 String collectionClasses[] = 145 { "java.util.List", "java.util.Array" }; 146 147 int expectedValues[] = 148 { ClassUtils.COLLECTION_TYPE_LIST, 149 ClassUtils.COLLECTION_TYPE_ARRAY }; 150 151 for (int i=0; i<expectedValues.length; i++) { 152 try { 153 assertEquals(expectedValues[i], 154 ClassUtils.getCollectionTypeAsInt(collectionClasses[i])); 155 } catch (InvalidCollectionTypeException neverHappens) { 156 assertTrue(false); 157 } 158 } 159 } 160 161 167 public void testGetCollectionTypeAsIntNegative() { 168 boolean success = true; 169 170 String collectionType = "java.util.List"; 171 try { 172 int tossedOut = ClassUtils.getCollectionTypeAsInt(collectionType); 173 } catch (InvalidCollectionTypeException e) { 174 success = false; 175 } 176 assertTrue(success); 177 178 collectionType = "java.util.NotACollectionType"; 179 success = false; 180 try { 181 int tossedOut = ClassUtils.getCollectionTypeAsInt(collectionType); 182 } catch (InvalidCollectionTypeException e) { 183 success = true; 184 } 185 assertTrue(success); 186 } 187 188 194 public void testIsCollectionConstant() { 195 int collectionConstant = ClassUtils.COLLECTION_TYPE_LIST; 196 assertTrue(ClassUtils.isCollectionConstant(collectionConstant)); 197 198 collectionConstant = -1; 199 assertTrue(!ClassUtils.isCollectionConstant(collectionConstant)); 200 201 collectionConstant = ClassUtils.COLLECTION_TYPE_ARRAY; 202 assertTrue(ClassUtils.isCollectionConstant(collectionConstant)); 203 204 collectionConstant = 3078; 205 assertTrue(!ClassUtils.isCollectionConstant(collectionConstant)); 206 } 207 208 214 public void testIsJavaPrimitive() { 215 String primitive = "Integer"; 216 assertTrue(!ClassUtils.isJavaPrimitive(primitive)); 217 218 primitive = "java.io.File"; 219 assertTrue(!ClassUtils.isJavaPrimitive(primitive)); 220 221 String primitives[] = 222 { "boolean", "byte", "char", "double", "float", "int", "long", 223 "short", "String", "java.lang.String" }; 224 225 for (int i=0; i<primitives.length; i++) { 226 assertTrue(ClassUtils.isJavaPrimitive(primitives[i])); 227 } 228 } 229 } 230 | Popular Tags |