1 19 20 package org.netbeans.modules.java.hints; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.File ; 24 import java.io.FilenameFilter ; 25 import java.io.IOException ; 26 import javax.swing.text.Document ; 27 import org.netbeans.modules.editor.NbEditorDocument; 28 import org.netbeans.spi.editor.hints.ErrorDescription; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.netbeans.api.java.source.CompilationInfo; 32 import org.netbeans.api.java.source.JavaSource; 33 import org.netbeans.api.java.source.JavaSource.Phase; 34 import org.netbeans.api.java.source.SourceUtilsTestUtil; 35 import org.netbeans.editor.BaseDocument; 36 import org.netbeans.junit.NbTestCase; 37 import org.netbeans.modules.editor.java.JavaKit; 38 import org.netbeans.modules.java.source.TestUtil; 39 import org.openide.cookies.EditorCookie; 40 import org.openide.filesystems.LocalFileSystem; 41 import org.openide.filesystems.Repository; 42 import org.openide.loaders.DataObject; 43 44 48 public class JavaHintsProviderTest extends NbTestCase { 49 50 public JavaHintsProviderTest(String testName) { 51 super(testName); 52 } 53 54 60 private FileObject testSource; 61 private JavaSource js; 62 private CompilationInfo info; 63 64 private static File cache; 65 private static FileObject cacheFO; 66 67 protected void setUp() throws Exception { 68 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 69 70 if (cache == null) { 71 cache = TestUtil.createWorkFolder(); 72 cacheFO = FileUtil.toFileObject(cache); 73 74 cache.deleteOnExit(); 75 } 76 } 77 78 private void prepareTest(String capitalizedName) throws Exception { 79 FileObject workFO = makeScratchDir(this); 80 81 assertNotNull(workFO); 82 83 FileObject sourceRoot = workFO.createFolder("src"); 84 FileObject buildRoot = workFO.createFolder("build"); 85 FileObject packageRoot = FileUtil.createFolder(sourceRoot, "javahints"); 87 88 SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO); 89 90 String testPackagePath = "javahints/"; 91 File testPackageFile = new File (getDataDir(), testPackagePath); 92 93 String [] names = testPackageFile.list(new FilenameFilter () { 94 public boolean accept(File dir, String name) { 95 if (name.endsWith(".java")) 96 return true; 97 98 return false; 99 } 100 }); 101 102 String [] files = new String [names.length]; 103 104 for (int cntr = 0; cntr < files.length; cntr++) { 105 files[cntr] = testPackagePath + names[cntr]; 106 } 107 108 TestUtil.copyFiles(getDataDir(), FileUtil.toFile(sourceRoot), files); 109 110 packageRoot.refresh(); 111 112 testSource = packageRoot.getFileObject(capitalizedName + ".java"); 113 114 assertNotNull(testSource); 115 116 js = JavaSource.forFileObject(testSource); 117 118 assertNotNull(js); 119 120 info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED); 121 122 assertNotNull(info); 123 } 124 125 130 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 131 test.clearWorkDir(); 132 File root = test.getWorkDir(); 133 assert root.isDirectory() && root.list().length == 0; 134 FileObject fo = FileUtil.toFileObject(root); 135 if (fo != null) { 136 return fo; 138 } else { 139 LocalFileSystem lfs = new LocalFileSystem(); 141 try { 142 lfs.setRootDirectory(root); 143 } catch (PropertyVetoException e) { 144 assert false : e; 145 } 146 Repository.getDefault().addFileSystem(lfs); 147 return lfs.getRoot(); 148 } 149 } 150 151 private void performTest(String name) throws Exception { 152 prepareTest(name); 153 154 DataObject testData = DataObject.find(testSource); 155 EditorCookie ec = (EditorCookie) testData.getCookie(EditorCookie.class); 156 Document doc = ec.openDocument(); 157 158 BaseDocument bdoc = new NbEditorDocument(JavaKit.class); 159 160 bdoc.putProperty("mimeType", "text/x-java"); 161 bdoc.putProperty(Document.StreamDescriptionProperty, testData); 162 bdoc.insertString(0, doc.getText(0, doc.getLength()), null); 163 164 for (ErrorDescription ed : new JavaHintsProvider(testSource).computeErrors(info, doc)) 165 ref(ed.toString()); 166 167 compareReferenceFiles(); 168 } 169 170 public void testShortErrors1() throws Exception { 171 performTest("TestShortErrors1"); 172 } 173 174 public void testShortErrors2() throws Exception { 175 performTest("TestShortErrors2"); 176 } 177 178 public void testShortErrors3() throws Exception { 179 performTest("TestShortErrors3"); 180 } 181 182 public void testShortErrors4() throws Exception { 183 performTest("TestShortErrors4"); 184 } 185 186 public void testShortErrors5() throws Exception { 187 performTest("TestShortErrors5"); 188 } 189 190 public void testShortErrors6() throws Exception { 191 performTest("TestShortErrors6"); 192 } 193 194 public void testShortErrors7() throws Exception { 195 performTest("TestShortErrors7"); 196 } 197 198 public void testShortErrors8() throws Exception { 199 performTest("TestShortErrors8"); 200 } 201 202 public void testShortErrors9() throws Exception { 203 performTest("TestShortErrors9"); 204 } 205 206 } 207 | Popular Tags |