1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import java.io.File ; 23 import java.util.Collections ; 24 import javax.lang.model.element.Modifier; 25 import javax.lang.model.element.TypeElement; 26 import org.netbeans.api.java.source.CancellableTask; 27 import org.netbeans.api.java.source.JavaSource; 28 import org.netbeans.api.java.source.JavaSource.Phase; 29 import org.netbeans.api.java.source.TestUtilities; 30 import org.netbeans.api.java.source.TreeMaker; 31 import org.netbeans.api.java.source.WorkingCopy; 32 import org.netbeans.junit.NbTestSuite; 33 import org.openide.filesystems.FileUtil; 34 35 39 public class MethodBodyTest extends GeneratorTest { 40 41 42 public MethodBodyTest(String name) { 43 super(name); 44 } 45 46 public static NbTestSuite suite() { 47 NbTestSuite suite = new NbTestSuite(); 48 suite.addTestSuite(MethodBodyTest.class); 49 return suite; 53 } 54 55 58 public void testAddFirstStatement() throws Exception { 59 testFile = new File (getWorkDir(), "Test.java"); 60 TestUtilities.copyStringToFile(testFile, 61 "package personal;\n" + 62 "\n" + 63 "public class Test {\n" + 64 " public Object method() {\n" + 65 " }\n" + 66 "}\n"); 67 68 String golden = 69 "package personal;\n" + 70 "\n" + 71 "public class Test {\n" + 72 " public Object method() {System.out.println(\"test\");\n\n" + 73 " }\n" + 74 "}\n"; 75 76 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 77 CancellableTask task = new CancellableTask<WorkingCopy>() { 78 79 public void run(WorkingCopy workingCopy) throws java.io.IOException { 80 workingCopy.toPhase(Phase.RESOLVED); 81 TreeMaker make = workingCopy.getTreeMaker(); 82 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 83 MethodTree method = (MethodTree) clazz.getMembers().get(1); 84 BlockTree block = method.getBody(); 85 ExpressionStatementTree est = make.ExpressionStatement( 86 make.MethodInvocation( 87 Collections.<ExpressionTree>emptyList(), 88 make.MemberSelect( 89 make.MemberSelect( 90 make.Identifier("System"), 91 "out" 92 ), 93 "println" 94 ), 95 Collections.<ExpressionTree>singletonList( 96 make.Literal("test") 97 ) 98 ) 99 ); 100 workingCopy.rewrite(block, make.addBlockStatement(block, est)); 101 } 102 103 public void cancel() { 104 } 105 }; 106 testSource.runModificationTask(task).commit(); 107 String res = TestUtilities.copyFileToString(testFile); 108 System.err.println(res); 109 assertEquals(golden, res); 110 } 111 112 115 public void testAddBodyText() throws Exception { 116 testFile = new File (getWorkDir(), "Test.java"); 117 TestUtilities.copyStringToFile(testFile, 118 "package personal;\n" + 119 "\n" + 120 "public class Test {\n" + 121 " public Object method() {\n" + 122 " }\n" + 123 "}\n"); 124 125 String golden = 126 "package personal;\n" + 127 "\n" + 128 "public class Test {\n" + 129 " public Object method() {java.lang.System.out.println(\"test\");\n\n" + 130 " }\n" + 131 "}\n"; 132 133 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 134 CancellableTask task = new CancellableTask<WorkingCopy>() { 135 136 public void run(WorkingCopy workingCopy) throws java.io.IOException { 137 workingCopy.toPhase(Phase.RESOLVED); 138 TreeMaker make = workingCopy.getTreeMaker(); 139 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 140 MethodTree method = (MethodTree) clazz.getMembers().get(1); 141 BlockTree newBody = make.createMethodBody(method, "{ System.out.println(\"test\"); }"); 142 workingCopy.rewrite(method.getBody(), newBody); 143 } 144 145 public void cancel() { 146 } 147 }; 148 testSource.runModificationTask(task).commit(); 149 String res = TestUtilities.copyFileToString(testFile); 150 System.err.println(res); 151 assertEquals(golden, res); 152 } 153 154 157 public void testAddVarDecl() throws Exception { 158 testFile = new File (getWorkDir(), "Test.java"); 159 TestUtilities.copyStringToFile(testFile, 160 "package personal;\n" + 161 "\n" + 162 "public class Test {\n" + 163 " public Object method() {\n" + 164 " }\n" + 165 "}\n"); 166 167 String golden = 168 "package personal;\n\n" + 169 "import java.util.HashMap;\n" + 170 "import java.util.Map;\n" + 171 "\n" + 172 "public class Test {\n" + 173 " public Object method() { Map env = new HashMap();\n\n" + 174 " }\n" + 175 "}\n"; 176 177 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 178 CancellableTask task = new CancellableTask<WorkingCopy>() { 179 180 public void run(WorkingCopy workingCopy) throws java.io.IOException { 181 workingCopy.toPhase(Phase.RESOLVED); 182 TreeMaker treeMaker = workingCopy.getTreeMaker(); 183 TypeElement hashMapClass = workingCopy.getElements().getTypeElement("java.util.HashMap"); ExpressionTree hashMapEx = treeMaker.QualIdent(hashMapClass); 185 TypeElement mapClass = workingCopy.getElements().getTypeElement("java.util.Map"); ExpressionTree mapEx = treeMaker.QualIdent(mapClass); 187 NewClassTree mapConstructor = treeMaker.NewClass( 188 null, 189 Collections.<ExpressionTree>emptyList(), 190 hashMapEx, 191 Collections.<ExpressionTree>emptyList(), null 192 ); 193 VariableTree vt = treeMaker.Variable( treeMaker.Modifiers( 194 Collections.<Modifier>emptySet(), 195 Collections.<AnnotationTree>emptyList() 196 ), "env", mapEx, mapConstructor 197 ); 198 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 199 MethodTree method = (MethodTree) clazz.getMembers().get(1); 200 workingCopy.rewrite(method.getBody(), treeMaker.addBlockStatement(method.getBody(), vt)); 201 } 202 203 public void cancel() { 204 } 205 }; 206 testSource.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 } 221 | Popular Tags |