KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > MethodTest1


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.jmi.javamodel.codegen.MethodTest1;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.JavaClass;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.jmi.javamodel.Method;
28 import org.netbeans.jmi.javamodel.MultipartId;
29 import org.netbeans.jmi.javamodel.TypeParameter;
30 import org.netbeans.jmi.javamodel.codegen.Utility;
31 import org.netbeans.junit.NbTestSuite;
32 import junit.textui.TestRunner;
33 import org.netbeans.jmi.javamodel.Parameter;
34 import org.netbeans.jmi.javamodel.Type;
35 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase;
36 import org.netbeans.jmi.javamodel.codegen.Utility;
37
38 /**
39  * Tests the method generator.
40  *
41  * @author Pavel Flaska
42  */

43 public class MethodTest1 extends CodegenTestCase {
44     
45     /** Need to be defined because of JUnit */
46     public MethodTest1(String JavaDoc name) {
47         super(name, "MethodTest1");
48     }
49     
50     public static NbTestSuite suite() {
51         NbTestSuite suite = new NbTestSuite();
52         suite.addTest(new MethodTest1("testMethodModifiers"));
53         suite.addTest(new MethodTest1("testMethodName"));
54         suite.addTest(new MethodTest1("testMethodParameters"));
55         suite.addTest(new MethodTest1("testMethodParameterChange"));
56         suite.addTest(new MethodTest1("testMethodThrows"));
57         suite.addTest(new MethodTest1("testMethodReturnType"));
58         suite.addTest(new MethodTest1("testMethodBody"));
59         suite.addTest(new MethodTest1("testParameterizedMethod"));
60         suite.addTest(new MethodTest1("testAddRemoveInOneTrans"));
61         return suite;
62     }
63     
64     JavaClass clazz;
65     JavaModelPackage pkg;
66     Method[] method = new Method[10];
67     
68     protected void setUp() {
69         clazz = Utility.findClass("org.netbeans.test.codegen.MethodTest1");
70         pkg = (JavaModelPackage) clazz.refImmediatePackage();
71         // initialize methods
72
int i = 0;
73         for (Iterator JavaDoc mIt = clazz.getFeatures().iterator(); mIt.hasNext(); i++) {
74             Object JavaDoc temp = mIt.next();
75             if (temp instanceof JavaClass) {
76                 method[i] = (Method) ((JavaClass) temp).getFeatures().get(0);
77             } else {
78                 method[i] = (Method) temp;
79             }
80         }
81     }
82
83     /**
84      * Changes the modifiers on method. Removes public modifier, sets static
85      * and private modifier.
86      */

87     public void testMethodModifiers() {
88         boolean fail = true;
89         Utility.beginTrans(true);
90         try {
91             int modifiers = method[0].getModifiers();
92             modifiers &= ~Modifier.PUBLIC;
93             modifiers |= Modifier.STATIC;
94             modifiers |= Modifier.PRIVATE;
95             method[0].setModifiers(modifiers);
96             fail = false;
97         }
98         finally {
99             Utility.endTrans(fail);
100         }
101         makeDiff("testMethodModifiers");
102     }
103
104     /**
105      * Changes the name on the method.
106      */

107     public void testMethodName() {
108         boolean fail = true;
109         Utility.beginTrans(true);
110         try {
111             method[1].setName("druhaMetoda");
112             fail = false;
113         }
114         finally {
115             Utility.endTrans(fail);
116         }
117         makeDiff("testMethodName");
118     }
119     
120     /**
121      * Removes the first parameter and adds it to the end
122      */

123     public void testMethodParameters() {
124         boolean fail = true;
125         Utility.beginTrans(true);
126         try {
127             List JavaDoc parameters = method[2].getParameters();
128             Parameter par = (Parameter) parameters.remove(0);
129             parameters.add(par);
130             fail = false;
131         }
132         finally {
133             Utility.endTrans(fail);
134         }
135         makeDiff("testMethodParameters");
136     }
137     
138     /**
139      * Changes the name of the parameter.
140      */

141     public void testMethodParameterChange() {
142         boolean fail = true;
143         Utility.beginTrans(true);
144         try {
145             List JavaDoc parameters = method[3].getParameters();
146             Parameter par = (Parameter) parameters.get(0);
147             par.setName("aParNewName");
148             fail = false;
149         }
150         finally {
151             Utility.endTrans(fail);
152         }
153         makeDiff("testMethodParameterChange");
154     }
155
156     /**
157      * Removes first exception thrown, adds another one to the end.
158      */

159     public void testMethodThrows() {
160         boolean fail = true;
161         Utility.beginTrans(true);
162         try {
163             List JavaDoc exceptions = method[4].getExceptionNames();
164             exceptions.remove(0);
165             exceptions.add(pkg.getMultipartId().createMultipartId("java.lang.IllegalMonitorStateException", null, null));
166             fail = false;
167         }
168         finally {
169             Utility.endTrans(fail);
170         }
171         makeDiff("testMethodThrows");
172     }
173     
174     /**
175      * Changes return type to String.
176      */

177     public void testMethodReturnType() {
178         boolean fail = true;
179         Utility.beginTrans(true);
180         try {
181             method[5].setTypeName(pkg.getMultipartId().createMultipartId("String", null, null));
182             fail = false;
183         }
184         finally {
185             Utility.endTrans(fail);
186         }
187         makeDiff("testMethodReturnType");
188     }
189
190     /**
191      * Tests method body.
192      */

193     public void testMethodBody() {
194         boolean fail = true;
195         Utility.beginTrans(true);
196         try {
197             method[6].setModifiers(Modifier.PUBLIC);
198             method[6].setBody(pkg.getStatementBlock().createStatementBlock());
199             method[7].setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
200             method[7].setBody(null);
201             method[8].getBody();
202             method[8].getExceptionNames().add(pkg.getMultipartId().createMultipartId("java.io.IOException", null, null));
203             fail = false;
204         }
205         finally {
206             Utility.endTrans(fail);
207         }
208         makeDiff("testMethodBody");
209     }
210     
211     public void testParameterizedMethod() {
212         boolean fail = true;
213         Utility.beginTrans(true);
214         try {
215             TypeParameter typePar = pkg.getTypeParameter().createTypeParameter();
216             typePar.setName("T");
217             // create parameter "T cl"
218
Parameter param = pkg.getParameter().createParameter();
219             param.setName("cl");
220             param.setType(typePar);
221             // create the method
222

223             Method method = pkg.getMethod().createMethod(
224                 "getIt",
225                 Collections.EMPTY_LIST,
226                 Modifier.PUBLIC,
227                 "new method",
228                 null, // jvadoc
229
null, // object body
230
"return null;", // string body
231
Collections.singletonList(typePar), // type params
232
Collections.singletonList(param), // parameters
233
null, // exceptions
234
null, // type
235
0);
236             method.setType(typePar);
237             fail = false;
238             clazz.getContents().add(method);
239         }
240         finally {
241             Utility.endTrans(fail);
242         }
243         makeDiff("testParameterizedMethod");
244     }
245     
246     public void testAddRemoveInOneTrans() {
247         boolean fail = true;
248         Utility.beginTrans(true);
249         try {
250             TypeParameter typePar = pkg.getTypeParameter().createTypeParameter();
251             typePar.setName("T2");
252             // create parameter "T newPar"
253
Parameter param = pkg.getParameter().createParameter();
254             param.setName("newPar");
255             param.setType(typePar);
256             // create the method
257

258             Method method = pkg.getMethod().createMethod(
259                 "setIt",
260                 Collections.EMPTY_LIST,
261                 Modifier.PUBLIC,
262                 "new javadoc",
263                 null, // jvadoc
264
null, // object body
265
"return null;", // string body
266
Collections.singletonList(typePar), // type params
267
Collections.singletonList(param), // parameters
268
null, // exceptions
269
null, // type
270
0);
271             method.setType(typePar);
272             fail = false;
273             List JavaDoc/*<Feature>*/ features = clazz.getContents();
274             features.remove(features.size()-1);
275             features.add(method);
276         }
277         finally {
278             Utility.endTrans(fail);
279         }
280         makeDiff("testAddRemoveInOneTrans");
281     }
282
283     /**
284      * @param args the command line arguments
285      */

286     public static void main(String JavaDoc[] args) {
287         TestRunner.run(suite());
288     }
289 }
290
Popular Tags