KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 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.MemberSelectTree;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.netbeans.api.java.source.CancellableTask;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.api.java.source.JavaSource.Phase;
31 import org.netbeans.api.java.source.TestUtilities;
32 import org.netbeans.api.java.source.TreeMaker;
33 import org.netbeans.api.java.source.WorkingCopy;
34
35 import org.netbeans.junit.NbTestSuite;
36
37 /**
38  * Tests imports matching and its correct adding/removing. Just generator
39  * test, does not do anything with import analysis.
40  *
41  * @author Pavel Flaska
42  */

43 public class ImportsTest extends GeneratorTestMDRCompat {
44     
45     /** Creates a new instance of MethodParametersTest */
46     public ImportsTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     public static NbTestSuite suite() {
51         NbTestSuite suite = new NbTestSuite();
52         suite.addTestSuite(ImportsTest.class);
53 // suite.addTest(new ImportsTest("testAddFirst"));
54
// suite.addTest(new ImportsTest("testAddFirstAgain"));
55
// suite.addTest(new ImportsTest("testAddSecondImport"));
56
// suite.addTest(new ImportsTest("testAddSecondImportWithEndLineCmt"));
57
// suite.addTest(new ImportsTest("testAddTwoImportsOrigWithComment"));
58
// suite.addTest(new ImportsTest("testAddBetweenImports"));
59
// suite.addTest(new ImportsTest("testRemoveBetweenImportsWithLineEndComment"));
60
// suite.addTest(new ImportsTest("testRemoveAllImports"));
61
// suite.addTest(new ImportsTest("testRemoveAllImports2"));
62
// suite.addTest(new ImportsTest("testAddFirstTwo"));
63
// suite.addTest(new ImportsTest("testAddFirstToExisting"));
64
// suite.addTest(new ImportsTest("testRemoveInnerImport"));
65
// suite.addTest(new ImportsTest("testEmptyLines"));
66
// suite.addTest(new ImportsTest("testIndentedImport"));
67
// suite.addTest(new ImportsTest("testIndentedImport2"));
68
// suite.addTest(new ImportsTest("testUnformatted"));
69
// suite.addTest(new ImportsTest("testMissingNewLine"));
70
// suite.addTest(new ImportsTest("testRemoveAllInDefault"));
71
// suite.addTest(new ImportsTest("testRemoveAllInDefault2"));
72
// suite.addTest(new ImportsTest("testRemoveAfterEmpty"));
73
// suite.addTest(new ImportsTest("testRemoveBeforeEmpty"));
74
// suite.addTest(new ImportsTest("testRenameIdentifier"));
75
return suite;
76     }
77
78     public void testAddFirst() throws Exception JavaDoc {
79         testFile = new File JavaDoc(getWorkDir(), "Test.java");
80         TestUtilities.copyStringToFile(testFile,
81             "package hierbas.del.litoral;\n" +
82             "\n" +
83             "public class Test {\n" +
84             " public void taragui() {\n" +
85             " }\n" +
86             "}\n"
87             );
88         String JavaDoc golden =
89             "package hierbas.del.litoral;\n" +
90             "\n" +
91             "import java.io.IOException;\n" +
92             "\n" +
93             "public class Test {\n" +
94             " public void taragui() {\n" +
95             " }\n" +
96             "}\n";
97
98         JavaSource src = getJavaSource(testFile);
99         CancellableTask task = new CancellableTask<WorkingCopy>() {
100
101             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
102                 workingCopy.toPhase(Phase.RESOLVED);
103                 TreeMaker make = workingCopy.getTreeMaker();
104                 CompilationUnitTree node = workingCopy.getCompilationUnit();
105                 CompilationUnitTree copy = make.addCompUnitImport(
106                        node,
107                        make.Import(make.Identifier("java.io.IOException"), false)
108                 );
109                 workingCopy.rewrite(node, copy);
110             }
111
112             public void cancel() {
113             }
114         };
115         src.runModificationTask(task).commit();
116         String JavaDoc res = TestUtilities.copyFileToString(testFile);
117         System.err.println(res);
118         assertEquals(golden, res);
119     }
120     
121     public void testAddFirstAgain() throws Exception JavaDoc {
122         testFile = new File JavaDoc(getWorkDir(), "Test.java");
123         TestUtilities.copyStringToFile(testFile,
124             "package hierbas.del.litoral;\n" +
125             "public class Test {\n" +
126             " public void taragui() {\n" +
127             " }\n" +
128             "}\n"
129             );
130         String JavaDoc golden =
131             "package hierbas.del.litoral;\n\n" +
132             "import java.io.IOException;\n" +
133             "public class Test {\n" +
134             " public void taragui() {\n" +
135             " }\n" +
136             "}\n";
137
138         JavaSource src = getJavaSource(testFile);
139         CancellableTask task = new CancellableTask<WorkingCopy>() {
140
141             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
142                 workingCopy.toPhase(Phase.RESOLVED);
143                 TreeMaker make = workingCopy.getTreeMaker();
144                 CompilationUnitTree node = workingCopy.getCompilationUnit();
145                 CompilationUnitTree copy = make.addCompUnitImport(
146                         node,
147                         make.Import(make.Identifier("java.io.IOException"), false)
148                 );
149                 workingCopy.rewrite(node, copy);
150             }
151
152             public void cancel() {
153             }
154         };
155         src.runModificationTask(task).commit();
156         String JavaDoc res = TestUtilities.copyFileToString(testFile);
157         System.err.println(res);
158         assertEquals(golden, res);
159     }
160     
161     public void testAddFirstToExisting() throws Exception JavaDoc {
162         testFile = new File JavaDoc(getWorkDir(), "Test.java");
163         TestUtilities.copyStringToFile(testFile,
164             "package hierbas.del.litoral;\n" +
165             "\n" +
166             "import java.lang.NullPointerException;\n" +
167             "\n" +
168             "public class Test {\n" +
169             " public void taragui() {\n" +
170             " }\n" +
171             "}\n"
172             );
173         String JavaDoc golden =
174             "package hierbas.del.litoral;\n" +
175             "\n" +
176             "import java.io.IOException;\n" +
177             "import java.lang.NullPointerException;\n" +
178             "\n" +
179             "public class Test {\n" +
180             " public void taragui() {\n" +
181             " }\n" +
182             "}\n";
183
184         JavaSource src = getJavaSource(testFile);
185         CancellableTask task = new CancellableTask<WorkingCopy>() {
186
187             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
188                 workingCopy.toPhase(Phase.RESOLVED);
189                 TreeMaker make = workingCopy.getTreeMaker();
190                 CompilationUnitTree node = workingCopy.getCompilationUnit();
191                 CompilationUnitTree copy = make.insertCompUnitImport(
192                         node,
193                         0,
194                         make.Import(make.Identifier("java.io.IOException"), false)
195                 );
196                 workingCopy.rewrite(node, copy);
197             }
198
199             public void cancel() {
200             }
201         };
202         src.runModificationTask(task).commit();
203         String JavaDoc res = TestUtilities.copyFileToString(testFile);
204         System.err.println(res);
205         assertEquals(golden, res);
206     }
207     
208     public void testAddFirstTwo() throws Exception JavaDoc {
209         testFile = new File JavaDoc(getWorkDir(), "Test.java");
210         TestUtilities.copyStringToFile(testFile,
211             "package hierbas.del.litoral;\n\n" +
212             "public class Test {\n" +
213             " public void taragui() {\n" +
214             " }\n" +
215             "}\n"
216             );
217         String JavaDoc golden =
218             "package hierbas.del.litoral;\n\n" +
219             "import java.io.IOException;\n" +
220             "import java.util.List;\n\n" +
221             "public class Test {\n" +
222             " public void taragui() {\n" +
223             " }\n" +
224             "}\n";
225
226         JavaSource src = getJavaSource(testFile);
227         CancellableTask task = new CancellableTask<WorkingCopy>() {
228
229             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
230                 workingCopy.toPhase(Phase.RESOLVED);
231                 TreeMaker make = workingCopy.getTreeMaker();
232                 CompilationUnitTree node = workingCopy.getCompilationUnit();
233                 CompilationUnitTree copy = make.addCompUnitImport(
234                         node,
235                         make.Import(make.Identifier("java.io.IOException"), false)
236                 );
237                 copy = make.addCompUnitImport(
238                         copy,
239                         make.Import(make.Identifier("java.util.List"), false)
240                 );
241                 workingCopy.rewrite(node, copy);
242             }
243
244             public void cancel() {
245             }
246         };
247         src.runModificationTask(task).commit();
248         String JavaDoc res = TestUtilities.copyFileToString(testFile);
249         System.err.println(res);
250         assertEquals(golden, res);
251     }
252     
253     public void testAddFirstTwoAgain() throws Exception JavaDoc {
254         testFile = new File JavaDoc(getWorkDir(), "Test.java");
255         TestUtilities.copyStringToFile(testFile,
256             "package hierbas.del.litoral;\n\n" +
257             "/** javadoc comment */\n" +
258             "public class Test {\n" +
259             " public void taragui() {\n" +
260             " }\n" +
261             "}\n"
262             );
263         String JavaDoc golden =
264             "package hierbas.del.litoral;\n\n" +
265             "import java.io.IOException;\n" +
266             "import java.util.List;\n\n" +
267             "/** javadoc comment */\n" +
268             "public class Test {\n" +
269             " public void taragui() {\n" +
270             " }\n" +
271             "}\n";
272
273         JavaSource src = getJavaSource(testFile);
274         CancellableTask task = new CancellableTask<WorkingCopy>() {
275
276             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
277                 workingCopy.toPhase(Phase.RESOLVED);
278                 TreeMaker make = workingCopy.getTreeMaker();
279                 CompilationUnitTree node = workingCopy.getCompilationUnit();
280                 CompilationUnitTree copy = make.addCompUnitImport(
281                         node,
282                         make.Import(make.Identifier("java.io.IOException"), false)
283                 );
284                 copy = make.addCompUnitImport(
285                         copy,
286                         make.Import(make.Identifier("java.util.List"), false)
287                 );
288                 workingCopy.rewrite(node, copy);
289             }
290
291             public void cancel() {
292             }
293         };
294         src.runModificationTask(task).commit();
295         String JavaDoc res = TestUtilities.copyFileToString(testFile);
296         System.err.println(res);
297         assertEquals(golden, res);
298     }
299     
300     public void testAddSecondImport() throws Exception JavaDoc {
301         testFile = new File JavaDoc(getWorkDir(), "Test.java");
302         TestUtilities.copyStringToFile(testFile,
303             "package hierbas.del.litoral;\n" +
304             "\n" +
305             "import java.io.IOException;\n" +
306             "\n" +
307             "public class Test {\n" +
308             " public void taragui() {\n" +
309             " }\n" +
310             "}\n"
311             );
312         String JavaDoc golden =
313             "package hierbas.del.litoral;\n\n" +
314             "import java.io.IOException;\n" +
315             "import java.util.List;\n\n" +
316             "public class Test {\n" +
317             " public void taragui() {\n" +
318             " }\n" +
319             "}\n";
320
321         JavaSource src = getJavaSource(testFile);
322         CancellableTask task = new CancellableTask<WorkingCopy>() {
323
324             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
325                 workingCopy.toPhase(Phase.RESOLVED);
326                 TreeMaker make = workingCopy.getTreeMaker();
327                 CompilationUnitTree node = workingCopy.getCompilationUnit();
328                 CompilationUnitTree copy = make.addCompUnitImport(
329                         node,
330                         make.Import(make.Identifier("java.util.List"), false)
331                 );
332                 workingCopy.rewrite(node, copy);
333             }
334
335             public void cancel() {
336             }
337         };
338         src.runModificationTask(task).commit();
339         String JavaDoc res = TestUtilities.copyFileToString(testFile);
340         assertEquals(golden, res);
341     }
342     
343     public void testAddSecondImportWithEndLineCmt() throws Exception JavaDoc {
344         testFile = new File JavaDoc(getWorkDir(), "Test.java");
345         TestUtilities.copyStringToFile(testFile,
346             "package hierbas.del.litoral;\n\n" +
347             "import java.io.IOException; // aa\n\n" +
348             "public class Test {\n" +
349             " public void taragui() {\n" +
350             " }\n" +
351             "}\n"
352             );
353         String JavaDoc golden =
354             "package hierbas.del.litoral;\n\n" +
355             "import java.io.IOException; // aa\n" +
356             "import java.util.List;\n\n" +
357             "public class Test {\n" +
358             " public void taragui() {\n" +
359             " }\n" +
360             "}\n";
361
362         JavaSource src = getJavaSource(testFile);
363         CancellableTask task = new CancellableTask<WorkingCopy>() {
364
365             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
366                 workingCopy.toPhase(Phase.RESOLVED);
367                 TreeMaker make = workingCopy.getTreeMaker();
368                 CompilationUnitTree node = workingCopy.getCompilationUnit();
369                 CompilationUnitTree copy = make.addCompUnitImport(
370                         node,
371                         make.Import(make.Identifier("java.util.List"), false)
372                 );
373                 workingCopy.rewrite(node, copy);
374             }
375
376             public void cancel() {
377             }
378         };
379         src.runModificationTask(task).commit();
380         String JavaDoc res = TestUtilities.copyFileToString(testFile);
381         System.err.println(res);
382         assertEquals(golden, res);
383     }
384     
385     public void testAddTwoImportsOrigWithComment() throws Exception JavaDoc {
386         testFile = new File JavaDoc(getWorkDir(), "Test.java");
387         TestUtilities.copyStringToFile(testFile,
388             "package hierbas.del.litoral;\n\n" +
389             "import java.io.IOException; // yerba mate\n\n" +
390             "public class Test {\n" +
391             " public void taragui() {\n" +
392             " }\n" +
393             "}\n"
394             );
395         String JavaDoc golden =
396             "package hierbas.del.litoral;\n\n" +
397             "import java.io.IOException; // yerba mate\n" +
398             "import java.util.List;\n" +
399             "import java.util.Collections;\n\n" +
400             "public class Test {\n" +
401             " public void taragui() {\n" +
402             " }\n" +
403             "}\n";
404
405         JavaSource src = getJavaSource(testFile);
406         CancellableTask task = new CancellableTask<WorkingCopy>() {
407
408             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
409                 workingCopy.toPhase(Phase.RESOLVED);
410                 TreeMaker make = workingCopy.getTreeMaker();
411                 CompilationUnitTree node = workingCopy.getCompilationUnit();
412                 CompilationUnitTree copy = make.addCompUnitImport(
413                         node,
414                         make.Import(make.Identifier("java.util.List"), false)
415                 );
416                 copy = make.addCompUnitImport(
417                         copy,
418                         make.Import(make.Identifier("java.util.Collections"), false)
419                 );
420                 workingCopy.rewrite(node, copy);
421             }
422
423             public void cancel() {
424             }
425         };
426         src.runModificationTask(task).commit();
427         String JavaDoc res = TestUtilities.copyFileToString(testFile);
428         System.err.println(res);
429         assertEquals(golden, res);
430     }
431     
432     public void testAddBetweenImports() throws Exception JavaDoc {
433         testFile = new File JavaDoc(getWorkDir(), "Test.java");
434         TestUtilities.copyStringToFile(testFile,
435             "package hierbas.del.litoral;\n" +
436             "\n" +
437             "import java.io.IOException; // yerba mate\n" +
438             "import java.util.List;\n" +
439             "import java.util.Collections;\n" +
440             "\n" +
441             "public class Test {\n" +
442             " public void taragui() {\n" +
443             " }\n" +
444             "}\n");
445         String JavaDoc golden =
446             "package hierbas.del.litoral;\n" +
447             "\n" +
448             "import java.io.IOException; // yerba mate\n" +
449             "import java.util.ArrayList;\n" +
450             "import java.util.List;\n" +
451             "import java.util.LinkedList;\n" +
452             "import java.util.Collections;\n" +
453             "\n" +
454             "public class Test {\n" +
455             " public void taragui() {\n" +
456             " }\n" +
457             "}\n";
458
459         JavaSource src = getJavaSource(testFile);
460         CancellableTask task = new CancellableTask<WorkingCopy>() {
461
462             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
463                 workingCopy.toPhase(Phase.RESOLVED);
464                 TreeMaker make = workingCopy.getTreeMaker();
465                 CompilationUnitTree node = workingCopy.getCompilationUnit();
466                 CompilationUnitTree copy = make.insertCompUnitImport(
467                         node, 1,
468                         make.Import(make.Identifier("java.util.ArrayList"), false)
469                 );
470                 copy = make.insertCompUnitImport(
471                         copy, 3,
472                         make.Import(make.Identifier("java.util.LinkedList"), false)
473                 );
474                 workingCopy.rewrite(node, copy);
475             }
476
477             public void cancel() {
478             }
479         };
480         src.runModificationTask(task).commit();
481         String JavaDoc res = TestUtilities.copyFileToString(testFile);
482         System.err.println(res);
483         assertEquals(golden, res);
484     }
485     
486     public void testRemoveBetweenImportsWithLineEndComment() throws Exception JavaDoc {
487         testFile = new File JavaDoc(getWorkDir(), "Test.java");
488         TestUtilities.copyStringToFile(testFile,
489             "package hierbas.del.litoral;\n\n" +
490             "import java.io.IOException;\n" +
491             "import java.util.ArrayList; // polovy seznam\n" +
492             "import java.util.List; // yerba mate\n" +
493             "import java.util.LinkedList;\n" +
494             "import java.util.Collections;\n\n" +
495             "public class Test {\n" +
496             " public void taragui() {\n" +
497             " }\n" +
498             "}\n");
499         String JavaDoc golden =
500             "package hierbas.del.litoral;\n\n" +
501             "import java.io.IOException;\n" +
502             "import java.util.ArrayList; // polovy seznam\n" +
503             "import java.util.LinkedList;\n" +
504             "import java.util.Collections;\n\n" +
505             "public class Test {\n" +
506             " public void taragui() {\n" +
507             " }\n" +
508             "}\n";
509
510         JavaSource src = getJavaSource(testFile);
511         CancellableTask task = new CancellableTask<WorkingCopy>() {
512
513             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
514                 workingCopy.toPhase(Phase.RESOLVED);
515                 TreeMaker make = workingCopy.getTreeMaker();
516                 CompilationUnitTree node = workingCopy.getCompilationUnit();
517                 CompilationUnitTree copy = make.removeCompUnitImport(node, 2);
518                 workingCopy.rewrite(node, copy);
519             }
520
521             public void cancel() {
522             }
523         };
524         src.runModificationTask(task).commit();
525         String JavaDoc res = TestUtilities.copyFileToString(testFile);
526         System.err.println(res);
527         assertEquals(golden, res);
528     }
529     
530     public void testRemoveInnerImport() throws Exception JavaDoc {
531         testFile = new File JavaDoc(getWorkDir(), "Test.java");
532         TestUtilities.copyStringToFile(testFile,
533             "package hierbas.del.litoral;\n\n" +
534             "import java.io.IOException;\n" +
535             "import java.util.ArrayList;\n" +
536             "import java.util.List;\n" +
537             "import java.util.LinkedList;\n" +
538             "import java.util.Collections;\n\n" +
539             "public class Test {\n" +
540             " public void taragui() {\n" +
541             " }\n" +
542             "}\n");
543         String JavaDoc golden =
544             "package hierbas.del.litoral;\n\n" +
545             "import java.io.IOException;\n" +
546             "import java.util.ArrayList;\n" +
547             "import java.util.LinkedList;\n" +
548             "import java.util.Collections;\n\n" +
549             "public class Test {\n" +
550             " public void taragui() {\n" +
551             " }\n" +
552             "}\n";
553
554         JavaSource src = getJavaSource(testFile);
555         CancellableTask task = new CancellableTask<WorkingCopy>() {
556
557             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
558                 workingCopy.toPhase(Phase.RESOLVED);
559                 TreeMaker make = workingCopy.getTreeMaker();
560                 CompilationUnitTree node = workingCopy.getCompilationUnit();
561                 CompilationUnitTree copy = make.removeCompUnitImport(node, 2);
562                 workingCopy.rewrite(node, copy);
563             }
564
565             public void cancel() {
566             }
567         };
568         src.runModificationTask(task).commit();
569         String JavaDoc res = TestUtilities.copyFileToString(testFile);
570         System.err.println(res);
571         assertEquals(golden, res);
572     }
573     
574     public void testRemoveAllImports() throws Exception JavaDoc {
575         testFile = new File JavaDoc(getWorkDir(), "Test.java");
576         TestUtilities.copyStringToFile(testFile,
577             "package hierbas.del.litoral;\n" +
578             "\n" +
579             "import java.util.ArrayList; // polovy seznam\n" +
580             "import java.util.List; // yerba mate\n" +
581             "import java.util.Collections;\n" +
582             "\n" +
583             "public class Test {\n" +
584             " public void taragui() {\n" +
585             " }\n" +
586             "}\n");
587         String JavaDoc golden =
588             "package hierbas.del.litoral;\n" +
589             "\n" +
590             "public class Test {\n" +
591             " public void taragui() {\n" +
592             " }\n" +
593             "}\n";
594
595         JavaSource src = getJavaSource(testFile);
596         CancellableTask task = new CancellableTask<WorkingCopy>() {
597
598             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
599                 workingCopy.toPhase(Phase.RESOLVED);
600                 TreeMaker make = workingCopy.getTreeMaker();
601                 CompilationUnitTree node = workingCopy.getCompilationUnit();
602                 CompilationUnitTree copy = make.removeCompUnitImport(node, 0);
603                 copy = make.removeCompUnitImport(copy, 0);
604                 copy = make.removeCompUnitImport(copy, 0);
605                 workingCopy.rewrite(node, copy);
606             }
607
608             public void cancel() {
609             }
610         };
611         src.runModificationTask(task).commit();
612         String JavaDoc res = TestUtilities.copyFileToString(testFile);
613         System.err.println(res);
614         assertEquals(golden, res);
615     }
616     
617     public void testRemoveAllImports2() throws Exception JavaDoc {
618         testFile = new File JavaDoc(getWorkDir(), "Test.java");
619         TestUtilities.copyStringToFile(testFile,
620             "package hierbas.del.litoral;\n" +
621             "\n" +
622             "import java.util.ArrayList; // polovy seznam\n" +
623             "import java.util.List; // yerba mate\n" +
624             "import java.util.Collections;\n" +
625             "\n" +
626             "/**\n" +
627             " * What?\n" +
628             " */\n" +
629             "public class Test {\n" +
630             " public void taragui() {\n" +
631             " }\n" +
632             "}\n");
633         String JavaDoc golden =
634             "package hierbas.del.litoral;\n" +
635             "\n" +
636             "/**\n" +
637             " * What?\n" +
638             " */\n" +
639             "public class Test {\n" +
640             " public void taragui() {\n" +
641             " }\n" +
642             "}\n";
643
644         JavaSource src = getJavaSource(testFile);
645         CancellableTask task = new CancellableTask<WorkingCopy>() {
646
647             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
648                 workingCopy.toPhase(Phase.RESOLVED);
649                 TreeMaker make = workingCopy.getTreeMaker();
650                 CompilationUnitTree node = workingCopy.getCompilationUnit();
651                 CompilationUnitTree copy = make.removeCompUnitImport(node, 0);
652                 copy = make.removeCompUnitImport(copy, 0);
653                 copy = make.removeCompUnitImport(copy, 0);
654                 workingCopy.rewrite(node, copy);
655             }
656
657             public void cancel() {
658             }
659         };
660         src.runModificationTask(task).commit();
661         String JavaDoc res = TestUtilities.copyFileToString(testFile);
662         System.err.println(res);
663         assertEquals(golden, res);
664     }
665     
666     public void testUnformatted() throws Exception JavaDoc {
667         testFile = new File JavaDoc(getWorkDir(), "Test.java");
668         TestUtilities.copyStringToFile(testFile,
669             "package hierbas.del.litoral;" +
670             "import java.util.ArrayList; // polovy seznam\n" +
671             "import java.util.List; // yerba mate\n" +
672             "import java.util.Collections;" +
673             "public class Test {\n" +
674             " public void taragui() {\n" +
675             " }\n" +
676             "}\n");
677         String JavaDoc golden =
678             "package hierbas.del.litoral;" +
679             "import java.util.List; // yerba mate\n" +
680             "import java.util.Collections;" +
681             "import static java.util.Arrays;\n" +
682             "public class Test {\n" +
683             " public void taragui() {\n" +
684             " }\n" +
685             "}\n";
686
687         JavaSource src = getJavaSource(testFile);
688         CancellableTask task = new CancellableTask<WorkingCopy>() {
689
690             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
691                 workingCopy.toPhase(Phase.RESOLVED);
692                 TreeMaker make = workingCopy.getTreeMaker();
693                 CompilationUnitTree node = workingCopy.getCompilationUnit();
694                 CompilationUnitTree copy = make.removeCompUnitImport(node, 0);
695                 copy = make.addCompUnitImport(copy, make.Import(make.Identifier("java.util.Arrays"), true));
696                 workingCopy.rewrite(node, copy);
697             }
698
699             public void cancel() {
700             }
701         };
702         src.runModificationTask(task).commit();
703         String JavaDoc res = TestUtilities.copyFileToString(testFile);
704         System.err.println(res);
705         assertEquals(golden, res);
706     }
707     
708     public void testEmptyLines() throws Exception JavaDoc {
709         testFile = new File JavaDoc(getWorkDir(), "Test.java");
710         TestUtilities.copyStringToFile(testFile,
711             "package hierbas.del.litoral;\n" +
712             "\n" +
713             "import java.util.ArrayList;\n" +
714             "\n" +
715             "import java.util.List;\n" +
716             "import java.util.Collections;\n" +
717             "\n" +
718             "public class Test {\n" +
719             " public void taragui() {\n" +
720             " }\n" +
721             "}\n");
722         String JavaDoc golden =
723             "package hierbas.del.litoral;\n" +
724             "\n" +
725             "import java.util.ArrayList;\n" +
726             "import java.util.LinkedList;\n" +
727             "\n" +
728             "import java.util.List;\n" +
729             "import java.util.Collections;\n" +
730             "\n" +
731             "public class Test {\n" +
732             " public void taragui() {\n" +
733             " }\n" +
734             "}\n";
735
736         JavaSource src = getJavaSource(testFile);
737         CancellableTask task = new CancellableTask<WorkingCopy>() {
738
739             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
740                 workingCopy.toPhase(Phase.RESOLVED);
741                 TreeMaker make = workingCopy.getTreeMaker();
742                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
743                 ImportTree novyDovoz = make.Import(make.Identifier("java.util.LinkedList"), false);
744                 CompilationUnitTree copy = make.insertCompUnitImport(cut, 1, novyDovoz);
745                 workingCopy.rewrite(cut, copy);
746             }
747
748             public void cancel() {
749             }
750         };
751         src.runModificationTask(task).commit();
752         String JavaDoc res = TestUtilities.copyFileToString(testFile);
753         System.err.println(res);
754         assertEquals(golden, res);
755     }
756     
757     public void testIndentedImport() throws Exception JavaDoc {
758         testFile = new File JavaDoc(getWorkDir(), "Test.java");
759         TestUtilities.copyStringToFile(testFile,
760             " import java.util.ArrayList;\n" +
761             "\n" +
762             "import java.util.List;\n" +
763             "import java.util.Collections;\n" +
764             "\n" +
765             "public class Test {\n" +
766             " public void taragui() {\n" +
767             " }\n" +
768             "}\n");
769         String JavaDoc golden =
770             "\n" +
771             "import java.util.List;\n" +
772             "import java.util.Collections;\n" +
773             "\n" +
774             "public class Test {\n" +
775             " public void taragui() {\n" +
776             " }\n" +
777             "}\n";
778
779         JavaSource src = getJavaSource(testFile);
780         CancellableTask task = new CancellableTask<WorkingCopy>() {
781
782             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
783                 workingCopy.toPhase(Phase.RESOLVED);
784                 TreeMaker make = workingCopy.getTreeMaker();
785                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
786                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 0);
787                 workingCopy.rewrite(cut, copy);
788             }
789
790             public void cancel() {
791             }
792         };
793         src.runModificationTask(task).commit();
794         String JavaDoc res = TestUtilities.copyFileToString(testFile);
795         System.err.println(res);
796         assertEquals(golden, res);
797     }
798     
799     public void testIndentedImport2() throws Exception JavaDoc {
800         testFile = new File JavaDoc(getWorkDir(), "Test.java");
801         TestUtilities.copyStringToFile(testFile,
802             "import java.util.List;\n" +
803             " import java.util.ArrayList;\n" +
804             "import java.util.Collections;\n" +
805             "\n" +
806             "public class Test {\n" +
807             " public void taragui() {\n" +
808             " }\n" +
809             "}\n");
810         String JavaDoc golden =
811             "import java.util.List;\n" +
812             "import java.util.Collections;\n" +
813             "\n" +
814             "public class Test {\n" +
815             " public void taragui() {\n" +
816             " }\n" +
817             "}\n";
818
819         JavaSource src = getJavaSource(testFile);
820         CancellableTask task = new CancellableTask<WorkingCopy>() {
821
822             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
823                 workingCopy.toPhase(Phase.RESOLVED);
824                 TreeMaker make = workingCopy.getTreeMaker();
825                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
826                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 1);
827                 workingCopy.rewrite(cut, copy);
828             }
829
830             public void cancel() {
831             }
832         };
833         src.runModificationTask(task).commit();
834         String JavaDoc res = TestUtilities.copyFileToString(testFile);
835         System.err.println(res);
836         assertEquals(golden, res);
837     }
838     
839     public void testMissingNewLine() throws Exception JavaDoc {
840         testFile = new File JavaDoc(getWorkDir(), "Test.java");
841         TestUtilities.copyStringToFile(testFile,
842             "import java.util.List;\n" +
843             "import java.util.ArrayList;import java.util.Collections;\n" +
844             "\n" +
845             "public class Test {\n" +
846             " public void taragui() {\n" +
847             " }\n" +
848             "}\n");
849         String JavaDoc golden =
850             "import java.util.List;\n" +
851             "import java.util.Collections;\n" +
852             "\n" +
853             "public class Test {\n" +
854             " public void taragui() {\n" +
855             " }\n" +
856             "}\n";
857
858         JavaSource src = getJavaSource(testFile);
859         CancellableTask task = new CancellableTask<WorkingCopy>() {
860
861             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
862                 workingCopy.toPhase(Phase.RESOLVED);
863                 TreeMaker make = workingCopy.getTreeMaker();
864                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
865                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 1);
866                 workingCopy.rewrite(cut, copy);
867             }
868
869             public void cancel() {
870             }
871         };
872         src.runModificationTask(task).commit();
873         String JavaDoc res = TestUtilities.copyFileToString(testFile);
874         System.err.println(res);
875         assertEquals(golden, res);
876     }
877     
878     public void testRemoveAllInDefault() throws Exception JavaDoc {
879         testFile = new File JavaDoc(getWorkDir(), "Test.java");
880         TestUtilities.copyStringToFile(testFile,
881             "import java.util.List;\n" +
882             "import java.util.ArrayList;\n" +
883             "import java.util.Collections;\n" +
884             "\n" +
885             "public class Test {\n" +
886             " public void taragui() {\n" +
887             " }\n" +
888             "}\n");
889         String JavaDoc golden =
890             "\n" +
891             "public class Test {\n" +
892             " public void taragui() {\n" +
893             " }\n" +
894             "}\n";
895
896         JavaSource src = getJavaSource(testFile);
897         CancellableTask task = new CancellableTask<WorkingCopy>() {
898
899             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
900                 workingCopy.toPhase(Phase.RESOLVED);
901                 TreeMaker make = workingCopy.getTreeMaker();
902                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
903                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 0);
904                 copy = make.removeCompUnitImport(copy, 0);
905                 copy = make.removeCompUnitImport(copy, 0);
906                 workingCopy.rewrite(cut, copy);
907             }
908
909             public void cancel() {
910             }
911         };
912         src.runModificationTask(task).commit();
913         String JavaDoc res = TestUtilities.copyFileToString(testFile);
914         System.err.println(res);
915         assertEquals(golden, res);
916     }
917     
918     public void testRemoveAllInDefault2() throws Exception JavaDoc {
919         testFile = new File JavaDoc(getWorkDir(), "Test.java");
920         TestUtilities.copyStringToFile(testFile,
921             "import java.util.List;\n" +
922             "import java.util.ArrayList;\n" +
923             "import java.util.Collections; // test\n" +
924             "/** test */\n" +
925             "public class Test {\n" +
926             " public void taragui() {\n" +
927             " }\n" +
928             "}\n");
929         String JavaDoc golden =
930             "/** test */\n" +
931             "public class Test {\n" +
932             " public void taragui() {\n" +
933             " }\n" +
934             "}\n";
935
936         JavaSource src = getJavaSource(testFile);
937         CancellableTask task = new CancellableTask<WorkingCopy>() {
938
939             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
940                 workingCopy.toPhase(Phase.RESOLVED);
941                 TreeMaker make = workingCopy.getTreeMaker();
942                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
943                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 0);
944                 copy = make.removeCompUnitImport(copy, 0);
945                 copy = make.removeCompUnitImport(copy, 0);
946                 workingCopy.rewrite(cut, copy);
947             }
948
949             public void cancel() {
950             }
951         };
952         src.runModificationTask(task).commit();
953         String JavaDoc res = TestUtilities.copyFileToString(testFile);
954         System.err.println(res);
955         assertEquals(golden, res);
956     }
957     
958     public void testRemoveAfterEmpty() throws Exception JavaDoc {
959         testFile = new File JavaDoc(getWorkDir(), "Test.java");
960         TestUtilities.copyStringToFile(testFile,
961             "import java.util.List;\n" +
962             "\n" +
963             "import java.util.ArrayList;\n" +
964             "import java.util.Collections; // test\n" +
965             "/** test */\n" +
966             "public class Test {\n" +
967             " public void taragui() {\n" +
968             " }\n" +
969             "}\n");
970         String JavaDoc golden =
971             "import java.util.List;\n" +
972             "\n" +
973             "import java.util.Collections; // test\n" +
974             "/** test */\n" +
975             "public class Test {\n" +
976             " public void taragui() {\n" +
977             " }\n" +
978             "}\n";
979
980         JavaSource src = getJavaSource(testFile);
981         CancellableTask task = new CancellableTask<WorkingCopy>() {
982
983             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
984                 workingCopy.toPhase(Phase.RESOLVED);
985                 TreeMaker make = workingCopy.getTreeMaker();
986                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
987                 CompilationUnitTree copy = make.removeCompUnitImport(cut, 1);
988                 workingCopy.rewrite(cut, copy);
989             }
990
991             public void cancel() {
992             }
993         };
994         src.runModificationTask(task).commit();
995         String JavaDoc res = TestUtilities.copyFileToString(testFile);
996         System.err.println(res);
997         assertEquals(golden, res);
998     }
999     
1000    public void testRemoveBeforeEmpty() throws Exception JavaDoc {
1001        testFile = new File JavaDoc(getWorkDir(), "Test.java");
1002        TestUtilities.copyStringToFile(testFile,
1003            "import java.util.List;\n" +
1004            "import java.util.ArrayList;\n" +
1005            "\n" +
1006            "import java.util.Collections; // test\n" +
1007            "/** test */\n" +
1008            "public class Test {\n" +
1009            " public void taragui() {\n" +
1010            " }\n" +
1011            "}\n");
1012        String JavaDoc golden =
1013            "import java.util.List;\n" +
1014            "\n" +
1015            "import java.util.Collections; // test\n" +
1016            "/** test */\n" +
1017            "public class Test {\n" +
1018            " public void taragui() {\n" +
1019            " }\n" +
1020            "}\n";
1021
1022        JavaSource src = getJavaSource(testFile);
1023        CancellableTask task = new CancellableTask<WorkingCopy>() {
1024
1025            public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
1026                workingCopy.toPhase(Phase.RESOLVED);
1027                TreeMaker make = workingCopy.getTreeMaker();
1028                CompilationUnitTree cut = workingCopy.getCompilationUnit();
1029                CompilationUnitTree copy = make.removeCompUnitImport(cut, 1);
1030                workingCopy.rewrite(cut, copy);
1031            }
1032
1033            public void cancel() {
1034            }
1035        };
1036        src.runModificationTask(task).commit();
1037        String JavaDoc res = TestUtilities.copyFileToString(testFile);
1038        System.err.println(res);
1039        assertEquals(golden, res);
1040    }
1041    
1042    public void testRenameIdentifier() throws Exception JavaDoc {
1043        testFile = new File JavaDoc(getWorkDir(), "Test.java");
1044        TestUtilities.copyStringToFile(testFile,
1045            "import java.util.List;\n" +
1046            "import java.util.ArrayList;\n" +
1047            "\n" +
1048            "import java.util.Collections; // test\n" +
1049            "/** test */\n" +
1050            "public class Test {\n" +
1051            " public void taragui() {\n" +
1052            " }\n" +
1053            "}\n");
1054        String JavaDoc golden =
1055            "import java.util.List;\n" +
1056            "import java.util.ArrayList;\n" +
1057            "\n" +
1058            "import java.util.Jitko; // test\n" +
1059            "/** test */\n" +
1060            "public class Test {\n" +
1061            " public void taragui() {\n" +
1062            " }\n" +
1063            "}\n";
1064
1065        JavaSource src = getJavaSource(testFile);
1066        CancellableTask task = new CancellableTask<WorkingCopy>() {
1067
1068            public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
1069                workingCopy.toPhase(Phase.RESOLVED);
1070                TreeMaker make = workingCopy.getTreeMaker();
1071                CompilationUnitTree cut = workingCopy.getCompilationUnit();
1072                ImportTree dovoz = cut.getImports().get(2);
1073                MemberSelectTree mst = (MemberSelectTree) dovoz.getQualifiedIdentifier();
1074                workingCopy.rewrite(mst, make.setLabel(mst, "Jitko"));
1075            }
1076
1077            public void cancel() {
1078            }
1079        };
1080        src.runModificationTask(task).commit();
1081        String JavaDoc res = TestUtilities.copyFileToString(testFile);
1082        System.err.println(res);
1083        assertEquals(golden, res);
1084    }
1085    
1086    String JavaDoc getGoldenPckg() {
1087        return "";
1088    }
1089
1090    String JavaDoc getSourcePckg() {
1091        return "";
1092    }
1093    
1094}
1095
Popular Tags