KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationTree;
22 import com.sun.source.tree.BlockTree;
23 import com.sun.source.tree.ClassTree;
24 import com.sun.source.tree.IdentifierTree;
25 import com.sun.source.tree.MethodTree;
26 import com.sun.source.tree.ModifiersTree;
27 import com.sun.source.tree.VariableTree;
28 import java.io.File JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.EnumSet JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.lang.model.element.Modifier;
34 import org.netbeans.api.java.source.CancellableTask;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.api.java.source.JavaSource.Phase;
37 import org.netbeans.api.java.source.TestUtilities;
38 import org.netbeans.api.java.source.TreeMaker;
39 import org.netbeans.api.java.source.WorkingCopy;
40 import org.netbeans.junit.NbTestSuite;
41 import org.openide.filesystems.FileUtil;
42
43 /**
44  * Tests modifiers changes.
45  *
46  * @author Pavel Flaska
47  */

48 public class ModifiersTest extends GeneratorTestMDRCompat {
49     
50     /** Creates a new instance of ModifiersTEst */
51     public ModifiersTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     public static NbTestSuite suite() {
56         NbTestSuite suite = new NbTestSuite();
57         suite.addTestSuite(ModifiersTest.class);
58 // suite.addTest(new ModifiersTest("testChangeToFinalLocVar"));
59
// suite.addTest(new ModifiersTest("testAddClassAbstract"));
60
// suite.addTest(new ModifiersTest("testMethodMods1"));
61
// suite.addTest(new ModifiersTest("testMethodMods2"));
62
// suite.addTest(new ModifiersTest("testMethodMods3"));
63
// suite.addTest(new ModifiersTest("testMethodMods4"));
64
// suite.addTest(new ModifiersTest("testMethodMods5"));
65
// suite.addTest(new ModifiersTest("testMethodMods6"));
66
// suite.addTest(new ModifiersTest("testMethodMods7"));
67
// suite.addTest(new ModifiersTest("testAnnRename"));
68
return suite;
69     }
70     
71     /**
72      * Tests the change of modifier in local variable
73      */

74     public void testChangeToFinalLocVar() throws Exception JavaDoc {
75         testFile = new File JavaDoc(getWorkDir(), "Test.java");
76         TestUtilities.copyStringToFile(testFile,
77                 "package hierbas.del.litoral;\n\n" +
78                 "import java.io.*;\n\n" +
79                 "public class Test {\n" +
80                 " public void taragui() {\n" +
81                 " int i = 10;\n" +
82                 " }\n" +
83                 "}\n"
84                 );
85         String JavaDoc golden =
86                 "package hierbas.del.litoral;\n\n" +
87                 "import java.io.*;\n\n" +
88                 "public class Test {\n" +
89                 " public void taragui() {\n" +
90                 " final int i = 10;\n" +
91                 " }\n" +
92                 "}\n";
93         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
94         CancellableTask task = new CancellableTask<WorkingCopy>() {
95             
96             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
97                 workingCopy.toPhase(Phase.RESOLVED);
98                 TreeMaker make = workingCopy.getTreeMaker();
99                 
100                 // finally, find the correct body and rewrite it.
101
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
102                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
103                 BlockTree block = method.getBody();
104                 VariableTree vt = (VariableTree) block.getStatements().get(0);
105                 ModifiersTree mods = vt.getModifiers();
106                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.FINAL)));
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         assertEquals(golden, res);
116     }
117     
118     /**
119      * Update top-level class modifiers.
120      */

