KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > ClassPathProviderImplTest


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.modules.web.project.classpath;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.api.java.platform.JavaPlatformManager;
27 import org.netbeans.api.java.project.JavaProjectConstants;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 //import org.netbeans.api.project.TestUtil;
31
import org.netbeans.api.project.SourceGroup;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.modules.web.project.TestPlatformProvider;
35 import org.netbeans.modules.web.project.WebProject;
36 import org.netbeans.modules.web.project.test.TestBase;
37 import org.netbeans.modules.web.project.test.TestUtil;
38 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
39 import org.netbeans.spi.java.classpath.ClassPathProvider;
40 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44
45 /**
46  *
47  * @author Andrei Badea
48  */

49 public class ClassPathProviderImplTest extends NbTestCase {
50     
51     private Project project;
52     private Project project59055;
53     private AntProjectHelper helper;
54     private AntProjectHelper helper59055;
55     private FileObject sourceRoot;
56     private FileObject sourceRoot59055_1;
57     private FileObject sourceRoot59055_2;
58     private FileObject testRoot;
59     private FileObject testRoot59055_1;
60     private FileObject testRoot59055_2;
61     private FileObject webRoot;
62     private FileObject webRoot59055;
63     private FileObject bootPlatformRoot;
64     private FileObject sourceClass;
65     private FileObject sourceClass59055_1;
66     private FileObject sourceClass59055_2;
67     private FileObject testClass;
68     private FileObject testClass59055_1;
69     private FileObject testClass59055_2;
70     private FileObject jspPage;
71     private FileObject jspPage59055;
72
73     public ClassPathProviderImplTest(String JavaDoc testName) {
74         super(testName);
75     }
76     
77     public void setUp() throws Exception JavaDoc {
78         // setup some platforms -- needed for testing findClassPath(FileObject, ClassPath.BOOT)
79
FileObject scratch = TestUtil.makeScratchDir(this);
80         bootPlatformRoot = scratch.createFolder("DefaultPlatformRoot");
81         ClassPath defBCP = ClassPathSupport.createClassPath(new URL JavaDoc[] { bootPlatformRoot.getURL() });
82         
83         TestBase.setLookup(new Object JavaDoc[] {
84             new TestPlatformProvider(defBCP, defBCP)
85         });
86         
87         assertTrue("No Java platforms found.", JavaPlatformManager.getDefault().getInstalledPlatforms().length >= 2);
88         
89         // setup the project
90
File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/WebApplication1");
91         project = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f));
92         Sources src = (Sources)project.getLookup().lookup(Sources.class);
93         SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
94         
95         sourceRoot = findSourceRoot(groups, "${src.dir}");
96         assertNotNull(sourceRoot);
97         testRoot = findSourceRoot(groups, "${test.src.dir}");
98         assertNotNull(testRoot);
99         
100         sourceClass = sourceRoot.getFileObject("pkg/NewClass.java");
101         assertNotNull(sourceClass);
102         testClass = testRoot.getFileObject("pkg/NewClassTest.java");
103         assertNotNull(testClass);
104         
105         // XXX should not cast to WebProject
106
helper = ((WebProject)project).getAntProjectHelper();
107         String JavaDoc web = helper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
108         webRoot = helper.resolveFileObject(web);
109         jspPage = webRoot.getFileObject("index.jsp");
110         
111         //===========================================================
112
// setup the project for issue #59055
113
//===========================================================
114
File JavaDoc f59055 = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/WebApplication59055");
115         project59055 = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f59055));
116         Sources src59055 = (Sources) project59055.getLookup().lookup(Sources.class);
117         SourceGroup[] groups59055 = src59055.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
118         
119         sourceRoot59055_1 = findSourceRoot(groups59055, "${src.dir}");
120         assertNotNull(sourceRoot59055_1);
121         sourceRoot59055_2 = findSourceRoot(groups59055, "${src.src2.dir}");
122         assertNotNull(sourceRoot59055_2);
123         testRoot59055_1 = findSourceRoot(groups59055, "${test.src.dir}");
124         assertNotNull(testRoot59055_1);
125         testRoot59055_2 = findSourceRoot(groups59055, "${test.test2.dir}");
126         assertNotNull(testRoot59055_2);
127         
128         sourceClass59055_1 = sourceRoot59055_1.getFileObject("org/wa59055/Class1.java");
129         assertNotNull(sourceClass59055_1);
130         sourceClass59055_2 = sourceRoot59055_2.getFileObject("org/wa59055/extra/Class2.java");
131         assertNotNull(sourceClass59055_2);
132         testClass59055_1 = testRoot59055_1.getFileObject("org/wa59055/Class1Test.java");
133         assertNotNull(testClass59055_1);
134         testClass59055_2 = testRoot59055_2.getFileObject("org/wa59055/extra/Class2Test.java");
135         assertNotNull(testClass59055_2);
136         
137         // XXX should not cast to WebProject
138
helper59055 = ((WebProject) project59055).getAntProjectHelper();
139         String JavaDoc web59055 = helper59055.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
140         webRoot59055 = helper59055.resolveFileObject(web59055);
141         jspPage59055 = webRoot59055.getFileObject("index.jsp");
142     }
143     
144     public void testClassPaths() throws Exception JavaDoc {
145         System.out.println("ClassPathProviderImplTest - WebApplication1");
146         ClassPathProvider cpp = (ClassPathProvider)project.getLookup().lookup(ClassPathProvider.class);
147         assertTrue("No ClassPathProvider in project lookup!", cpp != null);
148         
149         ClassPath cp;
150         
151         // testing all cp's twice as the second time they come from a cache
152

153         // sources
154

155         cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE);
156         checkSourceSourceClassPath(cp);
157         cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE);
158         checkSourceSourceClassPath(cp);
159         
160         cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE);
161         checkCompileClassPath(cp);
162         cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE);
163         checkCompileClassPath(cp);
164         
165         cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE);
166         checkSourceExecuteClassPath(cp);
167         cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE);
168         checkSourceExecuteClassPath(cp);
169         
170         cp = cpp.findClassPath(sourceClass, ClassPath.BOOT);
171         checkBootClassPath(cp);
172         cp = cpp.findClassPath(sourceClass, ClassPath.BOOT);
173         checkBootClassPath(cp);
174         
175         // test sources
176

