KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > UiUtilsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import junit.framework.*;
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.URL JavaDoc;
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 /**
53  *
54  * @author Tomas Zezula
55  */

56 public class UiUtilsTest extends NbTestCase {
57     
58     private static final String JavaDoc JTABLE_DATA = "jdk/JTable.java"; //NOI18N
59

60     public UiUtilsTest(String JavaDoc testName) {
61     super(testName);
62     
63     }
64
65     protected void setUp() throws Exception JavaDoc {
66     this.clearWorkDir();
67     File JavaDoc f = new File JavaDoc (this.getWorkDir(),"cache"); //NOI18N
68
f.mkdirs();
69     IndexUtil.setCacheFolder(f);
70     SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true);
71         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[] {
72         loader,
73         new DummyClassPathProvider ()
74     });
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78     }
79
80     
81
82     public void testOpen() throws IOException JavaDoc {
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 JavaDoc[] result = UiUtils.getOpenInfo(cpInfo, ce);
95         assertNotNull(result);
96     assertTrue (result[0] instanceof FileObject);
97     assertTrue (result[1] instanceof Integer JavaDoc);
98     assertEquals (srcFile, result[0]);
99         assertEquals (5924, ((Integer JavaDoc) result[1]).intValue());
100     }
101     
102     private static FileObject getSrcRoot (FileObject wrkRoot) throws IOException JavaDoc {
103     FileObject src = wrkRoot.getFileObject("src"); //NOI18N
104
if (src == null) {
105         src = wrkRoot.createFolder("src"); //NOI18N
106
}
107     return src;
108     }
109     
110     private static FileObject createSource (FileObject dataRoot, FileObject wrkRoot) throws IOException JavaDoc {
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"); //NOI18N
116
FileObject src = pkg.createData("JTable.java"); //NOI18N
117
FileLock lock = src.lock ();
118     try {
119         BufferedReader JavaDoc in = new BufferedReader JavaDoc ( new InputStreamReader JavaDoc (data.getInputStream()));
120         try {
121         PrintWriter JavaDoc out = new PrintWriter JavaDoc ( new OutputStreamWriter JavaDoc (src.getOutputStream(lock)));
122         try {
123             String JavaDoc 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 JavaDoc {
140     String JavaDoc bcp = System.getProperty ("sun.boot.class.path"); //NOI18N
141
assertNotNull (bcp);
142     StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc (bcp,File.pathSeparator);
143     List JavaDoc<URL JavaDoc> roots = new ArrayList JavaDoc<URL JavaDoc>();
144     while (tk.hasMoreTokens()) {
145         String JavaDoc token = tk.nextToken();
146         File JavaDoc f = new File JavaDoc (token);
147         URL JavaDoc url = f.toURI().toURL();
148         if (FileUtil.isArchiveFile(url)) {
149         url = FileUtil.getArchiveRoot(url);
150         }
151         else if (!f.exists()) {
152         url = new URL JavaDoc (url.toExternalForm()+'/');
153         }
154         roots.add (url);
155     }
156     return ClassPathSupport.createClassPath(roots.toArray(new URL JavaDoc[roots.size()]));
157     }
158     
159     private static ClassPath createSourcePath (FileObject wrkRoot) throws IOException JavaDoc {
160     return ClassPathSupport.createClassPath(new FileObject[] {getSrcRoot(wrkRoot)});
161     }
162     
163     private class DummyClassPathProvider implements ClassPathProvider {
164     
165         public ClassPath findClassPath(FileObject file, String JavaDoc 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 JavaDoc ioe) {
174         //Skeep it
175
}
176         return ClassPathSupport.createClassPath (Collections.<PathResourceImplementation>emptyList());
177         }
178     }
179     
180 }
181
Popular Tags