KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import javax.lang.model.element.Modifier;
25 import com.sun.source.tree.*;
26 import org.netbeans.api.java.source.*;
27 import org.netbeans.junit.NbTestSuite;
28 import static org.netbeans.api.java.source.JavaSource.*;
29
30 /**
31  * Tests method parameters.
32  *
33  * @author Pavel Flaska
34  */

35 public class MethodParametersTest extends GeneratorTestMDRCompat {
36     
37     /** Creates a new instance of MethodParametersTest */
38     public MethodParametersTest(String JavaDoc testName) {
39         super(testName);
40     }
41     
42     public static NbTestSuite suite() {
43         NbTestSuite suite = new NbTestSuite();
44 // suite.addTestSuite(MethodParametersTest.class);
45
// suite.addTest(new MethodParametersTest("testAddInsertReplaceParameters"));
46
suite.addTest(new MethodParametersTest("testAddFirst"));
47         suite.addTest(new MethodParametersTest("testAddToIndex0"));
48         suite.addTest(new MethodParametersTest("testRemoveFirstTwo"));
49         suite.addTest(new MethodParametersTest("testRemoveLast"));
50         suite.addTest(new MethodParametersTest("testRemoveLastTwo"));
51         suite.addTest(new MethodParametersTest("testRemoveMid"));
52         suite.addTest(new MethodParametersTest("testSwap"));
53         suite.addTest(new MethodParametersTest("testRenameInTypePar"));
54         suite.addTest(new MethodParametersTest("testRenameInParameterizedType"));
55         return suite;
56     }
57     
58     public void testAddInsertReplaceParameters() throws Exception JavaDoc {
59         testFile = new File JavaDoc(getWorkDir(), "Test.java");
60         TestUtilities.copyStringToFile(testFile,
61             "package hierbas.del.litoral;\n\n" +
62             "import java.io.File;\n\n" +
63             "public class Test {\n" +
64             " public void taragui(int a, long c, String s) {\n" +
65             " }\n" +
66             "}\n"
67             );
68         String JavaDoc golden =
69             "package hierbas.del.litoral;\n\n" +
70             "import java.io.File;\n\n" +
71             "public class Test {\n" +
72             " public void taragui(final File elaborada,File marcela, long c, String s,File cedron) {\n" +
73             " }\n" +
74             "}\n";
75
76         JavaSource src = getJavaSource(testFile);
77         
78         CancellableTask task = new CancellableTask<WorkingCopy>() {
79
80             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
81                 workingCopy.toPhase(Phase.RESOLVED);
82                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
83                 TreeMaker make = workingCopy.getTreeMaker();
84                 for (Tree typeDecl : cut.getTypeDecls()) {
85                     // ensure that it is correct type declaration, i.e. class
86
if (Tree.Kind.CLASS == typeDecl.getKind()) {
87                         ClassTree clazz = (ClassTree) typeDecl;
88                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
89                         MethodTree copy = make.insertMethodParameter(
90                             node, 0,
91                             make.Variable(
92                                 make.Modifiers(
93                                     Collections.singleton(Modifier.FINAL),
94                                     Collections.<AnnotationTree>emptyList()
95                                 ),
96                                 "elaborada",
97                                 make.Identifier("File"),
98                                 null
99                             )
100                         );
101                         copy = make.removeMethodParameter(copy, 1);
102                         copy = make.addMethodParameter(
103                             copy,
104                             make.Variable(
105                                 make.Modifiers(
106                                     Collections.<Modifier>emptySet(),
107                                     Collections.<AnnotationTree>emptyList()
108                                 ),
109                                 "cedron",
110                                 make.Identifier("File"),
111                                 null
112                             )
113                         );
114                         copy = make.insertMethodParameter(
115                             copy,
116                             1,
117                             make.Variable(
118                                 make.Modifiers(
119                                     Collections.<Modifier>emptySet(),
120                                     Collections.<AnnotationTree>emptyList()
121                                 ),
122                                 "marcela",
123                                 make.Identifier("File"),
124                                 null
125                             )
126                         );
127                         workingCopy.rewrite(node, copy);
128                     }
129                 }
130             }
131
132             public void cancel() {
133             }
134         };
135         src.runModificationTask(task).commit();
136         String JavaDoc res = TestUtilities.copyFileToString(testFile);
137         System.err.println(res);
138         assertEquals(golden, res);
139     }
140     
141     public void testAddFirst() throws Exception JavaDoc {
142         testFile = new File JavaDoc(getWorkDir(), "Test.java");
143         TestUtilities.copyStringToFile(testFile,
144             "package hierbas.del.litoral;\n\n" +
145             "import java.io.File;\n\n" +
146             "public class Test {\n" +
147             " public void taragui() {\n" +
148             " }\n" +
149             "}\n"
150             );
151         String JavaDoc golden =
152             "package hierbas.del.litoral;\n\n" +
153             "import java.io.File;\n\n" +
154             "public class Test {\n" +
155             " public void taragui(final File elaborada) {\n" +
156             " }\n" +
157             "}\n";
158
159         JavaSource src = getJavaSource(testFile);
160         
161         CancellableTask task = new CancellableTask<WorkingCopy>() {
162
163             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
164                 workingCopy.toPhase(Phase.RESOLVED);
165                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
166                 TreeMaker make = workingCopy.getTreeMaker();
167                 for (Tree typeDecl : cut.getTypeDecls()) {
168                     // ensure that it is correct type declaration, i.e. class
169
if (Tree.Kind.CLASS == typeDecl.getKind()) {
170                         ClassTree clazz = (ClassTree) typeDecl;
171                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
172                         MethodTree copy = make.insertMethodParameter(
173                             node, 0,
174                             make.Variable(
175                                 make.Modifiers(
176                                     Collections.singleton(Modifier.FINAL),
177                                     Collections.<AnnotationTree>emptyList()
178                                 ),
179                                 "elaborada",
180                                 make.Identifier("File"),
181                                 null
182                             )
183                         );
184                         workingCopy.rewrite(node, copy);
185                     }
186                 }
187             }
188
189             public void cancel() {
190             }
191         };
192         src.runModificationTask(task).commit();
193         String JavaDoc res = TestUtilities.copyFileToString(testFile);
194         System.err.println(res);
195         assertEquals(golden, res);
196     }
197     
198     public void testAddToIndex0() throws Exception JavaDoc {
199         testFile = new File JavaDoc(getWorkDir(), "Test.java");
200         TestUtilities.copyStringToFile(testFile,
201             "package hierbas.del.litoral;\n\n" +
202             "import java.io.File;\n\n" +
203             "public class Test {\n" +
204             " public void taragui(final File carqueja) {\n" +
205             " }\n" +
206             "}\n"
207             );
208         String JavaDoc golden =
209             "package hierbas.del.litoral;\n\n" +
210             "import java.io.File;\n\n" +
211             "public class Test {\n" +
212             " public void taragui(final File elaborada,final File carqueja) {\n" +
213             " }\n" +
214             "}\n";
215
216         JavaSource src = getJavaSource(testFile);
217         
218         CancellableTask task = new CancellableTask<WorkingCopy>() {
219
220             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
221                 workingCopy.toPhase(Phase.RESOLVED);
222                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
223                 TreeMaker make = workingCopy.getTreeMaker();
224                 for (Tree typeDecl : cut.getTypeDecls()) {
225                     // ensure that it is correct type declaration, i.e. class
226
if (Tree.Kind.CLASS == typeDecl.getKind()) {
227                         ClassTree clazz = (ClassTree) typeDecl;
228                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
229                         MethodTree copy = make.insertMethodParameter(
230                             node, 0,
231                             make.Variable(
232                                 make.Modifiers(
233                                     Collections.singleton(Modifier.FINAL),
234                                     Collections.<AnnotationTree>emptyList()
235                                 ),
236                                 "elaborada",
237                                 make.Identifier("File"),
238                                 null
239                             )
240                         );
241                         workingCopy.rewrite(node, copy);
242                     }
243                 }
244             }
245
246             public void cancel() {
247             }
248         };
249         src.runModificationTask(task).commit();
250         String JavaDoc res = TestUtilities.copyFileToString(testFile);
251         System.err.println(res);
252         assertEquals(golden, res);
253     }
254     
255     public void testRemoveLast() throws Exception JavaDoc {
256         testFile = new File JavaDoc(getWorkDir(), "Test.java");
257         TestUtilities.copyStringToFile(testFile,
258             "package hierbas.del.litoral;\n\n" +
259             "import java.io.File;\n\n" +
260             "public class Test {\n" +
261             " public void taragui(int b) {\n" +
262             " }\n" +
263             "}\n"
264             );
265         String JavaDoc golden =
266             "package hierbas.del.litoral;\n\n" +
267             "import java.io.File;\n\n" +
268             "public class Test {\n" +
269             " public void taragui() {\n" +
270             " }\n" +
271             "}\n";
272
273         JavaSource src = getJavaSource(testFile);
274         
275         CancellableTask task = new CancellableTask<WorkingCopy>() {
276
277             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
278                 workingCopy.toPhase(Phase.RESOLVED);
279                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
280                 TreeMaker make = workingCopy.getTreeMaker();
281                 for (Tree typeDecl : cut.getTypeDecls()) {
282                     // ensure that it is correct type declaration, i.e. class
283
if (Tree.Kind.CLASS == typeDecl.getKind()) {
284                         ClassTree clazz = (ClassTree) typeDecl;
285                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
286                         MethodTree copy = make.removeMethodParameter(
287                             node, 0
288                         );
289                         workingCopy.rewrite(node, copy);
290                     }
291                 }
292             }
293
294             public void cancel() {
295             }
296         };
297         src.runModificationTask(task).commit();
298         String JavaDoc res = TestUtilities.copyFileToString(testFile);
299         System.err.println(res);
300         assertEquals(golden, res);
301     }
302     
303     public void testRemoveMid() throws Exception JavaDoc {
304         testFile = new File JavaDoc(getWorkDir(), "Test.java");
305         TestUtilities.copyStringToFile(testFile,
306             "package hierbas.del.litoral;\n\n" +
307             "import java.io.File;\n\n" +
308             "public class Test {\n" +
309             " public void taragui(int para, int empezar, int sugerimos) {\n" +
310             " }\n" +
311             "}\n"
312             );
313         String JavaDoc golden =
314             "package hierbas.del.litoral;\n\n" +
315             "import java.io.File;\n\n" +
316             "public class Test {\n" +
317             " public void taragui(int para, int sugerimos) {\n" +
318             " }\n" +
319             "}\n";
320
321         JavaSource src = getJavaSource(testFile);
322         
323         CancellableTask task = new CancellableTask<WorkingCopy>() {
324
325             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
326                 workingCopy.toPhase(Phase.RESOLVED);
327                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
328                 TreeMaker make = workingCopy.getTreeMaker();
329                 for (Tree typeDecl : cut.getTypeDecls()) {
330                     // ensure that it is correct type declaration, i.e. class
331
if (Tree.Kind.CLASS == typeDecl.getKind()) {
332                         ClassTree clazz = (ClassTree) typeDecl;
333                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
334                         MethodTree copy = make.removeMethodParameter(
335                             node, 1
336                         );
337                         workingCopy.rewrite(node, copy);
338                     }
339                 }
340             }
341
342             public void cancel() {
343             }
344         };
345         src.runModificationTask(task).commit();
346         String JavaDoc res = TestUtilities.copyFileToString(testFile);
347         System.err.println(res);
348         assertEquals(golden, res);
349     }
350     
351     public void testRemoveLastTwo() throws Exception JavaDoc {
352         testFile = new File JavaDoc(getWorkDir(), "Test.java");
353         TestUtilities.copyStringToFile(testFile,
354             "package hierbas.del.litoral;\n\n" +
355             "import java.io.File;\n\n" +
356             "public class Test {\n" +
357             " public void taragui(int para, int empezar, int sugerimos) {\n" +
358             " }\n" +
359             "}\n"
360             );
361         String JavaDoc golden =
362             "package hierbas.del.litoral;\n\n" +
363             "import java.io.File;\n\n" +
364             "public class Test {\n" +
365             " public void taragui(int para) {\n" +
366             " }\n" +
367             "}\n";
368
369         JavaSource src = getJavaSource(testFile);
370         
371         CancellableTask task = new CancellableTask<WorkingCopy>() {
372
373             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
374                 workingCopy.toPhase(Phase.RESOLVED);
375                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
376                 TreeMaker make = workingCopy.getTreeMaker();
377                 for (Tree typeDecl : cut.getTypeDecls()) {
378                     // ensure that it is correct type declaration, i.e. class
379
if (Tree.Kind.CLASS == typeDecl.getKind()) {
380                         ClassTree clazz = (ClassTree) typeDecl;
381                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
382                         MethodTree copy = make.removeMethodParameter(node, 1);
383                         copy = make.removeMethodParameter(copy, 1);
384                         workingCopy.rewrite(node, copy);
385                     }
386                 }
387             }
388
389             public void cancel() {
390             }
391         };
392         src.runModificationTask(task).commit();
393         String JavaDoc res = TestUtilities.copyFileToString(testFile);
394         System.err.println(res);
395         assertEquals(golden, res);
396     }
397     
398     public void testRemoveFirstTwo() throws Exception JavaDoc {
399         testFile = new File JavaDoc(getWorkDir(), "Test.java");
400         TestUtilities.copyStringToFile(testFile,
401             "package hierbas.del.litoral;\n\n" +
402             "import java.io.File;\n\n" +
403             "public class Test {\n" +
404             " public void taragui(int para, int empezar, int sugerimos) {\n" +
405             " }\n" +
406             "}\n"
407             );
408         String JavaDoc golden =
409             "package hierbas.del.litoral;\n\n" +
410             "import java.io.File;\n\n" +
411             "public class Test {\n" +
412             " public void taragui( int sugerimos) {\n" +
413             " }\n" +
414             "}\n";
415
416         JavaSource src = getJavaSource(testFile);
417         
418         CancellableTask task = new CancellableTask<WorkingCopy>() {
419
420             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
421                 workingCopy.toPhase(Phase.RESOLVED);
422                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
423                 TreeMaker make = workingCopy.getTreeMaker();
424                 for (Tree typeDecl : cut.getTypeDecls()) {
425                     // ensure that it is correct type declaration, i.e. class
426
if (Tree.Kind.CLASS == typeDecl.getKind()) {
427                         ClassTree clazz = (ClassTree) typeDecl;
428                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
429                         MethodTree copy = make.removeMethodParameter(node, 0);
430                         copy = make.removeMethodParameter(copy, 0);
431                         workingCopy.rewrite(node, copy);
432                     }
433                 }
434             }
435
436             public void cancel() {
437             }
438         };
439         src.runModificationTask(task).commit();
440         String JavaDoc res = TestUtilities.copyFileToString(testFile);
441         System.err.println(res);
442         assertEquals(golden, res);
443     }
444     
445     public void testSwap() throws Exception JavaDoc {
446         testFile = new File JavaDoc(getWorkDir(), "Test.java");
447         TestUtilities.copyStringToFile(testFile,
448             "package hierbas.del.litoral;\n\n" +
449             "import java.io.File;\n\n" +
450             "public class Test {\n" +
451             " public void taragui(int empezar, int sugerimos) {\n" +
452             " }\n" +
453             "}\n"
454             );
455         String JavaDoc golden =
456             "package hierbas.del.litoral;\n\n" +
457             "import java.io.File;\n\n" +
458             "public class Test {\n" +
459             " public void taragui( int sugerimos,int empezar) {\n" +
460             " }\n" +
461             "}\n";
462
463         JavaSource src = getJavaSource(testFile);
464         
465         CancellableTask task = new CancellableTask<WorkingCopy>() {
466
467             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
468                 workingCopy.toPhase(Phase.RESOLVED);
469                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
470                 TreeMaker make = workingCopy.getTreeMaker();
471                 for (Tree typeDecl : cut.getTypeDecls()) {
472                     // ensure that it is correct type declaration, i.e. class
473
if (Tree.Kind.CLASS == typeDecl.getKind()) {
474                         ClassTree clazz = (ClassTree) typeDecl;
475                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
476                         VariableTree vt = node.getParameters().get(0);
477                         MethodTree copy = make.removeMethodParameter(node, 0);
478                         copy = make.addMethodParameter(copy, vt);
479                         workingCopy.rewrite(node, copy);
480                     }
481                 }
482             }
483
484             public void cancel() {
485             }
486         };
487         src.runModificationTask(task).commit();
488         String JavaDoc res = TestUtilities.copyFileToString(testFile);
489         System.err.println(res);
490         assertEquals(golden, res);
491     }
492     
493     /**
494      * #89746: Rename in type parameter/parameterized type
495      */

