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.*; 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 import org.openide.filesystems.FileUtil; 32 33 38 public class TryTest extends GeneratorTest { 39 40 41 public TryTest(String name) { 42 super(name); 43 } 44 45 public static NbTestSuite suite() { 46 NbTestSuite suite = new NbTestSuite(); 47 suite.addTestSuite(TryTest.class); 48 return suite; 49 } 50 51 54 public void testRenameInTryBody() throws Exception { 55 testFile = new File (getWorkDir(), "Test.java"); 56 TestUtilities.copyStringToFile(testFile, 57 "package hierbas.del.litoral;\n\n" + 58 "import java.io.*;\n\n" + 59 "public class Test {\n" + 60 " public void taragui() {\n" + 61 " try {\n" + 62 " File f = new File(\"auto\");\n" + 63 " FileInputStream fis = new FileInputStream(f);\n" + 64 " } catch (FileNotFoundException ex) {\n" + 65 " } catch (NullPointerException ex) {\n" + 66 " }\n" + 67 " }\n" + 68 "}\n" 69 ); 70 String golden = 71 "package hierbas.del.litoral;\n\n" + 72 "import java.io.*;\n\n" + 73 "public class Test {\n" + 74 " public void taragui() {\n" + 75 " try {\n" + 76 " File f = new File(\"auto\");\n" + 77 " FileInputStream input = new FileInputStream(f);\n" + 78 " } catch (FileNotFoundException ex) {\n" + 79 " } catch (NullPointerException ex) {\n" + 80 " }\n" + 81 " }\n" + 82 "}\n"; 83 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 84 CancellableTask task = new CancellableTask<WorkingCopy>() { 85 86 public void run(WorkingCopy workingCopy) throws java.io.IOException { 87 workingCopy.toPhase(Phase.RESOLVED); 88 TreeMaker make = workingCopy.getTreeMaker(); 89 90 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 91 MethodTree method = (MethodTree) clazz.getMembers().get(1); 92 TryTree tt = (TryTree) method.getBody().getStatements().get(0); 93 VariableTree var = (VariableTree) tt.getBlock().getStatements().get(1); 94 workingCopy.rewrite(var, make.setLabel(var, "input")); 95 } 96 97 public void cancel() { 98 } 99 }; 100 testSource.runModificationTask(task).commit(); 101 String res = TestUtilities.copyFileToString(testFile); 102 System.err.println(res); 103 assertEquals(golden, res); 104 } 105 106 String getGoldenPckg() { 107 return ""; 108 } 109 110 String getSourcePckg() { 111 return ""; 112 } 113 114 } 115 | Popular Tags |