KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.api.java.source.CancellableTask;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.api.java.source.JavaSource.Phase;
32 import org.netbeans.api.java.source.TestUtilities;
33 import org.netbeans.api.java.source.TreeMaker;
34 import org.netbeans.api.java.source.WorkingCopy;
35
36 /**
37  * Extends in interfaces can contain more identifiers (like implements
38  * in classes). Consider and test interface with more extends identifiers.
39  * (javac contains these part in ClassTree in implements part.)
40  *
41  * @author Pavel Flaska
42  */

43 public class InterfaceExtendsTest extends GeneratorTestMDRCompat {
44     
45     /** Creates a new instance of InterfaceExtendsTest */
46     public InterfaceExtendsTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     public void testAddExtends() throws Exception JavaDoc {
51         testFile = new File JavaDoc(getWorkDir(), "Test.java");
52         TestUtilities.copyStringToFile(testFile,
53             "package hierbas.del.litoral;\n\n" +
54             "import java.util.*;\n\n" +
55             "public interface Test {\n" +
56             " public void taragui();\n" +
57             "}\n"
58             );
59         String JavaDoc golden =
60             "package hierbas.del.litoral;\n\n" +
61             "import java.util.*;\n\n" +
62             "public interface Test extends Serializable {\n" +
63             " public void taragui();\n" +
64             "}\n";
65         JavaSource src = getJavaSource(testFile);
66         
67         CancellableTask task = new CancellableTask<WorkingCopy>() {
68
69             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
70                 workingCopy.toPhase(Phase.RESOLVED);
71                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
72                 TreeMaker make = workingCopy.getTreeMaker();
73
74                 for (Tree typeDecl : cut.getTypeDecls()) {
75                     // ensure that it is correct type declaration, i.e. class
76
if (Tree.Kind.CLASS == typeDecl.getKind()) {
77                         ClassTree classTree = (ClassTree) typeDecl;
78                         List JavaDoc<ExpressionTree> implementz =
79                             Collections.<ExpressionTree>singletonList(
80                                 make.Identifier("Serializable")
81                             );
82                         ClassTree copy = make.Class(
83                                 classTree.getModifiers(),
84                                 classTree.getSimpleName(),
85                                 classTree.getTypeParameters(),
86                                 classTree.getExtendsClause(),
87                                 implementz,
88                                 classTree.getMembers()
89                             );
90                         workingCopy.rewrite(classTree, copy);
91                     }
92                 }
93             }
94             
95             public void cancel() {
96             }
97         };
98         src.runModificationTask(task).commit();
99         String JavaDoc res = TestUtilities.copyFileToString(testFile);
100         assertEquals(golden, res);
101     }
102     
103     public void testAddTwoExtends() throws Exception JavaDoc {
104         testFile = new File JavaDoc(getWorkDir(), "Test.java");
105         TestUtilities.copyStringToFile(testFile,
106             "package hierbas.del.litoral;\n\n" +
107             "import java.util.*;\n\n" +
108             "public interface Test {\n" +
109             " public void taragui();\n" +
110             "}\n"
111             );
112         String JavaDoc golden =
113             "package hierbas.del.litoral;\n\n" +
114             "import java.util.*;\n\n" +
115             "public interface Test extends Serializable, Externalizable {\n" +
116             " public void taragui();\n" +
117             "}\n";
118         JavaSource src = getJavaSource(testFile);
119         
120         CancellableTask task = new CancellableTask<WorkingCopy>() {
121
122             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
123                 workingCopy.toPhase(Phase.RESOLVED);
124                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
125                 TreeMaker make = workingCopy.getTreeMaker();
126
127                 for (Tree typeDecl : cut.getTypeDecls()) {
128                     // ensure that it is correct type declaration, i.e. class
129
if (Tree.Kind.CLASS == typeDecl.getKind()) {
130                         ClassTree classTree = (ClassTree) typeDecl;
131                         ClassTree copy = make.addClassImplementsClause(
132                                 classTree, make.Identifier("Serializable")
133                             );
134                         copy = make.addClassImplementsClause(
135                                 copy, make.Identifier("Externalizable")
136                             );
137                         workingCopy.rewrite(classTree, copy);
138                     }
139                 }
140             }
141             
142             public void cancel() {
143             }
144         };
145         src.runModificationTask(task).commit();
146         String JavaDoc res = TestUtilities.copyFileToString(testFile);
147         assertEquals(golden, res);
148     }
149     
150     public void testRemoveTwoExtends() throws Exception JavaDoc {
151         testFile = new File JavaDoc(getWorkDir(), "Test.java");
152         TestUtilities.copyStringToFile(testFile,
153             "package hierbas.del.litoral;\n\n" +
154             "import java.util.*;\n\n" +
155             "public interface Test extends Serializable, Externalizable {\n" +
156             " public void taragui();\n" +
157             "}\n"
158             );
159         String JavaDoc golden =
160             "package hierbas.del.litoral;\n\n" +
161             "import java.util.*;\n\n" +
162             "public interface Test {\n" +
163             " public void taragui();\n" +
164             "}\n";
165         JavaSource src = getJavaSource(testFile);
166         
167         CancellableTask task = new CancellableTask<WorkingCopy>() {
168
169             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
170                 workingCopy.toPhase(Phase.RESOLVED);
171                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
172                 TreeMaker make = workingCopy.getTreeMaker();
173
174                 for (Tree typeDecl : cut.getTypeDecls()) {
175                     // ensure that it is correct type declaration, i.e. class
176
if (Tree.Kind.CLASS == typeDecl.getKind()) {
177                         ClassTree classTree = (ClassTree) typeDecl;
178                         ClassTree copy = make.removeClassImplementsClause(classTree, 0);
179                         copy = make.removeClassImplementsClause(copy, 0);
180                         workingCopy.rewrite(classTree, copy);
181                     }
182                 }
183             }
184             
185             public void cancel() {
186             }
187         };
188         src.runModificationTask(task).commit();
189         String JavaDoc res = TestUtilities.copyFileToString(testFile);
190         assertEquals(golden, res);
191     }
192     
193     String JavaDoc getGoldenPckg() {
194         return "";
195     }
196     
197     String JavaDoc getSourcePckg() {
198         return "";
199     }
200     
201 }
202
Popular Tags