496     public void testRenameInTypePar() throws Exception JavaDoc {
497         testFile = new File JavaDoc(getWorkDir(), "Test.java");
498         TestUtilities.copyStringToFile(testFile,
499             "package hierbas.del.litoral;\n\n" +
500             "import java.io.File;\n\n" +
501             "public class Test {\n" +
502             " public void taragui(List<Something> empezar, int sugerimos) {\n" +
503             " }\n" +
504             "}\n"
505             );
506         String JavaDoc golden =
507             "package hierbas.del.litoral;\n\n" +
508             "import java.io.File;\n\n" +
509             "public class Test {\n" +
510             " public void taragui(List<Neco> empezar, int sugerimos) {\n" +
511             " }\n" +
512             "}\n";
513
514         JavaSource src = getJavaSource(testFile);
515         
516         CancellableTask task = new CancellableTask<WorkingCopy>() {
517
518             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
519                 workingCopy.toPhase(Phase.RESOLVED);
520                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
521                 TreeMaker make = workingCopy.getTreeMaker();
522                 for (Tree typeDecl : cut.getTypeDecls()) {
523                     // ensure that it is correct type declaration, i.e. class
524
if (Tree.Kind.CLASS == typeDecl.getKind()) {
525                         ClassTree clazz = (ClassTree) typeDecl;
526                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
527                         VariableTree vt = node.getParameters().get(0);
528                         ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType();
529                         IdentifierTree it = (IdentifierTree) ptt.getTypeArguments().get(0);
530                         workingCopy.rewrite(it, make.Identifier("Neco"));
531                     }
532                 }
533             }
534
535             public void cancel() {
536             }
537         };
538         src.runModificationTask(task).commit();
539         String JavaDoc res = TestUtilities.copyFileToString(testFile);
540         System.err.println(res);
541         assertEquals(golden, res);
542     }
543
544     /**
545      * #89746: Rename in type parameter/parameterized type
546      */

