KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > util > UtilsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.validation.util;
21
22 import junit.framework.*;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  *
28  * @author Rajeshwar Patil
29  * @version %I%, %G%
30  */

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

77         Integer JavaDoc integer = (Integer JavaDoc) utils.getElement("integer", object);//NOI18N
78
assertTrue(5 == integer.intValue());
79         String JavaDoc[] colours =
80             (String JavaDoc[]) utils.getElements("primaryColours", object); //NOI18N
81
assertTrue("green".equals(colours[1])); //NOI18N
82

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

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

293     public static Test suite(){
294         TestSuite suite = new TestSuite(UtilsTest.class);
295         return suite;
296     }
297
298
299     /**
300      * Initialize; allocate any resources needed to perform Tests.
301      */

302     protected void setUp() {
303     }
304     
305     /**
306      * Free all the resources initilized/allocated to perform Tests.
307      */

308     protected void tearDown() {
309     }
310     
311     private void nyi() {
312         ///fail("Not yet implemented"); //NOI18N
313
}
314     
315     class CustomObject extends Object JavaDoc
316     {
317         String JavaDoc name = "foo"; //NOI18N
318
Integer JavaDoc integer = new Integer JavaDoc(5);
319         String JavaDoc[] primaryColours =
320                 new String JavaDoc[] {"red", "green", "blue", "yellow"}; //NOI18N
321

322         public String JavaDoc getFoo(){
323           return name;
324         }
325         
326         public int getInteger(){
327             return integer.intValue();
328         }
329         
330         public String JavaDoc[] getPrimaryColours(){
331             return primaryColours;
332         }
333         
334         public String JavaDoc getPrimaryColours(int index){
335             return primaryColours[index];
336         }
337         
338         public Integer JavaDoc sizePrimaryColours(){
339             return new Integer JavaDoc(4);
340         }
341         
342         public Integer JavaDoc primaryColours(){
343             return new Integer JavaDoc(3);
344         }
345     }
346 }
347
Popular Tags