KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > indent > 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.jmi.javamodel.codegen.indent;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.SortedMap JavaDoc;
32 import org.netbeans.jmi.javamodel.Import;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.jmi.javamodel.JavaModelPackage;
35 import org.netbeans.jmi.javamodel.codegen.Utility;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.junit.NbTestSuite;
38 import org.openide.filesystems.FileStateInvalidException;
39
40 /**
41  *
42  * @author Pavel Flaska
43  */

44 public class ImportFormatTest extends NbTestCase {
45     
46     JavaClass clazz;
47     JavaModelPackage pkg;
48     
49     /** Creates a new instance of ImportFormatTest */
50     public ImportFormatTest() {
51         super("ImportFormatTest");
52     }
53
54     public static NbTestSuite suite() {
55         NbTestSuite suite = new NbTestSuite(ImportFormatTest.class);
56         return suite;
57     }
58     
59     protected void setUp() {
60         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.imports.ImportClass1");
61         pkg = (JavaModelPackage) clazz.refImmediatePackage();
62     }
63
64     public void testFirstAddition() throws IOException JavaDoc, FileStateInvalidException {
65         boolean fail = true;
66         Utility.beginTrans(true);
67         try {
68             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
69             imports.add(createImport("java.util.List"));
70             imports.add(createImport("java.util.Set"));
71             fail = false;
72         }
73         finally {
74             Utility.endTrans(fail);
75         }
76         assertFile("File is not correctly generated.",
77             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
78             getGoldenFile("testFirstAddition_ImportFormatTest.pass"),
79             getWorkDir()
80         );
81     }
82     
83     public void testAddFirstImport() throws IOException JavaDoc, FileStateInvalidException {
84         boolean fail = true;
85         Utility.beginTrans(true);
86         try {
87             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
88             imports.add(0, createImport("java.util.AbstractList"));
89             fail = false;
90         }
91         finally {
92             Utility.endTrans(fail);
93         }
94         assertFile("File is not correctly generated.",
95             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
96             getGoldenFile("testAddFirstImport_ImportFormatTest.pass"),
97             getWorkDir()
98         );
99     }
100     
101     public void testAddLastImport() throws IOException JavaDoc, FileStateInvalidException {
102         boolean fail = true;
103         Utility.beginTrans(true);
104         try {
105             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
106             imports.add(createImport("java.io.IOException"));
107             fail = false;
108         }
109         finally {
110             Utility.endTrans(fail);
111         }
112         assertFile("File is not correctly generated.",
113             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
114             getGoldenFile("testAddLastImport_ImportFormatTest.pass"),
115             getWorkDir()
116         );
117     }
118     
119     public void testRemoveInnerImport() throws IOException JavaDoc, FileStateInvalidException {
120         boolean fail = true;
121         Utility.beginTrans(true);
122         try {
123             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
124             getLog().print(imports.size());
125             imports.remove(1);
126             fail = false;
127         }
128         finally {
129             Utility.endTrans(fail);
130         }
131         assertFile("File is not correctly generated.",
132             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
133             getGoldenFile("testRemoveInnerImport_ImportFormatTest.pass"),
134             getWorkDir()
135         );
136     }
137     
138     public void testRemoveFirstImport() throws IOException JavaDoc, FileStateInvalidException {
139         boolean fail = true;
140         Utility.beginTrans(true);
141         try {
142             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
143             getLog().print(imports.size());
144             imports.remove(0);
145             fail = false;
146         }
147         finally {
148             Utility.endTrans(fail);
149         }
150         assertFile("File is not correctly generated.",
151             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
152             getGoldenFile("testRemoveFirstImport_ImportFormatTest.pass"),
153             getWorkDir()
154         );
155     }
156     
157     public void testRemoveLastImport() throws IOException JavaDoc, FileStateInvalidException {
158         boolean fail = true;
159         Utility.beginTrans(true);
160         try {
161             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
162             getLog().print(imports.size());
163             imports.remove(1);
164             fail = false;
165         }
166         finally {
167             Utility.endTrans(fail);
168         }
169         assertFile("File is not correctly generated.",
170             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
171             getGoldenFile("testRemoveLastImport_ImportFormatTest.pass"),
172             getWorkDir()
173         );
174     }
175     
176     public void testRemoveAllRemaning() throws IOException JavaDoc, FileStateInvalidException {
177         boolean fail = true;
178         Utility.beginTrans(true);
179         try {
180             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
181             getLog().print(imports.size());
182             imports.clear();
183             fail = false;
184         }
185         finally {
186             Utility.endTrans(fail);
187         }
188         assertFile("File is not correctly generated.",
189             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
190             getGoldenFile("testRemoveAllRemaning_ImportFormatTest.pass"),
191             getWorkDir()
192         );
193     }
194
195     public void testAddSeveral() throws IOException JavaDoc, FileStateInvalidException {
196         boolean fail = true;
197         Utility.beginTrans(true);
198         try {
199             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
200             imports.add(createImport("java.util.List"));
201             imports.add(createImport("java.util.Set"));
202             imports.add(createImport("javax.swing.CellRendererPane"));
203             imports.add(createImport("javax.swing.BorderFactory"));
204             imports.add(createImport("javax.swing.ImageIcon"));
205             imports.add(createImport("javax.swing.InputVerifier"));
206             imports.add(createImport("javax.swing.GrayFilter"));
207             imports.add(createImport("javax.swing.JFileChooser"));
208             imports.add(createImport("javax.swing.AbstractAction"));
209             fail = false;
210         }
211         finally {
212             Utility.endTrans(fail);
213         }
214         assertFile("File is not correctly generated.",
215             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
216             getGoldenFile("testAddSeveral_ImportFormatTest.pass"),
217             getWorkDir()
218         );
219     }
220     
221     public void testRemoveInside() throws IOException JavaDoc, FileStateInvalidException {
222         boolean fail = true;
223         Utility.beginTrans(true);
224         try {
225             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
226             imports.remove(4);
227             fail = false;
228         }
229         finally {
230             Utility.endTrans(fail);
231         }
232         assertFile("File is not correctly generated.",
233             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
234             getGoldenFile("testRemoveInside_ImportFormatTest.pass"),
235             getWorkDir()
236         );
237     }
238     
239     public void testMoveFirst() throws IOException JavaDoc, FileStateInvalidException {
240         boolean fail = true;
241         Utility.beginTrans(true);
242         try {
243             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
244             Object JavaDoc first = imports.remove(0);
245             imports.add(3, first);
246             fail = false;
247         }
248         finally {
249             Utility.endTrans(fail);
250         }
251         assertFile("File is not correctly generated.",
252             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
253             getGoldenFile("testMoveFirst_ImportFormatTest.pass"),
254             getWorkDir()
255         );
256     }
257     
258     public void testMoveLast() throws IOException JavaDoc, FileStateInvalidException {
259         boolean fail = true;
260         Utility.beginTrans(true);
261         try {
262             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
263             Object JavaDoc o = imports.remove(7);
264             imports.add(1, o);
265             fail = false;
266         }
267         finally {
268             Utility.endTrans(fail);
269         }
270         assertFile("File is not correctly generated.",
271             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
272             getGoldenFile("testMoveLast_ImportFormatTest.pass"),
273             getWorkDir()
274         );
275     }
276     
277     public void testReplaceLine() throws IOException JavaDoc, FileStateInvalidException {
278         boolean fail = true;
279         Utility.beginTrans(true);
280         try {
281             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
282             Object JavaDoc first = imports.remove(4);
283             imports.add(4, createImport("java.util.Collection"));
284             fail = false;
285         }
286         finally {
287             Utility.endTrans(fail);
288         }
289         assertFile("File is not correctly generated.",
290             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
291             getGoldenFile("testReplaceLine_ImportFormatTest.pass"),
292             getWorkDir()
293         );
294     }
295
296     public void testSort() throws IOException JavaDoc, FileStateInvalidException {
297         boolean fail = true;
298         Utility.beginTrans(true);
299         try {
300             List JavaDoc/*<Import>*/ imports = clazz.getResource().getImports();
301             List JavaDoc copy = new ArrayList JavaDoc(imports);
302             Collections.sort(copy, new Comparator JavaDoc() {
303                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
304                     if (o1 == o2) {
305                         return 0;
306                     }
307                     Import i1 = (Import) o1;
308                     Import i2 = (Import) o2;
309                     
310                     return i1.getIdentifier().getType().getName().compareTo(i2.getIdentifier().getType().getName());
311                 }
312                 
313                 public boolean equals(Object JavaDoc obj) {
314                     return this == obj ? true : false;
315                 }
316             });
317             for (Iterator JavaDoc it = copy.iterator(); it.hasNext(); ) {
318                 getLog().println(((Import) it.next()).getIdentifier().getType().getName());
319             }
320             imports.clear();
321             imports.addAll(copy);
322             fail = false;
323         }
324         finally {
325             Utility.endTrans(fail);
326         }
327         assertFile("File is not correctly generated.",
328             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/imports/ImportClass1.java"),
329             getGoldenFile("testSort_ImportFormatTest.pass"),
330             getWorkDir()
331         );
332     }
333     
334     // has to be called transaction!!!
335
private Import createImport(String JavaDoc importSymbol) {
336         //MultipartId id = pkg.getMultipartId().createMultipartId(importSymbol, null, null);
337
return pkg.getImport().createImport(importSymbol, null, false, false);
338     }
339 }
340
Popular Tags