KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > ConstructorRenameTest


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.api.java.source.gen;
20
21 import com.sun.source.tree.*;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.api.java.source.CancellableTask;
25 import org.netbeans.api.java.source.JavaSource;
26 import org.netbeans.api.java.source.JavaSource.Phase;
27 import org.netbeans.api.java.source.TestUtilities;
28 import org.netbeans.api.java.source.TreeMaker;
29 import org.netbeans.api.java.source.WorkingCopy;
30 import org.netbeans.junit.NbTestSuite;
31
32 /**
33  *
34  * @author Pavel Flaska
35  */

36 public class ConstructorRenameTest extends GeneratorTest {
37
38     /** Creates a new instance of ClassMemberTest */
39     public ConstructorRenameTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static NbTestSuite suite() {
44         NbTestSuite suite = new NbTestSuite();
45         suite.addTestSuite(ConstructorRenameTest.class);
46 // suite.addTest(new ConstructorRenameTest("testClassRename"));
47
// suite.addTest(new ConstructorRenameTest("testClassRename"));
48
// suite.addTest(new ConstructorRenameTest("testEnumRename"));
49
return suite;
50     }
51     
52
53     public void testClassRename() throws Exception JavaDoc {
54         testFile = new File JavaDoc(getWorkDir(), "Test.java");
55         TestUtilities.copyStringToFile(testFile,
56             "package hierbas.del.litoral;\n\n" +
57             "import java.io.File;\n\n" +
58             "public class Test {\n" +
59             " Test(int a, long c, String s) {\n" +
60             " }\n\n" +
61             " Test() {\n" +
62             " }\n\n" +
63             "}\n"
64             );
65         String JavaDoc golden =
66             "package hierbas.del.litoral;\n\n" +
67             "import java.io.File;\n\n" +
68             "public class Test2 {\n" +
69             " Test2(int a, long c, String s) {\n" +
70             " }\n\n" +
71             " Test2() {\n" +
72             " }\n\n" +
73             "}\n";
74
75         JavaSource src = getJavaSource(testFile);
76         CancellableTask task = new CancellableTask<WorkingCopy>() {
77
78             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
79                 workingCopy.toPhase(Phase.RESOLVED);
80                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
81                 TreeMaker make = workingCopy.getTreeMaker();
82
83                 for (Tree typeDecl : cut.getTypeDecls()) {
84                     // ensure that it is correct type declaration, i.e. class
85
if (Tree.Kind.CLASS == typeDecl.getKind()) {
86                         ClassTree copy = make.setLabel((ClassTree) typeDecl, "Test2");
87                         workingCopy.rewrite(typeDecl, copy);
88                     }
89                 }
90             }
91             
92             public void cancel() {
93             }
94         };
95         src.runModificationTask(task).commit();
96         String JavaDoc res = TestUtilities.copyFileToString(testFile);
97         System.err.println(res);
98         assertEquals(golden, res);
99     }
100     
101     public void testClassRename2() throws Exception JavaDoc {
102         testFile = new File JavaDoc(getWorkDir(), "Test.java");
103         TestUtilities.copyStringToFile(testFile,
104             "package hierbas.del.litoral;\n\n" +
105             "import java.io.File;\n\n" +
106             "public class Test {\n" +
107             " Test(int a, long c, String s) {\n" +
108             " }\n\n" +
109             " void method() {\n" +
110             " }\n\n" +
111             " Test() {\n" +
112             " }\n\n" +
113             "}\n"
114             );
115         String JavaDoc golden =
116             "package hierbas.del.litoral;\n\n" +
117             "import java.io.File;\n\n" +
118             "public class Test2 {\n" +
119             " Test2(int a, long c, String s) {\n" +
120             " }\n\n" +
121             " void method() {\n" +
122             " }\n\n" +
123             " Test2() {\n" +
124             " }\n\n" +
125             "}\n";
126
127         JavaSource src = getJavaSource(testFile);
128         CancellableTask task = new CancellableTask<WorkingCopy>() {
129
130             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
131                 workingCopy.toPhase(Phase.RESOLVED);
132                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
133                 TreeMaker make = workingCopy.getTreeMaker();
134
135                 for (Tree typeDecl : cut.getTypeDecls()) {
136                     // ensure that it is correct type declaration, i.e. class
137
if (Tree.Kind.CLASS == typeDecl.getKind()) {
138                         ClassTree copy = make.setLabel((ClassTree) typeDecl, "Test2");
139                         workingCopy.rewrite(typeDecl, copy);
140                     }
141                 }
142             }
143             
144             public void cancel() {
145             }
146         };
147         src.runModificationTask(task).commit();
148         String JavaDoc res = TestUtilities.copyFileToString(testFile);
149         System.err.println(res);
150         assertEquals(golden, res);
151     }
152     
153     public void testEnumRename() throws Exception JavaDoc {
154         testFile = new File JavaDoc(getWorkDir(), "Test.java");
155         TestUtilities.copyStringToFile(testFile,
156             "package hierbas.del.litoral;\n\n" +
157             "public enum Enum {\n" +
158             "}\n"
159             );
160         String JavaDoc golden =
161             "package hierbas.del.litoral;\n\n" +
162             "public enum Test2 {\n" +
163             "}\n";
164
165         JavaSource src = getJavaSource(testFile);
166         CancellableTask task = new CancellableTask<WorkingCopy>() {
167
168             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
169                 workingCopy.toPhase(Phase.RESOLVED);
170                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
171                 TreeMaker make = workingCopy.getTreeMaker();
172
173                 for (Tree typeDecl : cut.getTypeDecls()) {
174                     // ensure that it is correct type declaration, i.e. class
175
if (Tree.Kind.CLASS == typeDecl.getKind()) {
176                         ClassTree copy = make.setLabel((ClassTree) typeDecl, "Test2");
177                         workingCopy.rewrite(typeDecl, copy);
178                     }
179                 }
180             }
181             
182             public void cancel() {
183             }
184         };
185         src.runModificationTask(task).commit();
186         String JavaDoc res = TestUtilities.copyFileToString(testFile);
187         System.err.println(res);
188         assertEquals(golden, res);
189     }
190     
191     String JavaDoc getGoldenPckg() {
192         return "";
193     }
194
195     String JavaDoc getSourcePckg() {
196         return "";
197     }
198     
199 }
200
Popular Tags