KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.api.java.source.gen;
21
22 import com.sun.source.tree.*;
23 import com.sun.source.util.TreePath;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.lang.model.element.Element;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.lang.model.element.Modifier;
32 import javax.lang.model.element.PackageElement;
33 import javax.lang.model.element.TypeElement;
34 import javax.lang.model.element.VariableElement;
35 import javax.lang.model.type.TypeMirror;
36 import org.netbeans.api.java.source.CancellableTask;
37 import org.netbeans.api.java.source.JavaSource;
38 import org.netbeans.api.java.source.JavaSource.Phase;
39 import org.netbeans.api.java.source.SourceUtils;
40 import org.netbeans.api.java.source.TestUtilities;
41 import org.netbeans.api.java.source.TreeMaker;
42 import org.netbeans.api.java.source.WorkingCopy;
43 import org.netbeans.api.java.source.transform.Transformer;
44 import org.netbeans.junit.NbTestSuite;
45 import org.openide.filesystems.FileStateInvalidException;
46 import org.openide.filesystems.FileUtil;
47
48 /**
49  *
50  * @author Dusan Balek, Jan Lahoda
51  */

52 public class ImportAnalysisTest extends GeneratorTest {
53
54     /** Creates a new instance of ClashingImportsTest */
55     public ImportAnalysisTest(String JavaDoc name) {
56         super(name);
57     }
58     
59     public static NbTestSuite suite() {
60         NbTestSuite suite = new NbTestSuite();
61 // suite.addTestSuite(ImportAnalysisTest.class);
62
suite.addTest(new ImportAnalysisTest("testAddImport1"));
63         suite.addTest(new ImportAnalysisTest("testAddImport2"));
64         suite.addTest(new ImportAnalysisTest("testAddImport3"));
65         suite.addTest(new ImportAnalysisTest("testAddImport4"));
66         suite.addTest(new ImportAnalysisTest("testAddImport5"));
67         suite.addTest(new ImportAnalysisTest("testAddImport6"));
68         suite.addTest(new ImportAnalysisTest("testAddImport7"));
69         suite.addTest(new ImportAnalysisTest("testAddImport8"));
70         suite.addTest(new ImportAnalysisTest("testAddImport9"));
71         suite.addTest(new ImportAnalysisTest("testAddImport10"));
72 // suite.addTest(new ImportAnalysisTest("testAddImport11"));
73
// suite.addTest(new ImportAnalysisTest("testAddImport12"));
74
suite.addTest(new ImportAnalysisTest("testAddImport13"));
75 // suite.addTest(new ImportAnalysisTest("testAddImport14"));
76
suite.addTest(new ImportAnalysisTest("testAddImport15"));
77 // suite.addTest(new ImportAnalysisTest("testAddImport16"));
78
suite.addTest(new ImportAnalysisTest("testAddImport17"));
79         suite.addTest(new ImportAnalysisTest("testAddImport18"));
80         suite.addTest(new ImportAnalysisTest("testAddImportOrder1"));
81         suite.addTest(new ImportAnalysisTest("testAddImportSamePackage"));
82         suite.addTest(new ImportAnalysisTest("testAddImportThroughMethod1"));
83         suite.addTest(new ImportAnalysisTest("testAddImportThroughMethod2"));
84         suite.addTest(new ImportAnalysisTest("testAddImportThroughMethod3"));
85         suite.addTest(new ImportAnalysisTest("testAddImportThroughMethod4"));
86         suite.addTest(new ImportAnalysisTest("testAddImportThroughMethod5"));
87         suite.addTest(new ImportAnalysisTest("testDefaultPackage1"));
88         suite.addTest(new ImportAnalysisTest("testImportAddedAfterThrows"));
89         return suite;
90     }
91     
92     protected void setUp() throws Exception JavaDoc {
93         super.setUp();
94         testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports.java");
95     }
96
97     public void testAddImport1() throws IOException JavaDoc {
98         process(
99             new Transformer<Void JavaDoc, Object JavaDoc>() {
100                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
101                     if ("<init>".contentEquals(node.getName())) {
102                         BlockTree body = node.getBody();
103                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
104                         for(StatementTree st : body.getStatements())
105                             stats.add(st);
106                         TypeElement e = env.getElements().getTypeElement("java.util.List");
107                         ExpressionTree type = make.QualIdent(e);
108                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", type, null));
109                         changes.rewrite(body, make.Block(stats, false));
110                     }
111                     return null;
112                 }
113             }
114         );
115         assertFiles("testAddImport1.pass");
116     }
117
118     public void testAddImport2() throws IOException JavaDoc {
119         process(
120             new Transformer<Void JavaDoc, Object JavaDoc>() {
121                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
122                     if ("<init>".contentEquals(node.getName())) {
123                         BlockTree body = node.getBody();
124                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
125                         for(StatementTree st : body.getStatements())
126                             stats.add(st);
127                         TypeElement list = env.getElements().getTypeElement("java.util.List");
128                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(types.erasure(list.asType())), null));
129                         changes.rewrite(body, make.Block(stats, false));
130                     }
131                     return null;
132                 }
133             }
134         );
135         assertFiles("testAddImport2.pass");
136     }
137     
138     public void testAddImport3() throws IOException JavaDoc {
139         process(
140             new Transformer<Void JavaDoc, Object JavaDoc>() {
141                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
142                     if ("<init>".contentEquals(node.getName())) {
143                         BlockTree body = node.getBody();
144                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
145                         for(StatementTree st : body.getStatements())
146                             stats.add(st);
147                         TypeElement list = env.getElements().getTypeElement("java.util.List");
148                         TypeElement collection = env.getElements().getTypeElement("java.util.Collection");
149                         TypeMirror tm = types.getDeclaredType(list, types.erasure(collection.asType()));
150                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
151                         changes.rewrite(body, make.Block(stats, false));
152                     }
153                     return null;
154                 }
155             }
156         );
157         assertFiles("testAddImport3.pass");
158     }
159     
160     public void testAddImport4() throws IOException JavaDoc {
161         process(
162             new Transformer<Void JavaDoc, Object JavaDoc>() {
163                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
164                     if ("<init>".contentEquals(node.getName())) {
165                         BlockTree body = node.getBody();
166                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
167                         for(StatementTree st : body.getStatements())
168                             stats.add(st);
169                         TypeElement list = env.getElements().getTypeElement("java.util.List");
170                         TypeElement collection = env.getElements().getTypeElement("java.util.Collection");
171                         TypeMirror tm = types.getDeclaredType(list, types.getWildcardType(types.erasure(collection.asType()), null));
172                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
173                         changes.rewrite(body, make.Block(stats, false));
174                     }
175                     return null;
176                 }
177             }
178         );
179         assertFiles("testAddImport4.pass");
180     }
181     
182     public void testAddImport5() throws IOException JavaDoc {
183         process(
184             new Transformer<Void JavaDoc, Object JavaDoc>() {
185                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
186                     if ("<init>".contentEquals(node.getName())) {
187                         BlockTree body = node.getBody();
188                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
189                         for(StatementTree st : body.getStatements())
190                             stats.add(st);
191                         TypeElement list = env.getElements().getTypeElement("java.util.List");
192                         TypeElement collection = env.getElements().getTypeElement("java.util.Collection");
193                         TypeMirror tm = types.getDeclaredType(list, types.getWildcardType(null, types.erasure(collection.asType())));
194                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
195                         changes.rewrite(body, make.Block(stats, false));
196                     }
197                     return null;
198                 }
199             }
200         );
201         assertFiles("testAddImport5.pass");
202     }
203     
204     public void testAddImport6() throws IOException JavaDoc {
205         process(
206             new Transformer<Void JavaDoc, Object JavaDoc>() {
207                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
208                     if ("<init>".contentEquals(node.getName())) {
209                         BlockTree body = node.getBody();
210                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
211                         for(StatementTree st : body.getStatements())
212                             stats.add(st);
213                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
214                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(types.erasure(list.asType())), null));
215                         changes.rewrite(body, make.Block(stats, false));
216                     }
217                     return null;
218                 }
219             }
220         );
221         assertFiles("testAddImport6.pass");
222     }
223     
224     public void testAddImport7() throws IOException JavaDoc {
225         process(
226             new Transformer<Void JavaDoc, Object JavaDoc>() {
227                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
228                     if ("<init>".contentEquals(node.getName())) {
229                         BlockTree body = node.getBody();
230                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
231                         for(StatementTree st : body.getStatements())
232                             stats.add(st);
233                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
234                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.QualIdent(list), null));
235                         changes.rewrite(body, make.Block(stats, false));
236                     }
237                     return null;
238                 }
239             }
240         );
241         assertFiles("testAddImport6.pass"); //the same as testAddImport6, so using only one golden file
242
}
243     
244     public void testAddImport8() throws IOException JavaDoc {
245         process(
246             new Transformer<Void JavaDoc, Object JavaDoc>() {
247                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
248                     if ("<init>".contentEquals(node.getName())) {
249                         BlockTree body = node.getBody();
250                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
251                         for(StatementTree st : body.getStatements())
252                             stats.add(st);
253                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
254                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
255                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
256                         changes.rewrite(body, make.Block(stats, false));
257                     }
258                     return null;
259                 }
260             }
261         );
262         assertFiles("testAddImport8.pass");
263     }
264     
265     public void testAddImport9() throws IOException JavaDoc {
266         process(
267             new Transformer<Void JavaDoc, Object JavaDoc>() {
268                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
269                     if ("<init>".contentEquals(node.getName())) {
270                         BlockTree body = node.getBody();
271                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
272                         for(StatementTree st : body.getStatements())
273                             stats.add(st);
274                         TypeElement list = env.getElements().getTypeElement("java.util.List");
275                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "list1", make.QualIdent(list), null));
276                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "list2", make.QualIdent(list), null));
277                         changes.rewrite(body, make.Block(stats, false));
278                     }
279                     return null;
280                 }
281             }
282         );
283         assertFiles("testAddImport9.pass");
284     }
285     
286     public void testAddImport10() throws IOException JavaDoc {
287         process(
288             new Transformer<Void JavaDoc, Object JavaDoc>() {
289                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
290                     if ("<init>".contentEquals(node.getName())) {
291                         BlockTree body = node.getBody();
292                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
293                         for(StatementTree st : body.getStatements())
294                             stats.add(st);
295                         TypeElement list = env.getElements().getTypeElement("java.util.List");
296                         TypeElement awtList = env.getElements().getTypeElement("java.awt.List");
297                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "list1", make.QualIdent(list), null));
298                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "list2", make.QualIdent(awtList), null));
299                         changes.rewrite(body, make.Block(stats, false));
300                     }
301                     return null;
302                 }
303             }
304         );
305         assertFiles("testAddImport10.pass");
306     }
307     
308     public void testAddImport11() throws IOException JavaDoc {
309         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest1.java");
310         process(
311             new Transformer<Void JavaDoc, Object JavaDoc>() {
312                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
313                     if ("<init>".contentEquals(node.getName())) {
314                         BlockTree body = node.getBody();
315                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
316                         for(StatementTree st : body.getStatements())
317                             stats.add(st);
318                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
319                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
320                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
321                         changes.rewrite(body, make.Block(stats, false));
322                     }
323                     return null;
324                 }
325             }
326         );
327         assertFiles("testAddImport11.pass");
328     }
329     
330     public void testAddImport12() throws IOException JavaDoc {
331         process(
332             new Transformer<Void JavaDoc, Object JavaDoc>() {
333                 public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
334                     TypeElement map = env.getElements().getTypeElement("java.util.Map");
335                     ClassTree nue = make.addClassImplementsClause(node, make.QualIdent(map));
336                     
337                     changes.rewrite(node, nue);
338                     return super.visitClass(node, p);
339                 }
340                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
341                     if ("<init>".contentEquals(node.getName())) {
342                         BlockTree body = node.getBody();
343                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
344                         for(StatementTree st : body.getStatements())
345                             stats.add(st);
346                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
347                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
348                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
349                         changes.rewrite(body, make.Block(stats, false));
350                     }
351                     return null;
352                 }
353             }
354         );
355         assertFiles("testAddImport12.pass");
356     }
357     
358     public void testAddImport13() throws IOException JavaDoc {
359         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest2.java");
360         process(
361             new Transformer<Void JavaDoc, Object JavaDoc>() {
362                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
363                     if ("<init>".contentEquals(node.getName())) {
364                         BlockTree body = node.getBody();
365                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
366                         for(StatementTree st : body.getStatements())
367                             stats.add(st);
368                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
369                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
370                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
371                         changes.rewrite(body, make.Block(stats, false));
372                     }
373                     return null;
374                 }
375             }
376         );
377         assertFiles("testAddImport13.pass");
378     }
379     
380     public void testAddImport14() throws IOException JavaDoc {
381         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest3.java");
382         process(
383             new Transformer<Void JavaDoc, Object JavaDoc>() {
384                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
385                     if ("<init>".contentEquals(node.getName())) {
386                         BlockTree body = node.getBody();
387                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
388                         for(StatementTree st : body.getStatements())
389                             stats.add(st);
390                         TypeElement list = env.getElements().getTypeElement("java.util.Map.Entry");
391                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
392                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
393                         changes.rewrite(body, make.Block(stats, false));
394                     }
395                     return null;
396                 }
397             }
398         );
399         assertFiles("testAddImport14.pass");
400     }
401     
402     public void testAddImport15() throws IOException JavaDoc {
403         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest4.java");
404         process(
405             new Transformer<Void JavaDoc, Object JavaDoc>() {
406                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
407                     if ("<init>".contentEquals(node.getName())) {
408                         BlockTree body = node.getBody();
409                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
410                         for(StatementTree st : body.getStatements())
411                             stats.add(st);
412                         TypeElement list = env.getElements().getTypeElement("java.lang.Math");
413                         ExecutableElement maxMethod = null;
414                         VariableElement pi = null;
415                         for (Element ee : list.getEnclosedElements()) {
416                             if ("max".equals(ee.getSimpleName().toString())) {
417                                 maxMethod = (ExecutableElement) ee;
418                             }
419                             if ("PI".equals(ee.getSimpleName().toString())) {
420                                 pi = (VariableElement) ee;
421                             }
422                         }
423                         
424                         assertNotNull(maxMethod);
425                         assertNotNull(pi);
426                         
427                         stats.add(make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.QualIdent(maxMethod), Arrays.asList(make.QualIdent(pi), make.Literal(2)))));
428                         
429                         changes.rewrite(body, make.Block(stats, false));
430                     }
431                     return null;
432                 }
433             }
434         );
435         assertFiles("testAddImport15.pass");
436     }
437     
438     public void testAddImport16() throws IOException JavaDoc {
439         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest5.java");
440         process(
441             new Transformer<Void JavaDoc, Object JavaDoc>() {
442                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
443                     if ("<init>".contentEquals(node.getName())) {
444                         BlockTree body = node.getBody();
445                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
446                         for(StatementTree st : body.getStatements())
447                             stats.add(st);
448                         TypeElement list = env.getElements().getTypeElement("java.lang.Math");
449                         ExecutableElement maxMethod = null;
450                         ExecutableElement minMethod = null;
451                         VariableElement pi = null;
452                         for (Element ee : list.getEnclosedElements()) {
453                             if ("max".equals(ee.getSimpleName().toString())) {
454                                 maxMethod = (ExecutableElement) ee;
455                             }
456                             if ("min".equals(ee.getSimpleName().toString())) {
457                                 minMethod = (ExecutableElement) ee;
458                             }
459                             if ("PI".equals(ee.getSimpleName().toString())) {
460                                 pi = (VariableElement) ee;
461                             }
462                         }
463                         
464                         assertNotNull(maxMethod);
465                         assertNotNull(minMethod);
466                         assertNotNull(pi);
467                         
468                         stats.add(make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.QualIdent(maxMethod), Arrays.asList(make.QualIdent(pi), make.Literal(2)))));
469                         stats.add(make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.QualIdent(minMethod), Arrays.asList(make.QualIdent(pi), make.Literal(2)))));
470                         
471                         changes.rewrite(body, make.Block(stats, false));
472                     }
473                     return null;
474                 }
475             }
476         );
477         assertFiles("testAddImport16.pass");
478     }
479     
480     public void testAddImport17() throws IOException JavaDoc {
481         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest6.java");
482         process(
483             new Transformer<Void JavaDoc, Object JavaDoc>() {
484                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
485                     if ("<init>".contentEquals(node.getName())) {
486                         BlockTree body = node.getBody();
487                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
488                         for(StatementTree st : body.getStatements())
489                             stats.add(st);
490                         TypeElement list = env.getElements().getTypeElement("java.lang.String");
491                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
492                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
493                         changes.rewrite(body, make.Block(stats, false));
494                     }
495                     return null;
496                 }
497             }
498         );
499         assertFiles("testAddImport17.pass");
500     }
501     
502     public void testAddImport18() throws IOException JavaDoc {
503         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest6.java");
504         process(
505             new Transformer<Void JavaDoc, Object JavaDoc>() {
506                 public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
507                     ExpressionTree pack = node.getPackageName();
508                     PackageElement pe = env.getElements().getPackageElement("org.netbeans.test");
509                     ExpressionTree nuePack = make.QualIdent(pe);
510                     
511                     changes.rewrite(pack, nuePack);
512                     return null;
513                 }
514             }
515         );
516         assertFiles("testAddImport18.pass");
517     }
518     
519     public void testAddImportOrder1() throws IOException JavaDoc {
520         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest7.java");
521         process(
522             new Transformer<Void JavaDoc, Object JavaDoc>() {
523                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
524                     if ("<init>".contentEquals(node.getName())) {
525                         BlockTree body = node.getBody();
526                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
527                         for(StatementTree st : body.getStatements())
528                             stats.add(st);
529                         TypeElement list = env.getElements().getTypeElement("java.util.LinkedList");
530                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
531                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
532                         changes.rewrite(body, make.Block(stats, false));
533                     }
534                     return null;
535                 }
536             }
537         );
538         assertFiles("testAddImportOrder1.pass");
539     }
540     
541     public void testAddImportSamePackage() throws IOException JavaDoc {
542         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest7.java");
543         process(
544             new Transformer<Void JavaDoc, Object JavaDoc>() {
545                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
546                     if ("<init>".contentEquals(node.getName())) {
547                         BlockTree body = node.getBody();
548                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
549                         for(StatementTree st : body.getStatements())
550                             stats.add(st);
551                         TypeElement list = env.getElements().getTypeElement("org.netbeans.test.codegen.ImportsTest6");
552                         TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
553                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
554                         changes.rewrite(body, make.Block(stats, false));
555                     }
556                     return null;
557                 }
558             }
559         );
560         assertFiles("testAddImportSamePackage.pass");
561     }
562     
563     //XXX: more tests default package
564
public void testDefaultPackage1() throws IOException JavaDoc {
565         testFile = getFile(getSourceDir(), "ImportAnalysisDefaultPackage1.java");
566         process(
567                 new Transformer<Void JavaDoc, Object JavaDoc>() {
568             public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
569                 if ("<init>".contentEquals(node.getName())) {
570                     BlockTree body = node.getBody();
571                     List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
572                     for(StatementTree st : body.getStatements())
573                         stats.add(st);
574                     TypeElement exc = env.getElements().getTypeElement("ImportAnalysisDefaultPackage2");
575                     
576                     stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.QualIdent(exc), null));
577                     
578                     changes.rewrite(body, make.Block(stats, false));
579                 }
580                 return null;
581             }
582         }
583         );
584         assertFiles("testDefaultPackage1.pass");
585     }
586     
587     public void testImportAddedAfterThrows() throws IOException JavaDoc {
588         testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest7.java");
589         process(
590                 new Transformer<Void JavaDoc, Object JavaDoc>() {
591             public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
592                 if ("<init>".contentEquals(node.getName())) {
593                     TypeElement exc = env.getElements().getTypeElement("javax.swing.text.BadLocationException");
594                     MethodTree nueMethod = make.addMethodThrows(node, (ExpressionTree) make.Type(exc.asType()));
595                     
596                     changes.rewrite(node, nueMethod);
597                 }
598                 return null;
599             }
600         }
601         );
602         assertFiles("testImportAddedAfterThrows.pass");
603     }
604     
605     public void testAddImportThroughMethod1() throws IOException JavaDoc {
606         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
607         CancellableTask task = new CancellableTask<WorkingCopy>() {
608
609             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
610                 workingCopy.toPhase(Phase.RESOLVED);
611                 TreeMaker make = workingCopy.getTreeMaker();
612                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
613                 MethodTree node = (MethodTree) clazz.getMembers().get(0);
614                 int offset = (int) (workingCopy.getTrees().getSourcePositions().getStartPosition(workingCopy.getCompilationUnit(), node) + 1);
615                 TreePath context = workingCopy.getTreeUtilities().pathFor(offset);
616                 try {
617                     assertEquals("List", SourceUtils.resolveImport(workingCopy, context, "java.util.List"));
618                     assertEquals("java.awt.List", SourceUtils.resolveImport(workingCopy, context, "java.awt.List"));
619                 } catch (IOException JavaDoc e) {
620                     throw new IllegalStateException JavaDoc(e);
621                 }
622             }
623             
624             public void cancel() {
625             }
626         };
627         testSource.runModificationTask(task).commit();
628         String JavaDoc res = TestUtilities.copyFileToString(testFile);
629         System.err.println(res);
630         assertFiles("testAddImportThroughMethod1.pass");
631     }
632     
633     public void testAddImportThroughMethod2() throws IOException JavaDoc {
634         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
635         CancellableTask task = new CancellableTask<WorkingCopy>() {
636
637             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
638                 workingCopy.toPhase(Phase.RESOLVED);
639                 TreeMaker make = workingCopy.getTreeMaker();
640                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
641                 MethodTree node = (MethodTree) clazz.getMembers().get(0);
642                 int offset = (int) (workingCopy.getTrees().getSourcePositions().getStartPosition(workingCopy.getCompilationUnit(), node) + 1);
643                 TreePath context = workingCopy.getTreeUtilities().pathFor(offset);
644                 try {
645                     assertEquals("List", SourceUtils.resolveImport(workingCopy, context, "java.util.List"));
646                     assertEquals("java.awt.List", SourceUtils.resolveImport(workingCopy, context, "java.awt.List"));
647                 } catch (IOException JavaDoc e) {
648                     throw new IllegalStateException JavaDoc(e);
649                 }
650             }
651             
652             public void cancel() {
653             }
654         };
655         testSource.runModificationTask(task).commit();
656         String JavaDoc res = TestUtilities.copyFileToString(testFile);
657         System.err.println(res);
658         assertFiles("testAddImportThroughMethod2.pass");
659     }
660     
661     public void testAddImportThroughMethod3() throws IOException JavaDoc {
662         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
663         CancellableTask task = new CancellableTask<WorkingCopy>() {
664
665             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
666                 workingCopy.toPhase(Phase.RESOLVED);
667                 TreeMaker make = workingCopy.getTreeMaker();
668                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
669                 MethodTree node = (MethodTree) clazz.getMembers().get(0);
670                 int offset = (int) (workingCopy.getTrees().getSourcePositions().getStartPosition(workingCopy.getCompilationUnit(), node) + 1);
671                 TreePath context = workingCopy.getTreeUtilities().pathFor(offset);
672                 try {
673                     assertEquals("List", SourceUtils.resolveImport(workingCopy, context, "java.util.List"));
674                     assertEquals("Map", SourceUtils.resolveImport(workingCopy, context, "java.util.Map"));
675                     assertEquals("java.awt.List", SourceUtils.resolveImport(workingCopy, context, "java.awt.List"));
676                 } catch (IOException JavaDoc e) {
677                     throw new IllegalStateException JavaDoc(e);
678                 }
679             }
680             
681             public void cancel() {
682             }
683         };
684         testSource.runModificationTask(task).commit();
685         String JavaDoc res = TestUtilities.copyFileToString(testFile);
686         System.err.println(res);
687         assertFiles("testAddImportThroughMethod3.pass");
688     }
689     
690     public void testAddImportThroughMethod4() throws IOException JavaDoc {
691         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
692         CancellableTask task = new CancellableTask<WorkingCopy>() {
693
694             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
695                 workingCopy.toPhase(Phase.RESOLVED);
696                 TreeMaker make = workingCopy.getTreeMaker();
697                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
698                 MethodTree node = (MethodTree) clazz.getMembers().get(0);
699                 int offset = (int) (workingCopy.getTrees().getSourcePositions().getStartPosition(workingCopy.getCompilationUnit(), node) + 1);
700                 TreePath context = workingCopy.getTreeUtilities().pathFor(offset);
701                 try {
702                     assertEquals("SuperClassTest", SourceUtils.resolveImport(workingCopy, context, "org.netbeans.test.codegen.SuperClassTest"));
703                 } catch (IOException JavaDoc e) {
704                     throw new IllegalStateException JavaDoc(e);
705                 }
706             }
707             
708             public void cancel() {
709             }
710         };
711         testSource.runModificationTask(task).commit();
712         String JavaDoc res = TestUtilities.copyFileToString(testFile);
713         System.err.println(res);
714         assertFiles("testAddImportThroughMethod4.pass");
715     }
716
717     public void testAddImportThroughMethod5() throws IOException JavaDoc {
718         JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
719         CancellableTask task = new CancellableTask<WorkingCopy>() {
720
721             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
722                 workingCopy.toPhase(Phase.RESOLVED);
723                 TreeMaker make = workingCopy.getTreeMaker();
724                 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
725                 MethodTree node = (MethodTree) clazz.getMembers().get(0);
726                 int offset = (int) (workingCopy.getTrees().getSourcePositions().getStartPosition(workingCopy.getCompilationUnit(), node) + 1);
727                 TreePath context = workingCopy.getTreeUtilities().pathFor(offset);
728                 try {
729                     assertEquals("SuperClassTest.FirstInnerClass", SourceUtils.resolveImport(workingCopy, context, "org.netbeans.test.codegen.SuperClassTest.FirstInnerClass"));
730                 } catch (IOException JavaDoc e) {
731                     throw new IllegalStateException JavaDoc(e);
732                 }
733             }
734             
735             public void cancel() {
736             }
737         };
738         testSource.runModificationTask(task).commit();
739         String JavaDoc res = TestUtilities.copyFileToString(testFile);
740         System.err.println(res);
741         assertFiles("testAddImportThroughMethod4.pass");
742     }
743     
744     String JavaDoc getSourcePckg() {
745         if ("testDefaultPackage1".equals(getName())) {
746             return "";
747         } else {
748             return "org/netbeans/test/codegen/";
749         }
750     }
751
752     String JavaDoc getGoldenPckg() {
753         return "org/netbeans/jmi/javamodel/codegen/ImportAnalysisTest/";
754     }
755
756     void assertFiles(final String JavaDoc aGoldenFile) throws IOException JavaDoc, FileStateInvalidException {
757         assertFile("File is not correctly generated.",
758                 getTestFile(),
759                 getFile(getGoldenDir(), getGoldenPckg() + aGoldenFile),
760                 getWorkDir(),
761                 new WhitespaceIgnoringDiff()
762                 );
763     }
764 }
765
Popular Tags