KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.api.java.source.*;
25 import static org.netbeans.api.java.source.JavaSource.*;
26 import org.netbeans.jackpot.test.TestUtilities;
27 import org.netbeans.junit.NbTestSuite;
28
29 /**
30  * Test packages.
31  *
32  * @author Pavel Flaska
33  */

34 public class PackageTest extends GeneratorTestMDRCompat {
35     
36     /** Creates a new instance of PackageTest */
37     public PackageTest(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static NbTestSuite suite() {
42         NbTestSuite suite = new NbTestSuite();
43         suite.addTestSuite(PackageTest.class);
44 // suite.addTest(new PackageTest("testChangePackage"));
45
// suite.addTest(new PackageTest("testChangeDefToNamedPackage"));
46
// suite.addTest(new PackageTest("testChangeDefToNamedPackageWithImport"));
47
// suite.addTest(new PackageTest("testChangeToDefPackage"));
48
// suite.addTest(new PackageTest("testPackageRenameWithAnnotation"));
49
return suite;
50     }
51     
52     /**
53      * Change package declaration 'package org.nothing;' to
54      * 'package com.unit;'.
55      */

56     public void testChangePackage() throws Exception JavaDoc {
57         testFile = new File JavaDoc(getWorkDir(), "Test.java");
58         TestUtilities.copyStringToFile(testFile,
59             "/**\n" +
60             " * What?\n" +
61             " */\n" +
62             "package org.nothing;\n\n" +
63             "class Test {\n" +
64             "}\n"
65             );
66         String JavaDoc golden =
67             "/**\n" +
68             " * What?\n" +
69             " */\n" +
70             "package com.unit;\n\n" +
71             "class Test {\n" +
72             "}\n";
73         
74         JavaSource src = getJavaSource(testFile);
75         CancellableTask task = new CancellableTask<WorkingCopy>() {
76
77             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
78                 workingCopy.toPhase(Phase.RESOLVED);
79                 TreeMaker make = workingCopy.getTreeMaker();
80                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
81                 CompilationUnitTree copy = make.CompilationUnit(
82                     make.Identifier("com.unit"),
83                     cut.getImports(),
84                     cut.getTypeDecls(),
85                     cut.getSourceFile()
86                 );
87                 workingCopy.rewrite(cut, copy);
88             }
89
90             public void cancel() {
91             }
92         };
93         src.runModificationTask(task).commit();
94         String JavaDoc res = TestUtilities.copyFileToString(testFile);
95         System.err.println(res);
96         assertEquals(golden, res);
97     }
98     
99     /**
100      * Remove the package declartion, i.e. make the class part of
101      * default package.
102      */

103     public void testChangeToDefPackage() throws Exception JavaDoc {
104         testFile = new File JavaDoc(getWorkDir(), "Test.java");
105         TestUtilities.copyStringToFile(testFile,
106             "/**\n" +
107             " * What?\n" +
108             " */\n" +
109             "package org.nothing;\n\n" +
110             "class Test {\n" +
111             "}\n"
112             );
113         String JavaDoc golden =
114             "/**\n" +
115             " * What?\n" +
116             " */\n" +
117             "\n\n" +
118             "class Test {\n" +
119             "}\n";
120
121         JavaSource src = getJavaSource(testFile);
122         CancellableTask task = new CancellableTask<WorkingCopy>() {
123
124             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
125                 workingCopy.toPhase(Phase.RESOLVED);
126                 TreeMaker make = workingCopy.getTreeMaker();
127                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
128                 CompilationUnitTree copy = make.CompilationUnit(
129                     null,
130                     cut.getImports(),
131                     cut.getTypeDecls(),
132                     cut.getSourceFile()
133                 );
134                 workingCopy.rewrite(cut, copy);
135             }
136
137             public void cancel() {
138             }
139         };
140         src.runModificationTask(task).commit();
141         String JavaDoc res = TestUtilities.copyFileToString(testFile);
142         System.err.println(res);
143         assertEquals(golden, res);
144     }
145     
146     /**
147      * Remove the package declartion, i.e. make the class part of
148      * default package.
149      */

