1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.BlockTree; 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.CompilationUnitTree; 24 import com.sun.source.tree.ExpressionTree; 25 import com.sun.source.tree.ForLoopTree; 26 import com.sun.source.tree.MethodTree; 27 import com.sun.source.tree.VariableTree; 28 import java.io.File ; 29 import java.io.IOException ; 30 import org.netbeans.api.java.source.CancellableTask; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.JavaSource.Phase; 33 import org.netbeans.api.java.source.TreeMaker; 34 import org.netbeans.api.java.source.WorkingCopy; 35 import org.netbeans.jackpot.test.TestUtilities; 36 import org.netbeans.junit.NbTestSuite; 37 38 43 public class AddCastTest extends GeneratorTestMDRCompat { 44 45 46 public AddCastTest(String name) { 47 super(name); 48 } 49 50 public static NbTestSuite suite() { 51 NbTestSuite suite = new NbTestSuite(); 52 suite.addTestSuite(AddCastTest.class); 53 return suite; 54 } 55 56 public void testAddCastToDeclStmt() throws Exception { 57 testFile = new File (getWorkDir(), "Test.java"); 58 TestUtilities.copyStringToFile(testFile, 59 "package hierbas.del.litoral;\n\n" + 60 "import java.util.*;\n\n" + 61 "public class Test<E> {\n" + 62 " public void taragui() {\n" + 63 " System.err.println(\"taragui() method\");\n" + 64 " String s = \"Oven.\";\n" + 65 "// line comment\n" + 66 " }\n" + 67 "}\n" 68 ); 69 String golden = 70 "package hierbas.del.litoral;\n\n" + 71 "import java.util.*;\n\n" + 72 "public class Test<E> {\n" + 73 " public void taragui() {\n" + 74 " System.err.println(\"taragui() method\");\n" + 75 " String s = (String) \"Oven.\";\n" + 76 "// line comment\n" + 77 " }\n" + 78 "}\n"; 79 JavaSource src = getJavaSource(testFile); 80 81 CancellableTask task = new CancellableTask<WorkingCopy>() { 82 83 public void run(WorkingCopy workingCopy) throws IOException { 84 workingCopy.toPhase(Phase.RESOLVED); 86 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 87 TreeMaker make = workingCopy.getTreeMaker(); 88 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 90 MethodTree method = (MethodTree) clazz.getMembers().get(1); 91 VariableTree var = (VariableTree) method.getBody().getStatements().get(1); 92 ExpressionTree init = var.getInitializer(); 93 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); 94 workingCopy.rewrite(init, cast); 95 } 96 97 public void cancel() { 98 } 99 }; 100 src.runModificationTask(task).commit(); 101 String res = TestUtilities.copyFileToString(testFile); 102 System.err.println(res); 103 assertEquals(golden, res); 104 } 105 106 public void testAddCastInForWithoutSteps() throws Exception { 107 testFile = new File (getWorkDir(), "Test.java"); 108 TestUtilities.copyStringToFile(testFile, 109 "package hierbas.del.litoral;\n\n" + 110 "import java.util.*;\n\n" + 111 "public class Test<E> {\n" + 112 " public void cast() {\n" + 113 " Object o = null;\n" + 114 " for (int i = 0; i < 5; ) {\n" + 115 " String s = o;\n" + 116 " }\n" + 117 " }\n" + 118 "}\n" 119 ); 120 String golden = 121 "package hierbas.del.litoral;\n\n" + 122 "import java.util.*;\n\n" + 123 "public class Test<E> {\n" + 124 " public void cast() {\n" + 125 " Object o = null;\n" + 126 " for (int i = 0; i < 5; ) {\n" + 127 " String s = (String) o;\n" + 128 " }\n" + 129 " }\n" + 130 "}\n"; 131 JavaSource src = getJavaSource(testFile); 132 133 CancellableTask task = new CancellableTask<WorkingCopy>() { 134 135 public void run(WorkingCopy workingCopy) throws IOException { 136 workingCopy.toPhase(Phase.RESOLVED); 137 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 138 TreeMaker make = workingCopy.getTreeMaker(); 139 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 140 MethodTree method = (MethodTree) clazz.getMembers().get(1); 141 ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(1); 142 BlockTree block = (BlockTree) forLoop.getStatement(); 143 VariableTree vt = (VariableTree) block.getStatements().get(0); 144 ExpressionTree init = vt.getInitializer(); 145 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); 146 workingCopy.rewrite(init, cast); 147 } 148 149 public void cancel() { 150 } 151 }; 152 src.runModificationTask(task).commit(); 153 String res = TestUtilities.copyFileToString(testFile); 154 System.err.println(res); 155 assertEquals(golden, res); 156 } 157 158 String getGoldenPckg() { 159 return ""; 160 } 161 162 String getSourcePckg() { 163 return ""; 164 } 165 166 167 } 168 | Popular Tags |