1 19 package org.netbeans.jmi.javamodel.codegen.RenameTests; 20 21 import java.util.Iterator ; 22 import java.util.List ; 23 import junit.textui.TestRunner; 24 import org.netbeans.jmi.javamodel.Assignment; 25 import org.netbeans.jmi.javamodel.Constructor; 26 import org.netbeans.jmi.javamodel.ExpressionStatement; 27 import org.netbeans.jmi.javamodel.Feature; 28 import org.netbeans.jmi.javamodel.ForStatement; 29 import org.netbeans.jmi.javamodel.InfixExpression; 30 import org.netbeans.jmi.javamodel.JavaClass; 31 import org.netbeans.jmi.javamodel.JavaModelPackage; 32 import org.netbeans.jmi.javamodel.Literal; 33 import org.netbeans.jmi.javamodel.LocalVarDeclaration; 34 import org.netbeans.jmi.javamodel.LocalVariable; 35 import org.netbeans.jmi.javamodel.Method; 36 import org.netbeans.jmi.javamodel.MethodInvocation; 37 import org.netbeans.jmi.javamodel.NewClassExpression; 38 import org.netbeans.jmi.javamodel.StatementBlock; 39 import org.netbeans.jmi.javamodel.codegen.Utility; 40 import org.netbeans.jmi.javamodel.VariableAccess; 41 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase; 42 import org.netbeans.jmi.javamodel.codegen.Utility; 43 import org.netbeans.junit.NbTestSuite; 44 45 50 public class RenameTestField extends CodegenTestCase { 51 52 53 public RenameTestField(String name) { 54 super(name, "RenameTestField"); 55 } 56 57 public static NbTestSuite suite() { 58 NbTestSuite suite = new NbTestSuite(); 59 suite.addTest(new RenameTestField("testRename")); 60 return suite; 61 } 62 63 JavaClass clazz; 64 JavaModelPackage pkg; 65 Feature[] feature = new Feature[4]; 66 67 private static final String newName = "renamedStr"; 68 69 protected void setUp() { 70 clazz = Utility.findClass("org.netbeans.test.codegen.RenameTestField"); 71 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 72 int i = 0; 74 for (Iterator fIt = clazz.getFeatures().iterator(); fIt.hasNext(); i++) { 75 feature[i] = (Feature) fIt.next(); 76 } 77 } 78 79 public void testRename() { 80 boolean fail = true; 81 Utility.beginTrans(true); 82 try { 83 Constructor constructor = (Constructor) feature[1]; 84 StatementBlock body = constructor.getBody(); 85 List st = body.getStatements(); 86 Assignment ass = (Assignment) ((ExpressionStatement) st.get(0)).getExpression(); 87 VariableAccess va = (VariableAccess) ass.getLeftSide(); 88 va.setName(newName); 89 90 Method method = (Method) feature[2]; 91 body = method.getBody(); 92 st = body.getStatements(); 93 94 ForStatement fs = (ForStatement) st.get(0); 96 InfixExpression ie = (InfixExpression) fs.getExpression(); 97 MethodInvocation mi = (MethodInvocation) ie.getRightSide(); 98 va = (VariableAccess) mi.getParentClass(); 99 va.setName(newName); 100 101 ass = (Assignment) ((ExpressionStatement) st.get(1)).getExpression(); 103 va = (VariableAccess) ass.getLeftSide(); 104 va.setName(newName); 105 106 LocalVarDeclaration lvd = (LocalVarDeclaration) st.get(2); 108 LocalVariable lv = (LocalVariable) lvd.getVariables().get(0); 109 lv.setInitialValueText(newName); 110 111 lvd = (LocalVarDeclaration) st.get(3); 113 lv = (LocalVariable) lvd.getVariables().get(0); 114 NewClassExpression nce = (NewClassExpression) lv.getInitialValue(); 115 va = (VariableAccess) nce.getParameters().get(0); 116 va.setName(newName); 117 118 ass = (Assignment) ((ExpressionStatement) st.get(4)).getExpression(); 120 va = (VariableAccess) ass.getLeftSide(); 121 va.setName(newName); 122 123 mi = (MethodInvocation) ((ExpressionStatement) st.get(5)).getExpression(); 125 va = (VariableAccess) mi.getParameters().get(0); 126 va.setName(newName); 127 128 feature[0].setName(newName); 129 130 fail = false; 131 } 132 finally { 133 Utility.endTrans(fail); 134 } 135 makeDiff("testRename"); 136 } 137 138 141 public static void main(String [] args) { 142 TestRunner.run(suite()); 143 } 144 145 } 146 | Popular Tags |