1 19 20 package org.netbeans.modules.java.editor.completion; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.Writer ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.List ; 35 import java.util.StringTokenizer ; 36 import java.util.regex.Pattern ; 37 import javax.swing.JEditorPane ; 38 import javax.swing.text.Document ; 39 import junit.framework.Assert; 40 41 import org.netbeans.api.editor.mimelookup.MimePath; 42 import org.netbeans.api.java.classpath.ClassPath; 43 import org.netbeans.api.java.lexer.JavaTokenId; 44 import org.netbeans.api.java.source.JavaSource; 45 import org.netbeans.api.java.source.SourceUtilsTestUtil2; 46 import org.netbeans.api.java.source.gen.WhitespaceIgnoringDiff; 47 import org.netbeans.api.lexer.Language; 48 import org.netbeans.junit.NbTestCase; 49 import org.netbeans.modules.editor.completion.CompletionItemComparator; 50 import org.netbeans.modules.editor.java.JavaCompletionProvider; 51 import org.netbeans.modules.editor.java.JavaKit; 52 import org.netbeans.modules.editor.java.Utilities; 53 import org.netbeans.modules.java.JavaDataLoader; 54 import org.netbeans.modules.java.JavaDataObject.JavaEditorSupport; 55 import org.netbeans.modules.java.source.usages.ClassIndexImpl; 56 import org.netbeans.modules.java.source.usages.ClassIndexManager; 57 import org.netbeans.modules.java.source.usages.IndexUtil; 58 import org.netbeans.spi.editor.completion.CompletionItem; 59 import org.netbeans.spi.editor.completion.CompletionProvider; 60 import org.netbeans.spi.editor.mimelookup.MimeDataProvider; 61 import org.netbeans.spi.java.classpath.ClassPathProvider; 62 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 63 64 import org.openide.cookies.EditorCookie; 65 import org.openide.filesystems.FileObject; 66 import org.openide.filesystems.FileUtil; 67 import org.openide.filesystems.FileUtil; 68 import org.openide.filesystems.Repository; 69 import org.openide.filesystems.XMLFileSystem; 70 import org.openide.loaders.DataObject; 71 import org.openide.util.Lookup; 72 import org.openide.util.SharedClassObject; 73 import org.openide.util.lookup.Lookups; 74 import org.openide.util.lookup.ProxyLookup; 75 76 80 public class CompletionTestBase extends NbTestCase { 81 82 static { 83 JavaCompletionProviderBasicTest.class.getClassLoader().setDefaultAssertionStatus(true); 84 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 85 Assert.assertEquals(Lkp.class, Lookup.getDefault().getClass()); 86 } 87 88 static final int FINISH_OUTTIME = 5 * 60 * 1000; 89 90 public static class Lkp extends ProxyLookup { 91 92 private static Lkp DEFAULT; 93 94 public Lkp() { 95 Assert.assertNull(DEFAULT); 96 DEFAULT = this; 97 } 98 99 public static void initLookups(Object [] objs) throws Exception { 100 ClassLoader l = Lkp.class.getClassLoader(); 101 DEFAULT.setLookups(new Lookup [] { 102 Lookups.fixed(objs), 103 Lookups.metaInfServices(l), 104 Lookups.singleton(l) 105 }); 106 } 107 } 108 109 public CompletionTestBase(String testName) { 110 super(testName); 111 } 112 113 protected void setUp() throws Exception { 114 XMLFileSystem system = new XMLFileSystem(); 115 system.setXmlUrls(new URL [] { 116 JavaCompletionProviderBasicTest.class.getResource("/org/netbeans/modules/java/editor/resources/layer.xml"), 117 JavaCompletionProviderBasicTest.class.getResource("/org/netbeans/modules/defaults/mf-layer.xml") 118 }); 119 Repository repository = new Repository(system); 120 File cacheFolder = new File (getWorkDir(), "var/cache/index"); 121 cacheFolder.mkdirs(); 122 IndexUtil.setCacheFolder(cacheFolder); 123 final ClassPath sourcePath = ClassPathSupport.createClassPath(new FileObject[] {FileUtil.toFileObject(getDataDir())}); 124 final ClassIndexManager mgr = ClassIndexManager.getDefault(); 125 for (ClassPath.Entry entry : sourcePath.entries()) { 126 mgr.createUsagesQuery(entry.getURL(), true); 127 } 128 final ClassPath bootPath = createClassPath(System.getProperty("sun.boot.class.path")); 129 for (ClassPath.Entry entry : bootPath.entries()) { 130 URL url = entry.getURL(); 131 ClassIndexImpl cii = mgr.createUsagesQuery(url, false); 132 cii.getBinaryAnalyser().analyse(url, null); 133 134 } 135 ClassPathProvider cpp = new ClassPathProvider() { 136 public ClassPath findClassPath(FileObject file, String type) { 137 if (type == ClassPath.SOURCE) 138 return sourcePath; 139 if (type == ClassPath.COMPILE) 140 return ClassPathSupport.createClassPath(new FileObject[0]); 141 if (type == ClassPath.BOOT) 142 return bootPath; 143 return null; 144 } 145 }; 146 SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true); 147 MimeDataProvider mdp = new MimeDataProvider() { 148 public Lookup getLookup(MimePath mimePath) { 149 return Lookups.singleton(new JavaKit()); 150 } 151 }; 152 Lkp.initLookups(new Object [] {repository, loader, cpp, mdp}); 153 JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit"); 154 Utilities.setCaseSensitive(true); 155 } 156 157 protected void tearDown() throws Exception { 158 } 159 160 protected void performTest(String source, int caretPos, String textToInsert, String goldenFileName) throws Exception { 161 performTest(source, caretPos, textToInsert, goldenFileName, null, null); 162 } 163 164 protected void performTest(String source, int caretPos, String textToInsert, String goldenFileName, String toPerformItemRE, String goldenFileName2) throws Exception { 165 File testSource = new File (getWorkDir(), "test/Test.java"); 166 testSource.getParentFile().mkdirs(); 167 copyToWorkDir(new File (getDataDir(), "org/netbeans/modules/java/editor/completion/data/" + source + ".java"), testSource); 168 FileObject testSourceFO = FileUtil.toFileObject(testSource); 169 assertNotNull(testSourceFO); 170 DataObject testSourceDO = DataObject.find(testSourceFO); 171 assertNotNull(testSourceDO); 172 EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class); 173 assertNotNull(ec); 174 final Document doc = ec.openDocument(); 175 assertNotNull(doc); 176 doc.putProperty(Language.class, JavaTokenId.language()); 177 int textToInsertLength = textToInsert != null ? textToInsert.length() : 0; 178 if (textToInsertLength > 0) 179 doc.insertString(caretPos, textToInsert, null); 180 JavaSource js = JavaSource.forDocument(doc); 181 List <? extends CompletionItem> items = JavaCompletionProvider.query(js, CompletionProvider.COMPLETION_QUERY_TYPE, caretPos + textToInsertLength, caretPos + textToInsertLength); 182 Collections.sort(items, CompletionItemComparator.BY_PRIORITY); 183 184 File output = new File (getWorkDir(), getName() + ".out"); 185 Writer out = new FileWriter (output); 186 for (Object item : items) { 187 out.write(item.toString()); 188 out.write("\n"); 189 } 190 out.close(); 191 192 File goldenFile = new File (getDataDir(), "/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/" + goldenFileName); 193 File diffFile = new File (getWorkDir(), getName() + ".diff"); 194 assertFile(output, goldenFile, diffFile); 195 196 if (toPerformItemRE != null) { 197 assertNotNull(goldenFileName2); 198 199 Pattern p = Pattern.compile(toPerformItemRE); 200 CompletionItem item = null; 201 for (CompletionItem i : items) { 202 if (p.matcher(i.toString()).find()) { 203 item = i; 204 break; 205 } 206 } 207 assertNotNull(item); 208 209 JEditorPane editor = new JEditorPane (); 210 editor.setDocument(doc); 211 editor.setCaretPosition(caretPos + textToInsertLength); 212 item.defaultAction(editor); 213 214 File output2 = new File (getWorkDir(), getName() + ".out2"); 215 Writer out2 = new FileWriter (output2); 216 out2.write(doc.getText(0, doc.getLength())); 217 out2.close(); 218 219 File goldenFile2 = new File (getDataDir(), "/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/" + goldenFileName2); 220 File diffFile2 = new File (getWorkDir(), getName() + ".diff2"); 221 222 assertFile(output2, goldenFile2, diffFile2, new WhitespaceIgnoringDiff()); 223 } 224 225 JavaEditorSupport s = (JavaEditorSupport) ec; 226 SourceUtilsTestUtil2.ignoreCompileRequests(); 227 s.close(false); 228 } 229 230 private void copyToWorkDir(File resource, File toFile) throws IOException { 231 InputStream is = new FileInputStream (resource); 232 OutputStream outs = new FileOutputStream (toFile); 233 int read; 234 while ((read = is.read()) != (-1)) { 235 outs.write(read); 236 } 237 outs.close(); 238 is.close(); 239 } 240 241 private static ClassPath createClassPath(String classpath) { 242 StringTokenizer tokenizer = new StringTokenizer (classpath, File.pathSeparator); 243 List list = new ArrayList (); 244 while (tokenizer.hasMoreTokens()) { 245 String item = tokenizer.nextToken(); 246 File f = FileUtil.normalizeFile(new File (item)); 247 URL url = getRootURL(f); 248 if (url!=null) { 249 list.add(ClassPathSupport.createResource(url)); 250 } 251 } 252 return ClassPathSupport.createClassPath(list); 253 } 254 255 private static URL getRootURL (File f) { 257 URL url = null; 258 try { 259 if (isArchiveFile(f)) { 260 url = FileUtil.getArchiveRoot(f.toURI().toURL()); 261 } else { 262 url = f.toURI().toURL(); 263 String surl = url.toExternalForm(); 264 if (!surl.endsWith("/")) { 265 url = new URL (surl+"/"); 266 } 267 } 268 } catch (MalformedURLException e) { 269 throw new AssertionError (e); 270 } 271 return url; 272 } 273 274 private static boolean isArchiveFile(File f) { 275 String fileName = f.getName().toLowerCase(); 277 return fileName.endsWith(".jar") || fileName.endsWith(".zip"); } 279 } 280 | Popular Tags |