KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompilationUnitTree;
22 import com.sun.source.tree.ImportTree;
23 import com.sun.source.tree.Tree;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.modules.java.source.engine.ASTModel;
30 import org.netbeans.api.java.source.transform.Transformer;
31 import org.netbeans.junit.NbTestSuite;
32 import org.openide.filesystems.FileStateInvalidException;
33
34 /**
35  *
36  * @author Pavel Flaska
37  */

38 public class ImportFormatTest extends GeneratorTestMDRCompat {
39     
40     /** Creates a new instance of ImportFormatTest */
41     public ImportFormatTest(String JavaDoc name) {
42         super(name);
43     }
44
45     public static NbTestSuite suite() {
46         NbTestSuite suite = new NbTestSuite();
47         suite.addTest(new ImportFormatTest("testFirstAddition"));
48         suite.addTest(new ImportFormatTest("testAddFirstImport"));
49         suite.addTest(new ImportFormatTest("testAddLastImport"));
50         suite.addTest(new ImportFormatTest("testRemoveInnerImport"));
51         suite.addTest(new ImportFormatTest("testRemoveFirstImport"));
52         suite.addTest(new ImportFormatTest("testRemoveLastImport"));
53         suite.addTest(new ImportFormatTest("testRemoveAllRemaning"));
54         return suite;
55     }
56     
57     protected void setUp() throws FileStateInvalidException, Exception JavaDoc {
58         super.setUp();
59         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportClass1.java");
60     }
61
62     public void testFirstAddition() throws IOException JavaDoc, FileStateInvalidException {
63         System.err.println("testFirstAddition");
64         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
65             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
66                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>();
67                 imports.add(make.Import(make.Identifier("java.util.List"), false));
68                 imports.add(make.Import(make.Identifier("java.util.Set"), false));
69                 CompilationUnitTree unit = make.CompilationUnit(
70                         node.getPackageName(),
71                         imports,
72                         node.getTypeDecls(),
73                         node.getSourceFile()
74                 );
75                 changes.rewrite(node, unit);
76                 return null;
77             }
78         });
79         assertFiles("testFirstAddition_ImportFormatTest.pass");
80     }
81
82     public void testAddFirstImport() throws IOException JavaDoc, FileStateInvalidException {
83         System.err.println("testAddFirstImport");
84         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
85             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
86                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
87                 imports.add(0, make.Import(make.Identifier("java.util.AbstractList"), false));
88                 CompilationUnitTree unit = make.CompilationUnit(
89                         node.getPackageName(),
90                         imports,
91                         node.getTypeDecls(),
92                         node.getSourceFile()
93                 );
94                 changes.rewrite(node, unit);
95                 return null;
96             }
97         });
98         assertFiles("testAddFirstImport_ImportFormatTest.pass");
99     }
100     
101
102     public void testAddLastImport() throws IOException JavaDoc, FileStateInvalidException {
103         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
104             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
105                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
106                 imports.add(make.Import(make.Identifier("java.io.IOException"), false));
107                 CompilationUnitTree unit = make.CompilationUnit(
108                         node.getPackageName(),
109                         imports,
110                         node.getTypeDecls(),
111                         node.getSourceFile()
112                 );
113                 changes.rewrite(node, unit);
114                 return null;
115             }
116         });
117         assertFiles("testAddLastImport_ImportFormatTest.pass");
118     }
119     
120     public void testRemoveInnerImport() throws IOException JavaDoc, FileStateInvalidException {
121         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
122             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
123                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
124                 imports.remove(1);
125                 CompilationUnitTree unit = make.CompilationUnit(
126                         node.getPackageName(),
127                         imports,
128                         node.getTypeDecls(),
129                         node.getSourceFile()
130                 );
131                 changes.rewrite(node, unit);
132                 return null;
133             }
134         });
135         assertFiles("testRemoveInnerImport_ImportFormatTest.pass");
136     }
137     
138     public void testRemoveFirstImport() throws IOException JavaDoc, FileStateInvalidException {
139         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
140             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
141                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
142                 imports.remove(0);
143                 CompilationUnitTree unit = make.CompilationUnit(
144                         node.getPackageName(),
145                         imports,
146                         node.getTypeDecls(),
147                         node.getSourceFile()
148                 );
149                 changes.rewrite(node, unit);
150                 return null;
151             }
152         });
153         assertFiles("testRemoveFirstImport_ImportFormatTest.pass");
154     }
155
156     public void testRemoveLastImport() throws IOException JavaDoc, FileStateInvalidException {
157         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
158             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
159                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
160                 imports.remove(1);
161                 CompilationUnitTree unit = make.CompilationUnit(
162                         node.getPackageName(),
163                         imports,
164                         node.getTypeDecls(),
165                         node.getSourceFile()
166                 );
167                 changes.rewrite(node, unit);
168                 return null;
169             }
170         });
171         assertFiles("testRemoveLastImport_ImportFormatTest.pass");
172     }
173     
174     public void testRemoveAllRemaning() throws IOException JavaDoc, FileStateInvalidException {
175         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
176             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
177                 CompilationUnitTree unit = make.CompilationUnit(
178                         node.getPackageName(),
179                         Collections.EMPTY_LIST,
180                         node.getTypeDecls(),
181                         node.getSourceFile()
182                 );
183                 changes.rewrite(node, unit);
184                 return null;
185             }
186         });
187         assertFiles("testRemoveAllRemaning_ImportFormatTest.pass");
188     }
189
190     
191     static class T extends Transformer<Void JavaDoc, Object JavaDoc> {
192             public Void JavaDoc visitImport(ImportTree node, Object JavaDoc p) {
193                 make.Identifier("java.util.List");
194                 System.err.println(getParent(node, model));
195                 return null;
196             }
197     }
198
199     public void testAddSeveral() throws IOException JavaDoc, FileStateInvalidException {
200         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
201             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
202                 List JavaDoc<ImportTree> imports = (List JavaDoc<ImportTree>) node.getImports();
203                 imports.add(make.Import(make.Identifier("java.util.List"), false));
204                 imports.add(make.Import(make.Identifier("java.util.Set"), false));
205                 imports.add(make.Import(make.Identifier("javax.swing.CellRendererPane"), false));
206                 imports.add(make.Import(make.Identifier("javax.swing.BorderFactory"), false));
207                 imports.add(make.Import(make.Identifier("javax.swing.ImageIcon"), false));
208                 imports.add(make.Import(make.Identifier("javax.swing.InputVerifier"), false));
209                 imports.add(make.Import(make.Identifier("javax.swing.GrayFilter"), false));
210                 imports.add(make.Import(make.Identifier("javax.swing.JFileChooser"), false));
211                 imports.add(make.Import(make.Identifier("javax.swing.AbstractAction"), false));
212                 CompilationUnitTree unit = make.CompilationUnit(
213                         node.getPackageName(),
214                         imports,
215                         node.getTypeDecls(),
216                         node.getSourceFile()
217                 );
218                 changes.rewrite(node, unit);
219                 return null;
220             }
221         });
222         assertFiles("testAddSeveral_ImportFormatTest.pass");
223     }
224     
225     public void testRemoveInside() throws IOException JavaDoc, FileStateInvalidException {
226         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
227             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
228                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
229                 imports.remove(4);
230                 CompilationUnitTree unit = make.CompilationUnit(
231                         node.getPackageName(),
232                         imports,
233                         node.getTypeDecls(),
234                         node.getSourceFile()
235                 );
236                 changes.rewrite(node, unit);
237                 return null;
238             }
239         });
240         assertFiles("testRemoveInside_ImportFormatTest.pass");
241     }
242     
243     public void testMoveFirst() throws IOException JavaDoc, FileStateInvalidException {
244         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
245             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
246                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
247                 ImportTree oneImport = imports.remove(0);
248                 imports.add(3, oneImport);
249                 CompilationUnitTree unit = make.CompilationUnit(
250                         node.getPackageName(),
251                         imports,
252                         node.getTypeDecls(),
253                         node.getSourceFile()
254                 );
255                 changes.rewrite(node, unit);
256                 return null;
257             }
258         });
259         assertFiles("testMoveFirst_ImportFormatTest.pass");
260     }
261     
262     public void testMoveLast() throws IOException JavaDoc, FileStateInvalidException {
263         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
264             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
265                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
266                 ImportTree oneImport = imports.remove(7);
267                 imports.add(1, oneImport);
268                 CompilationUnitTree unit = make.CompilationUnit(
269                         node.getPackageName(),
270                         imports,
271                         node.getTypeDecls(),
272                         node.getSourceFile()
273                 );
274                 changes.rewrite(node, unit);
275                 return null;
276             }
277         });
278         assertFiles("testMoveLast_ImportFormatTest.pass");
279     }
280     
281     public void testReplaceLine() throws IOException JavaDoc, FileStateInvalidException {
282         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
283             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
284                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
285                 ImportTree oneImport = imports.remove(4);
286                 imports.add(4, make.Import(make.Identifier("java.util.Collection"), false));
287                 CompilationUnitTree unit = make.CompilationUnit(
288                         node.getPackageName(),
289                         imports,
290                         node.getTypeDecls(),
291                         node.getSourceFile()
292                 );
293                 changes.rewrite(node, unit);
294                 return null;
295             }
296         });
297         assertFiles("testReplaceLine_ImportFormatTest.pass");
298     }
299
300     public void testSort() throws IOException JavaDoc, FileStateInvalidException {
301         process(new Transformer<Void JavaDoc, Object JavaDoc>() {
302             public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
303                 List JavaDoc<ImportTree> imports = new ArrayList JavaDoc<ImportTree>(node.getImports());
304                 ImportTree oneImport = imports.remove(4);
305                 imports.add(4, make.Import(make.Identifier("java.util.Collection"), false));
306                 Collections.sort(imports, new Comparator JavaDoc() {
307                     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
308                         if (o1 == o2) {
309                             return 0;
310                         }
311                         ImportTree i1 = (ImportTree) o1;
312                         ImportTree i2 = (ImportTree) o2;
313
314                         return i1.toString().compareTo(i2.toString());
315                     }
316
317                     public boolean equals(Object JavaDoc obj) {
318                         return this == obj ? true : false;
319                     }
320                 });
321                 CompilationUnitTree unit = make.CompilationUnit(
322                         node.getPackageName(),
323                         imports,
324                         node.getTypeDecls(),
325                         node.getSourceFile()
326                 );
327                 changes.rewrite(node, unit);
328                 return null;
329             }
330         });
331         assertFiles("testSort_ImportFormatTest.pass");
332     }
333     
334     String JavaDoc getGoldenPckg() {
335         return "org/netbeans/jmi/javamodel/codegen/indent/ImportFormatTest/";
336     }
337
338     String JavaDoc getSourcePckg() {
339         return "org/netbeans/test/codegen/indent/imports/";
340     }
341     
342     private static Tree getParent(Tree t, ASTModel model) {
343         Tree root = model.getRoot();
344         Tree[] tp = model.makePath(root, t);
345         if (tp.length == 0)
346             return null; // tree not found
347
if (tp.length == 1) {
348             assert t == root;
349             return t;
350         }
351         return tp[tp.length - 2];
352     }
353
354 }
Popular Tags