|
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; |
12 | 16 | import java.util.Collection; |
| 17 | +import java.util.HashSet; |
13 | 18 | import java.util.Iterator; |
| 19 | +import java.util.LinkedList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
14 | 22 |
|
| 23 | +import org.apache.commons.io.IOUtils; |
15 | 24 | import org.eclipse.core.resources.IFile; |
| 25 | +import org.eclipse.core.resources.IFolder; |
16 | 26 | import org.eclipse.core.resources.IProject; |
| 27 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
17 | 28 | import org.eclipse.core.runtime.CoreException; |
18 | 29 | import org.eclipse.ui.IWorkingSet; |
19 | 30 | import org.junit.After; |
|
43 | 54 | public class ProjectPropertiesModelTest { |
44 | 55 | private IProject testProject; |
45 | 56 | private RuleSet initialPluginRuleSet; |
| 57 | + private List<IProject> additionalProjects = new LinkedList<>(); |
46 | 58 |
|
47 | 59 | @Before |
48 | 60 | public void setUp() throws Exception { |
@@ -88,6 +100,13 @@ public void tearDown() throws Exception { |
88 | 100 |
|
89 | 101 | // 2. Restore the plugin initial rule set |
90 | 102 | PMDPlugin.getDefault().getPreferencesManager().setRuleSet(this.initialPluginRuleSet); |
| 103 | + |
| 104 | + // 3. Delete additional projects |
| 105 | + for (IProject project : additionalProjects) { |
| 106 | + if (project.exists() && project.isAccessible()) { |
| 107 | + project.delete(true, true, null); |
| 108 | + } |
| 109 | + } |
91 | 110 | } |
92 | 111 |
|
93 | 112 | public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) { |
@@ -503,4 +522,63 @@ private void dumpRuleSet(final RuleSet ruleSet) { |
503 | 522 | System.out.println(); |
504 | 523 | } |
505 | 524 |
|
| 525 | + @Test |
| 526 | + public void testProjectClasspath() throws Exception { |
| 527 | + IProject otherProject = EclipseUtils.createJavaProject("OtherProject"); |
| 528 | + additionalProjects.add(otherProject); |
| 529 | + IFile sampleLib1 = otherProject.getFile("sample-lib1.jar"); |
| 530 | + sampleLib1.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 531 | + File realSampleLib1 = sampleLib1.getLocation().toFile().getCanonicalFile(); |
| 532 | + IFile sampleLib2 = otherProject.getFile("sample-lib2.jar"); |
| 533 | + sampleLib2.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 534 | + File realSampleLib2 = sampleLib2.getLocation().toFile().getCanonicalFile(); |
| 535 | + IFolder libFolder = this.testProject.getFolder("lib"); |
| 536 | + libFolder.create(false, true, null); |
| 537 | + IFile sampleLib3 = libFolder.getFile("sample-lib3.jar"); |
| 538 | + sampleLib3.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 539 | + File realSampleLib3 = sampleLib3.getLocation().toFile().getCanonicalFile(); |
| 540 | + |
| 541 | + IProject otherProject2 = EclipseUtils.createJavaProject("OtherProject2"); |
| 542 | + additionalProjects.add(otherProject2); |
| 543 | + // build the project, so that the output folder "bin/" is created |
| 544 | + otherProject2.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 545 | + |
| 546 | + IFile file = this.testProject.getFile(".classpath"); |
| 547 | + String newClasspathContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 548 | + + "<classpath>\n" |
| 549 | + + " <classpathentry kind=\"src\" path=\"src\"/>\n" |
| 550 | + + " <!-- <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/> -->\n" |
| 551 | + + " <classpathentry combineaccessrules=\"false\" kind=\"src\" path=\"/OtherProject2\"/>\n" |
| 552 | + + " <classpathentry kind=\"lib\" path=\"/OtherProject/sample-lib1.jar\"/>\n" |
| 553 | + + " <classpathentry kind=\"lib\" path=\"" + realSampleLib2.getAbsolutePath() + "\"/>\n" |
| 554 | + + " <classpathentry kind=\"lib\" path=\"lib/sample-lib3.jar\"/>\n" |
| 555 | + + " <classpathentry kind=\"output\" path=\"bin\"/>\n" |
| 556 | + + "</classpath>\n"; |
| 557 | + file.setContents(IOUtils.toInputStream(newClasspathContent, "UTF-8"), 0, null); |
| 558 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 559 | + IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 560 | + URLClassLoader auxClasspath = (URLClassLoader) model.getAuxClasspath(); |
| 561 | + Set<URI> urls = new HashSet<>(); |
| 562 | + for (URL url : auxClasspath.getURLs()) { |
| 563 | + urls.add(url.toURI()); |
| 564 | + } |
| 565 | + |
| 566 | + Assert.assertEquals(5, urls.size()); |
| 567 | + |
| 568 | + // own project's output folder |
| 569 | + Assert.assertTrue(urls.remove( |
| 570 | + URI.create(this.testProject.getLocation().toFile().getAbsoluteFile().toURI() + "bin/"))); |
| 571 | + // output folder of other project 2 (project dependency) |
| 572 | + Assert.assertTrue(urls.remove( |
| 573 | + URI.create(otherProject2.getLocation().toFile().getAbsoluteFile().toURI() + "bin/"))); |
| 574 | + // sample-lib1.jar stored in OtherProject |
| 575 | + Assert.assertTrue(urls.remove(realSampleLib1.toURI())); |
| 576 | + // sample-lib2.jar referenced with absolute path |
| 577 | + Assert.assertTrue(urls.remove(realSampleLib2.toURI())); |
| 578 | + // sample-lib3.jar stored in own project folder lib |
| 579 | + Assert.assertTrue(urls.remove(realSampleLib3.toURI())); |
| 580 | + |
| 581 | + // no remaining urls |
| 582 | + Assert.assertTrue(urls.isEmpty()); |
| 583 | + } |
506 | 584 | } |
0 commit comments