1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.io.File ; 23 import java.io.IOException ; 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 36 public class ConstructorRenameTest extends GeneratorTest { 37 38 39 public ConstructorRenameTest(String testName) { 40 super(testName); 41 } 42 43 public static NbTestSuite suite() { 44 NbTestSuite suite = new NbTestSuite(); 45 suite.addTestSuite(ConstructorRenameTest.class); 46 return suite; 50 } 51 52 53 public void testClassRename() throws Exception { 54 testFile = new File (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 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 { 79 workingCopy.toPhase(Phase.RESOLVED); 80 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 81 TreeMaker make = workingCopy.getTreeMaker(); 82 83 for (Tree typeDecl : cut.getTypeDecls()) { 84 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 res = TestUtilities.copyFileToString(testFile); 97 System.err.println(res); 98 assertEquals(golden, res); 99 } 100 101 public void testClassRename2() throws Exception { 102 testFile = new File (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 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 { 131 workingCopy.toPhase(Phase.RESOLVED); 132 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 133 TreeMaker make = workingCopy.getTreeMaker(); 134 135 for (Tree typeDecl : cut.getTypeDecls()) { 136 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 res = TestUtilities.copyFileToString(testFile); 149 System.err.println(res); 150 assertEquals(golden, res); 151 } 152 153 public void testEnumRename() throws Exception { 154 testFile = new File (getWorkDir(), "Test.java"); 155 TestUtilities.copyStringToFile(testFile, 156 "package hierbas.del.litoral;\n\n" + 157 "public enum Enum {\n" + 158 "}\n" 159 ); 160 String 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 { 169 workingCopy.toPhase(Phase.RESOLVED); 170 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 171 TreeMaker make = workingCopy.getTreeMaker(); 172 173 for (Tree typeDecl : cut.getTypeDecls()) { 174 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 res = TestUtilities.copyFileToString(testFile); 187 System.err.println(res); 188 assertEquals(golden, res); 189 } 190 191 String getGoldenPckg() { 192 return ""; 193 } 194 195 String getSourcePckg() { 196 return ""; 197 } 198 199 } 200 | Popular Tags |