KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > usages > FindSubTypesTest


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.jmi.javamodel.usages;
21
22 import java.io.PrintStream JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import junit.textui.TestRunner;
28 import org.netbeans.jmi.javamodel.JavaClass;
29 import org.netbeans.jmi.javamodel.codegen.Utility;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.junit.NbTestSuite;
32
33
34 /**
35  * @author Martin Matula
36  */

37 public class FindSubTypesTest extends NbTestCase {
38
39     public static final String JavaDoc[] SUBTYPES_ABSTRACT_COLLECTION = {
40         "org.netbeans.test.subtypes.AbstractList",
41         "org.netbeans.test.subtypes.AbstractSet",
42         "org.netbeans.test.subtypes.HashMap.Values"
43     };
44     
45     public static final String JavaDoc[] SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE = {
46         "org.netbeans.test.subtypes.AbstractList",
47         "org.netbeans.test.subtypes.AbstractSet",
48         "org.netbeans.test.subtypes.HashMap.Values",
49         "org.netbeans.test.subtypes.ArrayList",
50         "org.netbeans.test.subtypes.HashSet"
51     };
52     
53     public static final String JavaDoc[] SUBTYPES_COLLECTION = {
54         "org.netbeans.test.subtypes.Set",
55         "org.netbeans.test.subtypes.List",
56         "org.netbeans.test.subtypes.AbstractCollection"
57     };
58     
59     public static final String JavaDoc[] SUBTYPES_COLLECTION_TRANSITIVE = {
60         "org.netbeans.test.subtypes.AbstractList",
61         "org.netbeans.test.subtypes.AbstractCollection",
62         "org.netbeans.test.subtypes.List",
63         "org.netbeans.test.subtypes.HashMap.Values",
64         "org.netbeans.test.subtypes.AbstractSet",
65         "org.netbeans.test.subtypes.Set",
66         "org.netbeans.test.subtypes.HashSet",
67         "org.netbeans.test.subtypes.ArrayList"
68     };
69     
70     public static final String JavaDoc[] SUBCLASSES_ABSTRACT_COLLECTION = {
71         "org.netbeans.test.subtypes.AbstractSet",
72         "org.netbeans.test.subtypes.HashMap.Values",
73         "org.netbeans.test.subtypes.AbstractList"
74     };
75     
76     public static final String JavaDoc[] SUBCLASSES_COLLECTION = {};
77     public static final String JavaDoc[] IMPLEMENTORS_ABSTRACT_COLLECTION = {};
78     public static final String JavaDoc[] IMPLEMENTORS_COLLECTION = {
79         "org.netbeans.test.subtypes.Set",
80         "org.netbeans.test.subtypes.List",
81         "org.netbeans.test.subtypes.AbstractCollection"
82     };
83     
84     private JavaClass abstractCol;
85     private JavaClass col;
86     
87     /** Need to be defined because of JUnit */
88     public FindSubTypesTest(String JavaDoc name) {
89         super(name);
90     }
91     
92     public static NbTestSuite suite() {
93         return new NbTestSuite(FindSubTypesTest.class);
94     }
95     
96     /** Use for execution inside IDE */
97     public static void main(java.lang.String JavaDoc[] args) {
98         TestRunner.run(suite());
99     }
100     
101     public void setUp() {
102         abstractCol = Utility.findClass("org.netbeans.test.subtypes.AbstractCollection");
103         col = Utility.findClass("org.netbeans.test.subtypes.Collection");
104     }
105     
106     public void testFindAbstractCollectionSubTypes() {
107         compare(abstractCol.findSubTypes(false), SUBTYPES_ABSTRACT_COLLECTION);
108     }
109
110     public void testFindAbstractCollectionSubTypesTrans() {
111         compare(abstractCol.findSubTypes(true), SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE);
112     }
113
114     public void testFindCollectionSubTypes() {
115         compare(col.findSubTypes(false), SUBTYPES_COLLECTION);
116     }
117
118     public void testFindCollectionSubTypesTrans() {
119         compare(col.findSubTypes(true), SUBTYPES_COLLECTION_TRANSITIVE);
120     }
121     
122     public void testFindAbstractCollectionSubClasses() {
123         compare(abstractCol.getSubClasses(), SUBCLASSES_ABSTRACT_COLLECTION);
124     }
125
126     public void testFindCollectionSubClasses() {
127         compare(col.getSubClasses(), SUBCLASSES_COLLECTION);
128     }
129     
130     public void testFindAbstractCollectionImplementors() {
131         compare(abstractCol.getImplementors(), IMPLEMENTORS_ABSTRACT_COLLECTION);
132     }
133
134     public void testFindCollectionImplementors() {
135         compare(col.getImplementors(), IMPLEMENTORS_COLLECTION);
136     }
137     
138     private void compare(Collection JavaDoc col, String JavaDoc[] classNames) {
139         PrintStream JavaDoc log = getLog();
140         HashSet JavaDoc names = new HashSet JavaDoc(Arrays.asList(classNames));
141         
142         boolean passed = true;
143         for (Iterator JavaDoc it = col.iterator(); it.hasNext();) {
144             String JavaDoc name = ((JavaClass) it.next()).getName();
145             if (!names.remove(name)) {
146                 log.println("Unexpected type found: " + name);
147                 passed = false;
148             }
149         }
150         
151         if (!names.isEmpty()) {
152             log.println();
153             log.println("Missing types:");
154             passed = false;
155             for (Iterator JavaDoc it = names.iterator(); it.hasNext();) {
156                 log.println(it.next());
157             }
158         }
159         if (!passed) {
160             fail("Test failed, see log file for details");
161         }
162     }
163 }
164
Popular Tags