1 19 package org.netbeans.modules.java.editor.semantic; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.Writer ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.List ; 33 import java.util.Set ; 34 import java.util.TreeSet ; 35 import java.util.concurrent.CountDownLatch ; 36 import java.util.regex.Pattern ; 37 import javax.swing.event.ChangeListener ; 38 import javax.swing.text.Document ; 39 import junit.framework.Test; 40 import junit.framework.TestSuite; 41 import org.netbeans.api.java.classpath.ClassPath; 42 import org.netbeans.api.java.lexer.JavaTokenId; 43 import org.netbeans.api.java.queries.SourceForBinaryQuery; 44 import org.netbeans.api.java.source.CancellableTask; 45 import org.netbeans.api.java.source.CompilationController; 46 import org.netbeans.api.java.source.JavaSource; 47 import org.netbeans.api.java.source.JavaSource.Phase; 48 import org.netbeans.api.java.source.SourceUtilsTestUtil; 49 import org.netbeans.api.lexer.Language; 50 import org.netbeans.junit.NbTestCase; 51 import org.netbeans.modules.editor.highlights.HighlightComparator; 52 import org.netbeans.modules.editor.highlights.spi.Highlight; 53 import org.netbeans.modules.java.JavaDataLoader; 54 import org.netbeans.spi.java.classpath.ClassPathProvider; 55 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 56 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 57 import org.openide.cookies.EditorCookie; 58 import org.openide.filesystems.FileObject; 59 import org.openide.filesystems.FileUtil; 60 import org.openide.loaders.DataObject; 61 62 66 public abstract class TestBase extends NbTestCase { 67 68 private static final boolean SHOW_GUI_DIFF = false; 69 70 73 public TestBase(String name) { 74 super(name); 75 } 76 77 private FileObject testSourceFO; 78 private URL testBuildDir; 79 80 protected final void copyToWorkDir(File resource, File toFile) throws IOException { 81 InputStream is = new FileInputStream (resource); 83 OutputStream outs = new FileOutputStream (toFile); 84 85 int read; 86 87 while ((read = is.read()) != (-1)) { 88 outs.write(read); 89 } 90 91 outs.close(); 92 93 is.close(); 94 } 95 96 protected void performTest(String fileName, final Performer performer) throws Exception { 97 performTest(fileName, performer, false); 98 } 99 100 protected void performTest(String fileName, final Performer performer, boolean doCompileRecursively) throws Exception { 101 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 102 103 FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this); 104 FileObject cache = scratch.createFolder("cache"); 105 106 File wd = getWorkDir(); 107 File testSource = new File (wd, "test/" + fileName + ".java"); 108 109 testSource.getParentFile().mkdirs(); 110 111 File dataFolder = new File (getDataDir(), "org/netbeans/modules/java/editor/semantic/data/"); 112 113 for (File f : dataFolder.listFiles()) { 114 copyToWorkDir(f, new File (wd, "test/" + f.getName())); 115 } 116 117 testSourceFO = FileUtil.toFileObject(testSource); 118 119 assertNotNull(testSourceFO); 120 121 File testBuildTo = new File (wd, "test-build"); 122 123 testBuildTo.mkdirs(); 124 125 SourceUtilsTestUtil.prepareTest(FileUtil.toFileObject(dataFolder), FileUtil.toFileObject(testBuildTo), cache); 126 127 if (doCompileRecursively) { 128 SourceUtilsTestUtil.compileRecursively(FileUtil.toFileObject(dataFolder)); 129 } 130 131 final Document doc = getDocument(testSourceFO); 132 final Set <Highlight> highlights = new TreeSet <Highlight>(new HighlightComparator()); 133 134 JavaSource source = JavaSource.forFileObject(testSourceFO); 135 136 assertNotNull(source); 137 138 final CountDownLatch l = new CountDownLatch (1); 139 140 source.runUserActionTask(new CancellableTask<CompilationController>() { 141 142 public void cancel() { 143 } 144 145 public void run(CompilationController parameter) { 146 try { 147 parameter.toPhase(Phase.UP_TO_DATE); 148 highlights.addAll(performer.compute(parameter, doc)); 149 } catch (IOException e) { 150 e.printStackTrace(); 151 } finally { 152 l.countDown(); 153 } 154 } 155 156 }, true); 157 158 l.await(); 159 160 File output = new File (getWorkDir(), getName() + ".out"); 161 Writer out = new FileWriter (output); 162 163 for (Highlight h : highlights) { 164 if (h instanceof HighlightImpl) { 165 out.write(((HighlightImpl) h).getHighlightTestData()); 166 } else { 167 out.write("Unsupported highlight type: " + h.getClass()); 168 } 169 170 out.write("\n"); 171 } 172 173 out.close(); 174 175 boolean wasException = true; 176 177 try { 178 File goldenFile = getGoldenFile(); 179 File diffFile = new File (getWorkDir(), getName() + ".diff"); 180 181 assertFile(output, goldenFile, diffFile); 182 wasException = false; 183 } finally { 184 if (wasException && SHOW_GUI_DIFF) { 185 try { 186 String name = getClass().getName(); 187 188 name = name.substring(name.lastIndexOf('.') + 1); 189 190 ShowGoldenFiles.run(name, getName(), fileName); 191 192 } catch (Exception e) { 193 e.printStackTrace(); 194 } 195 } 196 } 197 } 198 199 public static interface Performer { 200 201 public Collection <Highlight> compute(CompilationController parameter, Document doc); 202 203 } 204 205 protected final Document getDocument(FileObject file) throws IOException { 206 DataObject od = DataObject.find(file); 207 EditorCookie ec = (EditorCookie) od.getCookie(EditorCookie.class); 208 209 if (ec != null) { 210 Document doc = ec.openDocument(); 211 212 doc.putProperty(Language.class, JavaTokenId.language()); 213 214 return doc; 215 } else { 216 return null; 217 } 218 } 219 220 } 221 | Popular Tags |