KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
23 import java.io.IOException JavaDoc;
24 import javax.lang.model.element.Modifier;
25 import org.netbeans.junit.NbTestSuite;
26 import junit.textui.TestRunner;
27 import org.netbeans.api.java.source.*;
28 import static org.netbeans.api.java.source.JavaSource.*;
29 import org.openide.filesystems.FileUtil;
30
31 /**
32  * Tests the method generator.
33  *
34  * @author Pavel Flaska
35  */

36 public class Method1Test extends GeneratorTestMDRCompat {
37     
38     /** Need to be defined because of JUnit */
39     public Method1Test(String JavaDoc name) {
40         super(name);
41     }
42     
43     public static NbTestSuite suite() {
44         NbTestSuite suite = new NbTestSuite();
45         suite.addTest(new Method1Test("testMethodModifiers"));
46         suite.addTest(new Method1Test("testMethodName"));
47         suite.addTest(new Method1Test("testMethodParameters"));
48         suite.addTest(new Method1Test("testMethodParameterChange"));
49         suite.addTest(new Method1Test("testMethodThrows"));
50         suite.addTest(new Method1Test("testMethodReturnType"));
51         // suite.addTest(new Method1Test("testMethodBody"));
52
// suite.addTest(new Method1Test("testParameterizedMethod"));
53
// suite.addTest(new Method1Test("testAddRemoveInOneTrans"));
54
// suite.addTest(new Method1Test("testCreateNewMethod"));
55
return suite;
56     }
57     
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest1.java");
61     }
62
63     /**
64      * Changes the modifiers on method. Removes public modifier, sets static
65      * and private modifier.
66      */

67     public void testMethodModifiers() throws IOException JavaDoc {
68         System.err.println("testMethodModifiers");
69         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
70         CancellableTask task = new CancellableTask<WorkingCopy>() {
71
72             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
73                 workingCopy.toPhase(Phase.RESOLVED);
74                 TreeMaker make = workingCopy.getTreeMaker();
75                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
76                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
77                 ModifiersTree origMods = method.getModifiers();
78                 Set<Modifier> njuMods = new HashSet<Modifier>();
79                 njuMods.add(Modifier.PRIVATE);
80                 njuMods.add(Modifier.STATIC);
81                 workingCopy.rewrite(origMods, make.Modifiers(njuMods));
82             }
83             
84             public void cancel() {
85             }
86         };
87         testSource.runModificationTask(task).commit();
88         String JavaDoc res = TestUtilities.copyFileToString(testFile);
89         System.err.println(res);
90         assertFiles("testMethodModifiers.pass");
91     }
92     
93     /**
94      * Changes the name on the method.
95      */

96     public void testMethodName() throws IOException JavaDoc {
97         System.err.println("testMethodName");
98         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
99         CancellableTask task = new CancellableTask<WorkingCopy>() {
100
101             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
102                 workingCopy.toPhase(Phase.RESOLVED);
103                 TreeMaker make = workingCopy.getTreeMaker();
104                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
105                 MethodTree method = (MethodTree) clazz.getMembers().get(2);
106                 workingCopy.rewrite(method, make.setLabel(method, "druhaMetoda"));
107             }
108             
109             public void cancel() {
110             }
111         };
112         testSource.runModificationTask(task).commit();
113         String JavaDoc res = TestUtilities.copyFileToString(testFile);
114         System.err.println(res);
115         assertFiles("testMethodName.pass");
116     }
117     
118     /**
119      * Removes the first parameter and adds it to the end
120      */

