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