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.ClassTree;
044: import com.sun.source.tree.CompilationUnitTree;
045: import com.sun.source.tree.ExpressionTree;
046: import com.sun.source.tree.Tree;
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.List;
050: import org.netbeans.api.java.source.Task;
051: import org.netbeans.api.java.source.JavaSource;
052: import org.netbeans.api.java.source.TestUtilities;
053: import org.netbeans.api.java.source.TreeMaker;
054: import static org.netbeans.api.java.source.JavaSource.*;
055: import org.netbeans.api.java.source.WorkingCopy;
056: import org.netbeans.junit.NbTestSuite;
057:
058: /**
059: * Test adding/removing/modifying extends clause in source.
060: * In addition to, tries to work with extends in interfaces.
061: *
062: * @author Pavel Flaska
063: */
064: public class ClassExtendsTest extends GeneratorTestMDRCompat {
065:
066: /** Creates a new instance of ClassExtendsTest */
067: public ClassExtendsTest(String testName) {
068: super (testName);
069: }
070:
071: public static NbTestSuite suite() {
072: NbTestSuite suite = new NbTestSuite();
073: suite.addTestSuite(ClassExtendsTest.class);
074: return suite;
075: }
076:
077: /*
078: * Tests the modifcation of extends clause. From
079: *
080: * public class Test<E> extends Object {
081: * to
082: *
083: * public class Test<E> extends String {
084: */
085: public void testModifyExtends() throws Exception {
086: testFile = new File(getWorkDir(), "Test.java");
087: TestUtilities.copyStringToFile(testFile,
088: "package hierbas.del.litoral;\n\n"
089: + "import java.util.*;\n\n"
090: + "public class Test<E> extends Object {\n"
091: + " public void taragui() {\n" + " }\n"
092: + "}\n");
093: String golden = "package hierbas.del.litoral;\n\n"
094: + "import java.util.*;\n\n"
095: + "public class Test<E> extends String {\n"
096: + " public void taragui() {\n" + " }\n" + "}\n";
097: JavaSource src = getJavaSource(testFile);
098:
099: Task<WorkingCopy> task = new Task<WorkingCopy>() {
100:
101: public void run(WorkingCopy workingCopy) throws IOException {
102: workingCopy.toPhase(Phase.RESOLVED);
103: CompilationUnitTree cut = workingCopy
104: .getCompilationUnit();
105: TreeMaker make = workingCopy.getTreeMaker();
106:
107: for (Tree typeDecl : cut.getTypeDecls()) {
108: // ensure that it is correct type declaration, i.e. class
109: if (Tree.Kind.CLASS == typeDecl.getKind()) {
110: ClassTree clazz = (ClassTree) typeDecl;
111: workingCopy.rewrite(clazz.getExtendsClause(),
112: make.Identifier("String"));
113: }
114: }
115: }
116:
117: };
118: src.runModificationTask(task).commit();
119: String res = TestUtilities.copyFileToString(testFile);
120: System.err.println(res);
121: assertEquals(golden, res);
122: }
123:
124: public void testExtendsNoOrig() throws Exception {
125: testFile = new File(getWorkDir(), "Test.java");
126: TestUtilities.copyStringToFile(testFile,
127: "package hierbas.del.litoral;\n\n"
128: + "import java.util.*;\n\n"
129: + "public class Test<E> {\n"
130: + " public void taragui() {\n" + " }\n"
131: + "}\n");
132: String golden = "package hierbas.del.litoral;\n\n"
133: + "import java.util.*;\n\n"
134: + "public class Test<E> extends String {\n"
135: + " public void taragui() {\n" + " }\n" + "}\n";
136: JavaSource src = getJavaSource(testFile);
137:
138: Task<WorkingCopy> task = new Task<WorkingCopy>() {
139:
140: public void run(WorkingCopy workingCopy) throws IOException {
141: workingCopy.toPhase(Phase.RESOLVED);
142: CompilationUnitTree cut = workingCopy
143: .getCompilationUnit();
144: TreeMaker make = workingCopy.getTreeMaker();
145:
146: for (Tree typeDecl : cut.getTypeDecls()) {
147: // ensure that it is correct type declaration, i.e. class
148: if (Tree.Kind.CLASS == typeDecl.getKind()) {
149: ClassTree classTree = (ClassTree) typeDecl;
150: ClassTree copy = make.setExtends(classTree,
151: make.Identifier("String"));
152: // ClassTree copy = make.Class(
153: // classTree.getModifiers(),
154: // classTree.getSimpleName(),
155: // classTree.getTypeParameters(),
156: // make.Identifier("String"),
157: // (List<ExpressionTree>) classTree.getImplementsClause(),
158: // classTree.getMembers()
159: // );
160: workingCopy.rewrite(classTree, copy);
161: }
162: }
163: }
164:
165: };
166: src.runModificationTask(task).commit();
167: String res = TestUtilities.copyFileToString(testFile);
168: System.err.println(res);
169: assertEquals(golden, res);
170: }
171:
172: public void testRemoveExtends() throws Exception {
173: testFile = new File(getWorkDir(), "Test.java");
174: TestUtilities.copyStringToFile(testFile,
175: "package hierbas.del.litoral;\n\n"
176: + "import java.util.*;\n\n"
177: + "public class Test<E> extends Object {\n"
178: + " public void taragui() {\n" + " }\n"
179: + "}\n");
180: String golden = "package hierbas.del.litoral;\n\n"
181: + "import java.util.*;\n\n"
182: + "public class Test<E> {\n"
183: + " public void taragui() {\n" + " }\n" + "}\n";
184: JavaSource src = getJavaSource(testFile);
185:
186: Task<WorkingCopy> task = new Task<WorkingCopy>() {
187:
188: public void run(WorkingCopy workingCopy) throws IOException {
189: workingCopy.toPhase(Phase.RESOLVED);
190: CompilationUnitTree cut = workingCopy
191: .getCompilationUnit();
192: TreeMaker make = workingCopy.getTreeMaker();
193:
194: for (Tree typeDecl : cut.getTypeDecls()) {
195: // ensure that it is correct type declaration, i.e. class
196: if (Tree.Kind.CLASS == typeDecl.getKind()) {
197: ClassTree classTree = (ClassTree) typeDecl;
198: ClassTree copy = make.Class(classTree
199: .getModifiers(), classTree
200: .getSimpleName(), classTree
201: .getTypeParameters(), null, classTree
202: .getImplementsClause(), classTree
203: .getMembers());
204: workingCopy.rewrite(classTree, copy);
205: }
206: }
207: }
208:
209: };
210: src.runModificationTask(task).commit();
211: String res = TestUtilities.copyFileToString(testFile);
212: System.err.println(res);
213: assertEquals(golden, res);
214: }
215:
216: String getGoldenPckg() {
217: return "";
218: }
219:
220: String getSourcePckg() {
221: return "";
222: }
223: }
|