KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.clientproject.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.junit.NbTestCase;
35 import org.netbeans.modules.j2ee.clientproject.TestPlatformProvider;
36 import org.netbeans.modules.j2ee.clientproject.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  * @author Andrei Badea
44  */

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

96         // sources
97

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

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