KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > getters > StringMethodsSigTest


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.jmi.javamodel.getters;
20
21 import java.io.PrintStream JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.jmi.javamodel.*;
28 import org.netbeans.jmi.javamodel.codegen.Utility;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.modules.javacore.ClassIndex;
32 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
33
34 /**
35  * Test contains two major parts. It tries to resolve hashCode and equals
36  * method in both - rt.jar and src.zip. Then, it checks signature of this
37  * both methods.
38  *
39  * @author Pavel Flaska
40  */

41 public class StringMethodsSigTest extends NbTestCase {
42     
43     JavaClass clazz;
44     JavaModelPackage pkg;
45     JavaMetamodel model;
46     
47     /** Creates a new instance of StringMethodsSigTest */
48     public StringMethodsSigTest() {
49         super("StringMethodsSigTest");
50     }
51
52     protected void setUp() {
53         clazz = (JavaClass) Utility.findClass("examples.colorpicker.ColorPicker");
54         pkg = (JavaModelPackage) clazz.refImmediatePackage();
55         model = JavaMetamodel.getManager();
56     }
57     
58     public static NbTestSuite suite() {
59         NbTestSuite suite = new NbTestSuite(StringMethodsSigTest.class);
60         return suite;
61     }
62
63     /**
64      * Tries to find String source file in src.zip, resolves it and check if
65      * correct equals and hashCode methods are resolved.
66      */

67     public void testMethodsFromSources() {
68         boolean fail = true;
69         Utility.beginTrans(true);
70         try {
71             model.setClassPath(model.getFileObject(clazz.getResource()), true);
72             doTest();
73             fail = false;
74         }
75         finally {
76             Utility.endTrans(fail);
77         }
78     }
79     
80     public void testMethodsFromSources2() {
81         boolean fail = true;
82         Utility.beginTrans(true);
83         try {
84             model.setClassPath(model.getFileObject(clazz.getResource()), true);
85             doTest2();
86             fail = false;
87         }
88         finally {
89             Utility.endTrans(fail);
90         }
91     }
92     
93     /**
94      * Tries to find String class in rt.jar, resolves it and check if
95      * correct equals and hashCode methods are resolved.
96      */

97     public void testMethodsFromClasses() {
98         boolean fail = true;
99         Utility.beginTrans(true);
100         try {
101             model.setClassPath(model.getFileObject(clazz.getResource()));
102             doTest();
103             fail = false;
104         }
105         finally {
106             Utility.endTrans(fail);
107         }
108     }
109     
110     public void testMethodsFromClasses2() {
111         boolean fail = true;
112         Utility.beginTrans(true);
113         try {
114             model.setClassPath(model.getFileObject(clazz.getResource()));
115             doTest2();
116             fail = false;
117         }
118         finally {
119             Utility.endTrans(fail);
120         }
121     }
122     
123     /**
124      * @param args the command line arguments
125      */

126     public static void main(String JavaDoc[] args) {
127         TestRunner.run(suite());
128     }
129     
130     ////////////////////////////////////////////////////////////////////////////
131
// PRIVATE METHODS
132
////////////////////////////////////////////////////////////////////////////
133
private void doTest() {
134         PrintStream JavaDoc log = getLog();
135         JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String");
136         Resource res = strType.getResource();
137         log.println("doTest: Found in resource '" + res.getName() + "'.");
138         Method method = strType.getMethod("hashCode", Collections.EMPTY_LIST, false);
139         assertNotNull(method);
140         log.println("\tMethod '" + method.getName() + "' found.");
141         log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'.");
142         assertEquals(true, Modifier.isFinal(method.getModifiers()));
143         Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object");
144         method = strType.getMethod("equals", Collections.singletonList(objectType), false);
145         log.println("\tMethod '" + method.getName() + "' found.");
146         assertNotNull(method);
147     }
148     
149     private void doTest2() {
150         PrintStream JavaDoc log = getLog();
151         JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String");
152         ParameterizedType parType = model.getDefaultExtent().getParameterizedType().resolveParameterizedType(strType, null, null);
153         Resource res = strType.getResource();
154         log.println("doTest2: Found in resource '" + res.getName() + "'.");
155         Method method = findMethod(parType, "hashCode");
156         assertNotNull(method);
157         log.println("\tMethod '" + method.getName() + "' found.");
158         log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'.");
159         assertEquals(true, Modifier.isFinal(method.getModifiers()));
160         Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object");
161         method = findMethod(parType, "equals");
162         log.println("\tMethod '" + method.getName() + "' found.");
163         assertNotNull(method);
164     }
165     
166     private Method findMethod(ParameterizedType type, String JavaDoc name) {
167         List JavaDoc features = type.getFeatures();
168         for (Iterator JavaDoc it = features.iterator(); it.hasNext(); ) {
169             Feature feature = (Feature) it.next();
170             if (name.equals(feature.getName()) && (feature instanceof Method)) {
171                 return (Method) feature;
172             }
173         }
174         return null;
175     }
176 }
177
Popular Tags