KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > reflection > test > ClassUtilTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.reflection.test;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.sape.carbon.core.util.reflection.ClassUtil;
25
26 import junit.extensions.ActiveTestSuite;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 /**
32  * <p></p>
33  *
34  * Copyright 2003 Sapient
35  * @since carbon 2.0
36  * @author Greg Hinkle, March 2003
37  * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
38  */

39 public class ClassUtilTest extends TestCase {
40
41     public ClassUtilTest(String JavaDoc name) {
42         super(name);
43     }
44
45
46     public static interface A {}
47     public static interface B extends A {}
48     public static interface C extends A {}
49     public static interface D extends B, C {}
50     public static class E implements C, B {}
51     public static interface F extends B {}
52     public static class G extends E implements F {}
53     public static class H {}
54     public static class I extends H {}
55
56    /* Test Class Structure
57                A* Object
58               / \ /\
59             B* C* ------- \
60            / \ / / H
61          F* D*,E \
62           \ / \
63            \ / I
64             \ |
65               G
66
67         (* interfaces)
68     */

69
70     public void testSuperInterfaces() {
71
72         Set JavaDoc expectedResult =
73             new HashSet JavaDoc(Arrays.asList(
74                 new Class JavaDoc[] { A.class, B.class, C.class, D.class } ));
75
76
77         Class JavaDoc[] resultArray = ClassUtil.getSuperInterfaces(new Class JavaDoc[] { D.class });
78
79         Set JavaDoc results =
80             new HashSet JavaDoc(Arrays.asList(resultArray));
81
82         assertTrue(
83             "There were more expected results then found.",
84             results.containsAll(expectedResult));
85
86         assertTrue(
87             "There were more results then expected.",
88             expectedResult.containsAll(results));
89
90     }
91
92     public void testGeneralizations() {
93         Set JavaDoc expectedResult =
94             new HashSet JavaDoc(Arrays.asList(
95                 new Class JavaDoc[] { A.class, B.class, C.class, E.class, Object JavaDoc.class }));
96
97         Set JavaDoc generalizations = ClassUtil.getGeneralizations(E.class);
98
99
100         assertTrue(
101             "There were more expected results then found.",
102             generalizations.containsAll(expectedResult));
103
104         assertTrue(
105             "There were more results then expected.",
106             expectedResult.containsAll(generalizations));
107
108     }
109
110
111
112
113     /**
114      * Method called by jUnit to get all the tests in this test case.
115      * @return Test the suite of tests in this test case
116      */

117     public static Test suite() {
118         TestSuite masterSuite = new TestSuite();
119
120         // add single threaded tests
121
Test singleThreadedTests = getSingleThreadedTests();
122         if (singleThreadedTests != null) {
123             masterSuite.addTest(singleThreadedTests);
124         }
125
126         // add multi threaded tests
127
Test multiThreadedTests = getMultiThreadedTests();
128         if (multiThreadedTests != null) {
129             masterSuite.addTest(multiThreadedTests);
130         }
131
132         return masterSuite;
133     }
134
135     /**
136      * This method is used within the suite method to get all of the single
137      * threaded tests.
138      *
139      * Add all your single threaded tests in this method with a line like:
140      * suite.addTest(new ClassUtilTest("testFunction1"));
141      *
142      * @return Test the suite of single threaded tests in this test case
143      */

144     private static Test getSingleThreadedTests() {
145         TestSuite suite = new TestSuite();
146         /*
147          * add your tests here following these examples:
148          *
149          * suite.addTest(new ClassUtilTest("testFunction1"));
150          * suite.addTest(new ClassUtilTest("testFunction2"));
151          */

152         suite.addTest(new ClassUtilTest("testSuperInterfaces"));
153         suite.addTest(new ClassUtilTest("testGeneralizations"));
154
155         return suite;
156     }
157
158     /**
159      * This method is used within the suite method to get all of the multi
160      * threaded tests.
161      *
162      * Add all your multi threaded tests in this method with a line like:
163      * addTest(suite, "testFunction1", 5);
164      *
165      * @return Test the suite of multi-threaded tests in this test case
166      */

167     private static Test getMultiThreadedTests() {
168         TestSuite suite = new ActiveTestSuite();
169         /*
170          * add your tests here following these examples:
171          *
172          * addTest(suite, "testFunction1", 5);
173          * addTest(suite, "testFunction2", 10);
174          */

175         return suite;
176     }
177
178     /**
179      * This method will add the give test to the give suite the specified
180      * number of times. This is best used for multi-threaded tests where
181      * suite is an instance of ActiveTestSuite and you want to run the same
182      * test in multiple threads.
183      *
184      * @param suite the suite to add the test to.
185      * @param testName the name of the test to add.
186      * @param number the number of times to add the test to the suite
187      */

188     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
189         for (int count = 0; count < number; count++) {
190             suite.addTest(new ClassUtilTest(testName));
191         }
192     }
193 }
Popular Tags