1 19 package org.netbeans.modules.java.editor.imports; 20 21 import java.io.File ; 22 import java.io.FilenameFilter ; 23 import java.io.PrintStream ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.Comparator ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import javax.lang.model.element.TypeElement; 34 import javax.swing.text.Document ; 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 61 public class ComputeImportsTest extends NbTestCase { 62 63 private static final Set <String > JDK16_MASKS = new HashSet (Arrays.asList(new String [] { 64 "com.sun.xml.bind.v2.schemagen.xmlschema.List", 65 "com.sun.xml.txw2.Document", 66 })); 67 68 private static final Set <String > NO_MASKS = new HashSet (); 69 70 private FileObject testSource; 71 private JavaSource js; 72 private CompilationInfo info; 73 74 private static File cache; 75 private static FileObject cacheFO; 76 77 public ComputeImportsTest(String testName) { 78 super(testName); 79 } 80 81 protected void setUp() throws Exception { 82 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [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 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 { 105 doTest("TestSimple", JDK16_MASKS, JDK16_MASKS); 106 } 107 108 public void testFilterDeclaration() throws Exception { 109 doTest("TestFilterDeclaration", JDK16_MASKS, NO_MASKS); 110 } 111 112 public void testFilterTypedInitializator() throws Exception { 113 doTest("TestFilterTypedInitializator", JDK16_MASKS, NO_MASKS); 114 } 115 116 public void testFilterWithMethods() throws Exception { 117 doTest("TestFilterWithMethods", NO_MASKS, NO_MASKS); 118 } 119 120 public void testGetCookie() throws Exception { 121 doTest("TestGetCookie", JDK16_MASKS, JDK16_MASKS); 122 } 123 124 public void testNew() throws Exception { 125 doTest("TestNew", NO_MASKS, NO_MASKS); 126 } 127 128 public void testException() throws Exception { 129 doTest("TestException", NO_MASKS, NO_MASKS); 130 } 131 132 public void testEmptyCatch() throws Exception { 133 doTest("TestEmptyCatch", NO_MASKS, NO_MASKS); 134 } 135 136 public void testUnfinishedMethod() throws Exception { 137 doTest("TestUnfinishedMethod", NO_MASKS, NO_MASKS); 138 } 139 140 public void testUnsupportedOperation1() throws Exception { 141 doTest("TestUnsupportedOperation1", NO_MASKS, NO_MASKS); 142 } 143 144 public void testPackageDoesNotExist() throws Exception { 145 doTest("TestPackageDoesNotExist", NO_MASKS, NO_MASKS); 146 } 147 148 public void testUnfinishedMethod2() throws Exception { 149 doTest("TestUnfinishedMethod2", NO_MASKS, NO_MASKS); 150 } 151 152 public void testAnnotation() throws Exception { 153 doTest("TestAnnotation", NO_MASKS, NO_MASKS); 154 } 155 156 public void testAnnotation2() throws Exception { 157 doTest("TestAnnotation2", NO_MASKS, NO_MASKS); 158 } 159 160 private void prepareTest(String capitalizedName) throws Exception { 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 packageRoot = FileUtil.createFolder(sourceRoot, "org/netbeans/modules/java/editor/imports/data"); 169 170 SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO); 171 172 String testPackagePath = "org/netbeans/modules/java/editor/imports/data/"; 173 File testPackageFile = new File (getDataDir(), testPackagePath); 174 175 String [] names = testPackageFile.list(new FilenameFilter () { 176 public boolean accept(File dir, String name) { 177 if (name.endsWith(".java")) 178 return true; 179 180 return false; 181 } 182 }); 183 184 String [] files = new String [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 out, Map <String , List <TypeElement>> set, Set <String > masks) { 208 for (Map.Entry <String , List <TypeElement>> e : set.entrySet()) { 209 List <String > fqns = new ArrayList <String >(); 210 211 for (TypeElement t : e.getValue()) { 212 String 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 name, Set <String > unfilteredMasks, Set <String > filteredMasks) throws Exception { 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 doc = ec.openDocument(); 231 232 Pair<Map <String , List <TypeElement>>, Map <String , List <TypeElement>>> candidates = new ComputeImports().computeCandidates(info); 233 234 for (List <TypeElement> cand : candidates.b.values()) { 235 Collections.sort(cand, new Comparator <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 <TypeElement> cand : candidates.a.values()) { 243 Collections.sort(cand, new Comparator <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 |