KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassTree;
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.tree.Tree;
24 import com.sun.source.tree.VariableTree;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import org.netbeans.api.java.source.CancellableTask;
28 import org.netbeans.api.java.source.JavaSource;
29 import org.netbeans.api.java.source.JavaSource.*;
30 import org.netbeans.api.java.source.TestUtilities;
31 import org.netbeans.api.java.source.TreeMaker;
32 import org.netbeans.api.java.source.WorkingCopy;
33 import org.netbeans.junit.NbTestSuite;
34
35 /**
36  * Test enum modifications.
37  *
38  * @author Pavel Flaska
39  */

40 public class EnumTest extends GeneratorTest {
41     
42     /** Creates a new instance of EnumTest */
43     public EnumTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     public static NbTestSuite suite() {
48         NbTestSuite suite = new NbTestSuite();
49         suite.addTestSuite(EnumTest.class);
50         return suite;
51     }
52     
53     /**
54      * Test renames 'A' constant to 'A2' constant in code written below:
55      *
56      * <code>
57      * public enum Test {
58      * A, B, C;
59      *
60      * public void enumMethod() {
61      * }
62      * }
63      * </code>
64      */

65     public void testConstantRename() throws Exception JavaDoc {
66         testFile = new File JavaDoc(getWorkDir(), "Test.java");
67         TestUtilities.copyStringToFile(testFile,
68             "package hierbas.del.litoral;\n\n" +
69             "import java.util.*;\n\n" +
70             "public enum Test {\n" +
71             " A, B, C;\n" +
72             " \n" +
73             " public void enumMethod() {\n" +
74             " }\n" +
75             "}\n"
76             );
77         String JavaDoc golden =
78             "package hierbas.del.litoral;\n\n" +
79             "import java.util.*;\n\n" +
80             "public enum Test {\n" +
81             " A2, B, C;\n" +
82             " \n" +
83             " public void enumMethod() {\n" +
84             " }\n" +
85             "}\n";
86         JavaSource src = getJavaSource(testFile);
87         
88         CancellableTask task = new CancellableTask<WorkingCopy>() {
89
90             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
91                 workingCopy.toPhase(Phase.RESOLVED);
92                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
93                 TreeMaker make = workingCopy.getTreeMaker();
94
95                 for (Tree typeDecl : cut.getTypeDecls()) {
96                     // ensure that it is correct type declaration, i.e. class
97
if (Tree.Kind.CLASS == typeDecl.getKind()) {
98                         ClassTree clazz = (ClassTree) typeDecl;
99                         VariableTree vt = (VariableTree) clazz.getMembers().get(1);
100                         VariableTree copy = make.setLabel(vt, "A2");
101                         workingCopy.rewrite(vt, copy);
102                     }
103                 }
104             }
105
106             public void cancel() {
107             }
108         };
109         src.runModificationTask(task).commit();
110         String JavaDoc res = TestUtilities.copyFileToString(testFile);
111         System.err.println(res);
112         assertEquals(golden, res);
113     }
114     
115     String JavaDoc getGoldenPckg() {
116         return "";
117     }
118     
119     String JavaDoc getSourcePckg() {
120         return "";
121     }
122     
123     /**
124      * @param args the command line arguments
125      */

126     public static void main(String JavaDoc[] args) {
127         // TODO code application logic here
128
}
129
130 }
131
Popular Tags