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.Field; 29 import org.netbeans.jmi.javamodel.ForStatement; 30 import org.netbeans.jmi.javamodel.IfStatement; 31 import org.netbeans.jmi.javamodel.InfixExpression; 32 import org.netbeans.jmi.javamodel.JavaClass; 33 import org.netbeans.jmi.javamodel.JavaModelPackage; 34 import org.netbeans.jmi.javamodel.Literal; 35 import org.netbeans.jmi.javamodel.LocalVarDeclaration; 36 import org.netbeans.jmi.javamodel.LocalVariable; 37 import org.netbeans.jmi.javamodel.Method; 38 import org.netbeans.jmi.javamodel.MethodInvocation; 39 import org.netbeans.jmi.javamodel.NewClassExpression; 40 import org.netbeans.jmi.javamodel.ReturnStatement; 41 import org.netbeans.jmi.javamodel.StatementBlock; 42 import org.netbeans.jmi.javamodel.Type; 43 import org.netbeans.jmi.javamodel.TypeCast; 44 import org.netbeans.jmi.javamodel.codegen.Utility; 45 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase; 46 import org.netbeans.jmi.javamodel.codegen.Utility; 47 import org.netbeans.junit.NbTestSuite; 48 49 54 public class RenameTestClass extends CodegenTestCase { 55 56 57 public RenameTestClass(String name) { 58 super(name, "RenameTestClass"); 59 } 60 61 public static NbTestSuite suite() { 62 NbTestSuite suite = new NbTestSuite(); 63 suite.addTest(new RenameTestClass("testRename")); 64 return suite; 65 } 66 67 JavaClass clazz; 68 JavaModelPackage pkg; 69 Feature[] feature = new Feature[4]; 70 71 private static final String newName = "RenamedClass"; 72 73 protected void setUp() { 74 clazz = Utility.findClass("org.netbeans.test.codegen.RenameTestClass"); 75 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 76 int i = 0; 78 for (Iterator fIt = clazz.getFeatures().iterator(); fIt.hasNext(); i++) { 79 feature[i] = (Feature) fIt.next(); 80 } 81 } 82 83 public void testRename() { 84 boolean fail = true; 85 Utility.beginTrans(true); 86 try { 87 ((JavaClass) feature[3]).setSimpleName(newName); 88 JavaClass inner = (JavaClass) pkg.getType().resolve("org.netbeans.test.codegen.RenameTestClass.RenamedClass"); 89 Field field = (Field) feature[0]; 91 field.getTypeName().setName(newName); 92 93 Constructor constructor = (Constructor) feature[1]; 95 StatementBlock body = constructor.getBody(); 96 List statement = body.getStatements(); 97 Assignment asi = (Assignment) ((ExpressionStatement) statement.get(0)).getExpression(); 99 NewClassExpression nce = (NewClassExpression) asi.getRightSide(); 100 nce.getClassName().setName(newName); 101 102 Method method = (Method) feature[2]; 104 body = method.getBody(); 105 statement = body.getStatements(); 106 method.getTypeName().setName(newName); 108 LocalVarDeclaration lvd = (LocalVarDeclaration) statement.get(0); 110 lvd.getTypeName().setName(newName); 111 lvd = (LocalVarDeclaration) statement.get(2); 113 lvd.getTypeName().setName(newName); 114 LocalVariable lv = (LocalVariable) lvd.getVariables().get(0); 115 TypeCast typeCast = (TypeCast) lv.getInitialValue(); 116 typeCast.getTypeName().setName(newName); 117 ReturnStatement retSt = (ReturnStatement) statement.get(4); 119 nce = (NewClassExpression) retSt.getExpression(); 120 nce.getClassName().setName(newName); 121 122 fail = false; 123 124 } 125 finally { 126 Utility.endTrans(fail); 127 } 128 makeDiff("testRename"); 129 } 130 131 134 public static void main(String [] args) { 135 TestRunner.run(suite()); 136 } 137 138 } 139 | Popular Tags |