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.Tree; 24 import com.sun.source.tree.VariableTree; 25 import java.io.File ; 26 import java.io.IOException ; 27 import org.netbeans.api.java.source.CancellableTask; 28 import org.netbeans.api.java.source.JavaSource; 29 import org.netbeans.api.java.source.JavaSource.*; 30 import org.netbeans.api.java.source.TestUtilities; 31 import org.netbeans.api.java.source.TreeMaker; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.junit.NbTestSuite; 34 35 40 public class EnumTest extends GeneratorTest { 41 42 43 public EnumTest(String name) { 44 super(name); 45 } 46 47 public static NbTestSuite suite() { 48 NbTestSuite suite = new NbTestSuite(); 49 suite.addTestSuite(EnumTest.class); 50 return suite; 51 } 52 53 65 public void testConstantRename() throws Exception { 66 testFile = new File (getWorkDir(), "Test.java"); 67 TestUtilities.copyStringToFile(testFile, 68 "package hierbas.del.litoral;\n\n" + 69 "import java.util.*;\n\n" + 70 "public enum Test {\n" + 71 " A, B, C;\n" + 72 " \n" + 73 " public void enumMethod() {\n" + 74 " }\n" + 75 "}\n" 76 ); 77 String golden = 78 "package hierbas.del.litoral;\n\n" + 79 "import java.util.*;\n\n" + 80 "public enum Test {\n" + 81 " A2, B, C;\n" + 82 " \n" + 83 " public void enumMethod() {\n" + 84 " }\n" + 85 "}\n"; 86 JavaSource src = getJavaSource(testFile); 87 88 CancellableTask task = new CancellableTask<WorkingCopy>() { 89 90 public void run(WorkingCopy workingCopy) throws IOException { 91 workingCopy.toPhase(Phase.RESOLVED); 92 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 93 TreeMaker make = workingCopy.getTreeMaker(); 94 95 for (Tree typeDecl : cut.getTypeDecls()) { 96 if (Tree.Kind.CLASS == typeDecl.getKind()) { 98 ClassTree clazz = (ClassTree) typeDecl; 99 VariableTree vt = (VariableTree) clazz.getMembers().get(1); 100 VariableTree copy = make.setLabel(vt, "A2"); 101 workingCopy.rewrite(vt, copy); 102 } 103 } 104 } 105 106 public void cancel() { 107 } 108 }; 109 src.runModificationTask(task).commit(); 110 String res = TestUtilities.copyFileToString(testFile); 111 System.err.println(res); 112 assertEquals(golden, res); 113 } 114 115 String getGoldenPckg() { 116 return ""; 117 } 118 119 String getSourcePckg() { 120 return ""; 121 } 122 123 126 public static void main(String [] args) { 127 } 129 130 } 131 | Popular Tags |