KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class VarArgsTest extends NbTestCase {
47     
48     /** Creates a new instance of VarArgsTest */
49     public VarArgsTest() {
50         super("VarArgsTest");
51     }
52
53     public static NbTestSuite suite() {
54         NbTestSuite suite = new NbTestSuite(VarArgsTest.class);
55         return suite;
56     }
57     
58     private JavaModelPackage pkg;
59     private JavaClass clazz;
60     
61     protected void setUp() {
62         clazz = Utility.findClass("org.netbeans.test.codegen.VarArgsClass");
63         pkg = (JavaModelPackage) clazz.refImmediatePackage();
64     }
65     
66     public void testCreation() throws IOException JavaDoc, FileStateInvalidException {
67         boolean fail = true;
68         Utility.beginTrans(true);
69         try {
70             // private boolean newlyCreated(String... varString) {
71
// return true;
72
// }
73
MultipartId typeName = pkg.getMultipartId().createMultipartId("boolean", null, null);
74             MultipartId parType = pkg.getMultipartId().createMultipartId("String", null, null);
75             Parameter par = pkg.getParameter().createParameter(
76                 "varString", // name
77
null, // annotations
78
false, // is final?
79
parType, // parameter's type
80
0, // dimCount
81
true // is variable arguments?
82
);
83             Method newMethod = pkg.getMethod().createMethod(
84                 "newlyCreated", // method name
85
null, // annotations
86
Modifier.PRIVATE, // modifiers
87
null, // javadocText
88
null, // javadoc
89
null, // body
90
"return true;", // bodyText
91
null,
92                 Collections.singletonList(par), // parameters
93
Collections.EMPTY_LIST, // exception names
94
typeName, // return type
95
0 // dimCount
96
);
97             clazz.getFeatures().add(newMethod);
98             fail = false;
99         } finally {
100             Utility.endTrans(fail);
101         }
102         assertFile("File is not correctly generated.",
103             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/VarArgsClass.java"),
104             getGoldenFile("testCreation_VarArgsTest.pass"),
105             getWorkDir()
106         );
107     }
108     
109     public void testParAdd() throws IOException JavaDoc, FileStateInvalidException {
110         boolean fail = true;
111         Utility.beginTrans(true);
112         try {
113             // public void addVarPar(int c, Object... varObject) {
114
// }
115
MultipartId parType = pkg.getMultipartId().createMultipartId("Object", null, null);
116             Parameter par = pkg.getParameter().createParameter(
117                 "varObject", // name
118
null, // annotations
119
false, // is final?
120
parType, // parameter's type
121
0, // dimCount
122
true // is variable arguments?
123
);
124             Method method = (Method) clazz.getContents().get(0);
125             List JavaDoc pars = method.getParameters();
126             pars.add(par);
127             fail = false;
128         } finally {
129             Utility.endTrans(fail);
130         }
131         assertFile("File is not correctly generated.",
132             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/VarArgsClass.java"),
133             getGoldenFile("testParAdd_VarArgsTest.pass"),
134             getWorkDir()
135         );
136     }
137     
138     public void testParChange() throws IOException JavaDoc, FileStateInvalidException {
139         boolean fail = true;
140         Utility.beginTrans(true);
141         try {
142             // public int changeVarPar(long... varLong) {
143
// return 10;
144
// }
145
Type type = pkg.getType().resolve("long");
146             Method method = (Method) clazz.getContents().get(1);
147             Parameter par = (Parameter) method.getParameters().get(0);
148             par.setType(type);
149             par.setName("varLong");
150             fail = false;
151         } finally {
152             Utility.endTrans(fail);
153         }
154         assertFile("File is not correctly generated.",
155             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/VarArgsClass.java"),
156             getGoldenFile("testParChange_VarArgsTest.pass"),
157             getWorkDir()
158         );
159     }
160     
161     /**
162      * @param args the command line arguments
163      */

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