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 java.util.Collections ; 25 import org.netbeans.api.java.source.CancellableTask; 26 import org.netbeans.api.java.source.JavaSource; 27 import org.netbeans.api.java.source.JavaSource.*; 28 import org.netbeans.api.java.source.TestUtilities; 29 import org.netbeans.api.java.source.TreeMaker; 30 import org.netbeans.api.java.source.WorkingCopy; 31 import org.netbeans.junit.NbTestSuite; 32 import org.openide.filesystems.FileUtil; 33 34 39 public class NewClassTreeTest extends GeneratorTest { 40 41 42 public NewClassTreeTest(String name) { 43 super(name); 44 } 45 46 public static NbTestSuite suite() { 47 NbTestSuite suite = new NbTestSuite(); 48 suite.addTestSuite(NewClassTreeTest.class); 49 return suite; 50 } 51 52 55 public void testRemoveClassBody() throws Exception { 56 testFile = new File (getWorkDir(), "Test.java"); 57 TestUtilities.copyStringToFile(testFile, 58 "package hierbas.del.litoral;\n\n" + 59 "public class Test {\n" + 60 " public void taragui() {\n" + 61 " new Runnable() {\n" + 62 " public void run() {}\n" + 63 " };\n" + 64 " }\n" + 65 " private static class X {}\n" + 66 "}\n" 67 ); 68 String golden = 69 "package hierbas.del.litoral;\n\n" + 70 "public class Test {\n" + 71 " public void taragui() {\n" + 72 " new X() ;\n" + " }\n" + 74 " private static class X {}\n" + 75 "}\n"; 76 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 77 CancellableTask task = new CancellableTask<WorkingCopy>() { 78 79 public void run(WorkingCopy workingCopy) throws IOException { 80 workingCopy.toPhase(Phase.RESOLVED); 81 TreeMaker make = workingCopy.getTreeMaker(); 82 83 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 84 MethodTree method = (MethodTree) clazz.getMembers().get(1); 85 ExpressionStatementTree st = (ExpressionStatementTree) method.getBody().getStatements().get(0); 86 NewClassTree nct = (NewClassTree) st.getExpression(); 87 workingCopy.rewrite(nct, make.NewClass(null, Collections.<ExpressionTree>emptyList(), make.Identifier("X"), nct.getArguments(), null)); 88 } 89 90 public void cancel() { 91 } 92 }; 93 testSource.runModificationTask(task).commit(); 94 String res = TestUtilities.copyFileToString(testFile); 95 System.err.println(res); 96 assertEquals(golden, res); 97 } 98 99 String getGoldenPckg() { 100 return ""; 101 } 102 103 String getSourcePckg() { 104 return ""; 105 } 106 107 } 108 | Popular Tags |