177         cp = cpp.findClassPath(testClass, ClassPath.SOURCE);
178         checkTestSourceClassPath(cp);
179         cp = cpp.findClassPath(testClass, ClassPath.SOURCE);
180         checkTestSourceClassPath(cp);
181         
182         cp = cpp.findClassPath(testClass, ClassPath.COMPILE);
183         checkCompileClassPath(cp);
184         cp = cpp.findClassPath(testClass, ClassPath.COMPILE);
185         checkCompileClassPath(cp);
186         
187         cp = cpp.findClassPath(testClass, ClassPath.EXECUTE);
188         checkTestExecuteClassPath(cp);
189         cp = cpp.findClassPath(testClass, ClassPath.EXECUTE);
190         checkTestExecuteClassPath(cp);
191         
192         cp = cpp.findClassPath(testClass, ClassPath.BOOT);
193         checkBootClassPath(cp);
194         cp = cpp.findClassPath(testClass, ClassPath.BOOT);
195         checkBootClassPath(cp);
196         
197         // JSP pages
198

199         cp = cpp.findClassPath(jspPage, ClassPath.SOURCE);
200         checkJSPSourceClassPath(cp);
201         cp = cpp.findClassPath(jspPage, ClassPath.SOURCE);
202         checkJSPSourceClassPath(cp);
203         
204         //===========================================================
205
//test special source structure described in issue #59055
206
//===========================================================
207
//TODO: test also ClassPath.COMPILE, ClassPath.EXECUTE, ClassPath.BOOT for WebApplication59055
208
System.out.println("ClassPathProviderImplTest - WebApplication59055");
209         cpp = (ClassPathProvider) project59055.getLookup().lookup(ClassPathProvider.class);
210         assertTrue("No ClassPathProvider in project lookup!", cpp != null);
211         
212         //ordinary sources
213
cp = cpp.findClassPath(sourceClass59055_1, ClassPath.SOURCE);
214         checkSourceSourceClassPath59055(cp);
215         cp = cpp.findClassPath(sourceClass59055_1, ClassPath.SOURCE);
216         checkSourceSourceClassPath59055(cp);
217         //sources under web pages directory
218
cp = cpp.findClassPath(sourceClass59055_2, ClassPath.SOURCE);
219         checkSourceSourceClassPath59055(cp);
220         cp = cpp.findClassPath(sourceClass59055_2, ClassPath.SOURCE);
221         checkSourceSourceClassPath59055(cp);
222         
223         //ordinary test sources
224
cp = cpp.findClassPath(testClass59055_1, ClassPath.SOURCE);
225         checkTestSourceClassPath59055(cp);
226         cp = cpp.findClassPath(testClass59055_1, ClassPath.SOURCE);
227         checkTestSourceClassPath59055(cp);
228         //test sources under web pages directory
229
cp = cpp.findClassPath(testClass59055_2, ClassPath.SOURCE);
230         checkTestSourceClassPath59055(cp);
231         cp = cpp.findClassPath(testClass59055_2, ClassPath.SOURCE);
232         checkTestSourceClassPath59055(cp);
233         
234         cp = cpp.findClassPath(jspPage59055, ClassPath.SOURCE);
235         checkJSPSourceClassPath59055(cp);
236         cp = cpp.findClassPath(jspPage59055, ClassPath.SOURCE);
237         checkJSPSourceClassPath59055(cp);
238     }
239     
240     private void checkSourceSourceClassPath(ClassPath cp) {
241         FileObject[] roots = cp.getRoots();
242         assertEquals(1, roots.length);
243         assertTrue(cp.getRoots()[0].equals(sourceRoot));
244     }
245     
246     private void checkSourceSourceClassPath59055(ClassPath cp) {
247         FileObject[] roots = cp.getRoots();
248         assertEquals(2, roots.length);
249         assertTrue(cp.getRoots()[0].equals(sourceRoot59055_1));
250         assertTrue(cp.getRoots()[1].equals(sourceRoot59055_2));
251     }
252     
253     private void checkSourceExecuteClassPath(ClassPath cp) throws Exception JavaDoc {
254         // this jar is on debug.classpath
255
assertTrue(classPathContainsURL(cp, resolveURL("libs/jar1.jar")));
256     }
257     
258     private void checkTestSourceClassPath(ClassPath cp) {
259         FileObject[] roots = cp.getRoots();
260         assertEquals(1, roots.length);
261         assertTrue(cp.getRoots()[0].equals(testRoot));
262     }
263     
264     private void checkTestSourceClassPath59055(ClassPath cp) {
265         FileObject[] roots = cp.getRoots();
266         assertEquals(2, roots.length);
267         assertTrue(cp.getRoots()[0].equals(testRoot59055_1));
268         assertTrue(cp.getRoots()[1].equals(testRoot59055_2));
269     }
270     
271     private void checkTestExecuteClassPath(ClassPath cp) throws Exception JavaDoc {
272         // this jar is on run.test.classpath
273
// this test fails!
274
// assertTrue(classPathContainsURL(cp, resolveURL("libs/jar2.jar")));
275
}
276     
277     private void checkJSPSourceClassPath(ClassPath cp) {
278         FileObject[] roots = cp.getRoots();
279         assertEquals(2, roots.length);
280         assertTrue(cp.getRoots()[0].equals(webRoot));
281         assertTrue(cp.getRoots()[1].equals(sourceRoot));
282     }
283
284     private void checkJSPSourceClassPath59055(ClassPath cp) {
285         FileObject[] roots = cp.getRoots();
286         assertEquals(3, roots.length);
287         assertTrue(cp.getRoots()[0].equals(webRoot59055));
288         assertTrue(cp.getRoots()[1].equals(sourceRoot59055_1));
289         assertTrue(cp.getRoots()[2].equals(sourceRoot59055_2));
290     }
291     
292     private void checkCompileClassPath(ClassPath cp) throws Exception JavaDoc {
293         // this jar is on javac.classpath
294
assertTrue(classPathContainsURL(cp, resolveURL("libs/jar0.jar")));
295         // XXX should also test J2EE classpath
296
}
297
298     private void checkBootClassPath(ClassPath cp) throws Exception JavaDoc {
299         assertTrue(classPathContainsURL(cp, bootPlatformRoot.getURL()));
300     }
301     
302     private URL JavaDoc resolveURL(String JavaDoc relative) throws Exception JavaDoc {
303         return helper.resolveFileObject(relative).getURL();
304     }
305     
306     private static final boolean classPathContainsURL(ClassPath cp, URL JavaDoc url) {
307         if (FileUtil.isArchiveFile(url)) {
308             url = FileUtil.getArchiveRoot(url);
309         }
310         for (Iterator JavaDoc i = cp.entries().iterator(); i.hasNext();) {
311             ClassPath.Entry e = (ClassPath.Entry)i.next();
312             if (e.getURL().equals(url)) {
313                 return true;
314             }
315         }
316         return false;
317     }
318     
319     private static FileObject findSourceRoot(SourceGroup[] groups, String JavaDoc name) {
320         for (int i = 0; i < groups.length; i++) {
321             if (name.equals(groups[i].getName())) {
322                 return groups[i].getRootFolder();
323             }
324         }
325         return null;
326     }
327 }
328
Popular Tags