121     public void testAddClassAbstract() throws Exception JavaDoc {
122         testFile = new File JavaDoc(getWorkDir(), "Test.java");
123         TestUtilities.copyStringToFile(testFile,
124                 "package hierbas.del.litoral;\n\n" +
125                 "import java.io.*;\n\n" +
126                 "public class Test {\n" +
127                 " public abstract void taragui();\n" +
128                 "}\n"
129                 );
130         String JavaDoc golden =
131                 "package hierbas.del.litoral;\n\n" +
132                 "import java.io.*;\n\n" +
133                 "public abstract class Test {\n" +
134                 " public abstract void taragui();\n" +
135                 "}\n";
136         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
137         CancellableTask task = new CancellableTask<WorkingCopy>() {
138             
139             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
140                 workingCopy.toPhase(Phase.RESOLVED);
141                 TreeMaker make = workingCopy.getTreeMaker();
142                 
143                 // finally, find the correct body and rewrite it.
144
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
145                 ModifiersTree mods = clazz.getModifiers();
146                 Set JavaDoc<Modifier> s = new HashSet JavaDoc<Modifier>(mods.getFlags());
147                 s.add(Modifier.ABSTRACT);
148                 workingCopy.rewrite(mods, make.Modifiers(s));
149             }
150             
151             public void cancel() {
152             }
153         };
154         testSource.runModificationTask(task).commit();
155         String JavaDoc res = TestUtilities.copyFileToString(testFile);
156         System.err.println(res);
157         assertEquals(golden, res);
158     }
159     
160     /**
161      * Original:
162      *
163      * void method() {
164      * }
165      *
166      * Result:
167      *
168      * public static void method() {
169      * }
170      */

171     public void testMethodMods1() throws Exception JavaDoc {
172         testFile = new File JavaDoc(getWorkDir(), "Test.java");
173         TestUtilities.copyStringToFile(testFile,
174                 "package hierbas.del.litoral;\n\n" +
175                 "import java.io.*;\n\n" +
176                 "public class Test {\n" +
177                 " void method() {\n" +
178                 " }\n" +
179                 "}\n"
180                 );
181         String JavaDoc golden =
182                 "package hierbas.del.litoral;\n\n" +
183                 "import java.io.*;\n\n" +
184                 "public class Test {\n" +
185                 " public static void method() {\n" +
186                 " }\n" +
187                 "}\n";
188         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
189         CancellableTask task = new CancellableTask<WorkingCopy>() {
190             
191             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
192                 workingCopy.toPhase(Phase.RESOLVED);
193                 TreeMaker make = workingCopy.getTreeMaker();
194                 
195                 // finally, find the correct body and rewrite it.
196
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
197                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
198                 ModifiersTree mods = method.getModifiers();
199                 workingCopy.rewrite(mods, make.Modifiers(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC)));
200             }
201             
202             public void cancel() {
203             }
204         };
205         testSource.runModificationTask(task).commit();
206         String JavaDoc res = TestUtilities.copyFileToString(testFile);
207         System.err.println(res);
208         assertEquals(golden, res);
209     }
210     
211     /**
212      * Original:
213      *
214      * public static void method() {
215      * }
216      *
217      * Result:
218      *
219      * void method() {
220      * }
221      */

222     public void testMethodMods2() throws Exception JavaDoc {
223         testFile = new File JavaDoc(getWorkDir(), "Test.java");
224         TestUtilities.copyStringToFile(testFile,
225                 "package hierbas.del.litoral;\n\n" +
226                 "import java.io.*;\n\n" +
227                 "public class Test {\n" +
228                 " public static void method() {\n" +
229                 " }\n" +
230                 "}\n"
231                 );
232         String JavaDoc golden =
233                 "package hierbas.del.litoral;\n\n" +
234                 "import java.io.*;\n\n" +
235                 "public class Test {\n" +
236                 " void method() {\n" +
237                 " }\n" +
238                 "}\n";
239         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
240         CancellableTask task = new CancellableTask<WorkingCopy>() {
241             
242             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
243                 workingCopy.toPhase(Phase.RESOLVED);
244                 TreeMaker make = workingCopy.getTreeMaker();
245                 
246                 // finally, find the correct body and rewrite it.
247
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
248                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
249                 ModifiersTree mods = method.getModifiers();
250                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet()));
251             }
252             
253             public void cancel() {
254             }
255         };
256         testSource.runModificationTask(task).commit();
257         String JavaDoc res = TestUtilities.copyFileToString(testFile);
258         System.err.println(res);
259         assertEquals(golden, res);
260     }
261     
262     /**
263      * Original:
264      *
265      * Test() {
266      * }
267      *
268      * Result:
269      *
270      * public Test() {
271      * }
272      */

