1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.classpath; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.util.Iterator ; 26 import java.util.List ; 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 46 public class ClassPathProviderImplTest extends TestBase { 47 48 private static final String 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 testName) { 57 super(testName); 58 } 59 60 public void setUp() throws Exception { 61 FileObject scratch = TestUtil.makeScratchDir(this); 63 FileObject defaultPlatformBootRoot = scratch.createFolder(DEFAULT_PLATFORM_ROOT); 64 ClassPath defBCP = ClassPathSupport.createClassPath(new URL [] { defaultPlatformBootRoot.getURL() }); 65 66 setLookup(new Object [] { 67 new TestPlatformProvider(defBCP, defBCP) 68 }); 69 70 assertTrue("No Java platforms found.", JavaPlatformManager.getDefault().getInstalledPlatforms().length >= 2); 71 72 File f = new File (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 { 90 ClassPathProvider cpp = (ClassPathProvider)project.getLookup().lookup(ClassPathProvider.class); 91 assertTrue("No ClassPathProvider in project lookup!", cpp != null); 92 93 ClassPath cp; 94 95 97 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 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 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 assertTrue(classPathEntriesContainJar(cp.entries(), "jar2.jar")); 162 } 163 164 private void checkCompileClassPath(ClassPath cp) { 165 assertTrue(classPathEntriesContainJar(cp.entries(), "jar0.jar")); 167 } 169 170 private void checkBootClassPath(ClassPath cp) { 171 assertTrue(classPathEntriesContainFolder(cp.entries(), DEFAULT_PLATFORM_ROOT)); 172 } 173 174 private static boolean classPathEntriesContainJar(List entries, String name) { 175 for (Iterator i = entries.iterator(); i.hasNext();) { 176 ClassPath.Entry e = (ClassPath.Entry)i.next(); 177 URL jar = FileUtil.getArchiveFile(e.getURL()); 178 if (jar != null) { 179 if (name.equals(new File (URI.create(jar.toExternalForm())).getName())) { 180 return true; 181 } 182 } 183 } 184 return false; 185 } 186 187 private static boolean classPathEntriesContainFolder(List entries, String name) { 188 for (Iterator i = entries.iterator(); i.hasNext();) { 189 ClassPath.Entry e = (ClassPath.Entry)i.next(); 190 URL folder = e.getURL(); 191 if ("file".equals(folder.getProtocol())) { 192 if (name.equals(new File (URI.create(folder.toExternalForm())).getName())) { 193 return true; 194 } 195 } 196 } 197 return false; 198 } 199 200 private static FileObject findSourceRoot(SourceGroup[] groups, String 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 |