KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodTree;
24 import com.sun.source.tree.ModifiersTree;
25 import com.sun.source.tree.StatementTree;
26 import com.sun.source.tree.TypeParameterTree;
27 import java.io.File JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.EnumSet JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.lang.model.element.Modifier;
33 import javax.lang.model.type.TypeKind;
34 import org.netbeans.api.java.source.TestUtilities;
35 import org.netbeans.api.java.source.transform.Transformer;
36 import org.netbeans.junit.NbTestSuite;
37
38 /**
39  *
40  * @author Pavel Flaska
41  */

42 public class MethodCreationTest extends GeneratorTestMDRCompat {
43     
44     /** Creates a new instance of MethodCreationTest */
45     public MethodCreationTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static NbTestSuite suite() {
50         NbTestSuite suite = new NbTestSuite();
51 // suite.addTestSuite(MethodCreationTest.class);
52
suite.addTest(new MethodCreationTest("testAddFirst"));
53 // suite.addTest(new MethodCreationTest(""));
54
// suite.addTest(new MethodCreationTest(""));
55
// suite.addTest(new MethodCreationTest(""));
56
// suite.addTest(new MethodCreationTest(""));
57
// suite.addTest(new MethodCreationTest(""));
58
// suite.addTest(new MethodCreationTest(""));
59
// suite.addTest(new MethodCreationTest(""));
60
// suite.addTest(new MethodCreationTest(""));
61
// suite.addTest(new MethodCreationTest(""));
62
// suite.addTest(new MethodCreationTest(""));
63
// suite.addTest(new MethodCreationTest(""));
64
// suite.addTest(new MethodCreationTest(""));
65
return suite;
66     }
67
68     /*
69      * create the method:
70      *
71      * public <T> void taragui(List menta, Object carqueja, int dulce, boolean compuesta) throws IOException {
72      * }
73      */

74     public void testAddFirst() throws Exception JavaDoc {
75         testFile = new File JavaDoc(getWorkDir(), "Test.java");
76         TestUtilities.copyStringToFile(testFile,
77             "package hierbas.del.litoral;\n\n" +
78             "import java.util.*;\n\n" +
79             "public class Test {\n" +
80             "}\n"
81             );
82         String JavaDoc golden =
83             "package hierbas.del.litoral;\n\n" +
84             "import java.util.*;\n\n" +
85             "public class Test {\n\n" +
86             "public <T> void taragui(List menta, T carqueja, int dulce, boolean compuesta,\n" +
87             " boolean logrando) throws IOException {\n" +
88             "}\n" +
89             "}\n";
90
91         process(
92             new Transformer<Void JavaDoc, Object JavaDoc>() {
93             
94                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
95                     super.visitClass(node, p);
96                     if ("Test".contentEquals(node.getSimpleName())) {
97                         List JavaDoc parametersList = new ArrayList JavaDoc(5);
98                         ModifiersTree mods = make.Modifiers(EnumSet.noneOf(Modifier.class));
99                         parametersList.add(make.Variable(mods, "menta", make.Identifier("List"), null));
100                         parametersList.add(make.Variable(mods, "carqueja", make.Identifier("T"), null));
101                         parametersList.add(make.Variable(mods, "dulce", make.PrimitiveType(TypeKind.INT), null));
102                         parametersList.add(make.Variable(mods, "compuesta", make.PrimitiveType(TypeKind.BOOLEAN), null));
103                         parametersList.add(make.Variable(mods, "logrando", make.PrimitiveType(TypeKind.BOOLEAN), null));
104                         MethodTree newMethod = make.Method(
105                                 make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)), // modifiers - public
106
"taragui", // name - targui
107
make.PrimitiveType(TypeKind.VOID), // return type - void
108
Collections.<TypeParameterTree>singletonList(make.TypeParameter("T", Collections.<ExpressionTree>emptyList())), // type parameter - <T>
109
parametersList, // parameters
110
Collections.<ExpressionTree>singletonList(make.Identifier("IOException")), // throws
111
make.Block(Collections.<StatementTree>emptyList(), false),
112                                 null // default value - not applicable
113
);
114                         ClassTree copy = make.addClassMember(
115                             node, newMethod
116                         );
117                         changes.rewrite(node, copy);
118                     }
119                     return null;
120                 }
121             }
122         );
123         String JavaDoc res = TestUtilities.copyFileToString(testFile);
124         System.err.println(res);
125         assertEquals(golden, res);
126     }
127     
128     String JavaDoc getGoldenPckg() {
129         return "";
130     }
131     
132     String JavaDoc getSourcePckg() {
133         return "";
134     }
135    
136 }
137
Popular Tags