1 19 package org.netbeans.modules.java.editor.overridden; 20 21 import java.beans.PropertyVetoException ; 22 import java.io.File ; 23 import java.io.FilenameFilter ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import javax.swing.text.StyledDocument ; 29 import org.netbeans.api.java.source.CompilationInfo; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.api.java.source.JavaSource.Phase; 32 import org.netbeans.api.java.source.SourceUtilsTestUtil; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.modules.java.source.TestUtil; 35 import org.openide.cookies.EditorCookie; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.LocalFileSystem; 39 import org.openide.filesystems.Repository; 40 import org.openide.loaders.DataObject; 41 42 46 public class IsOverriddenAnnotationCreatorTest extends NbTestCase { 47 48 private FileObject testSource; 49 private JavaSource js; 50 private CompilationInfo info; 51 52 private static File cache; 53 private static FileObject cacheFO; 54 55 public IsOverriddenAnnotationCreatorTest(String testName) { 56 super(testName); 57 } 58 59 protected void setUp() throws Exception { 60 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 61 62 if (cache == null) { 63 cache = TestUtil.createWorkFolder(); 64 cacheFO = FileUtil.toFileObject(cache); 65 66 cache.deleteOnExit(); 67 } 68 } 69 70 private void prepareTest(String capitalizedName) throws Exception { 71 FileObject workFO = makeScratchDir(this); 72 73 assertNotNull(workFO); 74 75 FileObject sourceRoot = workFO.createFolder("src"); 76 FileObject buildRoot = workFO.createFolder("build"); 77 FileObject packageRoot = FileUtil.createFolder(sourceRoot, "org/netbeans/modules/editor/java"); 79 80 SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO); 81 82 String testPackagePath = "org/netbeans/modules/editor/java/"; 83 File testPackageFile = new File (getDataDir(), testPackagePath); 84 85 String [] names = testPackageFile.list(new FilenameFilter () { 86 public boolean accept(File dir, String name) { 87 if (name.endsWith(".java")) 88 return true; 89 90 return false; 91 } 92 }); 93 94 String [] files = new String [names.length]; 95 96 for (int cntr = 0; cntr < files.length; cntr++) { 97 files[cntr] = testPackagePath + names[cntr]; 98 } 99 100 TestUtil.copyFiles(getDataDir(), FileUtil.toFile(sourceRoot), files); 101 102 packageRoot.refresh(); 103 104 SourceUtilsTestUtil.compileRecursively(sourceRoot); 105 106 testSource = packageRoot.getFileObject(capitalizedName + ".java"); 107 108 assertNotNull(testSource); 109 110 js = JavaSource.forFileObject(testSource); 111 112 assertNotNull(js); 113 114 info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED); 115 116 assertNotNull(info); 117 } 118 119 124 public void testOverrides() throws Exception { 125 doTest("TestOverrides"); 126 } 127 128 133 public void testInterfaceImplOverride() throws Exception { 134 doTest("TestInterfaceImplOverride"); 135 } 136 137 146 public void testHierarchy1() throws Exception { 147 doTest("TestHierarchy1"); 148 } 149 150 public void testHierarchy2() throws Exception { 151 doTest("TestHierarchy2"); 152 } 153 154 public void testBrokenSource() throws Exception { 155 doTest("TestBrokenSource"); 156 } 157 158 private void doTest(String name) throws Exception { 159 prepareTest(name); 160 161 DataObject testDO = DataObject.find(testSource); 162 EditorCookie ec = testDO.getCookie(EditorCookie.class); 163 164 assertNotNull(ec); 165 166 StyledDocument doc = ec.openDocument(); 167 168 List <IsOverriddenAnnotation> annotations = new IsOverriddenAnnotationHandler(testSource).process(info, doc); 169 List <String > result = new ArrayList <String >(); 170 171 for (IsOverriddenAnnotation annotation : annotations) { 172 result.add(annotation.debugDump()); 173 } 174 175 Collections.sort(result); 176 177 for (String r : result) { 178 ref(r); 179 } 180 181 compareReferenceFiles(); 182 } 183 184 189 @SuppressWarnings ("deprecation") 190 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 191 test.clearWorkDir(); 192 File root = test.getWorkDir(); 193 assert root.isDirectory() && root.list().length == 0; 194 FileObject fo = FileUtil.toFileObject(root); 195 if (fo != null) { 196 return fo; 198 } else { 199 LocalFileSystem lfs = new LocalFileSystem(); 201 try { 202 lfs.setRootDirectory(root); 203 } catch (PropertyVetoException e) { 204 assert false : e; 205 } 206 Repository.getDefault().addFileSystem(lfs); 207 return lfs.getRoot(); 208 } 209 } 210 } 211 | Popular Tags |