KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.io.File JavaDoc;
23 import java.util.Collections JavaDoc;
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 /**
36  *
37  * @author Pavel Flaska
38  */

39 public class MethodBodyTest extends GeneratorTest {
40     
41     /** Creates a new instance of MethodBodyTest */
42     public MethodBodyTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite();
48         suite.addTestSuite(MethodBodyTest.class);
49 // suite.addTest(new MethodBodyTest("testAddFirstStatement"));
50
// suite.addTest(new MethodBodyTest("testAddBodyText"));
51
// suite.addTest(new MethodBodyTest("testAddVarDecl"));
52
return suite;
53     }
54     
55     /**
56      * Add first method body statement
57      */

58     public void testAddFirstStatement() throws Exception JavaDoc {
59         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
108         System.err.println(res);
109         assertEquals(golden, res);
110     }
111     
112     /**
113      * Add method body as a text
114      */

115     public void testAddBodyText() throws Exception JavaDoc {
116         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
150         System.err.println(res);
151         assertEquals(golden, res);
152     }
153     
154     /**
155      * "Map env = new HashMap();"
156      */

157     public void testAddVarDecl() throws Exception JavaDoc {
158         testFile = new File JavaDoc(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 JavaDoc 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 JavaDoc {
181                 workingCopy.toPhase(Phase.RESOLVED);
182                 TreeMaker treeMaker = workingCopy.getTreeMaker();
183                 TypeElement hashMapClass = workingCopy.getElements().getTypeElement("java.util.HashMap"); // NOI18N
184
ExpressionTree hashMapEx = treeMaker.QualIdent(hashMapClass);
185                 TypeElement mapClass = workingCopy.getElements().getTypeElement("java.util.Map");// NOI18N
186
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 JavaDoc res = TestUtilities.copyFileToString(testFile);
208         System.err.println(res);
209         assertEquals(golden, res);
210     }
211     
212     String JavaDoc getGoldenPckg() {
213         return "";
214     }
215
216     String JavaDoc getSourcePckg() {
217         return "";
218     }
219
220 }
221
Popular Tags