1 19 package org.netbeans.test.j2ee.refactoring.rename; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 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 47 public class RenameTestCase extends RefactoringTestCase { 48 49 50 public RenameTestCase(String name) { 51 super(name); 52 } 53 54 protected void renameClass(String name, String 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 annotationName, String memberName, String 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 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 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 className, String fieldName, String 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 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 className, String methodName, String [] parameters, String fieldName, String newFieldName, boolean renameInComments, boolean undo) { 100 setJavaClass(className); 101 JavaMetamodel.getDefaultRepository().beginTrans(true); 102 Parameter param=null; 103 try { 104 String args=""; 105 ArrayList list=new ArrayList (); 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 params=method.getParameters(); 119 120 for (Iterator 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 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 className, String methodName, String [] parameters, int declarationStatementIndex, String fieldName, String newFieldName, boolean renameInComments, boolean undo) { 138 setJavaClass(className); 139 JavaMetamodel.getDefaultRepository().beginTrans(true); 140 LocalVariable variable=null; 141 try { 142 String args=""; 143 ArrayList list=new ArrayList (); 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 stmtList=method.getBody().getStatements(); 157 LocalVarDeclaration locDecl=(LocalVarDeclaration)stmtList.get(declarationStatementIndex); 158 List vars=locDecl.getVariables(); 159 for (Iterator 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 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 className, String methodName, String [] attributes, String newMethodName, boolean renameInComments, boolean undo) { 177 setJavaClass(className); 178 String args=""; 179 ArrayList list=new ArrayList (); 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 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 protected void renamePackage(String packageName, String newPackageName, String 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 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 ("Fatal problem"); 217 } 218 MoveClassRefactoring refactoring = new MoveClassRefactoring(sourceFolder, true); 219 220 if (refProblems(refactoring.preCheck())) throw new Exception ("Fatal problem"); 221 refactoring.setTargetPackageName(newPackageName); 222 if (refProblems(refactoring.checkParameters())) throw new Exception ("Fatal problem"); 223 if (refProblems(refactoring.prepare(result))) throw new Exception ("Fatal problem"); 224 files=getResources(result); 225 if (refProblems(result.doRefactoring(true))) throw new Exception ("Fatal problem"); 226 finish=true; 227 } catch (Throwable 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 ex) { 244 ex.printStackTrace(getLogStream()); 245 assertTrue(ex.getMessage(), false); 246 } 247 } 248 } 249 250 protected void rename(RefObject object, String name, String newName, boolean renameInComments, boolean undo) { 251 Utility.prepareTest(); 252 HashMap 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 ("Fatal problem"); 258 refactoring.setNewName(newName); 259 refactoring.setSearchInComments(renameInComments); 260 if (refProblems(refactoring.checkParameters())) throw new Exception ("Fatal problem"); 261 if (refProblems(refactoring.prepare(result))) throw new Exception ("Fatal problem"); 262 files=getResources(result); 263 if (refProblems(result.doRefactoring(true))) throw new Exception ("Fatal problem"); 264 finish=true; 265 } catch (Throwable 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 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 ex) { 281 ex.printStackTrace(getLogStream()); 282 assertTrue(ex.getMessage(), false); 283 } 284 } 285 } 286 } 287 | Popular Tags |