KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.io.File JavaDoc;
23 import java.util.Collections JavaDoc;
24 import javax.lang.model.element.Modifier;
25 import javax.lang.model.type.TypeKind;
26 import org.netbeans.jackpot.test.TestUtilities;
27 import org.netbeans.api.java.source.transform.Transformer;
28 import org.netbeans.junit.NbTestSuite;
29
30 /**
31  *
32  * @author Pavel Flaska
33  */

34 public class FeatureAddingTest extends GeneratorTest {
35     
36     /** Creates a new instance of FeatureAddingTest */
37     public FeatureAddingTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     public static NbTestSuite suite() {
42         NbTestSuite suite = new NbTestSuite();
43         suite.addTest(new FeatureAddingTest("testAddFieldToBeginning"));
44         suite.addTest(new FeatureAddingTest("testAddFieldToEnd"));
45         return suite;
46     }
47     
48     public void testAddFieldToBeginning() throws Exception JavaDoc {
49         testFile = new File JavaDoc(getWorkDir(), "Test.java");
50         TestUtilities.copyStringToFile(testFile,
51             "package hierbas.del.litoral;\n\n" +
52             "import java.io.File;\n\n" +
53             "public class Test {\n" +
54             " \n" +
55             " /* comment */\n" +
56             " Test(int a, long c, String s) {\n" +
57             " }\n\n" +
58             " void method() {\n" +
59             " }\n\n" +
60             "}\n"
61             );
62         String JavaDoc golden =
63             "package hierbas.del.litoral;\n\n" +
64             "import java.io.File;\n\n" +
65             "public class Test {\n" +
66             " int a;\n" +
67             " \n" +
68             " /* comment */\n" +
69             " Test(int a, long c, String s) {\n" +
70             " }\n\n" +
71             " void method() {\n" +
72             " }\n\n" +
73             "}\n";
74
75         process(
76             new Transformer<Void JavaDoc, Object JavaDoc>() {
77                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
78                     super.visitClass(node, p);
79                     if ("Test".contentEquals(node.getSimpleName())) {
80                         VariableTree member = make.Variable(
81                             make.Modifiers(
82                                 Collections.<Modifier>emptySet(),
83                                 Collections.<AnnotationTree>emptyList()
84                             ),
85                             "a",
86                             make.PrimitiveType(TypeKind.INT), null
87                         );
88                         ClassTree copy = make.insertClassMember(node, 0, member);
89                         changes.rewrite(node, copy);
90                     }
91                     return null;
92                 }
93             }
94         );
95         String JavaDoc res = TestUtilities.copyFileToString(testFile);
96         assertEquals(golden, res);
97     }
98     
99     public void testAddFieldToEnd() throws Exception JavaDoc {
100         testFile = new File JavaDoc(getWorkDir(), "Test.java");
101         TestUtilities.copyStringToFile(testFile,
102             "package hierbas.del.litoral;\n\n" +
103             "import java.io.File;\n\n" +
104             "public class Test {\n" +
105             " \n" +
106             " /* comment */\n" +
107             " Test(int a, long c, String s) {\n" +
108             " }\n\n" +
109             " void method() {\n" +
110             " }\n" +
111             " \n" +
112             "}\n"
113             );
114         String JavaDoc golden =
115             "package hierbas.del.litoral;\n\n" +
116             "import java.io.File;\n\n" +
117             "public class Test {\n" +
118             " \n" +
119             " /* comment */\n" +
120             " Test(int a, long c, String s) {\n" +
121             " }\n\n" +
122             " void method() {\n" +
123             " }\n" +
124             " int a;\n" +
125             " \n" +
126             "}\n";
127
128         process(
129             new Transformer<Void JavaDoc, Object JavaDoc>() {
130                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
131                     super.visitClass(node, p);
132                     if ("Test".contentEquals(node.getSimpleName())) {
133                         VariableTree member = make.Variable(
134                             make.Modifiers(
135                                 Collections.<Modifier>emptySet(),
136                                 Collections.<AnnotationTree>emptyList()
137                             ),
138                             "a",
139                             make.PrimitiveType(TypeKind.INT), null
140                         );
141                         ClassTree copy = make.addClassMember(node, member);
142                         changes.rewrite(node, copy);
143                     }
144                     return null;
145                 }
146             }
147         );
148         String JavaDoc res = TestUtilities.copyFileToString(testFile);
149         assertEquals(golden, res);
150     }
151     
152     String JavaDoc getGoldenPckg() {
153         return "";
154     }
155
156     String JavaDoc getSourcePckg() {
157         return "";
158     }
159 }
160
Popular Tags