KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassTree;
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.tree.ExpressionTree;
24 import com.sun.source.tree.Tree;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.java.source.CancellableTask;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.api.java.source.TreeMaker;
31 import static org.netbeans.api.java.source.JavaSource.*;
32 import org.netbeans.api.java.source.WorkingCopy;
33 import org.netbeans.jackpot.test.TestUtilities;
34 import org.netbeans.junit.NbTestSuite;
35
36 /**
37  * Test adding/removing/modifying extends clause in source.
38  * In addition to, tries to work with extends in interfaces.
39  *
40  * @author Pavel Flaska
41  */

42 public class ClassExtendsTest extends GeneratorTestMDRCompat {
43     
44     /** Creates a new instance of ClassExtendsTest */
45     public ClassExtendsTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static NbTestSuite suite() {
50         NbTestSuite suite = new NbTestSuite();
51         suite.addTestSuite(ClassExtendsTest.class);
52         return suite;
53     }
54     
55     /*
56      * Tests the modifcation of extends clause. From
57      *
58      * public class Test<E> extends Object {
59      * to
60      *
61      * public class Test<E> extends String {
62      */

63     public void testModifyExtends() throws Exception JavaDoc {
64         testFile = new File JavaDoc(getWorkDir(), "Test.java");
65         TestUtilities.copyStringToFile(testFile,
66             "package hierbas.del.litoral;\n\n" +
67             "import java.util.*;\n\n" +
68             "public class Test<E> extends Object {\n" +
69             " public void taragui() {\n" +
70             " }\n" +
71             "}\n"
72             );
73         String JavaDoc golden =
74             "package hierbas.del.litoral;\n\n" +
75             "import java.util.*;\n\n" +
76             "public class Test<E> extends String {\n" +
77             " public void taragui() {\n" +
78             " }\n" +
79             "}\n";
80         JavaSource src = getJavaSource(testFile);
81         
82         CancellableTask task = new CancellableTask<WorkingCopy>() {
83
84             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
85                 workingCopy.toPhase(Phase.RESOLVED);
86                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
87                 TreeMaker make = workingCopy.getTreeMaker();
88
89                 for (Tree typeDecl : cut.getTypeDecls()) {
90                     // ensure that it is correct type declaration, i.e. class
91
if (Tree.Kind.CLASS == typeDecl.getKind()) {
92                         ClassTree clazz = (ClassTree) typeDecl;
93                         workingCopy.rewrite(clazz.getExtendsClause(), make.Identifier("String"));
94                     }
95                 }
96             }
97
98             public void cancel() {
99             }
100         };
101         src.runModificationTask(task).commit();
102         String JavaDoc res = TestUtilities.copyFileToString(testFile);
103         System.err.println(res);
104         assertEquals(golden, res);
105     }
106
107     public void testExtendsNoOrig() throws Exception JavaDoc {
108         testFile = new File JavaDoc(getWorkDir(), "Test.java");
109         TestUtilities.copyStringToFile(testFile,
110             "package hierbas.del.litoral;\n\n" +
111             "import java.util.*;\n\n" +
112             "public class Test<E> {\n" +
113             " public void taragui() {\n" +
114             " }\n" +
115             "}\n"
116             );
117         String JavaDoc golden =
118             "package hierbas.del.litoral;\n\n" +
119             "import java.util.*;\n\n" +
120             "public class Test<E> extends String {\n" +
121             " public void taragui() {\n" +
122             " }\n" +
123             "}\n";
124         JavaSource src = getJavaSource(testFile);
125         
126         CancellableTask task = new CancellableTask<WorkingCopy>() {
127
128             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
129                 workingCopy.toPhase(Phase.RESOLVED);
130                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
131                 TreeMaker make = workingCopy.getTreeMaker();
132
133                 for (Tree typeDecl : cut.getTypeDecls()) {
134                     // ensure that it is correct type declaration, i.e. class
135
if (Tree.Kind.CLASS == typeDecl.getKind()) {
136                         ClassTree classTree = (ClassTree) typeDecl;
137                         ClassTree copy = make.setExtends(classTree, make.Identifier("String"));
138 // ClassTree copy = make.Class(
139
// classTree.getModifiers(),
140
// classTree.getSimpleName(),
141
// classTree.getTypeParameters(),
142
// make.Identifier("String"),
143
// (List<ExpressionTree>) classTree.getImplementsClause(),
144
// classTree.getMembers()
145
// );
146
workingCopy.rewrite(classTree, copy);
147                     }
148                 }
149             }
150             
151             public void cancel() {
152             }
153         };
154         src.runModificationTask(task).commit();
155         String JavaDoc res = TestUtilities.copyFileToString(testFile);
156         System.err.println(res);
157         assertEquals(golden, res);
158     }
159     
160     public void testRemoveExtends() throws Exception JavaDoc {
161         testFile = new File JavaDoc(getWorkDir(), "Test.java");
162         TestUtilities.copyStringToFile(testFile,
163             "package hierbas.del.litoral;\n\n" +
164             "import java.util.*;\n\n" +
165             "public class Test<E> extends Object {\n" +
166             " public void taragui() {\n" +
167             " }\n" +
168             "}\n"
169             );
170         String JavaDoc golden =
171             "package hierbas.del.litoral;\n\n" +
172             "import java.util.*;\n\n" +
173             "public class Test<E> {\n" +
174             " public void taragui() {\n" +
175             " }\n" +
176             "}\n";
177         JavaSource src = getJavaSource(testFile);
178         
179         CancellableTask task = new CancellableTask<WorkingCopy>() {
180
181             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
182                 workingCopy.toPhase(Phase.RESOLVED);
183                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
184                 TreeMaker make = workingCopy.getTreeMaker();
185
186                 for (Tree typeDecl : cut.getTypeDecls()) {
187                     // ensure that it is correct type declaration, i.e. class
188
if (Tree.Kind.CLASS == typeDecl.getKind()) {
189                         ClassTree classTree = (ClassTree) typeDecl;
190                         ClassTree copy = make.Class(
191                                 classTree.getModifiers(),
192                                 classTree.getSimpleName(),
193                                 classTree.getTypeParameters(),
194                                 null,
195                                 (List JavaDoc<ExpressionTree>) classTree.getImplementsClause(),
196                                 classTree.getMembers()
197                             );
198                         workingCopy.rewrite(classTree, copy);
199                     }
200                 }
201             }
202             
203             public void cancel() {
204             }
205         };
206         src.runModificationTask(task).commit();
207         String JavaDoc res = TestUtilities.copyFileToString(testFile);
208         System.err.println(res);
209         assertEquals(golden, res);
210     }
211     
212     String JavaDoc getGoldenPckg() {
213         return "";
214     }
215     
216     String JavaDoc getSourcePckg() {
217         return "";
218     }
219 }
220
Popular Tags