KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionTree;
23 import com.sun.source.tree.NewClassTree;
24 import com.sun.source.tree.PrimitiveTypeTree;
25 import com.sun.source.tree.Tree;
26 import com.sun.source.tree.VariableTree;
27 import java.io.IOException JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.lang.model.type.TypeKind;
31 import junit.textui.TestRunner;
32 import org.netbeans.api.java.source.*;
33 import org.netbeans.api.java.source.JavaSource.Phase;
34 import org.netbeans.api.java.source.transform.Transformer;
35 import org.netbeans.junit.NbTestSuite;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  * Test the field generator.
40  *
41  * @author Pavel Flaska
42  */

43 public class FieldTest1 extends GeneratorTestMDRCompat {
44     
45     public FieldTest1(String JavaDoc name) {
46         super(name);
47     }
48
49     public static NbTestSuite suite() {
50         NbTestSuite suite = new NbTestSuite();
51         suite.addTest(new FieldTest1("testFieldModifiers"));
52         suite.addTest(new FieldTest1("testFieldType"));
53         suite.addTest(new FieldTest1("testFieldName"));
54         suite.addTest(new FieldTest1("testFieldInitialValueText"));
55         suite.addTest(new FieldTest1("testFieldInitialValue"));
56         suite.addTest(new FieldTest1("testFieldChangeInInitValue"));
57         suite.addTest(new FieldTest1("testFieldInitialValueRemoval"));
58         return suite;
59     }
60     
61     protected void setUp() throws Exception JavaDoc {
62         super.setUp();
63         testFile = getFile(getSourceDir(), getSourcePckg() + "FieldTest1.java");
64     }
65
66     /**
67      * Removes all the modififers from the field.
68      */

69     public void testFieldModifiers() throws IOException JavaDoc {
70         System.err.println("testFieldModifiers");
71         process(
72             new Transformer<Void JavaDoc, Object JavaDoc>() {
73                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
74                     super.visitVariable(node, p);
75                     if ("modifiersField".contentEquals(node.getName())) {
76                         changes.rewrite(node.getModifiers(), make.Modifiers(Collections.EMPTY_SET));
77                     }
78                     return null;
79                 }
80             }
81         );
82         assertFiles("testFieldModifiers.pass");
83     }
84     
85     /**
86      * Changes long type of the field to short type.
87      */

88     public void testFieldType() throws IOException JavaDoc {
89         System.err.println("testFieldType");
90         process(
91             new Transformer<Void JavaDoc, Object JavaDoc>() {
92                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
93                     super.visitVariable(node, p);
94                     if ("typeField".contentEquals(node.getName())) {
95                         PrimitiveTypeTree pt = make.PrimitiveType(TypeKind.SHORT);
96                         VariableTree vt = make.Variable(
97                                 node.getModifiers(),
98                                 node.getName(),
99                                 pt,
100                                 node.getInitializer()
101                         );
102                         model.setElement(vt, model.getElement(node));
103                         model.setType(vt, model.getType(node));
104                         model.setPos(vt, model.getPos(node));
105                         //changes.rewrite(node.getType(), tree);
106
changes.rewrite(node, vt);
107                     }
108                     return null;
109                 }
110             }
111         );
112         assertFiles("testFieldType.pass");
113     }
114     
115     /**
116      * Changes field name to thisIsTheNewName.
117      */

118     public void testFieldName() throws IOException JavaDoc {
119         System.err.println("testFieldName");
120         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
121         CancellableTask task = new CancellableTask<WorkingCopy>() {
122
123             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
124                 workingCopy.toPhase(Phase.RESOLVED);
125                 TreeMaker make = workingCopy.getTreeMaker();
126                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
127                 VariableTree var = (VariableTree) clazz.getMembers().get(3);
128                 workingCopy.rewrite(var, make.setLabel(var, "thisIsTheNewName"));
129             }
130             
131             public void cancel() {
132             }
133         };
134         testSource.runModificationTask(task).commit();
135         String JavaDoc res = TestUtilities.copyFileToString(testFile);
136         System.err.println(res);
137         assertFiles("testFieldName.pass");
138     }
139     
140     /**
141      * Changes the initial value text.
142      */

