1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.MethodTree; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.Collections ; 26 import javax.lang.model.element.Modifier; 27 import javax.lang.model.type.TypeKind; 28 import com.sun.source.tree.*; 29 import java.io.File ; 30 import org.netbeans.api.java.source.*; 31 import static org.netbeans.api.java.source.JavaSource.*; 32 import org.netbeans.junit.NbTestSuite; 33 import org.openide.filesystems.FileStateInvalidException; 34 import org.openide.filesystems.FileUtil; 35 36 41 public class MethodBodyTextTest extends GeneratorTestMDRCompat { 42 43 44 public MethodBodyTextTest(String name) { 45 super(name); 46 } 47 48 public static NbTestSuite suite() { 49 NbTestSuite suite = new NbTestSuite(); 50 suite.addTest(new MethodBodyTextTest("testCreateReturnBooleanBodyText")); 54 suite.addTest(new MethodBodyTextTest("testReplaceConstrBody")); 56 suite.addTest(new MethodBodyTextTest("testReplaceMethod")); 57 return suite; 58 } 59 60 protected void setUp() throws Exception { 61 super.setUp(); 62 testFile = getFile(getSourceDir(), getSourcePckg() + "MethodBodyText.java"); 63 } 64 65 public void testSetBodyText() throws java.io.IOException , FileStateInvalidException { 66 System.err.println("testSetBodyText"); 67 JavaSource src = getJavaSource(testFile); 68 CancellableTask task = new CancellableTask<WorkingCopy>() { 69 70 public void run(WorkingCopy workingCopy) throws IOException { 71 workingCopy.toPhase(Phase.RESOLVED); 72 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 73 TreeMaker make = workingCopy.getTreeMaker(); 74 for (Tree typeDecl : cut.getTypeDecls()) { 75 if (Tree.Kind.CLASS == typeDecl.getKind()) { 77 ClassTree clazz = (ClassTree) typeDecl; 78 MethodTree node = (MethodTree) clazz.getMembers().get(1); 79 BlockTree newBody = make.createMethodBody(node, "{ System.err.println(\"Nothing.\"); }"); 80 workingCopy.rewrite(node.getBody(), newBody); 81 } 82 } 83 } 84 85 public void cancel() { 86 } 87 }; 88 src.runModificationTask(task).commit(); 89 String res = TestUtilities.copyFileToString(testFile); 90 String golden = TestUtilities.copyFileToString( 91 getFile(getGoldenDir(), getGoldenPckg() + "testSetBodyText_MethodBodyTextTest.pass") 92 ); 93 assertEquals(golden, res); 94 } 95 96 public void testCreateWithBodyText() throws java.io.IOException , FileStateInvalidException { 97 JavaSource src = getJavaSource(testFile); 98 99 CancellableTask task = new CancellableTask<WorkingCopy>() { 100 101 public void run(WorkingCopy workingCopy) throws IOException { 102 workingCopy.toPhase(Phase.RESOLVED); 103 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 104 TreeMaker make = workingCopy.getTreeMaker(); 105 for (Tree typeDecl : cut.getTypeDecls()) { 106 if (Tree.Kind.CLASS == typeDecl.getKind()) { 108 ClassTree clazz = (ClassTree) typeDecl; 109 StringBuffer body = new StringBuffer (); 110 body.append("{ System.out.println(\"Again Nothing\"); }"); 111 MethodTree method = make.Method( 112 make.Modifiers(Collections.singleton(Modifier.PUBLIC)), 113 "method2", 114 make.PrimitiveType(TypeKind.VOID), 115 Collections.EMPTY_LIST, 116 Collections.EMPTY_LIST, 117 Collections.EMPTY_LIST, 118 body.toString(), 119 null 120 ); 121 ClassTree copy = make.addClassMember(clazz, method); 122 workingCopy.rewrite(clazz, copy); 123 } 124 } 125 } 126 127 public void cancel() { 128 } 129 }; 130 src.runModificationTask(task).commit(); 131 String res = TestUtilities.copyFileToString(testFile); 132 String golden = TestUtilities.copyFileToString( 133 getFile(getGoldenDir(), getGoldenPckg() + "testCreateWithBodyText_MethodBodyTextTest.pass") 134 ); 135 assertEquals(golden, res); 136 } 137 138 public void testCreateReturnBooleanBodyText() throws java.io.IOException , FileStateInvalidException { 139 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 140 CancellableTask task = new CancellableTask<WorkingCopy>() { 141 142 public void run(WorkingCopy workingCopy) throws java.io.IOException { 143 workingCopy.toPhase(Phase.RESOLVED); 144 TreeMaker make = workingCopy.getTreeMaker(); 145 ClassTree node = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 146 StringBuffer body = new StringBuffer (); 147 body.append("{ return false; }"); 148 MethodTree method = make.Method( 149 make.Modifiers(Collections.singleton(Modifier.PUBLIC)), 150 "equals", 151 make.PrimitiveType(TypeKind.BOOLEAN), 152 Collections.EMPTY_LIST, 153 Collections.EMPTY_LIST, 154 Collections.EMPTY_LIST, 155 body.toString(), 156 null 157 ); 158 ClassTree clazz = make.addClassMember(node, method); 159 workingCopy.rewrite(node, clazz); 160 } 161 162 public void cancel() { 163 } 164 }; 165 testSource.runModificationTask(task).commit(); 166 String res = TestUtilities.copyFileToString(testFile); 167 System.err.println(res); 168 String result = TestUtilities.copyFileToString(testFile); 170 System.err.println(result); 171 assertTrue(result.contains("return false")); 172 } 173 174 public void testModifyBodyText() throws java.io.IOException , FileStateInvalidException { 175 System.err.println("testModifyBodyText"); 176 JavaSource src = getJavaSource(testFile); 177 178 CancellableTask task = new CancellableTask<WorkingCopy>() { 179 180 public void run(WorkingCopy workingCopy) throws IOException { 181 workingCopy.toPhase(Phase.RESOLVED); 182 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 183 TreeMaker make = workingCopy.getTreeMaker(); 184 for (Tree typeDecl : cut.getTypeDecls()) { 185 if (Tree.Kind.CLASS == typeDecl.getKind()) { 187 ClassTree clazz = (ClassTree) typeDecl; 188 MethodTree node = (MethodTree) clazz.getMembers().get(1); 189 String body = "{ List l; }"; 190 BlockTree copy = make.createMethodBody(node, body); 191 workingCopy.rewrite(node.getBody(), copy); 192 } 193 } 194 } 195 196 public void cancel() { 197 } 198 }; 199 src.runModificationTask(task).commit(); 200 String res = TestUtilities.copyFileToString(testFile); 201 String golden = TestUtilities.copyFileToString( 202 getFile(getGoldenDir(), getGoldenPckg() + "testModifyBodyText_MethodBodyTextTest.pass") 203 ); 204 assertEquals(golden, res); 205 } 206 207 213 public void testReplaceConstrBody() throws Exception { 214 System.err.println("testReplaceConstrBody"); 215 testFile = new File (getWorkDir(), "Test.java"); 216 TestUtilities.copyStringToFile(testFile, 217 "package personal;\n" + 218 "\n" + 219 "public class Test {\n" + 220 " public Test() {\n" + 221 " }\n" + 222 " \n" + 223 " public Object method() {\n" + 224 " }\n" + 225 "}\n"); 226 227 String golden = 228 "package personal;\n" + 229 "\n" + 230 "public class Test {\n" + 231 " public Test() {" + 232 "System.err.println(null);\n" + 233 "\n" + 234 " }\n" + 235 " \n" + 236 " public Object method() {\n" + 237 " }\n" + 238 "}\n"; 239 240 JavaSource src = getJavaSource(testFile); 241 242 CancellableTask task = new CancellableTask<WorkingCopy>() { 243 public void run(WorkingCopy workingCopy) throws IOException { 244 workingCopy.toPhase(Phase.RESOLVED); 245 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 246 TreeMaker make = workingCopy.getTreeMaker(); 247 for (Tree typeDecl : cut.getTypeDecls()) { 248 if (Tree.Kind.CLASS == typeDecl.getKind()) { 250 ClassTree clazz = (ClassTree) typeDecl; 251 MethodTree method = (MethodTree) clazz.getMembers().get(0); 252 ExpressionStatementTree statement = make.ExpressionStatement( 253 make.MethodInvocation( 254 Collections.<ExpressionTree>emptyList(), 255 make.MemberSelect( 256 make.MemberSelect( 257 make.Identifier("System"), 258 "err" 259 ), 260 "println" 261 ), 262 Collections.singletonList( 263 make.Literal(null) 264 ) 265 ) 266 ); 267 BlockTree newBody = make.Block( 268 Collections.<StatementTree>singletonList(statement), 269 false 270 ); 271 workingCopy.rewrite(method.getBody(), newBody); 272 } 273 } 274 } 275 276 public void cancel() { 277 } 278 }; 279 src.runModificationTask(task).commit(); 280 String res = TestUtilities.copyFileToString(testFile); 281 System.err.println(res); 282 assertEquals(golden, res); 283 } 284 285 288 public void testReplaceMethod() throws Exception { 289 System.err.println("testReplaceMethod"); 290 testFile = new File (getWorkDir(), "Test.java"); 291 TestUtilities.copyStringToFile(testFile, 292 "package personal;\n" + 293 "\n" + 294 "public class Test {\n" + 295 " public Test() {\n" + 296 " }\n" + 297 " \n" + 298 " public Object method() {\n" + 299 " for(int i = 0; i < 10; i++) {\n" + 300 " System.out.println(\"In loop\");\n" + 301 " }\n" + 302 " Thread.currentThread();\n" + 303 " }\n" + 304 "}\n"); 305 306 String golden = 307 "package personal;\n" + 308 "\n" + 309 "public class Test {\n" + 310 " public Test() {\n" + 311 " }\n" + 312 " \n" + 313 " public Object method() {\n" + 314 " System.out.println(\"Ahoj svete!\");\n" + 315 " }\n" + 316 "}\n"; 317 318 JavaSource src = getJavaSource(testFile); 319 320 CancellableTask task = new CancellableTask<WorkingCopy>() { 321 public void run(WorkingCopy workingCopy) throws IOException { 322 workingCopy.toPhase(Phase.RESOLVED); 323 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 324 TreeMaker make = workingCopy.getTreeMaker(); 325 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 326 MethodTree meth = (MethodTree) clazz.getMembers().get(1); 327 String bodyText = "{System.out.println(\"Ahoj svete!\");}"; 328 MethodTree newMeth = make.Method( 329 meth.getModifiers(), 330 meth.getName(), 331 meth.getReturnType(), 332 meth.getTypeParameters(), 333 meth.getParameters(), 334 meth.getThrows(), 335 bodyText, 336 (ExpressionTree) meth.getDefaultValue() 337 ); 338 workingCopy.rewrite(meth.getBody(), newMeth.getBody()); 339 } 340 341 public void cancel() { 342 } 343 }; 344 src.runModificationTask(task).commit(); 345 String res = TestUtilities.copyFileToString(testFile); 346 System.err.println(res); 347 assertEquals(golden, res); 348 } 349 350 String getSourcePckg() { 351 return "org/netbeans/test/codegen/indent/"; 352 } 353 354 String getGoldenPckg() { 355 return "org/netbeans/jmi/javamodel/codegen/indent/MethodBodyTextTest/"; 356 } 357 358 } 359 | Popular Tags |