273     public void testMethodMods3() throws Exception JavaDoc {
274         testFile = new File JavaDoc(getWorkDir(), "Test.java");
275         TestUtilities.copyStringToFile(testFile,
276                 "package hierbas.del.litoral;\n\n" +
277                 "import java.io.*;\n\n" +
278                 "public class Test {\n" +
279                 " Test() {\n" +
280                 " }\n" +
281                 "}\n"
282                 );
283         String JavaDoc golden =
284                 "package hierbas.del.litoral;\n\n" +
285                 "import java.io.*;\n\n" +
286                 "public class Test {\n" +
287                 " public Test() {\n" +
288                 " }\n" +
289                 "}\n";
290         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
291         CancellableTask task = new CancellableTask<WorkingCopy>() {
292             
293             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
294                 workingCopy.toPhase(Phase.RESOLVED);
295                 TreeMaker make = workingCopy.getTreeMaker();
296                 
297                 // finally, find the correct body and rewrite it.
298
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
299                 MethodTree method = (MethodTree) clazz.getMembers().get(0);
300                 ModifiersTree mods = method.getModifiers();
301                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)));
302             }
303             
304             public void cancel() {
305             }
306         };
307         testSource.runModificationTask(task).commit();
308         String JavaDoc res = TestUtilities.copyFileToString(testFile);
309         System.err.println(res);
310         assertEquals(golden, res);
311     }
312     
313     /**
314      * Original:
315      *
316      * public Test() {
317      * }
318      *
319      * Result:
320      *
321      * Test() {
322      * }
323      */

324     public void testMethodMods4() throws Exception JavaDoc {
325         testFile = new File JavaDoc(getWorkDir(), "Test.java");
326         TestUtilities.copyStringToFile(testFile,
327                 "package hierbas.del.litoral;\n\n" +
328                 "import java.io.*;\n\n" +
329                 "public class Test {\n" +
330                 " public Test() {\n" +
331                 " }\n" +
332                 "}\n"
333                 );
334         String JavaDoc golden =
335                 "package hierbas.del.litoral;\n\n" +
336                 "import java.io.*;\n\n" +
337                 "public class Test {\n" +
338                 " Test() {\n" +
339                 " }\n" +
340                 "}\n";
341         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
342         CancellableTask task = new CancellableTask<WorkingCopy>() {
343             
344             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
345                 workingCopy.toPhase(Phase.RESOLVED);
346                 TreeMaker make = workingCopy.getTreeMaker();
347                 
348                 // finally, find the correct body and rewrite it.
349
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
350                 MethodTree method = (MethodTree) clazz.getMembers().get(0);
351                 ModifiersTree mods = method.getModifiers();
352                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet()));
353             }
354             
355             public void cancel() {
356             }
357         };
358         testSource.runModificationTask(task).commit();
359         String JavaDoc res = TestUtilities.copyFileToString(testFile);
360         System.err.println(res);
361         assertEquals(golden, res);
362     }
363     /**
364      * Original:
365      *
366      * public static void method() {
367      * }
368      *
369      * Result:
370      *
371      * static void method() {
372      * }
373      */

374     
375     public void testMethodMods5() throws Exception JavaDoc {
376         testFile = new File JavaDoc(getWorkDir(), "Test.java");
377         TestUtilities.copyStringToFile(testFile,
378                 "package hierbas.del.litoral;\n\n" +
379                 "import java.io.*;\n\n" +
380                 "public class Test {\n" +
381                 " public static void method() {\n" +
382                 " }\n" +
383                 "}\n"
384                 );
385         String JavaDoc golden =
386                 "package hierbas.del.litoral;\n\n" +
387                 "import java.io.*;\n\n" +
388                 "public class Test {\n" +
389                 " static void method() {\n" +
390                 " }\n" +
391                 "}\n";
392         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
393         CancellableTask task = new CancellableTask<WorkingCopy>() {
394             
395             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
396                 workingCopy.toPhase(Phase.RESOLVED);
397                 TreeMaker make = workingCopy.getTreeMaker();
398                 
399                 // finally, find the correct body and rewrite it.
400
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
401                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
402                 ModifiersTree mods = method.getModifiers();
403                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.STATIC)));
404             }
405             
406             public void cancel() {
407             }
408         };
409         testSource.runModificationTask(task).commit();
410         String JavaDoc res = TestUtilities.copyFileToString(testFile);
411         System.err.println(res);
412         assertEquals(golden, res);
413     }
414     
415     /**
416      * Original:
417      *
418      * public Test() {
419      * }
420      *
421      * Result:
422      *
423      * protected Test() {
424      * }
425      */

