KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > rename > RenameTestCase


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.rename;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.Field;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.jmi.javamodel.JavaPackage;
28 import org.netbeans.jmi.javamodel.LocalVarDeclaration;
29 import org.netbeans.jmi.javamodel.LocalVariable;
30 import org.netbeans.jmi.javamodel.Method;
31 import org.netbeans.jmi.javamodel.Parameter;
32 import javax.jmi.reflect.RefObject;
33 import org.netbeans.jmi.javamodel.Feature;
34 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
35 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
36 import org.netbeans.modules.refactoring.api.RefactoringSession;
37 import org.netbeans.modules.refactoring.api.RenameRefactoring;
38 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase;
39 import org.netbeans.test.j2ee.refactoring.Utility;
40 import org.openide.filesystems.FileObject;
41
42
43 /**
44  *
45  * @author eh103527
46  */

47 public class RenameTestCase extends RefactoringTestCase {
48     
49     /** Creates a new instance of RenameTestCase */
50     public RenameTestCase(String JavaDoc name) {
51         super(name);
52     }
53     
54     protected void renameClass(String JavaDoc name, String JavaDoc newName, boolean renameInComments, boolean undo) {
55         setJavaClass(name);
56         ref("Rename class "+name+" to "+newName);
57         rename(jc, name, newName, renameInComments, undo);
58     }
59     
60     protected void renameAnnotationMember(String JavaDoc annotationName, String JavaDoc memberName, String JavaDoc newMemberName, boolean renameInComments, boolean undo) {
61         setJavaClass(annotationName);
62         ref("Rename annotation member "+memberName+" to "+newMemberName+" of AnnotationType "+annotationName);
63         Feature member=null;
64         JavaMetamodel.getDefaultRepository().beginTrans(true);
65         try {
66             for (Iterator JavaDoc it=jc.getFeatures().iterator();it.hasNext();) {
67                 Feature f=(Feature)(it.next());
68                 if (f.getName().equals(memberName)) {
69                     member=f;
70                     break;
71                 }
72             }
73         } catch (Throwable JavaDoc t) {
74             t.printStackTrace(getLogStream());
75             assertTrue(t.getMessage(), false);
76         } finally {
77             JavaMetamodel.getDefaultRepository().endTrans();
78         }
79         assertNotNull(memberName+" of "+annotationName+" is null.", member);
80         rename(member, memberName, newMemberName, renameInComments, undo);
81     }
82     
83     protected void renameField(String JavaDoc className, String JavaDoc fieldName, String JavaDoc newFieldName, boolean renameInComments, boolean undo) {
84         setJavaClass(className);
85         ref("Rename field "+fieldName+" to "+newFieldName+" of class "+className);
86         Field field=null;
87         JavaMetamodel.getDefaultRepository().beginTrans(true);
88         try {
89             field = jc.getField(fieldName, false);
90         } catch (Throwable JavaDoc t) {
91             t.printStackTrace(getLogStream());
92             assertTrue(t.getMessage(), false);
93         } finally {
94             JavaMetamodel.getDefaultRepository().endTrans();
95         }
96         rename(field, fieldName, newFieldName, renameInComments, undo);
97     }
98     
99     protected void renameMethodParameter(String JavaDoc className, String JavaDoc methodName, String JavaDoc[] parameters, String JavaDoc fieldName, String JavaDoc newFieldName, boolean renameInComments, boolean undo) {
100         setJavaClass(className);
101         JavaMetamodel.getDefaultRepository().beginTrans(true);
102         Parameter param=null;
103         try {
104             String JavaDoc args="";
105             ArrayList JavaDoc list=new ArrayList JavaDoc();
106             Method method = null;
107             if (parameters != null) {
108                 for (int i=0;i < parameters.length;i++) {
109                     list.add(Utility.findType(parameters[i], typeProxy, jc));
110                     args+=parameters[i];
111                     if (i < parameters.length-1) {
112                         args+=", ";
113                     }
114                 }
115             }
116             ref("Rename method's "+methodName+"("+args+") parameter "+fieldName+" to "+newFieldName+" of class "+className);
117             method = jc.getMethod(methodName, list, false);
118             List JavaDoc params=method.getParameters();
119             
120             for (Iterator JavaDoc it=params.iterator();it.hasNext();) {
121                 Parameter par=(Parameter)(it.next());
122                 if (par.getName().equals(fieldName)) {
123                     param=par;
124                     break;
125                 }
126             }
127         } catch (Throwable JavaDoc t) {
128             t.printStackTrace(getLogStream());
129             assertTrue(t.getMessage(), false);
130         } finally {
131             JavaMetamodel.getDefaultRepository().endTrans();
132         }
133         
134         rename(param, fieldName, newFieldName, renameInComments, undo);
135     }
136     
137     protected void renameLocalVariable(String JavaDoc className, String JavaDoc methodName, String JavaDoc[] parameters, int declarationStatementIndex, String JavaDoc fieldName, String JavaDoc newFieldName, boolean renameInComments, boolean undo) {
138         setJavaClass(className);
139         JavaMetamodel.getDefaultRepository().beginTrans(true);
140         LocalVariable variable=null;
141         try {
142             String JavaDoc args="";
143             ArrayList JavaDoc list=new ArrayList JavaDoc();
144             Method method = null;
145             if (parameters != null) {
146                 for (int i=0;i < parameters.length;i++) {
147                     list.add(Utility.findType(parameters[i], typeProxy, jc));
148                     args+=parameters[i];
149                     if (i < parameters.length-1) {
150                         args+=", ";
151                     }
152                 }
153             }
154             ref("Rename method's "+methodName+"("+args+") local variable "+fieldName+" to "+newFieldName+" of class "+className);
155             method = jc.getMethod(methodName, list, false);
156             List JavaDoc stmtList=method.getBody().getStatements();
157             LocalVarDeclaration locDecl=(LocalVarDeclaration)stmtList.get(declarationStatementIndex);
158             List JavaDoc vars=locDecl.getVariables();
159             for (Iterator JavaDoc it=vars.iterator();it.hasNext();) {
160                 LocalVariable par=(LocalVariable)(it.next());
161                 if (par.getName().equals(fieldName)) {
162                     variable=par;
163                     break;
164                 }
165             }
166         } catch (Throwable JavaDoc t) {
167             t.printStackTrace(getLogStream());
168             assertTrue(t.getMessage(), false);
169         } finally {
170             JavaMetamodel.getDefaultRepository().endTrans();
171         }
172         
173         rename(variable, fieldName, newFieldName, renameInComments, undo);
174     }
175     
176     protected void renameMethod(String JavaDoc className, String JavaDoc methodName, String JavaDoc[] attributes, String JavaDoc newMethodName, boolean renameInComments, boolean undo) {
177         setJavaClass(className);
178         String JavaDoc args="";
179         ArrayList JavaDoc list=new ArrayList JavaDoc();
180         Method method = null;
181         JavaMetamodel.getDefaultRepository().beginTrans(true);
182         try {
183             if (attributes != null) {
184                 for (int i=0;i < attributes.length;i++) {
185                     list.add(Utility.findType(attributes[i], typeProxy, jc));
186                     args+=attributes[i];
187                     if (i < attributes.length-1) {
188                         args+=", ";
189                     }
190                 }
191             }
192             ref("Rename method "+methodName+"("+args+") to "+newMethodName+" of class "+className);
193             method = jc.getMethod(methodName, list, false);
194         } catch (Throwable JavaDoc t) {
195             t.printStackTrace(getLogStream());
196             assertTrue(t.getMessage(), false);
197         } finally {
198             JavaMetamodel.getDefaultRepository().endTrans();
199         }
200         rename(method, methodName, newMethodName, renameInComments, undo);
201     }
202     
203     //through UI
204
protected void renamePackage(String JavaDoc packageName, String JavaDoc newPackageName, String JavaDoc initClass, boolean undo) {
205         setJavaClass(initClass);
206         Utility.prepareTest();
207         ref("Rename package "+packageName+" to "+newPackageName);
208         RefactoringSession result = RefactoringSession.create("Rename package");
209         boolean finish=false;
210         HashMap JavaDoc files=null;
211         try {
212             JavaPackage pack=((JavaModelPackage)(jc.refOutermostPackage())).getJavaPackage().resolvePackage(packageName);
213             FileObject sourceFolder = JavaMetamodel.getManager().getClassPath().findResource(pack.getName().replace('.','/'));
214             if (sourceFolder == null) {
215                 ref("Renamed package folder is null.");
216                 throw new Exception JavaDoc("Fatal problem");
217             }
218             MoveClassRefactoring refactoring = new MoveClassRefactoring(sourceFolder, true);
219             
220             if (refProblems(refactoring.preCheck())) throw new Exception JavaDoc("Fatal problem");
221             refactoring.setTargetPackageName(newPackageName);
222             if (refProblems(refactoring.checkParameters())) throw new Exception JavaDoc("Fatal problem");
223             if (refProblems(refactoring.prepare(result))) throw new Exception JavaDoc("Fatal problem");
224             files=getResources(result);
225             if (refProblems(result.doRefactoring(true))) throw new Exception JavaDoc("Fatal problem");
226             finish=true;
227         } catch (Throwable JavaDoc t) {
228             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
229                 t.printStackTrace(getLogStream());
230                 assertTrue(t.getMessage(), false);
231             }
232         } finally {
233             Utility.finishTest();
234         }
235         if (finish) {
236             try {
237                 Thread.sleep(2000);
238                 if (undo) {
239                     JavaMetamodel.getUndoManager().undo();
240                     Thread.sleep(2000);
241                 }
242                 compareResources(files, result, packageName, newPackageName);
243             } catch (Exception JavaDoc ex) {
244                 ex.printStackTrace(getLogStream());
245                 assertTrue(ex.getMessage(), false);
246             }
247         }
248     }
249     
250     protected void rename(RefObject object, String JavaDoc name, String JavaDoc newName, boolean renameInComments, boolean undo) {
251         Utility.prepareTest();
252         HashMap JavaDoc files=null;
253         RefactoringSession result = RefactoringSession.create("Rename");
254         boolean finish=false;
255         try {
256             RenameRefactoring refactoring = new RenameRefactoring(object);
257             if (refProblems(refactoring.preCheck())) throw new Exception JavaDoc("Fatal problem");
258             refactoring.setNewName(newName);
259             refactoring.setSearchInComments(renameInComments);
260             if (refProblems(refactoring.checkParameters())) throw new Exception JavaDoc("Fatal problem");
261             if (refProblems(refactoring.prepare(result))) throw new Exception JavaDoc("Fatal problem");
262             files=getResources(result);
263             if (refProblems(result.doRefactoring(true))) throw new Exception JavaDoc("Fatal problem");
264             finish=true;
265         } catch (Throwable JavaDoc t) {
266             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
267                 t.printStackTrace(getLogStream());
268                 assertTrue(t.getMessage(), false);
269             }
270         } finally {
271             Utility.finishTest();
272         }
273         if (finish) {
274             String JavaDoc nm = name.substring(0, name.lastIndexOf('.') + 1) + newName;
275             try {
276                 if (undo) {
277                     JavaMetamodel.getUndoManager().undo();
278                 }
279                 compareResources(files, result, name, nm);
280             } catch (Exception JavaDoc ex) {
281                 ex.printStackTrace(getLogStream());
282                 assertTrue(ex.getMessage(), false);
283             }
284         }
285     }
286 }
287
Popular Tags