KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > usages > WhereUsedTestCase


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.test.j2ee.refactoring.usages;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import org.netbeans.jmi.javamodel.Constructor;
24 import org.netbeans.jmi.javamodel.Feature;
25 import org.netbeans.jmi.javamodel.Method;
26 import org.netbeans.modules.refactoring.api.RefactoringSession;
27 import org.netbeans.modules.refactoring.api.WhereUsedQuery;
28 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase;
29 import org.netbeans.test.j2ee.refactoring.Utility;
30
31 /**
32  *
33  * @author eh103527
34  */

35 public class WhereUsedTestCase extends RefactoringTestCase {
36     
37     public static final int CLASS_USAGES=0;
38     public static final int CLASS_SUBTYPES=1;
39     public static final int CLASS_DIRECTSUBTYPES=2;
40     
41     /** Creates a new instance of WhereUsedTestCase */
42     public WhereUsedTestCase(String JavaDoc name) {
43         super(name);
44     }
45     
46     /*
47      * Don't search in comments by default.
48      */

49     protected void findClass(String JavaDoc className) {
50         setJavaClass(className);
51         Utility.beginTrans(false);
52         try {
53             ref("Find usages of: "+className);
54             WhereUsedQuery wu=new WhereUsedQuery(jc);
55             wu.setSearchInComments(false);
56             RefactoringSession result = RefactoringSession.create(null);
57             refProblems(wu.prepare(result));
58             refUsages(result);
59         } finally {
60             Utility.endTrans();
61         }
62         ref("");
63     }
64     
65     protected void findClassParams(String JavaDoc className, int type) {
66         setJavaClass(className);
67         Utility.beginTrans(false);
68         try {
69             ref("Find usages of: "+className);
70             WhereUsedQuery wu=new WhereUsedQuery(jc);
71             switch (type) {
72                 case CLASS_USAGES:
73                     wu.setFindUsages(true);
74                     break;
75                 case CLASS_SUBTYPES:
76                     wu.setFindSubclasses(true);
77                     break;
78                 case CLASS_DIRECTSUBTYPES:
79                     wu.setFindDirectSubclassesOnly(true);
80                     break;
81             }
82             wu.setSearchInComments(true);
83             if (refProblems(wu.checkParameters())) throw new Exception JavaDoc("Fatal problem");
84             RefactoringSession result = RefactoringSession.create(null);
85             refProblems(wu.prepare(result));
86             refUsages(result);
87         } catch (Throwable JavaDoc t) {
88             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
89                 t.printStackTrace(getLogStream());
90                 assertTrue(t.getMessage(), false);
91             }
92         } finally {
93             Utility.endTrans();
94         }
95         ref("");
96     }
97     
98     protected void findField(String JavaDoc className, String JavaDoc fieldName) {
99         setJavaClass(className);
100         Utility.beginTrans(false);
101         try {
102             org.netbeans.jmi.javamodel.Field field = jc.getField(fieldName, false);
103             assertNotNull("Field "+fieldName+" of "+className+" is null", field);
104             ref("Find usages of: "+fieldName);
105             WhereUsedQuery wu=new WhereUsedQuery(field);
106             wu.setSearchInComments(true);
107             RefactoringSession result = RefactoringSession.create(null);
108             refProblems(wu.prepare(result));
109             refUsages(result);
110         } finally {
111             Utility.endTrans();
112         }
113         ref("");
114     }
115
116     protected void findAnnotationTypeMember(String JavaDoc annotationName, String JavaDoc memberName) {
117         setJavaClass(annotationName);
118         Utility.beginTrans(false);
119         try {
120             Feature member = null;
121             for (Iterator JavaDoc it=jc.getFeatures().iterator();it.hasNext();) {
122                 Feature f=(Feature)(it.next());
123                 if (f.getName().equals(memberName)) {
124                     member=f;
125                     break;
126                 }
127             }
128             assertNotNull("Member "+memberName+" of "+annotationName+" is null", member);
129             ref("Find usages of: "+memberName);
130             WhereUsedQuery wu=new WhereUsedQuery(member);
131             wu.setSearchInComments(true);
132             RefactoringSession result = RefactoringSession.create(null);
133             refProblems(wu.prepare(result));
134             refUsages(result);
135         } finally {
136             Utility.endTrans();
137         }
138         ref("");
139     }
140     
141     protected void findConstructor(String JavaDoc className, String JavaDoc[] arguments) {
142         setJavaClass(className);
143         Utility.beginTrans(false);
144         try {
145             ArrayList JavaDoc list=new ArrayList JavaDoc();
146             String JavaDoc refs="Find usages of "+className+"(";
147             if (arguments != null && arguments.length > 0) {
148                 for (int i=0;i < arguments.length;i++) {
149                     list.add(Utility.findType(arguments[i], typeProxy, jc));
150                     if (i > 0) {
151                         refs+=", ";
152                     }
153                     refs+=arguments[i];
154                 }
155             }
156             refs+=")";
157             ref(refs);
158             Constructor cons = jc.getConstructor(list, false);
159             WhereUsedQuery wu=new WhereUsedQuery(cons);
160             wu.setSearchInComments(true);
161             RefactoringSession result = RefactoringSession.create(null);
162             refProblems(wu.prepare(result));
163             refUsages(result);
164         } finally {
165             Utility.endTrans();
166         }
167         ref("");
168     }
169     
170     protected void findMethod(String JavaDoc className, String JavaDoc methodName, String JavaDoc[] arguments) {
171         setJavaClass(className);
172         Utility.beginTrans(false);
173         try {
174             ArrayList JavaDoc list=new ArrayList JavaDoc();
175             String JavaDoc refs="Find usages of "+className+"."+methodName+"(";
176             if (arguments != null && arguments.length > 0) {
177                 for (int i=0;i < arguments.length;i++) {
178                     list.add(Utility.findType(arguments[i], typeProxy, jc));
179                     if (i > 0) {
180                         refs+=", ";
181                     }
182                     refs+=arguments[i];
183                 }
184             }
185             refs+=")";
186             ref(refs);
187             Method method = jc.getMethod(methodName, list, false);
188             assertNotNull("Method "+refs+" of "+className+" is null", method);
189             WhereUsedQuery wu=new WhereUsedQuery(method);
190             wu.setSearchInComments(true);
191             RefactoringSession result = RefactoringSession.create(null);
192             refProblems(wu.prepare(result));
193             refUsages(result);
194         } finally {
195             Utility.endTrans();
196         }
197         ref("");
198     }
199     
200     protected void findMethodParams(String JavaDoc className, String JavaDoc methodName, String JavaDoc[] arguments, boolean fromBaseClass, boolean methodOverriders, boolean findUsages) {
201         setJavaClass(className);
202         Utility.beginTrans(false);
203         try {
204             ArrayList JavaDoc list=new ArrayList JavaDoc();
205             String JavaDoc refs="Find usages of "+className+"."+methodName+"(";
206             if (arguments != null && arguments.length > 0) {
207                 for (int i=0;i < arguments.length;i++) {
208                     list.add(typeProxy.resolve(arguments[i]));
209                     if (i > 0) {
210                         refs+=", ";
211                     }
212                     refs+=arguments[i];
213                 }
214             }
215             refs+=")";
216             ref(refs);
217             Method method = jc.getMethod(methodName, list, false);
218             assertNotNull("Method "+refs+" of "+className+" is null", method);
219             WhereUsedQuery wu=new WhereUsedQuery(method);
220             wu.setSearchFromBaseClass(fromBaseClass);
221             wu.setFindOverridingMethods(methodOverriders);
222             wu.setFindUsages(findUsages);
223             wu.setSearchInComments(true);
224             if (refProblems(wu.checkParameters())) throw new Exception JavaDoc("Fatal problem");
225             RefactoringSession result = RefactoringSession.create(null);
226             refProblems(wu.prepare(result));
227             refUsages(result);
228         } catch (Throwable JavaDoc t) {
229             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
230                 t.printStackTrace(getLogStream());
231                 assertTrue(t.getMessage(), false);
232             }
233         } finally {
234             Utility.endTrans();
235         }
236         ref("");
237     }
238     
239 }
240
Popular Tags