KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > util > ClassUtilsTest


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.util;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 // JUnit imports
25
import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 // Zeus imports
30
import org.enhydra.zeus.InvalidCollectionTypeException;
31
32 /**
33  * <p>
34  * This is a test case for the <code>{@link CapitalizationUtils}</code> class.
35  * </p>
36  *
37  * @author Farhat Kaleem
38  * @author Brett McLaughlin
39  */

40 public class ClassUtilsTest extends TestCase {
41     
42     /**
43      * <p>
44      * This constructs a new <code>ClassUtilsTest</code>.
45      * </p>
46      *
47      * @param name the name of the test case.
48      */

49     public ClassUtilsTest(String JavaDoc name) {
50         super(name);
51     }
52
53     /**
54      * <p>
55      * This method maeks it easier to call these
56      * tests dynamically.
57      * </p>
58      *
59      * @return <code>TestSuite</code> - corresponds to this
60      * <code>TestCase</code>.
61      */

62     public static Test suite(){
63         return new TestSuite(ClassUtilsTest.class);
64     }
65
66     /**
67      * <p>
68      * This tests the <code>{@link ClassUtils#isCollectionClass}</code> method.
69      * </p>
70      */

71     public void testIsCollectionClass() {
72         String JavaDoc [] 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     /**
84      * <p>
85      * This tests the <code>{@link ClassUtils#getCollectionImplClass}</code>
86      * method.
87      * </p>
88      */

89     public void testGetCollectionImplClass() {
90         String JavaDoc [] 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 JavaDoc [] 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     /**
110      * <p>
111      * This tests the <code>{@link ClassUtils#getCollectionImplClass}</code>
112      * method.
113      * </p>
114      */

115     public void testGetParameter() {
116         Class JavaDoc [] paramTypes =
117             { String JavaDoc.class, int.class, Integer JavaDoc.class, long.class, Long JavaDoc.class,
118               float.class, Float JavaDoc.class, double.class, Double JavaDoc.class,
119               boolean.class, Boolean JavaDoc.class };
120
121         String JavaDoc [] values =
122             { "string", "0", "0", "0", "0", "0.0f", "0.0f", "0.0d", "0.0d",
123                "false", "false" };
124
125         Object JavaDoc[] expected =
126             { new String JavaDoc("string"), new Integer JavaDoc(0), new Integer JavaDoc(0),
127               new Long JavaDoc(0), new Long JavaDoc(0), new Float JavaDoc(0.0f), new Float JavaDoc(0.0f),
128               new Double JavaDoc(0.0d), new Double JavaDoc(0.0d), new Boolean JavaDoc(false),
129               new Boolean JavaDoc(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     /**
138      * <p>
139      * This tests the <code>{@link ClassUtils#getCollectionTypeAsInt}</code>
140      * method.
141      * </p>
142      */

143     public void testGetCollectionTypeAsInt() {
144         String JavaDoc 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     /**
162      * <p>
163      * This tests the <code>{@link ClassUtils#getCollectionTypeAsInt}</code>
164      * method, in a negative fashion.
165      * </p>
166      */

167     public void testGetCollectionTypeAsIntNegative() {
168         boolean success = true;
169         
170         String JavaDoc 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     /**
189      * <p>
190      * This tests the <code>{@link ClassUtils#isCollectionConstant}</code>
191      * method.
192      * </p>
193      */

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     /**
209      * <p>
210      * This tests the <code>{@link ClassUtils#isJavaPrimitive}</code>
211      * method.
212      * </p>
213      */

214     public void testIsJavaPrimitive() {
215         String JavaDoc primitive = "Integer";
216         assertTrue(!ClassUtils.isJavaPrimitive(primitive));
217         
218         primitive = "java.io.File";
219         assertTrue(!ClassUtils.isJavaPrimitive(primitive));
220         
221         String JavaDoc 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