KickJava   Java API By Example, From Geeks To Geeks.

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


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.IfStatement;
30 import org.netbeans.jmi.javamodel.InfixExpression;
31 import org.netbeans.jmi.javamodel.JavaClass;
32 import org.netbeans.jmi.javamodel.JavaModelPackage;
33 import org.netbeans.jmi.javamodel.Literal;
34 import org.netbeans.jmi.javamodel.LocalVarDeclaration;
35 import org.netbeans.jmi.javamodel.LocalVariable;
36 import org.netbeans.jmi.javamodel.Method;
37 import org.netbeans.jmi.javamodel.MethodInvocation;
38 import org.netbeans.jmi.javamodel.NewClassExpression;
39 import org.netbeans.jmi.javamodel.StatementBlock;
40 import org.netbeans.jmi.javamodel.codegen.Utility;
41 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase;
42 import org.netbeans.junit.NbTestSuite;
43
44 /**
45  * Tests re-generating of names in different elements.
46  *
47  * @author Pavel Flaska
48  */

49 public class RenameTestMethod extends CodegenTestCase {
50     
51     /** Need to be defined because of JUnit */
52     public RenameTestMethod(String JavaDoc name) {
53         super(name, "RenameTestMethod");
54     }
55     
56     public static NbTestSuite suite() {
57         NbTestSuite suite = new NbTestSuite();
58         suite.addTest(new RenameTestMethod("testRename"));
59         return suite;
60     }
61     
62     JavaClass clazz;
63     JavaModelPackage pkg;
64     Feature[] feature = new Feature[5];
65
66     private static final String JavaDoc newName = "renamedMethod";
67     
68     protected void setUp() {
69         clazz = Utility.findClass("org.netbeans.test.codegen.RenameTestMethod");
70         pkg = (JavaModelPackage) clazz.refImmediatePackage();
71         // initialize methods
72
int i = 0;
73         for (Iterator JavaDoc fIt = clazz.getFeatures().iterator(); fIt.hasNext(); i++) {
74             feature[i] = (Feature) fIt.next();
75         }
76     }
77     
78     public void testRename() {
79         boolean fail = true;
80         Utility.beginTrans(true);
81         try {
82             feature[2].setName(newName);
83             
84             Method callerMethod = (Method) feature[3];
85             StatementBlock body = callerMethod.getBody();
86             List JavaDoc st = body.getStatements();
87             
88             // int a = methodToBeRenamed(5);
89
LocalVarDeclaration lvd = (LocalVarDeclaration) st.get(0);
90             LocalVariable lv = (LocalVariable) lvd.getVariables().get(0);
91             MethodInvocation mi = (MethodInvocation) lv.getInitialValue();
92             mi.setName(newName);
93
94             // a = methodToBeRenamed(5) - methodToBeRenamed(4);
95
Assignment ass = (Assignment) ((ExpressionStatement) st.get(1)).getExpression();
96             InfixExpression infx = (InfixExpression) ass.getRightSide();
97             mi = (MethodInvocation) infx.getLeftSide();
98             mi.setName(newName);
99             mi = (MethodInvocation) infx.getRightSide();
100             mi.setName(newName);
101             
102             // if (a != 0) {
103
// methodToBeRenamed(methodToBeRenamed(c));
104
// }
105
IfStatement ifstmt = (IfStatement) st.get(2);
106             StatementBlock block = (StatementBlock) ifstmt.getThenPart();
107             mi = (MethodInvocation) ((ExpressionStatement) block.getStatements().get(0)).getExpression();
108             mi.setName(newName);
109             mi = (MethodInvocation) mi.getParameters().get(0);
110             mi.setName(newName);
111             
112             callerMethod = (Method) feature[4];
113             body = callerMethod.getBody();
114             
115             // new RenameTestMethod().methodToBeRenamed(11);
116
mi = (MethodInvocation) ((ExpressionStatement) body.getStatements().get(0)).getExpression();
117             mi.setName(newName);
118             
119             fail = false;
120             
121         }
122         finally {
123             Utility.endTrans(fail);
124         }
125         makeDiff("testRename");
126     }
127     
128     /**
129      * @param args the command line arguments
130      */

131     public static void main(String JavaDoc[] args) {
132         TestRunner.run(suite());
133     }
134     
135 }
136
Popular Tags