KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > imports > ComputeImportsTest


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.modules.java.editor.imports;
20
21 import java.io.File JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.lang.model.element.TypeElement;
34 import javax.swing.text.Document JavaDoc;
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37 import org.netbeans.api.java.source.CompilationInfo;
38 import org.netbeans.api.java.source.JavaSource;
39 import org.netbeans.api.java.source.JavaSource.Phase;
40 import org.netbeans.api.java.source.SourceUtilsTestUtil;
41 import org.netbeans.api.java.source.test.support.MemoryValidator;
42 import org.netbeans.api.progress.ProgressHandle;
43 import org.netbeans.api.progress.ProgressHandleFactory;
44 import org.netbeans.junit.NbTestCase;
45 import org.netbeans.modules.java.editor.overridden.IsOverriddenAnnotationCreatorTest;
46 import org.netbeans.modules.java.editor.imports.ComputeImports.Pair;
47 import org.netbeans.modules.java.source.TestUtil;
48 import org.netbeans.modules.java.source.usages.ClassIndexImpl;
49 import org.netbeans.modules.java.source.usages.ClassIndexManager;
50 import org.netbeans.modules.java.source.usages.IndexUtil;
51 import org.openide.cookies.EditorCookie;
52 import org.openide.filesystems.FileObject;
53 import org.openide.filesystems.FileUtil;
54 import org.openide.filesystems.FileUtil;
55 import org.openide.loaders.DataObject;
56
57 /**
58  *
59  * @author Jan Lahoda
60  */

