1 19 20 package org.netbeans.modules.web.project.classpath; 21 22 import java.io.File ; 23 import java.net.URL ; 24 import java.util.Iterator ; 25 import org.netbeans.api.java.classpath.ClassPath; 26 import org.netbeans.api.java.platform.JavaPlatformManager; 27 import org.netbeans.api.java.project.JavaProjectConstants; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.modules.web.project.TestPlatformProvider; 35 import org.netbeans.modules.web.project.WebProject; 36 import org.netbeans.modules.web.project.test.TestBase; 37 import org.netbeans.modules.web.project.test.TestUtil; 38 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 39 import org.netbeans.spi.java.classpath.ClassPathProvider; 40 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 41 import org.netbeans.spi.project.support.ant.AntProjectHelper; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 45 49 public class ClassPathProviderImplTest extends NbTestCase { 50 51 private Project project; 52 private Project project59055; 53 private AntProjectHelper helper; 54 private AntProjectHelper helper59055; 55 private FileObject sourceRoot; 56 private FileObject sourceRoot59055_1; 57 private FileObject sourceRoot59055_2; 58 private FileObject testRoot; 59 private FileObject testRoot59055_1; 60 private FileObject testRoot59055_2; 61 private FileObject webRoot; 62 private FileObject webRoot59055; 63 private FileObject bootPlatformRoot; 64 private FileObject sourceClass; 65 private FileObject sourceClass59055_1; 66 private FileObject sourceClass59055_2; 67 private FileObject testClass; 68 private FileObject testClass59055_1; 69 private FileObject testClass59055_2; 70 private FileObject jspPage; 71 private FileObject jspPage59055; 72 73 public ClassPathProviderImplTest(String testName) { 74 super(testName); 75 } 76 77 public void setUp() throws Exception { 78 FileObject scratch = TestUtil.makeScratchDir(this); 80 bootPlatformRoot = scratch.createFolder("DefaultPlatformRoot"); 81 ClassPath defBCP = ClassPathSupport.createClassPath(new URL [] { bootPlatformRoot.getURL() }); 82 83 TestBase.setLookup(new Object [] { 84 new TestPlatformProvider(defBCP, defBCP) 85 }); 86 87 assertTrue("No Java platforms found.", JavaPlatformManager.getDefault().getInstalledPlatforms().length >= 2); 88 89 File f = new File (getDataDir().getAbsolutePath(), "projects/WebApplication1"); 91 project = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f)); 92 Sources src = (Sources)project.getLookup().lookup(Sources.class); 93 SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 94 95 sourceRoot = findSourceRoot(groups, "${src.dir}"); 96 assertNotNull(sourceRoot); 97 testRoot = findSourceRoot(groups, "${test.src.dir}"); 98 assertNotNull(testRoot); 99 100 sourceClass = sourceRoot.getFileObject("pkg/NewClass.java"); 101 assertNotNull(sourceClass); 102 testClass = testRoot.getFileObject("pkg/NewClassTest.java"); 103 assertNotNull(testClass); 104 105 helper = ((WebProject)project).getAntProjectHelper(); 107 String web = helper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 108 webRoot = helper.resolveFileObject(web); 109 jspPage = webRoot.getFileObject("index.jsp"); 110 111 File f59055 = new File (getDataDir().getAbsolutePath(), "projects/WebApplication59055"); 115 project59055 = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f59055)); 116 Sources src59055 = (Sources) project59055.getLookup().lookup(Sources.class); 117 SourceGroup[] groups59055 = src59055.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 118 119 sourceRoot59055_1 = findSourceRoot(groups59055, "${src.dir}"); 120 assertNotNull(sourceRoot59055_1); 121 sourceRoot59055_2 = findSourceRoot(groups59055, "${src.src2.dir}"); 122 assertNotNull(sourceRoot59055_2); 123 testRoot59055_1 = findSourceRoot(groups59055, "${test.src.dir}"); 124 assertNotNull(testRoot59055_1); 125 testRoot59055_2 = findSourceRoot(groups59055, "${test.test2.dir}"); 126 assertNotNull(testRoot59055_2); 127 128 sourceClass59055_1 = sourceRoot59055_1.getFileObject("org/wa59055/Class1.java"); 129 assertNotNull(sourceClass59055_1); 130 sourceClass59055_2 = sourceRoot59055_2.getFileObject("org/wa59055/extra/Class2.java"); 131 assertNotNull(sourceClass59055_2); 132 testClass59055_1 = testRoot59055_1.getFileObject("org/wa59055/Class1Test.java"); 133 assertNotNull(testClass59055_1); 134 testClass59055_2 = testRoot59055_2.getFileObject("org/wa59055/extra/Class2Test.java"); 135 assertNotNull(testClass59055_2); 136 137 helper59055 = ((WebProject) project59055).getAntProjectHelper(); 139 String web59055 = helper59055.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 140 webRoot59055 = helper59055.resolveFileObject(web59055); 141 jspPage59055 = webRoot59055.getFileObject("index.jsp"); 142 } 143 144 public void testClassPaths() throws Exception { 145 System.out.println("ClassPathProviderImplTest - WebApplication1"); 146 ClassPathProvider cpp = (ClassPathProvider)project.getLookup().lookup(ClassPathProvider.class); 147 assertTrue("No ClassPathProvider in project lookup!", cpp != null); 148 149 ClassPath cp; 150 151 153 155 cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE); 156 checkSourceSourceClassPath(cp); 157 cp = cpp.findClassPath(sourceClass, ClassPath.SOURCE); 158 checkSourceSourceClassPath(cp); 159 160 cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE); 161 checkCompileClassPath(cp); 162 cp = cpp.findClassPath(sourceClass, ClassPath.COMPILE); 163 checkCompileClassPath(cp); 164 165 cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE); 166 checkSourceExecuteClassPath(cp); 167 cp = cpp.findClassPath(sourceClass, ClassPath.EXECUTE); 168 checkSourceExecuteClassPath(cp); 169 170 cp = cpp.findClassPath(sourceClass, ClassPath.BOOT); 171 checkBootClassPath(cp); 172 cp = cpp.findClassPath(sourceClass, ClassPath.BOOT); 173 checkBootClassPath(cp); 174 175 177 cp = cpp.findClassPath(testClass, ClassPath.SOURCE); 178 checkTestSourceClassPath(cp); 179 cp = cpp.findClassPath(testClass, ClassPath.SOURCE); 180 checkTestSourceClassPath(cp); 181 182 cp = cpp.findClassPath(testClass, ClassPath.COMPILE); 183 checkCompileClassPath(cp); 184 cp = cpp.findClassPath(testClass, ClassPath.COMPILE); 185 checkCompileClassPath(cp); 186 187 cp = cpp.findClassPath(testClass, ClassPath.EXECUTE); 188 checkTestExecuteClassPath(cp); 189 cp = cpp.findClassPath(testClass, ClassPath.EXECUTE); 190 checkTestExecuteClassPath(cp); 191 192 cp = cpp.findClassPath(testClass, ClassPath.BOOT); 193 checkBootClassPath(cp); 194 cp = cpp.findClassPath(testClass, ClassPath.BOOT); 195 checkBootClassPath(cp); 196 197 199 cp = cpp.findClassPath(jspPage, ClassPath.SOURCE); 200 checkJSPSourceClassPath(cp); 201 cp = cpp.findClassPath(jspPage, ClassPath.SOURCE); 202 checkJSPSourceClassPath(cp); 203 204 System.out.println("ClassPathProviderImplTest - WebApplication59055"); 209 cpp = (ClassPathProvider) project59055.getLookup().lookup(ClassPathProvider.class); 210 assertTrue("No ClassPathProvider in project lookup!", cpp != null); 211 212 cp = cpp.findClassPath(sourceClass59055_1, ClassPath.SOURCE); 214 checkSourceSourceClassPath59055(cp); 215 cp = cpp.findClassPath(sourceClass59055_1, ClassPath.SOURCE); 216 checkSourceSourceClassPath59055(cp); 217 cp = cpp.findClassPath(sourceClass59055_2, ClassPath.SOURCE); 219 checkSourceSourceClassPath59055(cp); 220 cp = cpp.findClassPath(sourceClass59055_2, ClassPath.SOURCE); 221 checkSourceSourceClassPath59055(cp); 222 223 cp = cpp.findClassPath(testClass59055_1, ClassPath.SOURCE); 225 checkTestSourceClassPath59055(cp); 226 cp = cpp.findClassPath(testClass59055_1, ClassPath.SOURCE); 227 checkTestSourceClassPath59055(cp); 228 cp = cpp.findClassPath(testClass59055_2, ClassPath.SOURCE); 230 checkTestSourceClassPath59055(cp); 231 cp = cpp.findClassPath(testClass59055_2, ClassPath.SOURCE); 232 checkTestSourceClassPath59055(cp); 233 234 cp = cpp.findClassPath(jspPage59055, ClassPath.SOURCE); 235 checkJSPSourceClassPath59055(cp); 236 cp = cpp.findClassPath(jspPage59055, ClassPath.SOURCE); 237 checkJSPSourceClassPath59055(cp); 238 } 239 240 private void checkSourceSourceClassPath(ClassPath cp) { 241 FileObject[] roots = cp.getRoots(); 242 assertEquals(1, roots.length); 243 assertTrue(cp.getRoots()[0].equals(sourceRoot)); 244 } 245 246 private void checkSourceSourceClassPath59055(ClassPath cp) { 247 FileObject[] roots = cp.getRoots(); 248 assertEquals(2, roots.length); 249 assertTrue(cp.getRoots()[0].equals(sourceRoot59055_1)); 250 assertTrue(cp.getRoots()[1].equals(sourceRoot59055_2)); 251 } 252 253 private void checkSourceExecuteClassPath(ClassPath cp) throws Exception { 254 assertTrue(classPathContainsURL(cp, resolveURL("libs/jar1.jar"))); 256 } 257 258 private void checkTestSourceClassPath(ClassPath cp) { 259 FileObject[] roots = cp.getRoots(); 260 assertEquals(1, roots.length); 261 assertTrue(cp.getRoots()[0].equals(testRoot)); 262 } 263 264 private void checkTestSourceClassPath59055(ClassPath cp) { 265 FileObject[] roots = cp.getRoots(); 266 assertEquals(2, roots.length); 267 assertTrue(cp.getRoots()[0].equals(testRoot59055_1)); 268 assertTrue(cp.getRoots()[1].equals(testRoot59055_2)); 269 } 270 271 private void checkTestExecuteClassPath(ClassPath cp) throws Exception { 272 } 276 277 private void checkJSPSourceClassPath(ClassPath cp) { 278 FileObject[] roots = cp.getRoots(); 279 assertEquals(2, roots.length); 280 assertTrue(cp.getRoots()[0].equals(webRoot)); 281 assertTrue(cp.getRoots()[1].equals(sourceRoot)); 282 } 283 284 private void checkJSPSourceClassPath59055(ClassPath cp) { 285 FileObject[] roots = cp.getRoots(); 286 assertEquals(3, roots.length); 287 assertTrue(cp.getRoots()[0].equals(webRoot59055)); 288 assertTrue(cp.getRoots()[1].equals(sourceRoot59055_1)); 289 assertTrue(cp.getRoots()[2].equals(sourceRoot59055_2)); 290 } 291 292 private void checkCompileClassPath(ClassPath cp) throws Exception { 293 assertTrue(classPathContainsURL(cp, resolveURL("libs/jar0.jar"))); 295 } 297 298 private void checkBootClassPath(ClassPath cp) throws Exception { 299 assertTrue(classPathContainsURL(cp, bootPlatformRoot.getURL())); 300 } 301 302 private URL resolveURL(String relative) throws Exception { 303 return helper.resolveFileObject(relative).getURL(); 304 } 305 306 private static final boolean classPathContainsURL(ClassPath cp, URL url) { 307 if (FileUtil.isArchiveFile(url)) { 308 url = FileUtil.getArchiveRoot(url); 309 } 310 for (Iterator i = cp.entries().iterator(); i.hasNext();) { 311 ClassPath.Entry e = (ClassPath.Entry)i.next(); 312 if (e.getURL().equals(url)) { 313 return true; 314 } 315 } 316 return false; 317 } 318 319 private static FileObject findSourceRoot(SourceGroup[] groups, String name) { 320 for (int i = 0; i < groups.length; i++) { 321 if (name.equals(groups[i].getName())) { 322 return groups[i].getRootFolder(); 323 } 324 } 325 return null; 326 } 327 } 328 | Popular Tags |