143     public void testFieldInitialValueText() throws IOException JavaDoc {
144         System.err.println("testFieldInitialValueText");
145         process(
146             new Transformer<Void JavaDoc, Object JavaDoc>() {
147                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
148                     super.visitVariable(node, p);
149                     if ("initialValueTextTester".contentEquals(node.getName())) {
150                         changes.rewrite(node.getInitializer(), make.Literal("This is a new text."));
151                     }
152                     return null;
153                 }
154             }
155         );
156         assertFiles("testFieldInitialValueText.pass");
157     }
158     
159     /**
160      * Change whole initial value expression to literal 78.
161      */

162     public void testFieldInitialValue() throws IOException JavaDoc {
163         System.err.println("testFieldInitialValue");
164         process(
165             new Transformer<Void JavaDoc, Object JavaDoc>() {
166                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
167                     super.visitVariable(node, p);
168                     if ("initialValueChanger".contentEquals(node.getName())) {
169                         changes.rewrite(node.getInitializer(), make.Literal(Long.valueOf(78)));
170                     }
171                     return null;
172                 }
173             }
174         );
175         assertFiles("testFieldInitialValue.pass");
176     }
177     
178     /**
179      * Changes the initialization 'new String("NetBeers")' to 'new String()'.
180      * It tests only change in expression, no expression swap.
181      */

182     public void testFieldChangeInInitValue() throws IOException JavaDoc {
183         System.err.println("testFieldChangeInInitValue");
184         process(
185             new Transformer<Void JavaDoc, Object JavaDoc>() {
186                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
187                     super.visitVariable(node, p);
188                     if ("initialValueReplacer".contentEquals(node.getName())) {
189                         if (Tree.Kind.NEW_CLASS.equals(node.getInitializer().getKind())) {
190                             NewClassTree nct = (NewClassTree) node.getInitializer();
191                             NewClassTree njuNct = make.NewClass(
192                                     nct.getEnclosingExpression(),
193                                     (List JavaDoc<ExpressionTree>) nct.getTypeArguments(),
194                                     nct.getIdentifier(),
195                                     Collections.singletonList(make.Literal("NetBeans")),
196                                     nct.getClassBody()
197                             );
198                             changes.rewrite(nct, njuNct);
199                         }
200                     }
201                     return null;
202                 }
203             }
204         );
205         assertFiles("testFieldChangeInInitValue.pass");
206     }
207
208     /**
209      * Removes the inital value from the field.
210      */

211     public void testFieldInitialValueRemoval() throws IOException JavaDoc {
212         System.err.println("testFieldInitialValueRemoval");
213         process(
214             new Transformer<Void JavaDoc, Object JavaDoc>() {
215                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
216                     super.visitVariable(node, p);
217                     if ("removeInitialValueField".contentEquals(node.getName())) {
218                         VariableTree vt = make.Variable(
219                                 node.getModifiers(),
220                                 node.getName(),
221                                 node.getType(),
222                                 null);
223                         changes.rewrite(node, vt);
224                     }
225                     return null;
226                 }
227             }
228         );
229         assertFiles("testFieldInitialValueRemoval.pass");
230     }
231                     
232     /**
233      * @param args the command line arguments
234      */

235     public static void main(String JavaDoc[] args) {
236         TestRunner.run(suite());
237     }
238     
239     String JavaDoc getSourcePckg() {
240         return "org/netbeans/test/codegen/";
241     }
242
243     String JavaDoc getGoldenPckg() {
244         return "org/netbeans/jmi/javamodel/codegen/FieldTest1/FieldTest1/";
245     }
246 }
247
Popular Tags