KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > util > UtilsTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * UtilsTest.java June 12, 2003, 4:21 PM
26  */

27
28 package com.sun.enterprise.tools.common.validation.util;
29
30 import junit.framework.*;
31 import java.lang.reflect.Constructor JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33
34 /**
35  *
36  * @author Rajeshwar Patil
37  * @version %I%, %G%
38  */

39
40 public class UtilsTest extends TestCase{
41     /* A class implementation comment can go here. */
42     
43     private Utils utils = null;
44
45     public UtilsTest(String JavaDoc name){
46         super(name);
47         utils = new Utils();
48     }
49
50
51     public static void main(String JavaDoc args[]){
52         junit.textui.TestRunner.run(suite());
53     }
54
55
56     public void testGetIndexedName(){
57         String JavaDoc element = "element"; //NOI18N
58
int index = 5;
59         assertTrue("element(5)".equals( //NOI18N
60
utils.getIndexedName(element,index)));
61         assertTrue(null == utils.getIndexedName(null,index));
62         
63     }
64
65
66     //test for :
67
// public static Object getElement(String elementName, Object object)
68
// public static Object[] getElements(String elementName,
69
// Object object)
70
// public static Object getElement(String elementName,
71
// int index, Object object)
72
// public static Object getElement(String elementName,
73
// int index, Object object)
74
// public static Object getElement(String elementName, Object object,
75
// String prefix)
76
public void testGetElement(){
77         Object JavaDoc object = new CustomObject();
78         assertTrue("foo".equals( //NOI18N
79
(String JavaDoc)utils.getElement("foo", object))); //NOI18N
80
assertTrue(null == utils.getElement("", object)); //NOI18N
81
assertTrue(null == utils.getElement(null, object));
82         assertTrue(null == utils.getElement("foo", null)); //NOI18N
83
assertTrue(null == utils.getElement(null, null)); //NOI18N
84

85         Integer JavaDoc integer = (Integer JavaDoc) utils.getElement("integer", object);//NOI18N
86
assertTrue(5 == integer.intValue());
87         String JavaDoc[] colours =
88             (String JavaDoc[]) utils.getElements("primaryColours", object); //NOI18N
89
assertTrue("green".equals(colours[1])); //NOI18N
90

91         String JavaDoc colour =
92             (String JavaDoc) utils.getElement("primaryColours", 2, object); //NOI18N
93
assertTrue("blue".equals(colour)); //NOI18N
94
assertTrue(null == utils.getElement("", 2, object)); //NOI18N
95
assertTrue(null == utils.getElement(null, 2, object));
96         assertTrue(null == utils.getElement("primaryColours", 2, null));//NOI18N
97
assertTrue(null == utils.getElement("primaryColours", -1, //NOI18N
98
object));
99         assertTrue(null == utils.getElement(null, -3, null));
100         
101         assertTrue(null == utils.getElement("", object, "")); //NOI18N
102
assertTrue(null == utils.getElement("primaryColours", //NOI18N
103
null, "size")); //NOI18N
104
integer = (Integer JavaDoc) utils.getElement(
105                 "primaryColours", object, ""); //NOI18N
106
assertTrue(3 == integer.intValue());
107         integer = (Integer JavaDoc) utils.getElement(
108                 "primaryColours", object, null); //NOI18N
109
assertTrue(3 == integer.intValue());
110         integer = (Integer JavaDoc) utils.getElement(
111                 "primaryColours", object, "size"); //NOI18N
112
assertTrue(4 == integer.intValue());
113     }
114
115
116     public void testMethodNameFromDtdName(){
117        String JavaDoc name = "ejb-name"; //NOI18N
118
String JavaDoc prefix = "set"; //NOI18N
119
assertTrue("setEjbName".equals( //NOI18N
120
utils.methodNameFromDtdName(name, prefix)));
121     }
122
123
124    public void testMethodNameFromBeanName(){
125        String JavaDoc beanName = "name"; //NOI18N
126
String JavaDoc prefix = "get"; //NOI18N
127
assertTrue("getName".equals( //NOI18N
128
utils.methodNameFromBeanName(beanName, prefix)));
129        
130        prefix = null;
131        assertTrue("name".equals( //NOI18N
132
utils.methodNameFromBeanName(beanName, prefix)));
133
134        prefix = ""; //NOI18N
135
assertTrue("name".equals( //NOI18N
136
utils.methodNameFromBeanName(beanName, prefix)));
137        
138        beanName = ""; //NOI18N
139
prefix = "set"; //NOI18N
140
assertTrue("set".equals( //NOI18N
141
utils.methodNameFromBeanName(beanName, prefix)));
142        
143        beanName = null;
144        prefix = "set"; //NOI18N
145
assertTrue(null == utils.methodNameFromBeanName(beanName, prefix));
146    }
147
148
149     public void testEleminateHypen(){
150         String JavaDoc str = "hello"; //NOI18N
151
assertTrue(null == utils.eleminateHypen(null));
152         assertTrue("".equals(utils.eleminateHypen(""))); //NOI18N
153
assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
154
str = "-hello"; //NOI18N
155
assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
156
str = "hello-"; //NOI18N
157
assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
158
str = "hello-world"; //NOI18N
159
assertTrue("helloWorld".equals(utils.eleminateHypen(str))); //NOI18N
160
str = "hello-whole-world"; //NOI18N
161
assertTrue("helloWholeWorld".equals(utils.eleminateHypen(str)));//NOI18N
162
str = "-hellO-WhOle-wOrld-"; //NOI18N
163
assertTrue("hellOWhOleWOrld".equals(utils.eleminateHypen(str)));//NOI18N
164
}
165     
166     //test for :
167
// public static Method getMethod(String type, String methodName)
168
// public static Method getMethod(Class classObject, String methodName)
169
// public static Object invoke(Object object, Method method)
170
public void testGetMethod_One(){
171         String JavaDoc str = "hello"; //NOI18N
172
Method JavaDoc method = utils.getMethod("java.lang.String", "length"); //NOI18N
173
int length = ((Integer JavaDoc)utils.invoke(str, method)).intValue();
174         assertTrue(5 == length);
175         
176         method = utils.getMethod(String JavaDoc.class, "length"); //NOI18N
177
length = ((Integer JavaDoc)utils.invoke(str, method)).intValue();
178         assertTrue(5 == length);
179     }
180
181     
182     //test for :
183
// public static Method getMethod(String type, String methodName,
184
// Class[] argumentClass)
185
// public static Method getMethod(Class classObject, String methodName,
186
// Class[] argumentClass){
187
// public static Object invoke(Object object, Method method,
188
// Object[] arguments)
189
public void testGetMethod(){
190         String JavaDoc str = "hello"; //NOI18N
191
Class JavaDoc[] argumentClass = new Class JavaDoc[] {char.class, char.class};
192         Method JavaDoc method =
193             utils.getMethod("java.lang.String", "replace", //NOI18N
194
argumentClass);
195         Character JavaDoc oldChar = new Character JavaDoc('h');
196         Character JavaDoc newChar = new Character JavaDoc('H');
197         Object JavaDoc[] parameters = new Object JavaDoc[] { oldChar, newChar };
198         assertTrue("Hello".equals(utils.invoke(str, method, //NOI18N
199
parameters)));
200         
201         method = utils.getMethod(String JavaDoc.class, "replace", //NOI18N
202
argumentClass);
203         assertTrue("Hello".equals(utils.invoke(str, method, //NOI18N
204
parameters)));
205     }
206
207         
208     //test for :
209
// public static Class getClass(String type)
210
// public static Class getClass(Object object)
211
public void testGetClass(){
212         assertTrue(String JavaDoc.class == utils.getClass("java.lang.String")); //NOI18N
213
Integer JavaDoc integer = new Integer JavaDoc(1234);
214         assertTrue(Integer JavaDoc.class == utils.getClass(integer));
215     }
216
217
218     //test for :
219
// public static Object createObject(Constructor constructor,
220
// Object[] arguments)
221
// public static Constructor getConstructor(String type,
222
// Class[] argumentClass)
223
// public static Constructor getConstructor(Class classObject,
224
// Class[] argumentClass)
225
public void testCreatObject_Two(){
226         Class JavaDoc[] argumentTypes = new Class JavaDoc[] {String JavaDoc.class};
227         Constructor JavaDoc constructor =
228             utils.getConstructor("java.lang.Integer", argumentTypes); //NOI18N
229
Object JavaDoc[] argumentValues = new Object JavaDoc[] {"1234"}; //NOI18N
230
Object JavaDoc object = utils.createObject(constructor, argumentValues);
231         Class JavaDoc objectClass = object.getClass();
232         String JavaDoc objectName = objectClass.getName();
233         assertTrue("java.lang.Integer".equals(objectName)); //NOI18N
234
object = (Integer JavaDoc)object;
235         assertTrue("1234".equals(object.toString())); //NOI18N
236

237         Class JavaDoc[] argTypes = new Class JavaDoc[] {int.class};
238         Constructor JavaDoc cons = utils.getConstructor(object.getClass(), argTypes);
239         Integer JavaDoc parameter = new Integer JavaDoc(4567);
240         Object JavaDoc[] arguments = new Object JavaDoc[] {parameter};
241         Object JavaDoc obj = utils.createObject(cons, arguments);
242         assertTrue("java.lang.Integer".equals(objectName)); //NOI18N
243
obj = (Integer JavaDoc)obj;
244         assertTrue("4567".equals(obj.toString())); //NOI18N
245
}
246
247
248     //test for : public static Object createObject(Class classObject)
249
public void testCreateObject_One(){
250         Object JavaDoc object = utils.createObject("java.lang.String"); //NOI18N
251
String JavaDoc str = "hello"; //NOI18N
252
Class JavaDoc objectClass = str.getClass();
253         String JavaDoc name = objectClass.getName();
254         assertTrue("java.lang.String".equals(name)); //NOI18N
255
}
256
257
258     //test for : public static Object createObject(String type)
259
public void testCreateObject(){
260         Object JavaDoc object = utils.createObject("java.lang.String"); //NOI18N
261
Class JavaDoc objectClass = object.getClass();
262         String JavaDoc name = objectClass.getName();
263         assertTrue("java.lang.String".equals(name)); //NOI18N
264
}
265
266
267     public void testGetUpperCaseFirstLetter() {
268         assertTrue("Hello".equals( //NOI18N
269
utils.upperCaseFirstLetter("hello"))); //NOI18N
270
assertTrue("Hello".equals( //NOI18N
271
utils.upperCaseFirstLetter("Hello"))); //NOI18N
272
assertTrue("H".equals( //NOI18N
273
utils.upperCaseFirstLetter("h"))); //NOI18N
274
assertTrue(null == utils.upperCaseFirstLetter(null)); //NOI18N
275
assertTrue("".equals( //NOI18N
276
utils.upperCaseFirstLetter(""))); //NOI18N
277
}
278
279
280     public void testGetParentName() {
281         assertTrue("/root/parent".equals( //NOI18N
282
utils.getParentName("/root/parent/child", '/'))); //NOI18N
283
assertTrue(null == utils.getParentName("child", '/')); //NOI18N
284
assertTrue(null == utils.getParentName("/root", '/')); //NOI18N
285
assertTrue(null == utils.getParentName(null, '/'));
286     }
287
288
289     public void testGetName() {
290         assertTrue("child".equals(
291                 utils.getName("/root/parent/child", '/'))); //NOI18N
292
assertTrue("child".equals(utils.getName("child", '/'))); //NOI18N
293
assertTrue("root".equals(utils.getName("/root", '/'))); //NOI18N
294
assertTrue(null == utils.getName(null, '/'));
295     }
296     
297     
298     /**
299      * Define suite of all the Tests to run.
300      */

301     public static Test suite(){
302         TestSuite suite = new TestSuite(UtilsTest.class);
303         return suite;
304     }
305
306
307     /**
308      * Initialize; allocate any resources needed to perform Tests.
309      */

310     protected void setUp() {
311     }
312     
313     /**
314      * Free all the resources initilized/allocated to perform Tests.
315      */

316     protected void tearDown() {
317     }
318     
319     private void nyi() {
320         fail("Not yet implemented"); //NOI18N
321
}
322     
323     class CustomObject extends Object JavaDoc
324     {
325         String JavaDoc name = "foo"; //NOI18N
326
Integer JavaDoc integer = new Integer JavaDoc(5);
327         String JavaDoc[] primaryColours =
328                 new String JavaDoc[] {"red", "green", "blue", "yellow"}; //NOI18N
329

330         public String JavaDoc getFoo(){
331           return name;
332         }
333         
334         public int getInteger(){
335             return integer.intValue();
336         }
337         
338         public String JavaDoc[] getPrimaryColours(){
339             return primaryColours;
340         }
341         
342         public String JavaDoc getPrimaryColours(int index){
343             return primaryColours[index];
344         }
345         
346         public Integer JavaDoc sizePrimaryColours(){
347             return new Integer JavaDoc(4);
348         }
349         
350         public Integer JavaDoc primaryColours(){
351             return new Integer JavaDoc(3);
352         }
353     }
354 }
355
Popular Tags