KickJava   Java API By Example, From Geeks To Geeks.

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


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.jmi.javamodel.codegen;
21
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.io.IOException JavaDoc;
26 import org.openide.filesystems.FileStateInvalidException;
27 import junit.textui.TestRunner;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.junit.NbTestSuite;
30 import org.netbeans.jmi.javamodel.JavaModelPackage;
31 import org.netbeans.jmi.javamodel.Method;
32 import org.netbeans.jmi.javamodel.Type;
33 import org.netbeans.jmi.javamodel.Import;
34 import org.netbeans.jmi.javamodel.Parameter;
35 import org.netbeans.jmi.javamodel.Resource;
36 import org.netbeans.jmi.javamodel.JavaClass;
37 import org.netbeans.jmi.javamodel.MultipartId;
38 import org.netbeans.jmi.javamodel.TypeReference;
39
40 /**
41  *
42  * @author Pavel Flaska
43  */

44 public class ParametersTest extends NbTestCase {
45     
46     /** Creates a new instance of ParametersTest */
47     public ParametersTest() {
48         super("ParametersTest");
49     }
50     
51     public static NbTestSuite suite() {
52         NbTestSuite suite = new NbTestSuite(ParametersTest.class);
53         return suite;
54     }
55     
56     private JavaModelPackage pkg;
57     private JavaClass clazz;
58     
59     protected void setUp() {
60         clazz = Utility.findClass("org.netbeans.test.codegen.ParametersTestClass");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62     }
63
64     /**
65      * Tests creation of method with varArg parameter.
66      */

67     public void testCreation() throws IOException JavaDoc, FileStateInvalidException {
68         boolean fail = true;
69         Utility.beginTrans(true);
70         try {
71             // private boolean tretiMethod(String... varString) {
72
// return true;
73
// }
74
MultipartId typeName = pkg.getMultipartId().createMultipartId("boolean", null, null);
75             MultipartId parType = pkg.getMultipartId().createMultipartId("long", null, null);
76             Parameter par = pkg.getParameter().createParameter(
77                 "varString", // name
78
null, // annotations
79
false, // is final?
80
parType, // parameter's type
81
0, // dimCount
82
true // is variable arguments?
83
);
84             Method newMethod = pkg.getMethod().createMethod(
85                 "tretiMethod", // method name
86
null, // annotations
87
Modifier.PRIVATE, // modifiers
88
null, // javadocText
89
null, // javadoc
90
null, // body
91
"return true;", // bodyText
92
null,
93                 Collections.singletonList(par), // parameters
94
Collections.EMPTY_LIST, // exception names
95
typeName, // return type
96
0 // dimCount
97
);
98             Type type = pkg.getType().resolve("String");
99             par.setType(type);
100             clazz.getFeatures().add(newMethod);
101             fail = false;
102         } finally {
103             Utility.endTrans(fail);
104         }
105         assertFile("File is not correctly generated.",
106             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"),
107             getGoldenFile("testCreation_ParametersTest.pass"),
108             getWorkDir()
109         );
110     }
111     
112     public void testSwapTypeChng() throws IOException JavaDoc, FileStateInvalidException {
113         boolean fail = true;
114         Utility.beginTrans(true);
115         try {
116             Method method = (Method) clazz.getContents().get(2);
117             List JavaDoc pars = method.getParameters();
118             Parameter par0 = (Parameter) pars.remove(0);
119             Type type = pkg.getType().resolve("long");
120             MultipartId typeName = pkg.getMultipartId().createMultipartId("short", null, null);
121             par0.setType(type);
122             par0.setTypeName(typeName);
123             pars.add(par0);
124             fail = false;
125         } finally {
126             Utility.endTrans(fail);
127         }
128         assertFile("File is not correctly generated.",
129             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"),
130             getGoldenFile("testSwapTypeChng_ParametersTest.pass"),
131             getWorkDir()
132         );
133     }
134     
135     public void testAddChng() throws IOException JavaDoc, FileStateInvalidException {
136         boolean fail = true;
137         Utility.beginTrans(true);
138         try {
139             Method method = (Method) clazz.getContents().get(1);
140             List JavaDoc pars = method.getParameters();
141             MultipartId typeName = pkg.getMultipartId().createMultipartId("boolean", null, null);
142             MultipartId parType = pkg.getMultipartId().createMultipartId("long", null, null);
143             Parameter par = (Parameter) pkg.getParameter().createParameter(
144                 "novyParameter", // name
145
null, // annotations
146
true, // is final?
147
parType, // parameter's type
148
1, // dimCount
149
false // is variable arguments?
150
);
151             Type type = pkg.getType().resolve("long[][]");
152             par.setTypeName(typeName);
153             par.setType(type);
154             pars.add(par);
155             fail = false;
156         } finally {
157             Utility.endTrans(fail);
158         }
159         assertFile("File is not correctly generated.",
160             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"),
161             getGoldenFile("testAddChng_ParametersTest.pass"),
162             getWorkDir()
163         );
164     }
165     
166     /**
167      * @param args the command line arguments
168      */

169     public static void main(String JavaDoc[] args) {
170         TestRunner.run(suite());
171     }
172 }
173
Popular Tags