|
15 | 15 | import java.net.URLClassLoader; |
16 | 16 | import java.nio.charset.StandardCharsets; |
17 | 17 | import java.nio.file.Files; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
18 | 20 | import java.util.Collection; |
19 | 21 | import java.util.HashSet; |
20 | 22 | import java.util.LinkedList; |
21 | 23 | import java.util.List; |
22 | 24 | import java.util.Objects; |
23 | 25 | import java.util.Set; |
| 26 | +import java.util.regex.Pattern; |
24 | 27 |
|
25 | 28 | import org.apache.commons.io.IOUtils; |
26 | 29 | import org.eclipse.core.resources.IFile; |
@@ -523,9 +526,11 @@ private void dumpRuleSet(final RuleSet ruleSet) { |
523 | 526 | * </ul> |
524 | 527 | * |
525 | 528 | * @throws Exception |
| 529 | + * @deprecated Since 7.21.0. Tests the deprecated {@link IProjectProperties#getAuxClasspath()} method. |
526 | 530 | */ |
527 | 531 | @Test |
528 | | - public void testProjectClasspath() throws Exception { |
| 532 | + @Deprecated |
| 533 | + public void testProjectClasspathClassloader() throws Exception { |
529 | 534 | IProject otherProject = EclipseUtils.createJavaProject("OtherProject"); |
530 | 535 | additionalProjects.add(otherProject); |
531 | 536 | IFile sampleLib1 = otherProject.getFile("sample-lib1.jar"); |
@@ -607,4 +612,95 @@ public void testProjectClasspath() throws Exception { |
607 | 612 | // no remaining urls |
608 | 613 | Assert.assertTrue(urls.isEmpty()); |
609 | 614 | } |
| 615 | + |
| 616 | + /** |
| 617 | + * Project structure: |
| 618 | + * <ul> |
| 619 | + * <li>this.testProject "ProjectPropertiesModelTest": main project, with build path, contains lib/sample-lib3.jar</li> |
| 620 | + * <li>otherProject "OtherProject": contains sample-lib1.jar, sample-lib2.jar</li> |
| 621 | + * <li>otherProject2 "OtherProject2": ProjectPropertiesModelTest depends on this</li> |
| 622 | + * <li>externalProject "ExternalProject": not stored within workspace, contains sample-lib4.jar</li> |
| 623 | + * </ul> |
| 624 | + * |
| 625 | + * @throws Exception |
| 626 | + */ |
| 627 | + @Test |
| 628 | + public void testProjectClasspath() throws Exception { |
| 629 | + IProject otherProject = EclipseUtils.createJavaProject("OtherProject"); |
| 630 | + additionalProjects.add(otherProject); |
| 631 | + IFile sampleLib1 = otherProject.getFile("sample-lib1.jar"); |
| 632 | + sampleLib1.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 633 | + File realSampleLib1 = sampleLib1.getLocation().toFile().getCanonicalFile(); |
| 634 | + IFile sampleLib2 = otherProject.getFile("sample-lib2.jar"); |
| 635 | + sampleLib2.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 636 | + File realSampleLib2 = sampleLib2.getLocation().toFile().getCanonicalFile(); |
| 637 | + |
| 638 | + IFolder libFolder = this.testProject.getFolder("lib"); |
| 639 | + libFolder.create(false, true, null); |
| 640 | + IFile sampleLib3 = libFolder.getFile("sample-lib3.jar"); |
| 641 | + sampleLib3.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 642 | + File realSampleLib3 = sampleLib3.getLocation().toFile().getCanonicalFile(); |
| 643 | + |
| 644 | + IProject otherProject2 = EclipseUtils.createJavaProject("OtherProject2"); |
| 645 | + additionalProjects.add(otherProject2); |
| 646 | + // build the project, so that the output folder "bin/" is created |
| 647 | + otherProject2.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 648 | + |
| 649 | + IProject externalProject = ResourcesPlugin.getWorkspace().getRoot().getProject("ExternalProject"); |
| 650 | + additionalProjects.add(externalProject); |
| 651 | + Assert.assertFalse("Project must not exist yet", externalProject.exists()); |
| 652 | + java.nio.file.Path externalProjectDir = Files.createTempDirectory("pmd-eclipse-plugin"); |
| 653 | + IProjectDescription description = externalProject.getWorkspace().newProjectDescription("ExternalProject"); |
| 654 | + description.setLocation(Path.fromOSString(externalProjectDir.toString())); |
| 655 | + externalProject.create(description, null); |
| 656 | + externalProject.open(null); |
| 657 | + IFile sampleLib4 = externalProject.getFile("sample-lib4.jar"); |
| 658 | + sampleLib4.create(IOUtils.toInputStream("", "UTF-8"), false, null); |
| 659 | + File realSampleLib4 = sampleLib4.getLocation().toFile().getCanonicalFile(); |
| 660 | + |
| 661 | + // build the project, so that the output folder "bin/" is created |
| 662 | + this.testProject.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 663 | + |
| 664 | + IFile file = this.testProject.getFile(".classpath"); |
| 665 | + String newClasspathContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 666 | + + "<classpath>\n" |
| 667 | + + " <classpathentry kind=\"src\" path=\"src\"/>\n" |
| 668 | + + " <!-- <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/> -->\n" |
| 669 | + + " <classpathentry combineaccessrules=\"false\" kind=\"src\" path=\"/OtherProject2\"/>\n" |
| 670 | + + " <classpathentry kind=\"lib\" path=\"/OtherProject/sample-lib1.jar\"/>\n" |
| 671 | + + " <classpathentry kind=\"lib\" path=\"" + realSampleLib2.getAbsolutePath() + "\"/>\n" |
| 672 | + + " <classpathentry kind=\"lib\" path=\"lib/sample-lib3.jar\"/>\n" |
| 673 | + + " <classpathentry kind=\"output\" path=\"bin\"/>\n" |
| 674 | + + " <classpathentry kind=\"lib\" path=\"/ExternalProject/sample-lib4.jar\"/>\n" |
| 675 | + + "</classpath>\n"; |
| 676 | + file.setContents(IOUtils.toInputStream(newClasspathContent, "UTF-8"), 0, null); |
| 677 | + // refresh, so that changed .classpath file is considered |
| 678 | + this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 679 | + // rebuild again, so that changed classpath is configured on java project |
| 680 | + this.testProject.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 681 | + |
| 682 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 683 | + IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 684 | + List<String> classpath = new ArrayList<>(Arrays.asList(model.getClasspath().split(Pattern.quote(File.pathSeparator)))); |
| 685 | + |
| 686 | + Assert.assertEquals("Found these paths: " + classpath, 6, classpath.size()); |
| 687 | + |
| 688 | + // own project's output folder |
| 689 | + Assert.assertTrue(classpath.remove( |
| 690 | + new File(this.testProject.getLocation().toFile().getAbsoluteFile(), "bin").toString())); |
| 691 | + // output folder of other project 2 (project dependency) |
| 692 | + Assert.assertTrue(classpath.remove( |
| 693 | + new File(otherProject2.getLocation().toFile().getAbsoluteFile(), "bin").toString())); |
| 694 | + // sample-lib1.jar stored in OtherProject |
| 695 | + Assert.assertTrue(classpath.remove(realSampleLib1.toString())); |
| 696 | + // sample-lib2.jar referenced with absolute path |
| 697 | + Assert.assertTrue(classpath.remove(realSampleLib2.toString())); |
| 698 | + // sample-lib3.jar stored in own project folder lib |
| 699 | + Assert.assertTrue(classpath.remove(realSampleLib3.toString())); |
| 700 | + // sample-lib4.jar stored in external project folder outside of workspace |
| 701 | + Assert.assertTrue(classpath.remove(realSampleLib4.toString())); |
| 702 | + |
| 703 | + // no remaining urls |
| 704 | + Assert.assertTrue(classpath.isEmpty()); |
| 705 | + } |
610 | 706 | } |
0 commit comments