KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > ClassPathTest


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.ejbfreeform;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.netbeans.api.java.classpath.GlobalPathRegistry;
32 import org.netbeans.spi.java.classpath.ClassPathProvider;
33 import org.netbeans.spi.project.ui.ProjectOpenedHook;
34 import org.openide.filesystems.FileObject;
35
36 /**
37  *
38  * @author jungi
39  */

40 public class ClassPathTest extends TestBase {
41     
42     public ClassPathTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48         assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory",
49             ClassPathTest.class.getResource("/META-INF/services/org.openide.modules.InstalledFileLocator"));
50         assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory",
51             ClassPathTest.class.getResource("/META-INF/services/org.netbeans.modules.java.platform.JavaPlatformProvider"));
52         setUpProject();
53     }
54     
55     protected void tearDown() throws Exception JavaDoc {
56     }
57     
58     public void testCompileClasspath() {
59         ClassPathProvider cpp = (ClassPathProvider) ejbFF.getLookup().lookup(ClassPathProvider.class);
60         FileObject prjDir = ejbFF.getProjectDirectory();
61         FileObject fo = prjDir.getFileObject("src/java");
62         ClassPath cp = cpp.findClassPath(fo, ClassPath.COMPILE);
63         assertEquals("one entry for src/java", 1, cp.entries().size());
64         assertEquals("one root for src/java", 1, cp.getRoots().length);
65         assertNotNull("found IAction in " + cp, cp.findResource("org/netbeans/modules/test/j2ee/ejbfreeform/IAction.class"));
66         fo = prjDir.getFileObject("src/ws");
67         cp = cpp.findClassPath(fo, ClassPath.COMPILE);
68         assertEquals("no entry for src/ws", 0, cp.entries().size());
69         assertEquals("one root for src/ws", 0, cp.getRoots().length);
70         fo = prjDir.getFileObject("src/beans");
71         cp = cpp.findClassPath(fo, ClassPath.COMPILE);
72         assertEquals("no entry for src/beans", 0, cp.entries().size());
73         assertEquals("one root for src/beans", 0, cp.getRoots().length);
74         fo = prjDir.getFileObject("test");
75         cp = cpp.findClassPath(fo, ClassPath.COMPILE);
76         assertEquals("one entry for test", 1, cp.entries().size());
77         assertEquals("one root for test", 1, cp.getRoots().length);
78         assertNotNull("found IAction in " + cp, cp.findResource("org/netbeans/modules/test/j2ee/ejbfreeform/IAction.class"));
79         fo = prjDir.getFileObject("resources");
80         cp = cpp.findClassPath(fo, ClassPath.COMPILE);
81         assertNull("null cp for resources", cp);
82         fo = prjDir.getFileObject("conf");
83         cp = cpp.findClassPath(fo, ClassPath.COMPILE);
84         assertNull("no entry for conf", cp);
85     }
86     
87     public void testSourcePath() {
88         ClassPathProvider cpp = (ClassPathProvider) ejbFF.getLookup().lookup(ClassPathProvider.class);
89         FileObject prjDir = ejbFF.getProjectDirectory();
90         FileObject fo = prjDir.getFileObject("src/java");
91         ClassPath cp = cpp.findClassPath(fo, ClassPath.SOURCE);
92         assertEquals("one entry for src/java", 1, cp.entries().size());
93         assertEquals("one root for src/java", 1, cp.getRoots().length);
94         fo = prjDir.getFileObject("src/ws");
95         cp = cpp.findClassPath(fo, ClassPath.SOURCE);
96         assertEquals("no entry for src/ws", 1, cp.entries().size());
97         assertEquals("one root for src/ws", 1, cp.getRoots().length);
98         fo = prjDir.getFileObject("src/beans");
99         cp = cpp.findClassPath(fo, ClassPath.SOURCE);
100         assertEquals("no entry for src/beans", 1, cp.entries().size());
101         assertEquals("one root for src/beans", 1, cp.getRoots().length);
102         fo = prjDir.getFileObject("test");
103         cp = cpp.findClassPath(fo, ClassPath.SOURCE);
104         assertEquals("one entry for test", 1, cp.entries().size());
105         assertEquals("one root for test", 1, cp.getRoots().length);
106         fo = prjDir.getFileObject("resources");
107         cp = cpp.findClassPath(fo, ClassPath.SOURCE);
108         assertNull("null cp for resources", cp);
109         fo = prjDir.getFileObject("conf");
110         cp = cpp.findClassPath(fo, ClassPath.SOURCE);
111         assertNull("no entry for conf", cp);
112     }
113     
114     public void testExecutePath() {
115         ClassPathProvider cpp = (ClassPathProvider) ejbFF.getLookup().lookup(ClassPathProvider.class);
116         FileObject prjDir = ejbFF.getProjectDirectory();
117         FileObject fo = prjDir.getFileObject("src/java");
118         ClassPath cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
119         assertEquals("two entries for src/java", 2, cp.entries().size());
120         assertEquals("two roots for src/java", 2, cp.getRoots().length);
121         fo = prjDir.getFileObject("src/ws");
122         cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
123         assertEquals("no entry for src/ws", 1, cp.entries().size());
124         assertEquals("one root for src/ws", 1, cp.getRoots().length);
125         fo = prjDir.getFileObject("src/beans");
126         cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
127         assertEquals("no entry for src/beans", 1, cp.entries().size());
128         assertEquals("one root for src/beans", 1, cp.getRoots().length);
129         fo = prjDir.getFileObject("test");
130         cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
131         assertEquals("two entries for test", 2, cp.entries().size());
132         assertEquals("two roots for test", 2, cp.getRoots().length);
133         fo = prjDir.getFileObject("resources");
134         cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
135         assertNull("null cp for resources", cp);
136         fo = prjDir.getFileObject("conf");
137         cp = cpp.findClassPath(fo, ClassPath.EXECUTE);
138         assertNull("no entry for conf", cp);
139     }
140     
141     protected boolean runInEQ() {
142         return false;
143     }
144     
145     public void testBootClasspath() throws Exception JavaDoc {
146         FileObject prjDir = ejbFF.getProjectDirectory();
147         ClassPath cp = ClassPath.getClassPath(prjDir.getFileObject("src/java"), ClassPath.BOOT);
148         assertNotNull("have some BOOT classpath for src/java", cp);
149         assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp));
150         ClassPath cp2 = ClassPath.getClassPath(prjDir.getFileObject("src/beans"), ClassPath.BOOT);
151         assertNotNull("have some BOOT classpath for src/beans", cp2);
152         assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp2));
153         /* Not actually required:
154         assertEquals("same BOOT classpath for all files (since use same spec level)", cp, cp2);
155          */

156         cp = ClassPath.getClassPath(buildXml, ClassPath.BOOT);
157         assertNull("have no BOOT classpath for build.xml", cp);
158     }
159     
160     private static String JavaDoc specOfBootClasspath(ClassPath cp) {
161         List JavaDoc/*<ClassPath.Entry>*/ entries = cp.entries();
162         if (entries.size() != 1) {
163             return null;
164         }
165         ClassPath.Entry entry = (ClassPath.Entry)entries.get(0);
166         String JavaDoc u = entry.getURL().toExternalForm();
167         // Cf. DummyJavaPlatformProvider.
168
Pattern JavaDoc p = Pattern.compile("jar:file:/c:/java/([0-9.]+)/jre/lib/rt\\.jar!/");
169         Matcher JavaDoc m = p.matcher(u);
170         if (m.matches()) {
171             return m.group(1);
172         } else {
173             return null;
174         }
175     }
176     
177 }
178
Popular Tags