426     public void testMethodMods6() throws Exception JavaDoc {
427         testFile = new File JavaDoc(getWorkDir(), "Test.java");
428         TestUtilities.copyStringToFile(testFile,
429                 "package hierbas.del.litoral;\n\n" +
430                 "import java.io.*;\n\n" +
431                 "public class Test {\n" +
432                 " public Test() {\n" +
433                 " }\n" +
434                 "}\n"
435                 );
436         String JavaDoc golden =
437                 "package hierbas.del.litoral;\n\n" +
438                 "import java.io.*;\n\n" +
439                 "public class Test {\n" +
440                 " protected Test() {\n" +
441                 " }\n" +
442                 "}\n";
443         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
444         CancellableTask task = new CancellableTask<WorkingCopy>() {
445             
446             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
447                 workingCopy.toPhase(Phase.RESOLVED);
448                 TreeMaker make = workingCopy.getTreeMaker();
449                 
450                 // finally, find the correct body and rewrite it.
451
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
452                 MethodTree method = (MethodTree) clazz.getMembers().get(0);
453                 ModifiersTree mods = method.getModifiers();
454                 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PROTECTED)));
455             }
456             
457             public void cancel() {
458             }
459         };
460         testSource.runModificationTask(task).commit();
461         String JavaDoc res = TestUtilities.copyFileToString(testFile);
462         System.err.println(res);
463         assertEquals(golden, res);
464     }
465     
466     /**
467      * Original:
468      *
469      * @Anotace()
470      * public Test() {
471      * }
472      *
473      * Result:
474      *
475      * @Annotaition()
476      * protected Test() {
477      * }
478      */

479     public void testAnnRename() throws Exception JavaDoc {
480         testFile = new File JavaDoc(getWorkDir(), "Test.java");
481         TestUtilities.copyStringToFile(testFile,
482                 "package hierbas.del.litoral;\n\n" +
483                 "import java.io.*;\n\n" +
484                 "@Annotace()\n" +
485                 "public class Test {\n" +
486                 " public Test() {\n" +
487                 " }\n" +
488                 "}\n"
489                 );
490         String JavaDoc golden =
491                 "package hierbas.del.litoral;\n\n" +
492                 "import java.io.*;\n\n" +
493                 "@Annotation()\n" +
494                 "public class Test {\n" +
495                 " public Test() {\n" +
496                 " }\n" +
497                 "}\n";
498         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
499         CancellableTask task = new CancellableTask<WorkingCopy>() {
500             
501             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
502                 workingCopy.toPhase(Phase.RESOLVED);
503                 TreeMaker make = workingCopy.getTreeMaker();
504                 
505                 // finally, find the correct body and rewrite it.
506
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
507                 ModifiersTree mods = clazz.getModifiers();
508                 AnnotationTree ann = mods.getAnnotations().get(0);
509                 IdentifierTree ident = (IdentifierTree) ann.getAnnotationType();
510                 workingCopy.rewrite(ident, make.Identifier("Annotation"));
511             }
512             
513             public void cancel() {
514             }
515         };
516         testSource.runModificationTask(task).commit();
517         String JavaDoc res = TestUtilities.copyFileToString(testFile);
518         System.err.println(res);
519         assertEquals(golden, res);
520     }
521     
522     String JavaDoc getGoldenPckg() {
523         return "";
524     }
525     
526     String JavaDoc getSourcePckg() {
527         return "";
528     }
529 }
530
Popular Tags