KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.util.Collections JavaDoc;
24 import java.util.EnumSet JavaDoc;
25 import javax.lang.model.element.Modifier;
26 import javax.lang.model.type.TypeKind;
27 import static org.netbeans.api.java.lexer.JavaTokenId.*;
28 import org.netbeans.api.java.source.*;
29 import org.netbeans.api.java.source.query.Query;
30 import static org.netbeans.api.java.source.JavaSource.*;
31 import org.netbeans.junit.NbTestSuite;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  *
36  * @author Pavel Flaska
37  */

38 public class CommentsTest extends GeneratorTestMDRCompat {
39     
40     /** Creates a new instance of CommentsTest */
41     public CommentsTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     public static NbTestSuite suite() {
46         NbTestSuite suite = new NbTestSuite();
47         suite.addTestSuite(CommentsTest.class);
48 // suite.addTest(new CommentsTest("testAddStatement"));
49
// suite.addTest(new CommentsTest("testAddJavaDocToMethod"));
50
// suite.addTest(new CommentsTest("testGetComment1"));
51
// suite.addTest(new CommentsTest("testAddJavaDocToExistingMethod"));
52
return suite;
53     }
54
55     public void testAddStatement() throws Exception JavaDoc {
56         testFile = new File(getWorkDir(), "Test.java");
57         TestUtilities.copyStringToFile(testFile,
58             "package hierbas.del.litoral;\n" +
59             "\n" +
60             "import java.io.File;\n" +
61             "\n" +
62             "public class Test {\n" +
63             "\n" +
64             " void method() {\n" +
65             " }\n\n" +
66             "}\n"
67             );
68         String JavaDoc golden =
69             "package hierbas.del.litoral;\n" +
70             "\n" +
71             "import java.io.File;\n" +
72             "\n" +
73             "public class Test {\n" +
74             "\n" +
75             " void method() {" +
76             " // test\n" +
77             " int a;\n" +
78             "\n" +
79             " /**\n" +
80             " * becko\n" +
81             " */\n" +
82             " int b;\n" +
83             " // cecko\n" +
84             " int c;\n" +
85             "\n" +
86             " }\n\n" +
87             "}\n";
88
89         JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
90         
91         CancellableTask task = new CancellableTask<WorkingCopy>() {
92
93             public void run(WorkingCopy workingCopy) throws IOException {
94                 workingCopy.toPhase(Phase.RESOLVED);
95                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
96                 TreeMaker make = workingCopy.getTreeMaker();
97                 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
98                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
99                 String JavaDoc bodyText =
100                         "{" +
101                         " // test\n" +
102                         " int a;\n" +
103                         " /** becko */\n" +
104                         " int b;\n" +
105                         " /* cecko\n */\n" +
106                         " int c; // trail\n" +
107                         "}";
108                 BlockTree block = make.createMethodBody(method, bodyText);
109                 workingCopy.rewrite(method.getBody(), block);
110             }
111
112             public void cancel() {
113             }
114         };
115         src.runModificationTask(task).commit();
116         String JavaDoc res = TestUtilities.copyFileToString(testFile);
117         System.err.println(res);
118         assertEquals(golden, res);
119     }
120     
121     public void testGetComment1() throws Exception JavaDoc {
122         testFile = new File(getWorkDir(), "Test.java");
123         TestUtilities.copyStringToFile(testFile,
124             "package hierbas.del.litoral;\n" +
125             "\n" +
126             "import java.io.File;\n" +
127             "\n" +
128             "public class Test {\n" +
129             "\n" +
130             " void method() {\n" +
131             " // preceding comment\n" +
132             " int a; // trailing comment\n" +
133             " // what's up?" +
134             " }\n" +
135             "\n" +
136             "}\n"
137             );
138         JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
139         
140         CancellableTask task = new CancellableTask<WorkingCopy>() {
141
142             public void run(WorkingCopy workingCopy) throws IOException {
143                 workingCopy.toPhase(Phase.RESOLVED);
144                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
145                 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
146                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
147 // CommentHandler comments = workingCopy.getCommentHandler();
148
// CommentSet s = comments.getComments(method.getBody().getStatements().get(0));
149
// System.err.println(s);
150
}
151
152             public void cancel() {
153             }
154         };
155         src.runModificationTask(task).commit();
156     }
157     
158     public void testAddJavaDocToMethod() throws Exception JavaDoc {
159         File testFile = new File(getWorkDir(), "Test.java");
160         TestUtilities.copyStringToFile(testFile,
161             "package hierbas.del.litoral;\n" +
162             "\n" +
163             "public class Test {\n" +
164             " Test() {\n" +
165             " }\n" +
166             "\n" +
167             "}\n"
168             );
169         
170         JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
171         CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
172             
173             public void run(final WorkingCopy copy) throws Exception JavaDoc {
174                 copy.toPhase(Phase.RESOLVED);
175                 
176                 TreeMaker make = copy.getTreeMaker();
177                 ClassTree node = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
178                 MethodTree method = make.Method(
179                         make.Modifiers(EnumSet.of(Modifier.PUBLIC)),
180                         "nuevoMetodo",
181                         make.PrimitiveType(TypeKind.VOID),
182                         Collections.<TypeParameterTree>emptyList(),
183                         Collections.<VariableTree>emptyList(),
184                         Collections.<ExpressionTree>emptyList(),
185                         "{ }",
186                         null
187                 );
188                 make.addComment(method, Comment.create(
189                         Comment.Style.JAVADOC,
190                         Query.NOPOS,
191                         Query.NOPOS,
192                         Query.NOPOS,
193                         "Comentario"),
194                         true
195                 );
196                 ClassTree clazz = make.addClassMember(node, method);
197                 copy.rewrite(node, clazz);
198             }
199             
200             public void cancel() {
201             }
202         };
203         src.runModificationTask(task).commit();
204         System.err.println(TestUtilities.copyFileToString(testFile));
205         assertTrue(TestUtilities.copyFileToString(testFile).contains("nuevoMetodo"));
206         assertTrue(TestUtilities.copyFileToString(testFile).contains("Comentario"));
207     }
208     
209     public void testAddJavaDocToExistingMethod() throws Exception JavaDoc {
210         File testFile = new File(getWorkDir(), "Test.java");
211         TestUtilities.copyStringToFile(testFile,
212             "package hierbas.del.litoral;\n" +
213             "\n" +
214             "public class Test {\n" +
215             " public void test(int a) {\n" +
216             " }\n\n" +
217             "}\n"
218             );
219         
220         JavaSource src = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
221         CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
222             
223             public void run(final WorkingCopy workingCopy) throws Exception JavaDoc {
224                 workingCopy.toPhase(Phase.RESOLVED);
225                 
226                 TreeMaker make = workingCopy.getTreeMaker();
227                 ClassTree node = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
228                 MethodTree method = (MethodTree) node.getMembers().get(1);
229                 MethodTree copy = make.Method(method.getModifiers(),
230                         method.getName(),
231                         method.getReturnType(),
232                         method.getTypeParameters(),
233                         method.getParameters(),
234                         method.getThrows(),
235                         method.getBody(),
236                         (ExpressionTree) method.getDefaultValue()
237                 );
238                 make.addComment(copy, Comment.create(
239                         Comment.Style.JAVADOC,
240                         Query.NOPOS,
241                         Query.NOPOS,
242                         Query.NOPOS,
243                         "/** Comentario \n*/"),
244                         true
245                 );
246                 workingCopy.rewrite(method, copy);
247             }
248             
249             public void cancel() {
250             }
251         };
252         src.runModificationTask(task).commit();
253         System.err.println(TestUtilities.copyFileToString(testFile));
254         assertTrue(TestUtilities.copyFileToString(testFile), TestUtilities.copyFileToString(testFile).contains("Comentario"));
255     }
256
257     String JavaDoc getGoldenPckg() {
258         return "";
259     }
260
261     String JavaDoc getSourcePckg() {
262         return "";
263     }
264
265 }
266
Popular Tags