61 public class ComputeImportsTest extends NbTestCase {
62     
63     private static final Set JavaDoc<String JavaDoc> JDK16_MASKS = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {
64         "com.sun.xml.bind.v2.schemagen.xmlschema.List",
65         "com.sun.xml.txw2.Document",
66     }));
67     
68     private static final Set JavaDoc<String JavaDoc> NO_MASKS = new HashSet JavaDoc();
69     
70     private FileObject testSource;
71     private JavaSource js;
72     private CompilationInfo info;
73     
74     private static File JavaDoc cache;
75     private static FileObject cacheFO;
76     
77     public ComputeImportsTest(String JavaDoc testName) {
78         super(testName);
79     }
80
81     protected void setUp() throws Exception JavaDoc {
82         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
83         
84         if (cache == null) {
85             cache = TestUtil.createWorkFolder();
86             cacheFO = FileUtil.toFileObject(cache);
87             
88             cache.deleteOnExit();
89             
90             IndexUtil.setCacheFolder(cache);
91             
92             for (URL JavaDoc u : SourceUtilsTestUtil.getBootClassPath()) {
93                 final ClassIndexImpl ci = ClassIndexManager.getDefault().createUsagesQuery(u, false);
94                 ProgressHandle handle = ProgressHandleFactory.createHandle("cache creation");
95                 ci.getBinaryAnalyser().analyse(u, handle);
96             }
97         }
98     }
99     
100     public static Test suite() {
101         return MemoryValidator.wrap(new TestSuite(ComputeImportsTest.class));
102     }
103     
104     public void testSimple() throws Exception JavaDoc {
105         doTest("TestSimple", JDK16_MASKS, JDK16_MASKS);
106     }
107     
108     public void testFilterDeclaration() throws Exception JavaDoc {
109         doTest("TestFilterDeclaration", JDK16_MASKS, NO_MASKS);
110     }
111     
112     public void testFilterTypedInitializator() throws Exception JavaDoc {
113         doTest("TestFilterTypedInitializator", JDK16_MASKS, NO_MASKS);
114     }
115     
116     public void testFilterWithMethods() throws Exception JavaDoc {
117         doTest("TestFilterWithMethods", NO_MASKS, NO_MASKS);
118     }
119     
120     public void testGetCookie() throws Exception JavaDoc {
121         doTest("TestGetCookie", JDK16_MASKS, JDK16_MASKS);
122     }
123     
124     public void testNew() throws Exception JavaDoc {
125         doTest("TestNew", NO_MASKS, NO_MASKS);
126     }
127     
128     public void testException() throws Exception JavaDoc {
129         doTest("TestException", NO_MASKS, NO_MASKS);
130     }
131     
132     public void testEmptyCatch() throws Exception JavaDoc {
133         doTest("TestEmptyCatch", NO_MASKS, NO_MASKS);
134     }
135     
136     public void testUnfinishedMethod() throws Exception JavaDoc {
137         doTest("TestUnfinishedMethod", NO_MASKS, NO_MASKS);
138     }
139     
140     public void testUnsupportedOperation1() throws Exception JavaDoc {
141         doTest("TestUnsupportedOperation1", NO_MASKS, NO_MASKS);
142     }
143     
144     public void testPackageDoesNotExist() throws Exception JavaDoc {
145         doTest("TestPackageDoesNotExist", NO_MASKS, NO_MASKS);
146     }
147
148     public void testUnfinishedMethod2() throws Exception JavaDoc {
149         doTest("TestUnfinishedMethod2", NO_MASKS, NO_MASKS);
150     }
151     
152     public void testAnnotation() throws Exception JavaDoc {
153         doTest("TestAnnotation", NO_MASKS, NO_MASKS);
154     }
155     
156     public void testAnnotation2() throws Exception JavaDoc {
157         doTest("TestAnnotation2", NO_MASKS, NO_MASKS);
158     }
159     
160     private void prepareTest(String JavaDoc capitalizedName) throws Exception JavaDoc {
161         FileObject workFO = IsOverriddenAnnotationCreatorTest.makeScratchDir(this);
162         
163         assertNotNull(workFO);
164         
165         FileObject sourceRoot = workFO.createFolder("src");
166         FileObject buildRoot = workFO.createFolder("build");
167 // FileObject cache = workFO.createFolder("cache");
168
FileObject packageRoot = FileUtil.createFolder(sourceRoot, "org/netbeans/modules/java/editor/imports/data");
169         
170         SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO);
171         
172         String JavaDoc testPackagePath = "org/netbeans/modules/java/editor/imports/data/";
173         File JavaDoc testPackageFile = new File JavaDoc(getDataDir(), testPackagePath);
174         
175         String JavaDoc[] names = testPackageFile.list(new FilenameFilter JavaDoc() {
176             public boolean accept(File JavaDoc dir, String JavaDoc name) {
177                 if (name.endsWith(".java"))
178                     return true;
179                 
180                 return false;
181             }
182         });
183         
184         String JavaDoc[] files = new String JavaDoc[names.length];
185         
186         for (int cntr = 0; cntr < files.length; cntr++) {
187             files[cntr] = testPackagePath + names[cntr];
188         }
189         
190         TestUtil.copyFiles(getDataDir(), FileUtil.toFile(sourceRoot), files);
191         
192         packageRoot.refresh();
193         
194         testSource = packageRoot.getFileObject(capitalizedName + ".java");
195         
196         assertNotNull(testSource);
197         
198         js = JavaSource.forFileObject(testSource);
199         
200         assertNotNull(js);
201         
202         info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
203         
204         assertNotNull(info);
205     }
206     
207     private void dump(PrintStream JavaDoc out, Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>> set, Set JavaDoc<String JavaDoc> masks) {
208         for (Map.Entry JavaDoc<String JavaDoc, List JavaDoc<TypeElement>> e : set.entrySet()) {
209             List JavaDoc<String JavaDoc> fqns = new ArrayList JavaDoc<String JavaDoc>();
210             
211             for (TypeElement t : e.getValue()) {
212                 String JavaDoc fqn = t.getQualifiedName().toString();
213                 
214                 if (!masks.contains(fqn))
215                     fqns.add(fqn);
216             }
217             
218             out.println(e.getKey() + ":" + fqns.toString());
219         }
220     }
221     
222     private void doTest(String JavaDoc name, Set JavaDoc<String JavaDoc> unfilteredMasks, Set JavaDoc<String JavaDoc> filteredMasks) throws Exception JavaDoc {
223         prepareTest(name);
224         
225         DataObject testDO = DataObject.find(testSource);
226         EditorCookie ec = (EditorCookie) testDO.getCookie(EditorCookie.class);
227         
228         assertNotNull(ec);
229         
230         Document JavaDoc doc = ec.openDocument();
231         
232         Pair<Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>, Map JavaDoc<String JavaDoc, List JavaDoc<TypeElement>>> candidates = new ComputeImports().computeCandidates(info);
233         
234         for (List JavaDoc<TypeElement> cand : candidates.b.values()) {
235             Collections.sort(cand, new Comparator JavaDoc<TypeElement>() {
236                 public int compare(TypeElement t1, TypeElement t2) {
237                     return t1.getQualifiedName().toString().compareTo(t2.getQualifiedName().toString());
238                 }
239             });
240         }
241         
242         for (List JavaDoc<TypeElement> cand : candidates.a.values()) {
243             Collections.sort(cand, new Comparator JavaDoc<TypeElement>() {
244                 public int compare(TypeElement t1, TypeElement t2) {
245                     return t1.getQualifiedName().toString().compareTo(t2.getQualifiedName().toString());
246                 }
247             });
248         }
249         
250         dump(getLog(getName() + "-unfiltered.ref"), candidates.b, unfilteredMasks);
251         dump(getLog(getName() + "-filtered.ref"), candidates.a, filteredMasks);
252         
253         compareReferenceFiles(getName() + "-unfiltered.ref", getName() + "-unfiltered.pass", getName() + "-unfiltered.diff");
254         compareReferenceFiles(getName() + "-filtered.ref", getName() + "-filtered.pass", getName() + "-filtered.diff");
255     }
256 }
257
Popular Tags