001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.java.source.gen;
042:
043: import com.sun.source.tree.*;
044: import java.io.File;
045: import java.io.IOException;
046: import org.netbeans.api.java.source.*;
047: import static org.netbeans.api.java.source.JavaSource.*;
048: import org.netbeans.junit.NbTestSuite;
049:
050: /**
051: * Test packages.
052: *
053: * @author Pavel Flaska
054: */
055: public class PackageTest extends GeneratorTestMDRCompat {
056:
057: /** Creates a new instance of PackageTest */
058: public PackageTest(String testName) {
059: super (testName);
060: }
061:
062: public static NbTestSuite suite() {
063: NbTestSuite suite = new NbTestSuite();
064: suite.addTestSuite(PackageTest.class);
065: // suite.addTest(new PackageTest("testChangePackage"));
066: // suite.addTest(new PackageTest("testChangeDefToNamedPackage"));
067: // suite.addTest(new PackageTest("testChangeDefToNamedPackageWithImport"));
068: // suite.addTest(new PackageTest("testChangeToDefPackage"));
069: // suite.addTest(new PackageTest("testPackageRenameWithAnnotation"));
070: return suite;
071: }
072:
073: /**
074: * Change package declaration 'package org.nothing;' to
075: * 'package com.unit;'.
076: */
077: public void testChangePackage() throws Exception {
078: testFile = new File(getWorkDir(), "Test.java");
079: TestUtilities.copyStringToFile(testFile, "/**\n" + " * What?\n"
080: + " */\n" + "package org.nothing;\n\n"
081: + "class Test {\n" + "}\n");
082: String golden = "/**\n" + " * What?\n" + " */\n"
083: + "package com.unit;\n\n" + "class Test {\n" + "}\n";
084:
085: JavaSource src = getJavaSource(testFile);
086: Task<WorkingCopy> task = new Task<WorkingCopy>() {
087:
088: public void run(WorkingCopy workingCopy) throws IOException {
089: workingCopy.toPhase(Phase.RESOLVED);
090: TreeMaker make = workingCopy.getTreeMaker();
091: CompilationUnitTree cut = workingCopy
092: .getCompilationUnit();
093: CompilationUnitTree copy = make.CompilationUnit(make
094: .Identifier("com.unit"), cut.getImports(), cut
095: .getTypeDecls(), cut.getSourceFile());
096: workingCopy.rewrite(cut, copy);
097: }
098:
099: };
100: src.runModificationTask(task).commit();
101: String res = TestUtilities.copyFileToString(testFile);
102: System.err.println(res);
103: assertEquals(golden, res);
104: }
105:
106: /**
107: * Remove the package declartion, i.e. make the class part of
108: * default package.
109: */
110: public void testChangeToDefPackage() throws Exception {
111: testFile = new File(getWorkDir(), "Test.java");
112: TestUtilities.copyStringToFile(testFile, "/**\n" + " * What?\n"
113: + " */\n" + "package org.nothing;\n\n"
114: + "class Test {\n" + "}\n");
115: String golden = "/**\n" + " * What?\n" + " */\n" + "\n\n"
116: + "class Test {\n" + "}\n";
117:
118: JavaSource src = getJavaSource(testFile);
119: Task<WorkingCopy> task = new Task<WorkingCopy>() {
120:
121: public void run(WorkingCopy workingCopy) throws IOException {
122: workingCopy.toPhase(Phase.RESOLVED);
123: TreeMaker make = workingCopy.getTreeMaker();
124: CompilationUnitTree cut = workingCopy
125: .getCompilationUnit();
126: CompilationUnitTree copy = make.CompilationUnit(null,
127: cut.getImports(), cut.getTypeDecls(), cut
128: .getSourceFile());
129: workingCopy.rewrite(cut, copy);
130: }
131:
132: };
133: src.runModificationTask(task).commit();
134: String res = TestUtilities.copyFileToString(testFile);
135: System.err.println(res);
136: assertEquals(golden, res);
137: }
138:
139: /**
140: * Remove the package declartion, i.e. make the class part of
141: * default package.
142: */
143: public void testChangeDefToNamedPackage() throws Exception {
144: testFile = new File(getWorkDir(), "Test.java");
145: TestUtilities.copyStringToFile(testFile, "/**\n" + " * What?\n"
146: + " */\n" + "class Test {\n" + "}\n");
147: String golden = "package gro.snaebten.seludom.avaj;\n" + "\n"
148: + "/**\n" + " * What?\n" + " */\n" + "class Test {\n"
149: + "}\n";
150:
151: JavaSource src = getJavaSource(testFile);
152: Task<WorkingCopy> task = new Task<WorkingCopy>() {
153:
154: public void run(WorkingCopy workingCopy) throws IOException {
155: workingCopy.toPhase(Phase.RESOLVED);
156: TreeMaker make = workingCopy.getTreeMaker();
157: CompilationUnitTree cut = workingCopy
158: .getCompilationUnit();
159: CompilationUnitTree copy = make.CompilationUnit(make
160: .Identifier("gro.snaebten.seludom.avaj"), cut
161: .getImports(), cut.getTypeDecls(), cut
162: .getSourceFile());
163: workingCopy.rewrite(cut, copy);
164: }
165:
166: };
167: src.runModificationTask(task).commit();
168: String res = TestUtilities.copyFileToString(testFile);
169: System.err.println(res);
170: assertEquals(golden, res);
171: }
172:
173: /**
174: * Remove the package declartion, i.e. make the class part of
175: * default package.
176: */
177: public void testChangeDefToNamedPackageWithImport()
178: throws Exception {
179: testFile = new File(getWorkDir(), "Test.java");
180: TestUtilities.copyStringToFile(testFile, "/**\n" + " * What?\n"
181: + " */\n" + "import gro;\n\n" + "class Test {\n"
182: + "}\n");
183: String golden = "package gro.snaebten.seludom.avaj;\n" + "\n"
184: + "/**\n" + " * What?\n" + " */\n" + "import gro;\n\n"
185: + "class Test {\n" + "}\n";
186:
187: JavaSource src = getJavaSource(testFile);
188: Task<WorkingCopy> task = new Task<WorkingCopy>() {
189:
190: public void run(WorkingCopy workingCopy) throws IOException {
191: workingCopy.toPhase(Phase.RESOLVED);
192: TreeMaker make = workingCopy.getTreeMaker();
193: CompilationUnitTree cut = workingCopy
194: .getCompilationUnit();
195: CompilationUnitTree copy = make.CompilationUnit(make
196: .Identifier("gro.snaebten.seludom.avaj"), cut
197: .getImports(), cut.getTypeDecls(), cut
198: .getSourceFile());
199: workingCopy.rewrite(cut, copy);
200: }
201:
202: };
203: src.runModificationTask(task).commit();
204: String res = TestUtilities.copyFileToString(testFile);
205: System.err.println(res);
206: assertEquals(golden, res);
207: }
208:
209: /**
210: * #93045: Package annotation removed when renaming package
211: */
212: public void testPackageRenameWithAnnotation() throws Exception {
213: testFile = new File(getWorkDir(), "Test.java");
214: TestUtilities
215: .copyStringToFile(testFile, "@SuppressWarnings()\n"
216: + "package javaapplication1;\n");
217: String golden = "@SuppressWarnings()\n" + "package app;\n";
218:
219: JavaSource src = getJavaSource(testFile);
220: Task<WorkingCopy> task = new Task<WorkingCopy>() {
221:
222: public void run(WorkingCopy workingCopy) throws IOException {
223: workingCopy.toPhase(Phase.RESOLVED);
224: TreeMaker make = workingCopy.getTreeMaker();
225: ExpressionTree pckgName = workingCopy
226: .getCompilationUnit().getPackageName();
227: workingCopy.rewrite(pckgName, make.setLabel(pckgName,
228: "app"));
229: }
230:
231: };
232: src.runModificationTask(task).commit();
233: String res = TestUtilities.copyFileToString(testFile);
234: System.err.println(res);
235: assertEquals(golden, res);
236: }
237:
238: // not important for this test
239: String getGoldenPckg() {
240: return "";
241: }
242:
243: String getSourcePckg() {
244: return "";
245: }
246:
247: }
|