KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
46  * Tests re-generating of names in different elements.
47  *
48  * @author Pavel Flaska
49  */

50 public class RenameTestField extends CodegenTestCase {
51     
52     /** Need to be defined because of JUnit */
53     public RenameTestField(String JavaDoc 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 JavaDoc newName = "renamedStr";
68     
69     protected void setUp() {
70         clazz = Utility.findClass("org.netbeans.test.codegen.RenameTestField");
71         pkg = (JavaModelPackage) clazz.refImmediatePackage();
72         // initialize methods
73
int i = 0;
74         for (Iterator JavaDoc 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 JavaDoc 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             // for...
95
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             // strToBeRenamed += " is not a trademark.";
102
ass = (Assignment) ((ExpressionStatement) st.get(1)).getExpression();
103             va = (VariableAccess) ass.getLeftSide();
104             va.setName(newName);
105             
106             // String nuovo = strToBeRenamed;
107
LocalVarDeclaration lvd = (LocalVarDeclaration) st.get(2);
108             LocalVariable lv = (LocalVariable) lvd.getVariables().get(0);
109             lv.setInitialValueText(newName);
110             
111             // StringBuffer buf = new StringBuffer(strToBeRenamed);
112
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             // strToBeRenamed = "Do not change!";
119
ass = (Assignment) ((ExpressionStatement) st.get(4)).getExpression();
120             va = (VariableAccess) ass.getLeftSide();
121             va.setName(newName);
122             
123             // getLength(strToBeRenamed);
124
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     /**
139      * @param args the command line arguments
140      */

141     public static void main(String JavaDoc[] args) {
142         TestRunner.run(suite());
143     }
144     
145 }
146
Popular Tags