KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > MethodBodyTextTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Collections JavaDoc;
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 JavaDoc;
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 /**
37  * Tests indentation of newly generated body text in method.
38  *
39  * @author Pavel Flaska
40   */

41 public class MethodBodyTextTest extends GeneratorTestMDRCompat {
42     
43     /** Creates a new instance of MethodBodyTextTest */
44     public MethodBodyTextTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     public static NbTestSuite suite() {
49         NbTestSuite suite = new NbTestSuite();
50 // suite.addTestSuite(MethodBodyTextTest.class);
51
// suite.addTest(new MethodBodyTextTest("testSetBodyText"));
52
// suite.addTest(new MethodBodyTextTest("testCreateWithBodyText"));
53
suite.addTest(new MethodBodyTextTest("testCreateReturnBooleanBodyText"));
54 // suite.addTest(new MethodBodyTextTest("testModifyBodyText"));
55
suite.addTest(new MethodBodyTextTest("testReplaceConstrBody"));
56         suite.addTest(new MethodBodyTextTest("testReplaceMethod"));
57         return suite;
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodBodyText.java");
63     }
64     
65     public void testSetBodyText() throws java.io.IOException JavaDoc, 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 JavaDoc {
71                 workingCopy.toPhase(Phase.RESOLVED);
72                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
73                 TreeMaker make = workingCopy.getTreeMaker();
74                 for (Tree typeDecl : cut.getTypeDecls()) {
75                     // ensure that it is correct type declaration, i.e. class
76
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
90         String JavaDoc 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 JavaDoc, FileStateInvalidException {
97         JavaSource src = getJavaSource(testFile);
98         
99         CancellableTask task = new CancellableTask<WorkingCopy>() {
100
101             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
102                 workingCopy.toPhase(Phase.RESOLVED);
103                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
104                 TreeMaker make = workingCopy.getTreeMaker();
105                 for (Tree typeDecl : cut.getTypeDecls()) {
106                     // ensure that it is correct type declaration, i.e. class
107
if (Tree.Kind.CLASS == typeDecl.getKind()) {
108                         ClassTree clazz = (ClassTree) typeDecl;
109                         StringBuffer JavaDoc body = new StringBuffer JavaDoc();
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
132         String JavaDoc 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 JavaDoc, 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 JavaDoc {
143                 workingCopy.toPhase(Phase.RESOLVED);
144                 TreeMaker make = workingCopy.getTreeMaker();
145                 ClassTree node = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
146                 StringBuffer JavaDoc body = new StringBuffer JavaDoc();
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
167         System.err.println(res);
168         // there is "return 0" instead
169
String JavaDoc 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 JavaDoc, 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 JavaDoc {
181                 workingCopy.toPhase(Phase.RESOLVED);
182                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
183                 TreeMaker make = workingCopy.getTreeMaker();
184                 for (Tree typeDecl : cut.getTypeDecls()) {
185                     // ensure that it is correct type declaration, i.e. class
186
if (Tree.Kind.CLASS == typeDecl.getKind()) {
187                         ClassTree clazz = (ClassTree) typeDecl;
188                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
189                         String JavaDoc 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 JavaDoc res = TestUtilities.copyFileToString(testFile);
201         String JavaDoc golden = TestUtilities.copyFileToString(
202             getFile(getGoldenDir(), getGoldenPckg() + "testModifyBodyText_MethodBodyTextTest.pass")
203         );
204         assertEquals(golden, res);
205     }
206
207     /**
208      * Replace constructor body. -- In old constructor, syntetic super()
209      * was in the body, no syntetic element in new constructor body.
210      *
211      * #93740
212      */

213     public void testReplaceConstrBody() throws Exception JavaDoc {
214         System.err.println("testReplaceConstrBody");
215         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
244                 workingCopy.toPhase(Phase.RESOLVED);
245                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
246                 TreeMaker make = workingCopy.getTreeMaker();
247                 for (Tree typeDecl : cut.getTypeDecls()) {
248                     // ensure that it is correct type declaration, i.e. class
249
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
281         System.err.println(res);
282         assertEquals(golden, res);
283     }
284     
285     /**
286      * #93730 - incorrectly diffed method invocation.
287      */

288     public void testReplaceMethod() throws Exception JavaDoc {
289         System.err.println("testReplaceMethod");
290         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc res = TestUtilities.copyFileToString(testFile);
346         System.err.println(res);
347         assertEquals(golden, res);
348     }
349
350     String JavaDoc getSourcePckg() {
351         return "org/netbeans/test/codegen/indent/";
352     }
353
354     String JavaDoc getGoldenPckg() {
355         return "org/netbeans/jmi/javamodel/codegen/indent/MethodBodyTextTest/";
356     }
357
358 }
359
Popular Tags