150     public void testChangeDefToNamedPackage() throws Exception JavaDoc {
151         testFile = new File JavaDoc(getWorkDir(), "Test.java");
152         TestUtilities.copyStringToFile(testFile,
153             "/**\n" +
154             " * What?\n" +
155             " */\n" +
156             "class Test {\n" +
157             "}\n"
158             );
159         String JavaDoc golden =
160             "package gro.snaebten.seludom.avaj;\n" +
161             "/**\n" +
162             " * What?\n" +
163             " */\n" +
164             "class Test {\n" +
165             "}\n";
166
167         JavaSource src = getJavaSource(testFile);
168         CancellableTask task = new CancellableTask<WorkingCopy>() {
169
170             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
171                 workingCopy.toPhase(Phase.RESOLVED);
172                 TreeMaker make = workingCopy.getTreeMaker();
173                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
174                     CompilationUnitTree copy = make.CompilationUnit(
175                         make.Identifier("gro.snaebten.seludom.avaj"),
176                         cut.getImports(),
177                         cut.getTypeDecls(),
178                         cut.getSourceFile()
179                     );
180                 workingCopy.rewrite(cut, copy);
181             }
182
183             public void cancel() {
184             }
185         };
186         src.runModificationTask(task).commit();
187         String JavaDoc res = TestUtilities.copyFileToString(testFile);
188         System.err.println(res);
189         assertEquals(golden, res);
190     }
191     
192     /**
193      * Remove the package declartion, i.e. make the class part of
194      * default package.
195      */

196     public void testChangeDefToNamedPackageWithImport() throws Exception JavaDoc {
197         testFile = new File JavaDoc(getWorkDir(), "Test.java");
198         TestUtilities.copyStringToFile(testFile,
199             "/**\n" +
200             " * What?\n" +
201             " */\n" +
202             "import gro;\n\n" +
203             "class Test {\n" +
204             "}\n"
205             );
206         String JavaDoc golden =
207             "package gro.snaebten.seludom.avaj;\n" +
208             "/**\n" +
209             " * What?\n" +
210             " */\n" +
211             "import gro;\n\n" +
212             "class Test {\n" +
213             "}\n";
214
215         JavaSource src = getJavaSource(testFile);
216         CancellableTask task = new CancellableTask<WorkingCopy>() {
217
218             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
219                 workingCopy.toPhase(Phase.RESOLVED);
220                 TreeMaker make = workingCopy.getTreeMaker();
221                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
222                     CompilationUnitTree copy = make.CompilationUnit(
223                         make.Identifier("gro.snaebten.seludom.avaj"),
224                         cut.getImports(),
225                         cut.getTypeDecls(),
226                         cut.getSourceFile()
227                     );
228                 workingCopy.rewrite(cut, copy);
229             }
230
231             public void cancel() {
232             }
233         };
234         src.runModificationTask(task).commit();
235         String JavaDoc res = TestUtilities.copyFileToString(testFile);
236         System.err.println(res);
237         assertEquals(golden, res);
238     }
239     
240     /**
241      * #93045: Package annotation removed when renaming package
242      */

243     public void testPackageRenameWithAnnotation() throws Exception JavaDoc {
244         testFile = new File JavaDoc(getWorkDir(), "Test.java");
245         TestUtilities.copyStringToFile(testFile,
246             "@SuppressWarnings()\n" +
247             "package javaapplication1;\n");
248         String JavaDoc golden =
249             "@SuppressWarnings()\n" +
250             "package app;\n";
251
252         JavaSource src = getJavaSource(testFile);
253         CancellableTask task = new CancellableTask<WorkingCopy>() {
254
255             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
256                 workingCopy.toPhase(Phase.RESOLVED);
257                 TreeMaker make = workingCopy.getTreeMaker();
258                 ExpressionTree pckgName = workingCopy.getCompilationUnit().getPackageName();
259                 workingCopy.rewrite(pckgName, make.setLabel(pckgName, "app"));
260             }
261
262             public void cancel() {
263             }
264         };
265         src.runModificationTask(task).commit();
266         String JavaDoc res = TestUtilities.copyFileToString(testFile);
267         System.err.println(res);
268         assertEquals(golden, res);
269     }
270     
271     // not important for this test
272
String JavaDoc getGoldenPckg() {
273         return "";
274     }
275
276     String JavaDoc getSourcePckg() {
277         return "";
278     }
279
280     
281 }
282
Popular Tags