121     public void testMethodParameters() throws IOException JavaDoc {
122         System.err.println("testMethodParameters");
123         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
124         CancellableTask task = new CancellableTask<WorkingCopy>() {
125
126             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
127                 workingCopy.toPhase(Phase.RESOLVED);
128                 TreeMaker make = workingCopy.getTreeMaker();
129                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
130                 MethodTree method = (MethodTree) clazz.getMembers().get(3);
131                 VariableTree vt = method.getParameters().get(0);
132                 VariableTree vtCopy = make.Variable(vt.getModifiers(), vt.getName(), vt.getType(), vt.getInitializer());
133                 MethodTree njuMethod = make.removeMethodParameter(method, 0);
134                 njuMethod = make.addMethodParameter(njuMethod, vt);
135                 workingCopy.rewrite(method, njuMethod);
136             }
137             
138             public void cancel() {
139             }
140         };
141         testSource.runModificationTask(task).commit();
142         String JavaDoc res = TestUtilities.copyFileToString(testFile);
143         System.err.println(res);
144         assertFiles("testMethodParameters.pass");
145     }
146     
147     /**
148      * Changes the name of the parameter.
149      */

150     public void testMethodParameterChange() throws IOException JavaDoc {
151         System.err.println("testMethodParameters");
152         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
153         CancellableTask task = new CancellableTask<WorkingCopy>() {
154
155             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
156                 workingCopy.toPhase(Phase.RESOLVED);
157                 TreeMaker make = workingCopy.getTreeMaker();
158                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
159                 MethodTree method = (MethodTree) clazz.getMembers().get(4);
160                 List<? extends VariableTree> parameters = method.getParameters();
161                 VariableTree vt = parameters.get(0);
162                 MethodTree njuMethod = make.removeMethodParameter(method, 0);
163                 VariableTree njuParameter = make.setLabel(vt, "aParNewName");
164                 workingCopy.rewrite(vt, njuParameter);
165                 // is this really needed? -- Probably bug, setting label for varTree
166
// should be enough.
167
njuMethod = make.insertMethodParameter(njuMethod, 0, njuParameter);
168                 workingCopy.rewrite(method, njuMethod);
169             }
170             
171             public void cancel() {
172             }
173         };
174         testSource.runModificationTask(task).commit();
175         String JavaDoc res = TestUtilities.copyFileToString(testFile);
176         System.err.println(res);
177         assertFiles("testMethodParameterChange.pass");
178     }
179
180     /**
181      * Removes first exception thrown, adds another one to the end.
182      */

183     public void testMethodThrows() throws IOException JavaDoc {
184         System.err.println("testMethodThrows");
185         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
186         CancellableTask task = new CancellableTask<WorkingCopy>() {
187
188             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
189                 workingCopy.toPhase(Phase.RESOLVED);
190                 TreeMaker make = workingCopy.getTreeMaker();
191                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
192                 MethodTree method = (MethodTree) clazz.getMembers().get(5);
193                 ExpressionTree ident = make.Identifier("java.lang.IllegalMonitorStateException");
194                 MethodTree njuMethod = make.removeMethodThrows(method, 0);
195                 njuMethod = make.addMethodThrows(njuMethod, ident);
196                 workingCopy.rewrite(method, njuMethod);
197             }
198             
199             public void cancel() {
200             }
201         };
202         testSource.runModificationTask(task).commit();
203         String JavaDoc res = TestUtilities.copyFileToString(testFile);
204         System.err.println(res);
205         assertFiles("testMethodThrows.pass");
206     }
207     
208     /**
209      * Changes return type to String.
210      */

211     public void testMethodReturnType() throws IOException JavaDoc {
212         System.err.println("testMethodReturnType");
213         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
214         CancellableTask task = new CancellableTask<WorkingCopy>() {
215
216             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
217                 workingCopy.toPhase(Phase.RESOLVED);
218                 TreeMaker make = workingCopy.getTreeMaker();
219                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
220                 MethodTree method = (MethodTree) clazz.getMembers().get(6);
221                 Tree returnType = method.getReturnType();
222                 IdentifierTree identifier = make.Identifier("String");
223                 workingCopy.rewrite(returnType, identifier);
224             }
225             
226             public void cancel() {
227             }
228         };
229         testSource.runModificationTask(task).commit();
230         String JavaDoc res = TestUtilities.copyFileToString(testFile);
231         System.err.println(res);
232         assertFiles("testMethodReturnType.pass");
233     }
234     
235     /**
236      * Tests method body.
237      */

