KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > RenameTests > RenameTestClass


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.jmi.javamodel.codegen.RenameTests;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
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 /**
50  * Tests re-generating of names in different elements.
51  *
52  * @author Pavel Flaska
53  */

54 public class RenameTestClass extends CodegenTestCase {
55     
56     /** Need to be defined because of JUnit */
57     public RenameTestClass(String JavaDoc 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 JavaDoc newName = "RenamedClass";
72     
73     protected void setUp() {
74         clazz = Utility.findClass("org.netbeans.test.codegen.RenameTestClass");
75         pkg = (JavaModelPackage) clazz.refImmediatePackage();
76         // initialize methods
77
int i = 0;
78         for (Iterator JavaDoc 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             // private ClassToRename renamedCl;
90
Field field = (Field) feature[0];
91             field.getTypeName().setName(newName);
92
93             // find constructor
94
Constructor constructor = (Constructor) feature[1];
95             StatementBlock body = constructor.getBody();
96             List JavaDoc statement = body.getStatements();
97             // renamedCl = new ClassToRename(5);
98
Assignment asi = (Assignment) ((ExpressionStatement) statement.get(0)).getExpression();
99             NewClassExpression nce = (NewClassExpression) asi.getRightSide();
100             nce.getClassName().setName(newName);
101             
102             // get method
103
Method method = (Method) feature[2];
104             body = method.getBody();
105             statement = body.getStatements();
106             // set return type
107
method.getTypeName().setName(newName);
108             // ClassToRename confusingVar;
109
LocalVarDeclaration lvd = (LocalVarDeclaration) statement.get(0);
110             lvd.getTypeName().setName(newName);
111             // ClassToRename result = (ClassToRename) confusingVar;
112
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             // return new ClassToRename(x);
118
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     /**
132      * @param args the command line arguments
133      */

134     public static void main(String JavaDoc[] args) {
135         TestRunner.run(suite());
136     }
137     
138 }
139
Popular Tags