KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.api.java.source.gen;
21
22 import com.sun.source.tree.*;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.EnumSet JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.lang.model.element.Modifier;
29 import javax.lang.model.type.TypeKind;
30 import junit.textui.TestRunner;
31 import org.netbeans.jackpot.test.TestUtilities;
32 import org.netbeans.api.java.source.transform.Transformer;
33 import org.netbeans.junit.NbTestSuite;
34
35 /**
36  * Tests indentation of newly added elements
37  * @author Max Sauer
38  */

39 public class IndentAddedElemTest extends GeneratorTest {
40     
41     public IndentAddedElemTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     /**
46      * Adds tests to suite
47      * @return created suite
48      */

49     public static NbTestSuite suite() {
50         //NbTestSuite suite = new NbTestSuite();
51
NbTestSuite suite = new NbTestSuite(IndentAddedElemTest.class);
52         //suite.addTest(new IndentMethodTest("testAddMethodToEmpty"));
53
return suite;
54     }
55     
56     /**
57      * Adding of methods
58      */

59     public void testAddMethodToEmpty() throws Exception JavaDoc {
60         testFile = new File JavaDoc(getWorkDir(), "Test.java");
61         TestUtilities.copyStringToFile(testFile,
62                 "package com.max.test.alfa;\n\n" +
63                 "public class Test {\n" +
64                 "}\n"
65                 );
66         String JavaDoc golden =
67                 "package com.max.test.alfa;\n\n" +
68                 "public class Test {\n" +
69                 "\n" +
70                 " public double Eval(int param) {\n" +
71                 " }\n" +
72                 "}\n";
73         
74         process(
75                 new Transformer<Void JavaDoc, Object JavaDoc>() {
76             public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
77                 super.visitClass(node, p);
78                 if ("Test".contentEquals(node.getSimpleName())) {
79                     ModifiersTree parMods = make.Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList());
80                     VariableTree param = make.Variable(parMods, "param", make.PrimitiveType(TypeKind.INT), null);
81                     List JavaDoc<VariableTree> parList = new ArrayList JavaDoc<VariableTree>(1);
82                     parList.add(param);
83                     MethodTree member = make.Method(
84                             make.Modifiers(
85                             Collections.singleton(Modifier.PUBLIC), // modifiers
86
Collections.EMPTY_LIST // annotations
87
), // modifiers and annotations
88
"Eval", // name
89
make.PrimitiveType(TypeKind.DOUBLE), // return type
90
Collections.EMPTY_LIST, // type parameters for parameters
91
parList, // parameters
92
Collections.EMPTY_LIST, // throws
93
make.Block(Collections.EMPTY_LIST, false), // empty statement block
94
null // default value - not applicable here, used by annotations
95
);
96                     
97                     ClassTree copy = make.addClassMember(node, member);
98                     changes.rewrite(node, copy);
99                 }
100                 return null;
101             }
102         }
103         );
104         String JavaDoc res = TestUtilities.copyFileToString(testFile);
105         assertEquals(golden, res);
106     }
107     
108     /**
109      * Adding of fields
110      */

111     public void testAddFieldEmpty() throws Exception JavaDoc {
112         testFile = new File JavaDoc(getWorkDir(), "Test.java");
113         TestUtilities.copyStringToFile(testFile,
114                 "package com.max.test.alfa;\n\n" +
115                 "public class Test {\n" +
116                 "}\n"
117                 );
118         String JavaDoc golden =
119                 "package com.max.test.alfa;\n\n" +
120                 "public class Test {\n" +
121                 " private double value;\n" +
122                 "}\n";
123         
124         process(
125                 new Transformer<Void JavaDoc, Object JavaDoc>() {
126             public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
127                 super.visitClass(node, p);
128                 if ("Test".contentEquals(node.getSimpleName())) {
129                     ModifiersTree modTree = make.Modifiers(EnumSet.of(Modifier.PRIVATE));
130                     VariableTree member = make.Variable(
131                             make.Modifiers(
132                                 modTree,
133                                 Collections.<AnnotationTree>emptyList()
134                             ),
135                             "value", //name
136
make.PrimitiveType(TypeKind.DOUBLE), null
137                             );
138                     
139                     ClassTree copy = make.addClassMember(node, member);
140                     changes.rewrite(node, copy);
141                 }
142                 return null;
143             }
144         }
145         );
146         String JavaDoc res = TestUtilities.copyFileToString(testFile);
147         assertEquals(golden, res);
148     }
149     
150     String JavaDoc getGoldenPckg() {
151         return "";
152     }
153     
154     String JavaDoc getSourcePckg() {
155         return "";
156     }
157     
158     /**
159      * @param args the command line arguments
160      */

161     public static void main(String JavaDoc[] args) {
162         TestRunner.run(suite());
163     }
164     
165 }
166
Popular Tags