238 // public void testMethodBody() throws IOException {
239
// System.err.println("testMethodBody");
240
// process(
241
// new Transformer<Void, Object>() {
242
// public Void visitMethod(MethodTree node, Object p) {
243
// super.visitMethod(node, p);
244
// if ("seventhMethod".contentEquals(node.getName())) {
245
// Set<Modifier> njuMods = new HashSet<Modifier>();
246
// njuMods.add(Modifier.PUBLIC);
247
// MethodTree njuMethod = make.Method(
248
// make.Modifiers(njuMods),
249
// node.getName(),
250
// (ExpressionTree) node.getReturnType(),
251
// node.getTypeParameters(),
252
// node.getParameters(),
253
// node.getThrows(),
254
// make.Block(Collections.EMPTY_LIST, false),
255
// (ExpressionTree) node.getDefaultValue()
256
// );
257
// changes.rewrite(node, njuMethod);
258
// } else if ("eighthMethod".contentEquals(node.getName())) {
259
// Set<Modifier> njuMods = new HashSet<Modifier>();
260
// njuMods.add(Modifier.PUBLIC);
261
// njuMods.add(Modifier.ABSTRACT);
262
// MethodTree njuMethod = make.Method(
263
// make.Modifiers(njuMods),
264
// node.getName(),
265
// (ExpressionTree) node.getReturnType(),
266
// node.getTypeParameters(),
267
// node.getParameters(),
268
// node.getThrows(),
269
// null,
270
// (ExpressionTree) node.getDefaultValue()
271
// );
272
// changes.rewrite(node, njuMethod);
273
// }
274
// else if ("interfaceMethod".contentEquals(node.getName())) {
275
// Set<Modifier> njuMods = new HashSet<Modifier>();
276
// njuMods.add(Modifier.PUBLIC);
277
// njuMods.add(Modifier.ABSTRACT);
278
// ExpressionTree ident = make.Identifier("java.io.IOException");
279
// MethodTree njuMethod = make.Method(
280
// make.Modifiers(njuMods),
281
// node.getName(),
282
// (ExpressionTree) node.getReturnType(),
283
// node.getTypeParameters(),
284
// node.getParameters(),
285
// Collections.<ExpressionTree>singletonList(ident),
286
// null,
287
// (ExpressionTree) node.getDefaultValue()
288
// );
289
// changes.rewrite(node, njuMethod);
290
// }
291
// return null;
292
// }
293
// });
294
// assertFiles("testMethodBody.pass");
295
// }
296
//
297
// public void testCreateNewMethod() throws IOException {
298
// System.err.println("testCreateNewMethod");
299
// process(new Transformer<Void, Object>() {
300
// public Void visitClass(ClassTree node, Object p) {
301
// super.visitClass(node, p);
302
// if ("TestInterface".equals(node.getSimpleName().toString())) {
303
// return null; // do it just for outer class
304
// }
305
// // create method modifiers
306
// ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST);
307
// // create parameters
308
// VariableTree par1 = make.Variable(parMods, "a", make.PrimitiveType(TypeKind.INT), null);
309
// VariableTree par2 = make.Variable(parMods, "b", make.PrimitiveType(TypeKind.FLOAT), null);
310
// List<VariableTree> parList = new ArrayList<VariableTree>(2);
311
// parList.add(par1);
312
// parList.add(par2);
313
// // create method
314
// MethodTree newMethod = make.Method(
315
// make.Modifiers(
316
// Collections.singleton(Modifier.PUBLIC), // modifiers
317
// Collections.EMPTY_LIST // annotations
318
// ), // modifiers and annotations
319
// "newlyCreatedMethod", // name
320
// make.PrimitiveType(TypeKind.VOID), // return type
321
// Collections.EMPTY_LIST, // type parameters for parameters
322
// parList, // parameters
323
// Collections.singletonList(make.Identifier("java.io.IOException")), // throws
324
// make.Block(Collections.EMPTY_LIST, false), // empty statement block
325
// null // default value - not applicable here, used by annotations
326
// );
327
// changes.rewrite(node, make.addClassMember(node, newMethod));
328
// return null;
329
// }
330
// });
331
// assertFiles("testCreateNewMethod.pass");
332
// }
333
//
334
// public void testParameterizedMethod() throws IOException {
335
// //Transformer tm; tm.
336
// System.err.println("testParameterizedMethod");
337
// process(new MutableTransformer<Void, Object>() {
338
// public Void visitClass(ClassTree node, Object p) {
339
// super.visitClass(node, p);
340
// if ("TestInterface".equals(node.getSimpleName().toString())) {
341
// return null; // do it just for outer class
342
// }
343
// // create method modifiers
344
// ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST);
345
// // create type params/parameters
346
// TypeParameterTree tpt = make.TypeParameter("T", Collections.<ExpressionTree>emptyList());
347
// VariableTree par1 = make.Variable(parMods, "cl", make.Identifier("T"), null);
348
// // create method
349
// MethodTree newMethod = Method( // remove mutable transformer
350
// make.Modifiers(
351
// Collections.singleton(Modifier.PUBLIC), // modifiers
352
// Collections.EMPTY_LIST // annotations
353
// ), // modifiers and annotations
354
// "getIt", // name
355
// make.Identifier("T"), // return type
356
// Collections.<TypeParameterTree>singletonList(tpt), // type parameters for parameters
357
// Collections.<VariableTree>singletonList(par1), // parameters
358
// Collections.<ExpressionTree>emptyList(), // throws
359
// "{ return null; }",
360
// null // default value - not applicable here, used by annotations
361
// );
362
// attach(newMethod, "New method.");
363
// changes.rewrite(node, make.addClassMember(node, newMethod));
364
// return null;
365
// }
366
// });
367
// assertFiles("testParameterizedMethod.pass");
368
// }
369
//
370
// public void testAddRemoveInOneTrans() throws IOException {
371
// System.err.println("testAddRemoveInOneTrans");
372
// process(new MutableTransformer<Void, Object>() {
373
// public Void visitClass(ClassTree node, Object p) {
374
// super.visitClass(node, p);
375
// if ("TestInterface".equals(node.getSimpleName().toString())) {
376
// return null; // do it just for outer class
377
// }
378
// // create method modifiers
379
// ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST);
380
// TypeParameterTree typePar = make.TypeParameter("T2", Collections.<ExpressionTree>emptyList());
381
// // create parameter "T newPar"
382
// VariableTree param = make.Variable(parMods, "newPar", make.Identifier("T2"), null);
383
// // create the method
384
// MethodTree method = Method(
385
// make.Modifiers(
386
// Collections.singleton(Modifier.PUBLIC), // modifiers
387
// Collections.EMPTY_LIST // annotations
388
// ), // modifiers and annotations
389
// "setIt",
390
// make.Identifier("T2"),
391
// Collections.<TypeParameterTree>singletonList(typePar), // type params
392
// Collections.<VariableTree>singletonList(param), // parameters
393
// Collections.<ExpressionTree>emptyList(), // exceptions
394
// "{ return null; }",
395
// null
396
// );
397
// ClassTree newClass = make.removeClassMember(node, node.getMembers().size()-1);
398
// newClass = make.addClassMember(newClass, method);
399
// changes.rewrite(node, newClass);
400
// return null;
401
// }
402
// });
403
// assertFiles("testAddRemoveInOneTrans.pass");
404
// }
405

406     ////////////////////////////////////////////////////////////////////////////
407
/**
408      * @param args the command line arguments
409      */

410     public static void main(String JavaDoc[] args) {
411         TestRunner.run(suite());
412     }
413
414     String JavaDoc getSourcePckg() {
415         return "org/netbeans/test/codegen/";
416     }
417
418     String JavaDoc getGoldenPckg() {
419         return "org/netbeans/jmi/javamodel/codegen/MethodTest1/MethodTest1/";
420     }
421
422 }
423
Popular Tags