1 19 20 package org.netbeans.api.java.source; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStreamReader ; 24 import java.io.OutputStreamWriter ; 25 import java.io.PrintWriter ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 import junit.framework.*; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.net.URL ; 34 import javax.lang.model.element.Element; 35 import javax.lang.model.util.Elements; 36 import org.netbeans.api.java.classpath.ClassPath; 37 import org.netbeans.api.java.source.JavaSource.Phase; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.modules.java.JavaDataLoader; 40 import org.netbeans.modules.java.source.usages.IndexUtil; 41 import org.netbeans.spi.java.classpath.ClassPathProvider; 42 import org.netbeans.spi.java.classpath.PathResourceImplementation; 43 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.Lookup; 48 import org.openide.util.SharedClassObject; 49 import org.openide.util.lookup.Lookups; 50 import org.openide.util.lookup.ProxyLookup; 51 52 56 public class UiUtilsTest extends NbTestCase { 57 58 private static final String JTABLE_DATA = "jdk/JTable.java"; 60 public UiUtilsTest(String testName) { 61 super(testName); 62 63 } 64 65 protected void setUp() throws Exception { 66 this.clearWorkDir(); 67 File f = new File (this.getWorkDir(),"cache"); f.mkdirs(); 69 IndexUtil.setCacheFolder(f); 70 SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true); 71 SourceUtilsTestUtil.prepareTest(new String [0], new Object [] { 72 loader, 73 new DummyClassPathProvider () 74 }); 75 } 76 77 protected void tearDown() throws Exception { 78 } 79 80 81 82 public void testOpen() throws IOException { 83 FileObject workDir = FileUtil.toFileObject(this.getWorkDir()); 84 assertNotNull (workDir); 85 FileObject dataDir = FileUtil.toFileObject(this.getDataDir()); 86 assertNotNull (dataDir); 87 FileObject srcFile = createSource (dataDir, workDir); 88 JavaSource js = JavaSource.forFileObject (srcFile); 89 ClasspathInfo cpInfo = js.getClasspathInfo(); 90 CompilationInfo ci = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED); 91 Elements elements = ci.getElements (); 92 Element ce = elements.getTypeElement("javax.swing.JTable"); 93 assertNotNull(ce); 94 Object [] result = UiUtils.getOpenInfo(cpInfo, ce); 95 assertNotNull(result); 96 assertTrue (result[0] instanceof FileObject); 97 assertTrue (result[1] instanceof Integer ); 98 assertEquals (srcFile, result[0]); 99 assertEquals (5924, ((Integer ) result[1]).intValue()); 100 } 101 102 private static FileObject getSrcRoot (FileObject wrkRoot) throws IOException { 103 FileObject src = wrkRoot.getFileObject("src"); if (src == null) { 105 src = wrkRoot.createFolder("src"); } 107 return src; 108 } 109 110 private static FileObject createSource (FileObject dataRoot, FileObject wrkRoot) throws IOException { 111 FileObject data = dataRoot.getFileObject(JTABLE_DATA); 112 assertNotNull(data); 113 FileObject srcRoot = getSrcRoot (wrkRoot); 114 assertNotNull (srcRoot); 115 FileObject pkg = FileUtil.createFolder(srcRoot,"javax/swing"); FileObject src = pkg.createData("JTable.java"); FileLock lock = src.lock (); 118 try { 119 BufferedReader in = new BufferedReader ( new InputStreamReader (data.getInputStream())); 120 try { 121 PrintWriter out = new PrintWriter ( new OutputStreamWriter (src.getOutputStream(lock))); 122 try { 123 String line; 124 while ((line = in.readLine()) != null) { 125 out.println(line); 126 } 127 } finally { 128 out.close (); 129 } 130 } finally { 131 in.close (); 132 } 133 } finally { 134 lock.releaseLock(); 135 } 136 return src; 137 } 138 139 private static ClassPath createBootClassPath () throws IOException { 140 String bcp = System.getProperty ("sun.boot.class.path"); assertNotNull (bcp); 142 StringTokenizer tk = new StringTokenizer (bcp,File.pathSeparator); 143 List <URL > roots = new ArrayList <URL >(); 144 while (tk.hasMoreTokens()) { 145 String token = tk.nextToken(); 146 File f = new File (token); 147 URL url = f.toURI().toURL(); 148 if (FileUtil.isArchiveFile(url)) { 149 url = FileUtil.getArchiveRoot(url); 150 } 151 else if (!f.exists()) { 152 url = new URL (url.toExternalForm()+'/'); 153 } 154 roots.add (url); 155 } 156 return ClassPathSupport.createClassPath(roots.toArray(new URL [roots.size()])); 157 } 158 159 private static ClassPath createSourcePath (FileObject wrkRoot) throws IOException { 160 return ClassPathSupport.createClassPath(new FileObject[] {getSrcRoot(wrkRoot)}); 161 } 162 163 private class DummyClassPathProvider implements ClassPathProvider { 164 165 public ClassPath findClassPath(FileObject file, String type) { 166 try { 167 if (type == ClassPath.SOURCE) { 168 return createSourcePath (FileUtil.toFileObject(getWorkDir())); 169 } 170 else if (type == ClassPath.BOOT) { 171 return createBootClassPath (); 172 } 173 } catch (IOException ioe) { 174 } 176 return ClassPathSupport.createClassPath (Collections.<PathResourceImplementation>emptyList()); 177 } 178 } 179 180 } 181 | Popular Tags |