1 19 20 package org.netbeans.modules.j2ee.ejbfreeform; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 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 40 public class ClassPathTest extends TestBase { 41 42 public ClassPathTest(String testName) { 43 super(testName); 44 } 45 46 protected void setUp() throws Exception { 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 { 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 { 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 156 cp = ClassPath.getClassPath(buildXml, ClassPath.BOOT); 157 assertNull("have no BOOT classpath for build.xml", cp); 158 } 159 160 private static String specOfBootClasspath(ClassPath cp) { 161 List entries = cp.entries(); 162 if (entries.size() != 1) { 163 return null; 164 } 165 ClassPath.Entry entry = (ClassPath.Entry)entries.get(0); 166 String u = entry.getURL().toExternalForm(); 167 Pattern p = Pattern.compile("jar:file:/c:/java/([0-9.]+)/jre/lib/rt\\.jar!/"); 169 Matcher 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 |