547     public void testRenameInParameterizedType() throws Exception JavaDoc {
548         testFile = new File JavaDoc(getWorkDir(), "Test.java");
549         TestUtilities.copyStringToFile(testFile,
550             "package hierbas.del.litoral;\n\n" +
551             "import java.io.File;\n\n" +
552             "public class Test {\n" +
553             " public void taragui(List<Something> empezar, int sugerimos) {\n" +
554             " }\n" +
555             "}\n"
556             );
557         String JavaDoc golden =
558             "package hierbas.del.litoral;\n\n" +
559             "import java.io.File;\n\n" +
560             "public class Test {\n" +
561             " public void taragui(Seznam<Something> empezar, int sugerimos) {\n" +
562             " }\n" +
563             "}\n";
564
565         JavaSource src = getJavaSource(testFile);
566         
567         CancellableTask task = new CancellableTask<WorkingCopy>() {
568
569             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
570                 workingCopy.toPhase(Phase.RESOLVED);
571                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
572                 TreeMaker make = workingCopy.getTreeMaker();
573                 for (Tree typeDecl : cut.getTypeDecls()) {
574                     // ensure that it is correct type declaration, i.e. class
575
if (Tree.Kind.CLASS == typeDecl.getKind()) {
576                         ClassTree clazz = (ClassTree) typeDecl;
577                         MethodTree node = (MethodTree) clazz.getMembers().get(1);
578                         VariableTree vt = node.getParameters().get(0);
579                         ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType();
580                         workingCopy.rewrite(ptt.getType(), make.Identifier("Seznam"));
581                     }
582                 }
583             }
584
585             public void cancel() {
586             }
587         };
588         src.runModificationTask(task).commit();
589         String JavaDoc res = TestUtilities.copyFileToString(testFile);
590         System.err.println(res);
591         assertEquals(golden, res);
592     }
593
594     protected void setUp() throws Exception JavaDoc {
595         super.setUp();
596         testFile = getFile(getSourceDir(), getSourcePckg() + "Test.java");
597     }
598
599     String JavaDoc getGoldenPckg() {
600         return "";
601     }
602
603     String JavaDoc getSourcePckg() {
604         return "";
605     }
606     
607 }
608
Popular Tags