KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > classindex > ClassIndexTest


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 package org.netbeans.modules.javacore.classindex;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import junit.framework.AssertionFailedError;
26 import junit.textui.TestRunner;
27 import org.netbeans.jmi.javamodel.JavaClass;
28 import org.netbeans.jmi.javamodel.JavaModelPackage;
29 import org.netbeans.jmi.javamodel.codegen.Utility;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.junit.NbTestSuite;
32 import org.netbeans.modules.javacore.ClassIndex;
33 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
34
35 /**
36  * Tests class index. Basic tests for looking for classes in index.
37  * Doesn't cover all functionality, only tests method getClassesBySimpleName()
38  * and getClassesBySNPrefix() from the class index.
39  *
40  * @author Pavel Flaska
41  */

42 public class ClassIndexTest extends NbTestCase {
43     private static final String JavaDoc NAME = "ClassIndexTest";
44     private static final String JavaDoc PREFIX = "ClassIndex";
45     
46     JavaModelPackage model;
47     ClassIndex index;
48     
49     /** Creates a new instance of ClassIndexTest */
50     public ClassIndexTest() {
51         super("ClassIndexTest");
52     }
53     
54     public static NbTestSuite suite() {
55         NbTestSuite suite = new NbTestSuite(ClassIndexTest.class);
56         return suite;
57     }
58
59     protected void setUp() {
60         JavaClass clazz = Utility.findClass("org.netbeans.test.classindex.index1." + NAME);
61         model = (JavaModelPackage) clazz.refImmediatePackage();
62         index = ClassIndex.getIndex(model);
63     }
64
65     public void testCaseSensitive() {
66         Collection JavaDoc c = index.getClassesBySimpleName(NAME);
67         printFound(c);
68         if (c.size() != 1) {
69             throw new AssertionFailedError("Found #" + c.size() + " classes instead of #1.");
70         }
71     }
72     
73     public void testCaseInsensitive() {
74         Collection JavaDoc c = index.getClassesBySimpleName(NAME, false);
75         printFound(c);
76         if (c.size() != 3) {
77             throw new AssertionFailedError("Found #" + c.size() + " classes instead of #3.");
78         }
79     }
80     
81     public void testPrefixCaseSensitive() {
82         Collection JavaDoc c = index.getClassesBySNPrefix(PREFIX);
83         printFound(c);
84         if (c.size() != 2) {
85             throw new AssertionFailedError("Found #" + c.size() + " classes instead of #2.");
86         }
87     }
88     
89     public void testPrefixCaseInsensitive() {
90         Collection JavaDoc c = index.getClassesBySNPrefix(PREFIX, false);
91         printFound(c);
92         if (c.size() != 6) {
93             throw new AssertionFailedError("Found #" + c.size() + " classes instead of #6.");
94         }
95     }
96     
97     private void printFound(Collection JavaDoc c) {
98         for (Iterator JavaDoc it = c.iterator(); it.hasNext(); ) {
99             getLog().println(it.next());
100         }
101     }
102     /**
103      * @param args the command line arguments
104      */

105     public static void main(String JavaDoc[] args) {
106         TestRunner.run(suite());
107     }
108
109 }
110
Popular Tags