001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.java.source.gen;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: import com.sun.source.tree.*;
047:
048: import javax.lang.model.element.Modifier;
049: import javax.lang.model.type.TypeKind;
050: import org.netbeans.api.java.source.GeneratorUtilities;
051:
052: import org.openide.filesystems.FileStateInvalidException;
053: import org.openide.filesystems.FileUtil;
054:
055: import org.netbeans.api.java.source.*;
056: import static org.netbeans.api.java.source.JavaSource.*;
057:
058: import org.netbeans.junit.NbTestSuite;
059:
060: /**
061: * Tests indentation of newly generated body text in method.
062: *
063: * @author Pavel Flaska
064: */
065: public class MethodBodyTextTest extends GeneratorTestMDRCompat {
066:
067: /** Creates a new instance of MethodBodyTextTest */
068: public MethodBodyTextTest(String name) {
069: super (name);
070: }
071:
072: public static NbTestSuite suite() {
073: NbTestSuite suite = new NbTestSuite();
074: // suite.addTestSuite(MethodBodyTextTest.class);
075: // suite.addTest(new MethodBodyTextTest("testSetBodyText"));
076: // suite.addTest(new MethodBodyTextTest("testCreateWithBodyText"));
077: suite.addTest(new MethodBodyTextTest(
078: "testCreateReturnBooleanBodyText"));
079: // suite.addTest(new MethodBodyTextTest("testModifyBodyText"));
080: suite.addTest(new MethodBodyTextTest("testReplaceConstrBody"));
081: suite.addTest(new MethodBodyTextTest("testReplaceMethod"));
082: suite.addTest(new MethodBodyTextTest("testReplaceMethodBody1"));
083: suite.addTest(new MethodBodyTextTest("testReplaceMethodBody2"));
084: suite.addTest(new MethodBodyTextTest("testReplaceMethodBody3"));
085: suite.addTest(new MethodBodyTextTest(
086: "testReplaceMethodBodyImports"));
087: return suite;
088: }
089:
090: @Override
091: protected void setUp() throws Exception {
092: super .setUp();
093: testFile = getFile(getSourceDir(), getSourcePckg()
094: + "MethodBodyText.java");
095: }
096:
097: public void testSetBodyText() throws java.io.IOException,
098: FileStateInvalidException {
099: System.err.println("testSetBodyText");
100: JavaSource src = getJavaSource(testFile);
101: Task<WorkingCopy> task = new Task<WorkingCopy>() {
102:
103: public void run(WorkingCopy workingCopy) throws IOException {
104: workingCopy.toPhase(Phase.RESOLVED);
105: CompilationUnitTree cut = workingCopy
106: .getCompilationUnit();
107: TreeMaker make = workingCopy.getTreeMaker();
108: for (Tree typeDecl : cut.getTypeDecls()) {
109: // ensure that it is correct type declaration, i.e. class
110: if (Tree.Kind.CLASS == typeDecl.getKind()) {
111: ClassTree clazz = (ClassTree) typeDecl;
112: MethodTree node = (MethodTree) clazz
113: .getMembers().get(1);
114: BlockTree newBody = make
115: .createMethodBody(node,
116: "{ System.err.println(\"Nothing.\"); }");
117: workingCopy.rewrite(node.getBody(), newBody);
118: }
119: }
120: }
121:
122: };
123: src.runModificationTask(task).commit();
124: String res = TestUtilities.copyFileToString(testFile);
125: String golden = TestUtilities.copyFileToString(getFile(
126: getGoldenDir(), getGoldenPckg()
127: + "testSetBodyText_MethodBodyTextTest.pass"));
128: assertEquals(golden, res);
129: }
130:
131: public void testCreateWithBodyText() throws java.io.IOException,
132: FileStateInvalidException {
133: JavaSource src = getJavaSource(testFile);
134:
135: Task<WorkingCopy> task = new Task<WorkingCopy>() {
136:
137: public void run(WorkingCopy workingCopy) throws IOException {
138: workingCopy.toPhase(Phase.RESOLVED);
139: CompilationUnitTree cut = workingCopy
140: .getCompilationUnit();
141: TreeMaker make = workingCopy.getTreeMaker();
142: for (Tree typeDecl : cut.getTypeDecls()) {
143: // ensure that it is correct type declaration, i.e. class
144: if (Tree.Kind.CLASS == typeDecl.getKind()) {
145: ClassTree clazz = (ClassTree) typeDecl;
146: StringBuffer body = new StringBuffer();
147: body
148: .append("{ System.out.println(\"Again Nothing\"); }");
149: MethodTree method = make
150: .Method(
151: make
152: .Modifiers(Collections
153: .singleton(Modifier.PUBLIC)),
154: "method2",
155: make
156: .PrimitiveType(TypeKind.VOID),
157: Collections
158: .<TypeParameterTree> emptyList(),
159: Collections
160: .<VariableTree> emptyList(),
161: Collections
162: .<ExpressionTree> emptyList(),
163: body.toString(), null);
164: ClassTree copy = make.addClassMember(clazz,
165: method);
166: workingCopy.rewrite(clazz, copy);
167: }
168: }
169: }
170:
171: };
172: src.runModificationTask(task).commit();
173: String res = TestUtilities.copyFileToString(testFile);
174: String golden = TestUtilities
175: .copyFileToString(getFile(
176: getGoldenDir(),
177: getGoldenPckg()
178: + "testCreateWithBodyText_MethodBodyTextTest.pass"));
179: assertEquals(golden, res);
180: }
181:
182: public void testCreateReturnBooleanBodyText()
183: throws java.io.IOException, FileStateInvalidException {
184: JavaSource testSource = JavaSource.forFileObject(FileUtil
185: .toFileObject(testFile));
186: Task<WorkingCopy> task = new Task<WorkingCopy>() {
187:
188: public void run(WorkingCopy workingCopy)
189: throws java.io.IOException {
190: workingCopy.toPhase(Phase.RESOLVED);
191: TreeMaker make = workingCopy.getTreeMaker();
192: ClassTree node = (ClassTree) workingCopy
193: .getCompilationUnit().getTypeDecls().get(0);
194: StringBuffer body = new StringBuffer();
195: body.append("{ return false; }");
196: MethodTree method = make.Method(make
197: .Modifiers(Collections
198: .singleton(Modifier.PUBLIC)), "equals",
199: make.PrimitiveType(TypeKind.BOOLEAN),
200: Collections.<TypeParameterTree> emptyList(),
201: Collections.<VariableTree> emptyList(),
202: Collections.<ExpressionTree> emptyList(), body
203: .toString(), null);
204: ClassTree clazz = make.addClassMember(node, method);
205: workingCopy.rewrite(node, clazz);
206: }
207:
208: };
209: testSource.runModificationTask(task).commit();
210: String res = TestUtilities.copyFileToString(testFile);
211: System.err.println(res);
212: // there is "return 0" instead
213: String result = TestUtilities.copyFileToString(testFile);
214: System.err.println(result);
215: assertTrue(result.contains("return false"));
216: }
217:
218: public void testModifyBodyText() throws java.io.IOException,
219: FileStateInvalidException {
220: System.err.println("testModifyBodyText");
221: JavaSource src = getJavaSource(testFile);
222:
223: Task<WorkingCopy> task = new Task<WorkingCopy>() {
224:
225: public void run(WorkingCopy workingCopy) throws IOException {
226: workingCopy.toPhase(Phase.RESOLVED);
227: CompilationUnitTree cut = workingCopy
228: .getCompilationUnit();
229: TreeMaker make = workingCopy.getTreeMaker();
230: for (Tree typeDecl : cut.getTypeDecls()) {
231: // ensure that it is correct type declaration, i.e. class
232: if (Tree.Kind.CLASS == typeDecl.getKind()) {
233: ClassTree clazz = (ClassTree) typeDecl;
234: MethodTree node = (MethodTree) clazz
235: .getMembers().get(1);
236: String body = "{ List l; }";
237: BlockTree copy = make.createMethodBody(node,
238: body);
239: workingCopy.rewrite(node.getBody(), copy);
240: }
241: }
242: }
243:
244: };
245: src.runModificationTask(task).commit();
246: String res = TestUtilities.copyFileToString(testFile);
247: String golden = TestUtilities
248: .copyFileToString(getFile(
249: getGoldenDir(),
250: getGoldenPckg()
251: + "testModifyBodyText_MethodBodyTextTest.pass"));
252: assertEquals(golden, res);
253: }
254:
255: /**
256: * Replace constructor body. -- In old constructor, syntetic super()
257: * was in the body, no syntetic element in new constructor body.
258: *
259: * #93740
260: */
261: public void testReplaceConstrBody() throws Exception {
262: System.err.println("testReplaceConstrBody");
263: testFile = new File(getWorkDir(), "Test.java");
264: TestUtilities.copyStringToFile(testFile, "package personal;\n"
265: + "\n" + "public class Test {\n"
266: + " public Test() {\n" + " }\n" + " \n"
267: + " public Object method() {\n" + " }\n" + "}\n");
268:
269: String golden = "package personal;\n" + "\n"
270: + "public class Test {\n" + " public Test() {\n"
271: + " System.err.println(null);\n" + " }\n"
272: + " \n" + " public Object method() {\n"
273: + " }\n" + "}\n";
274:
275: JavaSource src = getJavaSource(testFile);
276:
277: Task<WorkingCopy> task = new Task<WorkingCopy>() {
278: public void run(WorkingCopy workingCopy) throws IOException {
279: workingCopy.toPhase(Phase.RESOLVED);
280: CompilationUnitTree cut = workingCopy
281: .getCompilationUnit();
282: TreeMaker make = workingCopy.getTreeMaker();
283: for (Tree typeDecl : cut.getTypeDecls()) {
284: // ensure that it is correct type declaration, i.e. class
285: if (Tree.Kind.CLASS == typeDecl.getKind()) {
286: ClassTree clazz = (ClassTree) typeDecl;
287: MethodTree method = (MethodTree) clazz
288: .getMembers().get(0);
289: ExpressionStatementTree statement = make
290: .ExpressionStatement(make
291: .MethodInvocation(
292: Collections
293: .<ExpressionTree> emptyList(),
294: make
295: .MemberSelect(
296: make
297: .MemberSelect(
298: make
299: .Identifier("System"),
300: "err"),
301: "println"),
302: Collections
303: .singletonList(make
304: .Literal(null))));
305: BlockTree newBody = make
306: .Block(
307: Collections
308: .<StatementTree> singletonList(statement),
309: false);
310: workingCopy.rewrite(method.getBody(), newBody);
311: }
312: }
313: }
314:
315: };
316: src.runModificationTask(task).commit();
317: String res = TestUtilities.copyFileToString(testFile);
318: System.err.println(res);
319: assertEquals(golden, res);
320: }
321:
322: /**
323: * #93730 - incorrectly diffed method invocation.
324: */
325: public void testReplaceMethod() throws Exception {
326: System.err.println("testReplaceMethod");
327: testFile = new File(getWorkDir(), "Test.java");
328: TestUtilities.copyStringToFile(testFile, "package personal;\n"
329: + "\n" + "public class Test {\n"
330: + " public Test() {\n" + " }\n" + " \n"
331: + " public Object method() {\n"
332: + " for(int i = 0; i < 10; i++) {\n"
333: + " System.out.println(\"In loop\");\n"
334: + " }\n" + " Thread.currentThread();\n"
335: + " }\n" + "}\n");
336:
337: String golden = "package personal;\n" + "\n"
338: + "public class Test {\n" + " public Test() {\n"
339: + " }\n" + " \n"
340: + " public Object method() {\n"
341: + " System.out.println(\"Ahoj svete!\");\n"
342: + " }\n" + "}\n";
343:
344: JavaSource src = getJavaSource(testFile);
345:
346: Task<WorkingCopy> task = new Task<WorkingCopy>() {
347: public void run(WorkingCopy workingCopy) throws IOException {
348: workingCopy.toPhase(Phase.RESOLVED);
349: CompilationUnitTree cut = workingCopy
350: .getCompilationUnit();
351: TreeMaker make = workingCopy.getTreeMaker();
352: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
353: MethodTree meth = (MethodTree) clazz.getMembers()
354: .get(1);
355: String bodyText = "{System.out.println(\"Ahoj svete!\");}";
356: MethodTree newMeth = make.Method(meth.getModifiers(),
357: meth.getName(), meth.getReturnType(), meth
358: .getTypeParameters(), meth
359: .getParameters(), meth.getThrows(),
360: bodyText, (ExpressionTree) meth
361: .getDefaultValue());
362: workingCopy.rewrite(meth.getBody(), newMeth.getBody());
363: }
364:
365: };
366: src.runModificationTask(task).commit();
367: String res = TestUtilities.copyFileToString(testFile);
368: System.err.println(res);
369: assertEquals(golden, res);
370: }
371:
372: public void testReplaceMethodBody1() throws Exception {
373: System.err.println("testReplaceMethodBody1");
374: testFile = new File(getWorkDir(), "Test.java");
375: TestUtilities.copyStringToFile(testFile, "package personal;\n"
376: + "\n" + "public class Test {\n"
377: + " public Test() {\n" + " }\n" + " \n"
378: + " public Object method() {\n" + " }\n" + "}\n");
379:
380: String golden = "package personal;\n" + "\n"
381: + "public class Test {\n" + " public Test() {\n"
382: + " }\n" + " \n"
383: + " public Object method() {\n"
384: + " return new Integer(5);\n" + " }\n"
385: + "}\n";
386:
387: JavaSource src = getJavaSource(testFile);
388:
389: Task<WorkingCopy> task = new Task<WorkingCopy>() {
390: public void run(WorkingCopy workingCopy) throws IOException {
391: workingCopy.toPhase(Phase.RESOLVED);
392: CompilationUnitTree cut = workingCopy
393: .getCompilationUnit();
394: TreeMaker make = workingCopy.getTreeMaker();
395: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
396: MethodTree meth = (MethodTree) clazz.getMembers()
397: .get(1);
398: String bodyText = "{return new Integer(5);}";
399: MethodTree newMeth = make.Method(meth.getModifiers(),
400: meth.getName(), meth.getReturnType(), meth
401: .getTypeParameters(), meth
402: .getParameters(), meth.getThrows(),
403: bodyText, (ExpressionTree) meth
404: .getDefaultValue());
405: workingCopy.rewrite(meth.getBody(), newMeth.getBody());
406: }
407:
408: };
409: src.runModificationTask(task).commit();
410: String res = TestUtilities.copyFileToString(testFile);
411: System.err.println(res);
412: assertEquals(golden, res);
413: }
414:
415: public void testReplaceMethodBody2() throws Exception {
416: System.err.println("testReplaceMethodBody2");
417: testFile = new File(getWorkDir(), "Test.java");
418: TestUtilities.copyStringToFile(testFile, "package personal;\n"
419: + "\n" + "public class Test {\n"
420: + " public Test() {\n" + " }\n" + " \n"
421: + " public float method() {\n" + " }\n" + "}\n");
422:
423: String golden = "package personal;\n" + "\n"
424: + "public class Test {\n" + " public Test() {\n"
425: + " }\n" + " \n"
426: + " public float method() {\n"
427: + " return 0.0F;\n" + " }\n" + "}\n";
428:
429: JavaSource src = getJavaSource(testFile);
430:
431: Task<WorkingCopy> task = new Task<WorkingCopy>() {
432: public void run(WorkingCopy workingCopy) throws IOException {
433: workingCopy.toPhase(Phase.RESOLVED);
434: CompilationUnitTree cut = workingCopy
435: .getCompilationUnit();
436: TreeMaker make = workingCopy.getTreeMaker();
437: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
438: MethodTree meth = (MethodTree) clazz.getMembers()
439: .get(1);
440: String bodyText = "{return 0.0f;}";
441: MethodTree newMeth = make.Method(meth.getModifiers(),
442: meth.getName(), meth.getReturnType(), meth
443: .getTypeParameters(), meth
444: .getParameters(), meth.getThrows(),
445: bodyText, (ExpressionTree) meth
446: .getDefaultValue());
447: workingCopy.rewrite(meth.getBody(), newMeth.getBody());
448: }
449:
450: };
451: src.runModificationTask(task).commit();
452: String res = TestUtilities.copyFileToString(testFile);
453: System.err.println(res);
454: assertEquals(golden, res);
455: }
456:
457: // #90186 regression test
458: public void testReplaceMethodBody3() throws Exception {
459: System.err.println("testReplaceMethodBody3");
460: testFile = new File(getWorkDir(), "Test.java");
461: TestUtilities.copyStringToFile(testFile, "package personal;\n"
462: + "\n" + "public class Test {\n"
463: + " public Test() {\n" + " }\n" + " \n"
464: + " public float method() {\n" + " }\n" + "}\n");
465:
466: String golden = "package personal;\n" + "\n"
467: + "public class Test {\n" + " public Test() {\n"
468: + " }\n" + " \n"
469: + " public float method() {\n"
470: + " int hash;\n" + " hash += 2;\n"
471: + " return hash;\n" + " }\n" + "}\n";
472:
473: JavaSource src = getJavaSource(testFile);
474:
475: Task<WorkingCopy> task = new Task<WorkingCopy>() {
476: public void run(WorkingCopy workingCopy) throws IOException {
477: workingCopy.toPhase(Phase.RESOLVED);
478: CompilationUnitTree cut = workingCopy
479: .getCompilationUnit();
480: TreeMaker make = workingCopy.getTreeMaker();
481: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
482: MethodTree meth = (MethodTree) clazz.getMembers()
483: .get(1);
484: String bodyText = "{ int hash; hash += 2; return hash; }";
485: MethodTree newMeth = make.Method(meth.getModifiers(),
486: meth.getName(), meth.getReturnType(), meth
487: .getTypeParameters(), meth
488: .getParameters(), meth.getThrows(),
489: bodyText, (ExpressionTree) meth
490: .getDefaultValue());
491: workingCopy.rewrite(meth.getBody(), newMeth.getBody());
492: }
493:
494: };
495: src.runModificationTask(task).commit();
496: String res = TestUtilities.copyFileToString(testFile);
497: System.err.println(res);
498: assertEquals(golden, res);
499: }
500:
501: public void testReplaceMethodBodyImports() throws Exception {
502: System.err.println("testReplaceMethodBody3");
503: testFile = new File(getWorkDir(), "Test.java");
504: TestUtilities.copyStringToFile(testFile, "package personal;\n"
505: + "\n" + "public class Test {\n"
506: + " public Test() {\n" + " }\n" + " \n"
507: + " public float method() {\n" + " }\n" + "}\n");
508:
509: String golden = "package personal;\n" + "\n"
510: + "import java.util.ArrayList;\n"
511: + "import java.util.List;\n" + "\n"
512: + "public class Test {\n" + " public Test() {\n"
513: + " }\n" + " \n"
514: + " public float method() {\n"
515: + " List list = new ArrayList();\n"
516: + " int hash;\n" + " hash += 2;\n"
517: + " return hash;\n" + " }\n" + "}\n";
518:
519: JavaSource src = getJavaSource(testFile);
520:
521: Task<WorkingCopy> task = new Task<WorkingCopy>() {
522: public void run(WorkingCopy workingCopy) throws IOException {
523: workingCopy.toPhase(Phase.RESOLVED);
524: CompilationUnitTree cut = workingCopy
525: .getCompilationUnit();
526: TreeMaker make = workingCopy.getTreeMaker();
527: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
528: MethodTree meth = (MethodTree) clazz.getMembers()
529: .get(1);
530: String bodyText = "{ java.util.List list = new java.util.ArrayList(); int hash; hash += 2; return hash; }";
531: MethodTree newMeth = make.Method(meth.getModifiers(),
532: meth.getName(), meth.getReturnType(), meth
533: .getTypeParameters(), meth
534: .getParameters(), meth.getThrows(),
535: bodyText, (ExpressionTree) meth
536: .getDefaultValue());
537: workingCopy
538: .rewrite(meth.getBody(), GeneratorUtilities
539: .get(workingCopy).importFQNs(
540: newMeth.getBody()));
541:
542: }
543:
544: };
545: src.runModificationTask(task).commit();
546: String res = TestUtilities.copyFileToString(testFile);
547: System.err.println(res);
548: assertEquals(golden, res);
549: }
550:
551: String getSourcePckg() {
552: return "org/netbeans/test/codegen/indent/";
553: }
554:
555: String getGoldenPckg() {
556: return "org/netbeans/jmi/javamodel/codegen/indent/MethodBodyTextTest/";
557: }
558:
559: }
|