KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.sape.carbon.core.util.reflection.ClassTree;
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>This test harness tests the ClassTreeUtil class that builds a tree
33  * structure of Java Class inheritance hierarchies.</p>
34  *
35  * Copyright 2003 Sapient
36  * @since carbon 2.0
37  * @author Greg Hinkle, March 2003
38  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
39  */

40 public class ClassTreeTest extends TestCase {
41
42     public ClassTreeTest(String JavaDoc name) {
43         super(name);
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 testClassInterfaceMix() {
71         ClassTree tree = new ClassTree(G.class);
72
73         List JavaDoc expectedResults =
74             new ArrayList JavaDoc(Arrays.asList(
75                 new Class JavaDoc[] { Object JavaDoc.class, A.class, C.class, B.class, E.class, F.class, G.class }));
76
77         List JavaDoc results = tree.getOrderedList();
78
79         assertEquals(
80             "The expected and result lists where not the same.",
81             expectedResults,
82             results);
83     }
84
85     public void testInterface() {
86         ClassTree tree = new ClassTree(D.class);
87
88         List JavaDoc expectedResults =
89             new ArrayList JavaDoc(Arrays.asList(
90                 new Class JavaDoc[] { A.class, C.class, B.class, D.class }));
91
92         List JavaDoc results = tree.getOrderedList();
93
94         assertEquals(
95             "The expected and result lists where not the same.",
96             expectedResults,
97             results);
98     }
99
100     public void testBaseInterface() {
101         ClassTree tree = new ClassTree(A.class);
102
103         List JavaDoc expectedResults =
104             new ArrayList JavaDoc(Arrays.asList(
105                 new Class JavaDoc[] { A.class }));
106
107         List JavaDoc results = tree.getOrderedList();
108
109         assertEquals(
110             "The expected and result lists where not the same.",
111             expectedResults,
112             results);
113     }
114
115     public void testClass() {
116         ClassTree tree = new ClassTree(I.class);
117
118         List JavaDoc expectedResults =
119             new ArrayList JavaDoc(Arrays.asList(
120                 new Class JavaDoc[] { Object JavaDoc.class, H.class, I.class }));
121
122         List JavaDoc results = tree.getOrderedList();
123
124         assertEquals(
125             "The expected and result lists where not the same.",
126             expectedResults,
127             results);
128     }
129
130     public void testBaseClass() {
131         ClassTree tree = new ClassTree(H.class);
132
133         List JavaDoc expectedResults =
134             new ArrayList JavaDoc(Arrays.asList(
135                 new Class JavaDoc[] { Object JavaDoc.class, H.class }));
136
137         List JavaDoc results = tree.getOrderedList();
138
139         assertEquals(
140             "The expected and result lists where not the same.",
141             expectedResults,
142             results);
143     }
144
145     /**
146      * Method called by jUnit to get all the tests in this test case.
147      * @return Test the suite of tests in this test case
148      */

149     public static Test suite() {
150         TestSuite masterSuite = new TestSuite();
151
152         // add single threaded tests
153
Test singleThreadedTests = getSingleThreadedTests();
154         if (singleThreadedTests != null) {
155             masterSuite.addTest(singleThreadedTests);
156         }
157
158         // add multi threaded tests
159
Test multiThreadedTests = getMultiThreadedTests();
160         if (multiThreadedTests != null) {
161             masterSuite.addTest(multiThreadedTests);
162         }
163
164         return masterSuite;
165     }
166
167     /**
168      * This method is used within the suite method to get all of the single
169      * threaded tests.
170      *
171      * Add all your single threaded tests in this method with a line like:
172      * suite.addTest(new ClassTreeTest("testFunction1"));
173      *
174      * @return Test the suite of single threaded tests in this test case
175      */

176     private static Test getSingleThreadedTests() {
177         TestSuite suite = new TestSuite();
178
179         suite.addTest(new ClassTreeTest("testClassInterfaceMix"));
180         suite.addTest(new ClassTreeTest("testInterface"));
181         suite.addTest(new ClassTreeTest("testBaseInterface"));
182         suite.addTest(new ClassTreeTest("testClass"));
183         suite.addTest(new ClassTreeTest("testBaseClass"));
184
185         return suite;
186     }
187
188     /**
189      * This method is used within the suite method to get all of the multi
190      * threaded tests.
191      *
192      * Add all your multi threaded tests in this method with a line like:
193      * addTest(suite, "testFunction1", 5);
194      *
195      * @return Test the suite of multi-threaded tests in this test case
196      */

197     private static Test getMultiThreadedTests() {
198         TestSuite suite = new ActiveTestSuite();
199         /*
200          * add your tests here following these examples:
201          *
202          * addTest(suite, "testFunction1", 5);
203          * addTest(suite, "testFunction2", 10);
204          */

205         return suite;
206     }
207
208     /**
209      * This method will add the give test to the give suite the specified
210      * number of times. This is best used for multi-threaded tests where
211      * suite is an instance of ActiveTestSuite and you want to run the same
212      * test in multiple threads.
213      *
214      * @param suite the suite to add the test to.
215      * @param testName the name of the test to add.
216      * @param number the number of times to add the test to the suite
217      */

218     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
219         for (int count = 0; count < number; count++) {
220             suite.addTest(new ClassTreeTest(testName));
221         }
222     }
223 }
Popular Tags