KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodTree;
23 import com.sun.source.tree.ModifiersTree;
24 import com.sun.source.tree.PrimitiveTypeTree;
25 import com.sun.source.tree.VariableTree;
26 import java.io.File JavaDoc;
27 import java.util.Collections JavaDoc;
28 import javax.lang.model.element.Modifier;
29 import javax.lang.model.type.TypeKind;
30 import org.netbeans.api.java.source.TestUtilities;
31 import org.netbeans.api.java.source.transform.Transformer;
32 import org.netbeans.junit.NbTestSuite;
33
34 /**
35  * Tests correct fields addition and matching mechanism.
36  *
37  * @author Pavel Flaska
38  */

39 public class Field6Test extends GeneratorTest {
40     
41     /** Creates a new instance of MethodParametersTest */
42     public Field6Test(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite();
48 // suite.addTestSuite(Field6Test.class);
49
suite.addTest(new Field6Test("testAddFieldToIndex0"));
50         suite.addTest(new Field6Test("testRemoveInitialValue"));
51         suite.addTest(new Field6Test("testAddFirstParameter"));
52         return suite;
53     }
54     
55     public void testAddFieldToIndex0() throws Exception JavaDoc {
56         testFile = new File JavaDoc(getWorkDir(), "Test.java");
57         TestUtilities.copyStringToFile(testFile,
58             "package hierbas.del.litoral;\n\n" +
59             "import java.io.*;\n\n" +
60             "public class Test {\n" +
61             " public void taragui() {\n" +
62             " }\n" +
63             "}\n"
64             );
65         String JavaDoc golden =
66             "package hierbas.del.litoral;\n\n" +
67             "import java.io.*;\n\n" +
68             "public class Test {\n" +
69             " int field1;\n" +
70             " public void taragui() {\n" +
71             " }\n" +
72             "}\n";
73
74         process(
75             new Transformer<Void JavaDoc, Object JavaDoc>() {
76             
77                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
78                     super.visitClass(node, p);
79                     ModifiersTree mods = make.Modifiers(Collections.<Modifier>emptySet());
80                     PrimitiveTypeTree type = make.PrimitiveType(TypeKind.INT);
81                     VariableTree var = make.Variable(mods, "field1", type, null);
82                     ClassTree copy = make.addClassMember(node, var);
83                     changes.rewrite(node, copy);
84                     return null;
85                 }
86             }
87         );
88         String JavaDoc res = TestUtilities.copyFileToString(testFile);
89         assertEquals(golden, res);
90     }
91     
92     public void testRemoveInitialValue() throws Exception JavaDoc {
93         testFile = new File JavaDoc(getWorkDir(), "Test.java");
94         TestUtilities.copyStringToFile(testFile,
95             "package hierbas.del.litoral;\n\n" +
96             "import java.io.*;\n\n" +
97             "public class Test {\n" +
98             " int removeInitialValueField = null;\n" +
99             "}\n"
100             );
101         String JavaDoc golden =
102             "package hierbas.del.litoral;\n\n" +
103             "import java.io.*;\n\n" +
104             "public class Test {\n" +
105             " int removeInitialValueField;\n" +
106             "}\n";
107
108         process(
109             new Transformer<Void JavaDoc, Object JavaDoc>() {
110                 public Void JavaDoc visitVariable(VariableTree node, Object JavaDoc p) {
111                     super.visitVariable(node, p);
112                     if ("removeInitialValueField".contentEquals(node.getName())) {
113                         VariableTree vt = make.Variable(
114                                 node.getModifiers(),
115                                 node.getName(),
116                                 node.getType(),
117                                 null);
118                         changes.rewrite(node, vt);
119                     }
120                     return null;
121                 }
122             }
123         );
124         String JavaDoc res = TestUtilities.copyFileToString(testFile);
125         System.err.println(res);
126         assertEquals(golden, res);
127     }
128     
129     public void testAddFirstParameter() throws Exception JavaDoc {
130         testFile = new File JavaDoc(getWorkDir(), "Test.java");
131         TestUtilities.copyStringToFile(testFile,
132             "package hierbas.del.litoral;\n\n" +
133             "import java.io.*;\n\n" +
134             "public class Test {\n" +
135             " public void taragui() {\n" +
136             " }\n" +
137             "}\n"
138             );
139         String JavaDoc golden =
140             "package hierbas.del.litoral;\n\n" +
141             "import java.io.*;\n\n" +
142             "public class Test {\n" +
143             " public void taragui(int equilibrio) {\n" +
144             " }\n" +
145             "}\n";
146
147         process(
148             new Transformer<Void JavaDoc, Object JavaDoc>() {
149                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
150                     super.visitMethod(node, p);
151                     if ("taragui".contentEquals(node.getName())) {
152                         VariableTree vt = make.Variable(
153                                 make.Modifiers(Collections.<Modifier>emptySet()),
154                                 "equilibrio",
155                                 make.PrimitiveType(TypeKind.INT),
156                                 null);
157                         MethodTree copy = make.addMethodParameter(node, vt);
158                         changes.rewrite(node, copy);
159                     }
160                     return null;
161                 }
162             }
163         );
164         String JavaDoc res = TestUtilities.copyFileToString(testFile);
165         System.err.println(res);
166         assertEquals(golden, res);
167     }
168     
169     protected void setUp() throws Exception JavaDoc {
170         super.setUp();
171         testFile = getFile(getSourceDir(), getSourcePckg() + "Test.java");
172     }
173
174     String JavaDoc getGoldenPckg() {
175         return "";
176     }
177
178     String JavaDoc getSourcePckg() {
179         return "";
180     }
181     
182 }
183
Popular Tags