1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.ExpressionTree; 24 import com.sun.source.tree.Tree; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.List ; 28 import org.netbeans.api.java.source.CancellableTask; 29 import org.netbeans.api.java.source.JavaSource; 30 import org.netbeans.api.java.source.TreeMaker; 31 import static org.netbeans.api.java.source.JavaSource.*; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.jackpot.test.TestUtilities; 34 import org.netbeans.junit.NbTestSuite; 35 36 42 public class ClassExtendsTest extends GeneratorTestMDRCompat { 43 44 45 public ClassExtendsTest(String testName) { 46 super(testName); 47 } 48 49 public static NbTestSuite suite() { 50 NbTestSuite suite = new NbTestSuite(); 51 suite.addTestSuite(ClassExtendsTest.class); 52 return suite; 53 } 54 55 63 public void testModifyExtends() throws Exception { 64 testFile = new File (getWorkDir(), "Test.java"); 65 TestUtilities.copyStringToFile(testFile, 66 "package hierbas.del.litoral;\n\n" + 67 "import java.util.*;\n\n" + 68 "public class Test<E> extends Object {\n" + 69 " public void taragui() {\n" + 70 " }\n" + 71 "}\n" 72 ); 73 String golden = 74 "package hierbas.del.litoral;\n\n" + 75 "import java.util.*;\n\n" + 76 "public class Test<E> extends String {\n" + 77 " public void taragui() {\n" + 78 " }\n" + 79 "}\n"; 80 JavaSource src = getJavaSource(testFile); 81 82 CancellableTask task = new CancellableTask<WorkingCopy>() { 83 84 public void run(WorkingCopy workingCopy) throws IOException { 85 workingCopy.toPhase(Phase.RESOLVED); 86 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 87 TreeMaker make = workingCopy.getTreeMaker(); 88 89 for (Tree typeDecl : cut.getTypeDecls()) { 90 if (Tree.Kind.CLASS == typeDecl.getKind()) { 92 ClassTree clazz = (ClassTree) typeDecl; 93 workingCopy.rewrite(clazz.getExtendsClause(), make.Identifier("String")); 94 } 95 } 96 } 97 98 public void cancel() { 99 } 100 }; 101 src.runModificationTask(task).commit(); 102 String res = TestUtilities.copyFileToString(testFile); 103 System.err.println(res); 104 assertEquals(golden, res); 105 } 106 107 public void testExtendsNoOrig() throws Exception { 108 testFile = new File (getWorkDir(), "Test.java"); 109 TestUtilities.copyStringToFile(testFile, 110 "package hierbas.del.litoral;\n\n" + 111 "import java.util.*;\n\n" + 112 "public class Test<E> {\n" + 113 " public void taragui() {\n" + 114 " }\n" + 115 "}\n" 116 ); 117 String golden = 118 "package hierbas.del.litoral;\n\n" + 119 "import java.util.*;\n\n" + 120 "public class Test<E> extends String {\n" + 121 " public void taragui() {\n" + 122 " }\n" + 123 "}\n"; 124 JavaSource src = getJavaSource(testFile); 125 126 CancellableTask task = new CancellableTask<WorkingCopy>() { 127 128 public void run(WorkingCopy workingCopy) throws IOException { 129 workingCopy.toPhase(Phase.RESOLVED); 130 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 131 TreeMaker make = workingCopy.getTreeMaker(); 132 133 for (Tree typeDecl : cut.getTypeDecls()) { 134 if (Tree.Kind.CLASS == typeDecl.getKind()) { 136 ClassTree classTree = (ClassTree) typeDecl; 137 ClassTree copy = make.setExtends(classTree, make.Identifier("String")); 138 workingCopy.rewrite(classTree, copy); 147 } 148 } 149 } 150 151 public void cancel() { 152 } 153 }; 154 src.runModificationTask(task).commit(); 155 String res = TestUtilities.copyFileToString(testFile); 156 System.err.println(res); 157 assertEquals(golden, res); 158 } 159 160 public void testRemoveExtends() throws Exception { 161 testFile = new File (getWorkDir(), "Test.java"); 162 TestUtilities.copyStringToFile(testFile, 163 "package hierbas.del.litoral;\n\n" + 164 "import java.util.*;\n\n" + 165 "public class Test<E> extends Object {\n" + 166 " public void taragui() {\n" + 167 " }\n" + 168 "}\n" 169 ); 170 String golden = 171 "package hierbas.del.litoral;\n\n" + 172 "import java.util.*;\n\n" + 173 "public class Test<E> {\n" + 174 " public void taragui() {\n" + 175 " }\n" + 176 "}\n"; 177 JavaSource src = getJavaSource(testFile); 178 179 CancellableTask task = new CancellableTask<WorkingCopy>() { 180 181 public void run(WorkingCopy workingCopy) throws IOException { 182 workingCopy.toPhase(Phase.RESOLVED); 183 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 184 TreeMaker make = workingCopy.getTreeMaker(); 185 186 for (Tree typeDecl : cut.getTypeDecls()) { 187 if (Tree.Kind.CLASS == typeDecl.getKind()) { 189 ClassTree classTree = (ClassTree) typeDecl; 190 ClassTree copy = make.Class( 191 classTree.getModifiers(), 192 classTree.getSimpleName(), 193 classTree.getTypeParameters(), 194 null, 195 (List <ExpressionTree>) classTree.getImplementsClause(), 196 classTree.getMembers() 197 ); 198 workingCopy.rewrite(classTree, copy); 199 } 200 } 201 } 202 203 public void cancel() { 204 } 205 }; 206 src.runModificationTask(task).commit(); 207 String res = TestUtilities.copyFileToString(testFile); 208 System.err.println(res); 209 assertEquals(golden, res); 210 } 211 212 String getGoldenPckg() { 213 return ""; 214 } 215 216 String getSourcePckg() { 217 return ""; 218 } 219 } 220 | Popular Tags |