KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > 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 package org.netbeans.api.java.source.gen;
20
21 import com.sun.source.tree.*;
22 import java.io.*;
23 import java.util.Collections JavaDoc;
24 import org.netbeans.api.java.source.*;
25 import org.netbeans.api.java.source.JavaSource.Phase;
26 import org.netbeans.junit.NbTestSuite;
27
28 /**
29  * Variable arguments:
30  * abstract void method(int a, Object...);
31  *
32  * @author Pavel Flaska
33  */

34 public class VarArgsTest extends GeneratorTestMDRCompat {
35     
36     /** Creates a new instance of VarArgsTest */
37     public VarArgsTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     public static NbTestSuite suite() {
42         NbTestSuite suite = new NbTestSuite();
43         suite.addTestSuite(VarArgsTest.class);
44         return suite;
45     }
46
47     public void testMethodWithVarargs() throws Exception JavaDoc {
48         testFile = new File(getWorkDir(), "Test.java");
49         TestUtilities.copyStringToFile(testFile,
50             "package hierbas.del.litoral;\n\n" +
51             "import java.io.File;\n\n" +
52             "public class Test {\n\n" +
53             " void method(Object[] a) {\n" +
54             " }\n\n" +
55             "}\n"
56             );
57         String JavaDoc golden =
58             "package hierbas.del.litoral;\n\n" +
59             "import java.io.File;\n\n" +
60             "public class Test {\n\n" +
61             " void method(Object... a) {\n" +
62             " }\n\n" +
63             "}\n";
64
65         JavaSource src = getJavaSource(testFile);
66         
67         CancellableTask task = new CancellableTask<WorkingCopy>() {
68
69             public void run(WorkingCopy workingCopy) throws IOException {
70                 workingCopy.toPhase(Phase.RESOLVED);
71                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
72                 TreeMaker make = workingCopy.getTreeMaker();
73
74                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
75                 MethodTree method = (MethodTree) clazz.getMembers().get(1);
76                 VariableTree parameter = method.getParameters().get(0);
77                 long VARARGS = 1L<<34;
78                 ModifiersTree newMods = make.Modifiers(VARARGS, Collections.<AnnotationTree>emptyList());
79                 workingCopy.rewrite(parameter.getModifiers(), newMods);
80             }
81
82             public void cancel() {
83             }
84         };
85         src.runModificationTask(task).commit();
86         String JavaDoc res = TestUtilities.copyFileToString(testFile);
87         System.err.println(res);
88         assertEquals(golden, res);
89     }
90     
91     String JavaDoc getGoldenPckg() {
92         return "";
93     }
94     
95     String JavaDoc getSourcePckg() {
96         return "";
97     }
98 }
99
Popular Tags