|
6 | 6 |
|
7 | 7 | import java.io.ByteArrayInputStream; |
8 | 8 | import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.File; |
9 | 10 | import java.io.InputStream; |
10 | 11 | import java.io.PrintStream; |
| 12 | +import java.net.URI; |
| 13 | +import java.net.URL; |
| 14 | +import java.net.URLClassLoader; |
11 | 15 | import java.nio.charset.StandardCharsets; |
| 16 | +import java.nio.file.Files; |
12 | 17 | import java.util.Collection; |
| 18 | +import java.util.HashSet; |
13 | 19 | import java.util.Iterator; |
| 20 | +import java.util.LinkedList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
14 | 23 |
|
| 24 | +import org.apache.commons.io.IOUtils; |
15 | 25 | import org.eclipse.core.resources.IFile; |
| 26 | +import org.eclipse.core.resources.IFolder; |
16 | 27 | import org.eclipse.core.resources.IProject; |
| 28 | +import org.eclipse.core.resources.IProjectDescription; |
| 29 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 30 | +import org.eclipse.core.resources.ResourcesPlugin; |
17 | 31 | import org.eclipse.core.runtime.CoreException; |
| 32 | +import org.eclipse.core.runtime.Path; |
18 | 33 | import org.eclipse.ui.IWorkingSet; |
19 | 34 | import org.junit.After; |
20 | 35 | import org.junit.Assert; |
|
43 | 58 | public class ProjectPropertiesModelTest { |
44 | 59 | private IProject testProject; |
45 | 60 | private RuleSet initialPluginRuleSet; |
| 61 | + private List<IProject> additionalProjects = new LinkedList<>(); |
46 | 62 |
|
47 | 63 | @Before |
48 | 64 | public void setUp() throws Exception { |
@@ -88,6 +104,13 @@ public void tearDown() throws Exception { |
88 | 104 |
|
89 | 105 | // 2. Restore the plugin initial rule set |
90 | 106 | PMDPlugin.getDefault().getPreferencesManager().setRuleSet(this.initialPluginRuleSet); |
| 107 | + |
| 108 | + // 3. Delete additional projects |
| 109 | + for (IProject project : additionalProjects) { |
| 110 | + if (project.exists() && project.isAccessible()) { |
| 111 | + project.delete(true, true, null); |
| 112 | + } |
| 113 | + } |
91 | 114 | } |
92 | 115 |
|
93 | 116 | public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) { |
@@ -503,4 +526,93 @@ private void dumpRuleSet(final RuleSet ruleSet) { |
503 | 526 | System.out.println(); |
504 | 527 | } |
505 | 528 |
|
| 529 | + /** |
| 530 | + * Project structure: |
| 531 | + * <ul> |
| 532 | + * <li>this.testProject "PMDTestProject": main project, with build path, contains lib/sample-lib3.jar</li> |
| 533 | + * <li>otherProject "OtherProject": contains sample-lib1.jar, sample-lib2.jar</li> |
| 534 | + * <li>otherProject2 "OtherProject2": PMDTestProject depends on this</li> |
| 535 | + * <li>externalProject "ExternalProject": not stored within workspace, contains sample-lib4.jar</li> |
| 536 | + * </ul> |
| 537 | + * |
| 538 | + * @throws Exception |
| 539 | + */ |
| 540 | + @Test |
| 541 | + public void testProjectClasspath() throws Exception { |
| 542 | + IProject otherProject = EclipseUtils.createJavaProject("OtherProject"); |
| 543 | + additionalProjects.add(otherProject); |
| 544 | + IFile sampleLib1 = otherProject.getFile("sample-lib1.jar"); |
| 545 | + sampleLib1.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 546 | + File realSampleLib1 = sampleLib1.getLocation().toFile().getCanonicalFile(); |
| 547 | + IFile sampleLib2 = otherProject.getFile("sample-lib2.jar"); |
| 548 | + sampleLib2.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 549 | + File realSampleLib2 = sampleLib2.getLocation().toFile().getCanonicalFile(); |
| 550 | + |
| 551 | + IFolder libFolder = this.testProject.getFolder("lib"); |
| 552 | + libFolder.create(false, true, null); |
| 553 | + IFile sampleLib3 = libFolder.getFile("sample-lib3.jar"); |
| 554 | + sampleLib3.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 555 | + File realSampleLib3 = sampleLib3.getLocation().toFile().getCanonicalFile(); |
| 556 | + |
| 557 | + IProject otherProject2 = EclipseUtils.createJavaProject("OtherProject2"); |
| 558 | + additionalProjects.add(otherProject2); |
| 559 | + // build the project, so that the output folder "bin/" is created |
| 560 | + otherProject2.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 561 | + |
| 562 | + IProject externalProject = ResourcesPlugin.getWorkspace().getRoot().getProject("ExternalProject"); |
| 563 | + additionalProjects.add(externalProject); |
| 564 | + Assert.assertFalse("Project must not exist yet", externalProject.exists()); |
| 565 | + java.nio.file.Path externalProjectDir = Files.createTempDirectory("pmd-eclipse-plugin"); |
| 566 | + IProjectDescription description = externalProject.getWorkspace().newProjectDescription("ExternalProject"); |
| 567 | + description.setLocation(Path.fromOSString(externalProjectDir.toString())); |
| 568 | + externalProject.create(description, null); |
| 569 | + externalProject.open(null); |
| 570 | + IFile sampleLib4 = externalProject.getFile("sample-lib4.jar"); |
| 571 | + sampleLib4.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 572 | + File realSampleLib4 = sampleLib4.getLocation().toFile().getCanonicalFile(); |
| 573 | + |
| 574 | + // build the project, so that the output folder "bin/" is created |
| 575 | + this.testProject.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 576 | + |
| 577 | + IFile file = this.testProject.getFile(".classpath"); |
| 578 | + String newClasspathContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 579 | + + "<classpath>\n" |
| 580 | + + " <classpathentry kind=\"src\" path=\"src\"/>\n" |
| 581 | + + " <!-- <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/> -->\n" |
| 582 | + + " <classpathentry combineaccessrules=\"false\" kind=\"src\" path=\"/OtherProject2\"/>\n" |
| 583 | + + " <classpathentry kind=\"lib\" path=\"/OtherProject/sample-lib1.jar\"/>\n" |
| 584 | + + " <classpathentry kind=\"lib\" path=\"" + realSampleLib2.getAbsolutePath() + "\"/>\n" |
| 585 | + + " <classpathentry kind=\"lib\" path=\"lib/sample-lib3.jar\"/>\n" |
| 586 | + + " <classpathentry kind=\"output\" path=\"bin\"/>\n" |
| 587 | + + " <classpathentry kind=\"lib\" path=\"/ExternalProject/sample-lib4.jar\"/>\n" |
| 588 | + + "</classpath>\n"; |
| 589 | + file.setContents(IOUtils.toInputStream(newClasspathContent, "UTF-8"), 0, null); |
| 590 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 591 | + IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 592 | + URLClassLoader auxClasspath = (URLClassLoader) model.getAuxClasspath(); |
| 593 | + Set<URI> urls = new HashSet<>(); |
| 594 | + for (URL url : auxClasspath.getURLs()) { |
| 595 | + urls.add(url.toURI()); |
| 596 | + } |
| 597 | + |
| 598 | + Assert.assertEquals(6, urls.size()); |
| 599 | + |
| 600 | + // own project's output folder |
| 601 | + Assert.assertTrue(urls.remove( |
| 602 | + URI.create(this.testProject.getLocation().toFile().getAbsoluteFile().toURI() + "bin/"))); |
| 603 | + // output folder of other project 2 (project dependency) |
| 604 | + Assert.assertTrue(urls.remove( |
| 605 | + URI.create(otherProject2.getLocation().toFile().getAbsoluteFile().toURI() + "bin/"))); |
| 606 | + // sample-lib1.jar stored in OtherProject |
| 607 | + Assert.assertTrue(urls.remove(realSampleLib1.toURI())); |
| 608 | + // sample-lib2.jar referenced with absolute path |
| 609 | + Assert.assertTrue(urls.remove(realSampleLib2.toURI())); |
| 610 | + // sample-lib3.jar stored in own project folder lib |
| 611 | + Assert.assertTrue(urls.remove(realSampleLib3.toURI())); |
| 612 | + // sample-lib4.jar stored in external project folder outside of workspace |
| 613 | + Assert.assertTrue(urls.remove(realSampleLib4.toURI())); |
| 614 | + |
| 615 | + // no remaining urls |
| 616 | + Assert.assertTrue(urls.isEmpty()); |
| 617 | + } |
506 | 618 | } |
0 commit comments