KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.j2ee.ejbjarproject.classpath;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.api.java.platform.JavaPlatformManager;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.modules.j2ee.ejbjarproject.TestPlatformProvider;
35 import org.netbeans.modules.j2ee.ejbjarproject.test.TestBase;
36 import org.netbeans.modules.j2ee.ejbjarproject.test.TestUtil;
37 import org.netbeans.spi.java.classpath.ClassPathProvider;
38 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41
42 /**
43  *
44  * @author Andrei Badea
45  */

46 public class ClassPathProviderImplTest extends TestBase {
47     
48     private static final String JavaDoc DEFAULT_PLATFORM_ROOT = "DefaultPlatformRoot";
49     
50     private Project project;
51     private FileObject sourceRoot;
52     private FileObject testRoot;
53     private FileObject sourceClass;
54     private FileObject testClass;
55
56     public ClassPathProviderImplTest(String JavaDoc testName) {
57         super(testName);
58     }
59     
60     public void setUp() throws Exception JavaDoc {
61         // setup some platforms -- needed for testing findClassPath(FileObject, ClassPath.BOOT)
62
FileObject scratch = TestUtil.makeScratchDir(this);
63         FileObject defaultPlatformBootRoot = scratch.createFolder(DEFAULT_PLATFORM_ROOT);
64         ClassPath defBCP = ClassPathSupport.createClassPath(new URL JavaDoc[] { defaultPlatformBootRoot.getURL() });
65         
66         setLookup(new Object JavaDoc[] {
67             new TestPlatformProvider(defBCP, defBCP)
68         });
69         
70         assertTrue("No Java platforms found.", JavaPlatformManager.getDefault().getInstalledPlatforms().length >= 2);
71         
72         // setup the project
73
File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/EJBModule1");
74         project = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f));
75         Sources src = (Sources)project.getLookup().lookup(Sources.class);
76         SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
77         
78         sourceRoot = findSourceRoot(groups, "${src.dir}");
79         assertNotNull(sourceRoot);
80         testRoot = findSourceRoot(groups, "${test.src.dir}");
81         assertNotNull(testRoot);
82         
83         sourceClass = sourceRoot.getFileObject("pkg/NewClass.java");
84         assertNotNull(sourceClass);
85         testClass = testRoot.getFileObject("pkg/NewClassTest.java");
86         assertNotNull(testClass);
87     }
88     
89     public void testClassPaths() throws Exception JavaDoc {
90         ClassPathProvider cpp = (ClassPathProvider)project.getLookup().lookup(ClassPathProvider.class);
91         assertTrue("No ClassPathProvider in project lookup!", cpp != null);
92         
93         ClassPath cp;
94         
95         // testing all cp's twice as the second time they come from a cache
96

97         // sources
98

99         cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE);
100         checkSourceSourceClassPath(cp);
101         cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE);
102         checkSourceSourceClassPath(cp);
103         
104         cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE);
105         checkCompileClassPath(cp);
106         cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE);
107         checkCompileClassPath(cp);
108         
109         cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE);
110         checkSourceExecuteClassPath(cp);
111         cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE);
112         checkSourceExecuteClassPath(cp);
113         
114         cp = cpp.findClassPath(sourceClass, ClassPath.BOOT);
115         checkBootClassPath(cp);
116         cp = cpp.findClassPath(sourceClass, ClassPath.BOOT);
117         checkBootClassPath(cp);
118         
119         // test sources
120

121         cp = cpp.findClassPath(testClass, ClassPath.SOURCE);
122         checkTestSourceClassPath(cp);
123         cp = cpp.findClassPath(testClass, ClassPath.SOURCE);
124         checkTestSourceClassPath(cp);
125         
126         cp = cpp.findClassPath(testClass, ClassPath.COMPILE);
127         checkCompileClassPath(cp);
128         cp = cpp.findClassPath(testClass, ClassPath.COMPILE);
129         checkCompileClassPath(cp);
130         
131         cp = cpp.findClassPath(testClass, ClassPath.EXECUTE);
132         checkTestExecuteClassPath(cp);
133         cp = cpp.findClassPath(testClass, ClassPath.EXECUTE);
134         checkTestExecuteClassPath(cp);
135         
136         cp = cpp.findClassPath(testClass, ClassPath.BOOT);
137         checkBootClassPath(cp);
138         cp = cpp.findClassPath(testClass, ClassPath.BOOT);
139         checkBootClassPath(cp);
140     }
141     
142     private void checkSourceSourceClassPath(ClassPath cp) {
143         FileObject[] roots = cp.getRoots();
144         assertEquals(1, roots.length);
145         assertTrue(cp.getRoots()[0].equals(sourceRoot));
146     }
147     
148     private void checkSourceExecuteClassPath(ClassPath cp) {
149         // this jar is on debug.classpath
150
assertTrue(classPathEntriesContainJar(cp.entries(), "jar1.jar"));
151     }
152     
153     private void checkTestSourceClassPath(ClassPath cp) {
154         FileObject[] roots = cp.getRoots();
155         assertEquals(1, roots.length);
156         assertTrue(cp.getRoots()[0].equals(testRoot));
157     }
158     
159     private void checkTestExecuteClassPath(ClassPath cp) {
160         // this jar is on run.test.classpath
161
assertTrue(classPathEntriesContainJar(cp.entries(), "jar2.jar"));
162     }
163     
164     private void checkCompileClassPath(ClassPath cp) {
165         // this jar is on javac.classpath
166
assertTrue(classPathEntriesContainJar(cp.entries(), "jar0.jar"));
167         // XXX should also test J2EE classpath
168
}
169
170     private void checkBootClassPath(ClassPath cp) {
171         assertTrue(classPathEntriesContainFolder(cp.entries(), DEFAULT_PLATFORM_ROOT));
172     }
173     
174     private static boolean classPathEntriesContainJar(List JavaDoc entries, String JavaDoc name) {
175         for (Iterator JavaDoc i = entries.iterator(); i.hasNext();) {
176             ClassPath.Entry e = (ClassPath.Entry)i.next();
177             URL JavaDoc jar = FileUtil.getArchiveFile(e.getURL());
178             if (jar != null) {
179                 if (name.equals(new File JavaDoc(URI.create(jar.toExternalForm())).getName())) {
180                     return true;
181                 }
182             }
183         }
184         return false;
185     }
186     
187     private static boolean classPathEntriesContainFolder(List JavaDoc entries, String JavaDoc name) {
188         for (Iterator JavaDoc i = entries.iterator(); i.hasNext();) {
189             ClassPath.Entry e = (ClassPath.Entry)i.next();
190             URL JavaDoc folder = e.getURL();
191             if ("file".equals(folder.getProtocol())) {
192                 if (name.equals(new File JavaDoc(URI.create(folder.toExternalForm())).getName())) {
193                     return true;
194                 }
195             }
196         }
197         return false;
198     }
199     
200     private static FileObject findSourceRoot(SourceGroup[] groups, String JavaDoc name) {
201         for (int i = 0; i < groups.length; i++) {
202             if (name.equals(groups[i].getName())) {
203                 return groups[i].getRootFolder();
204             }
205         }
206         return null;
207     }
208 }
209
Popular Tags