Skip to content

Commit 3468475

Browse files
committed
Test with workspace external project (#135)
1 parent 1b8709b commit 3468475

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.net.URL;
1414
import java.net.URLClassLoader;
1515
import java.nio.charset.StandardCharsets;
16+
import java.nio.file.Files;
1617
import java.util.Collection;
1718
import java.util.HashSet;
1819
import java.util.Iterator;
@@ -24,8 +25,11 @@
2425
import org.eclipse.core.resources.IFile;
2526
import org.eclipse.core.resources.IFolder;
2627
import org.eclipse.core.resources.IProject;
28+
import org.eclipse.core.resources.IProjectDescription;
2729
import org.eclipse.core.resources.IncrementalProjectBuilder;
30+
import org.eclipse.core.resources.ResourcesPlugin;
2831
import org.eclipse.core.runtime.CoreException;
32+
import org.eclipse.core.runtime.Path;
2933
import org.eclipse.ui.IWorkingSet;
3034
import org.junit.After;
3135
import org.junit.Assert;
@@ -522,6 +526,17 @@ private void dumpRuleSet(final RuleSet ruleSet) {
522526
System.out.println();
523527
}
524528

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+
*/
525540
@Test
526541
public void testProjectClasspath() throws Exception {
527542
IProject otherProject = EclipseUtils.createJavaProject("OtherProject");
@@ -532,6 +547,7 @@ public void testProjectClasspath() throws Exception {
532547
IFile sampleLib2 = otherProject.getFile("sample-lib2.jar");
533548
sampleLib2.create(IOUtils.toInputStream("", "UTF-8"), false, null);
534549
File realSampleLib2 = sampleLib2.getLocation().toFile().getCanonicalFile();
550+
535551
IFolder libFolder = this.testProject.getFolder("lib");
536552
libFolder.create(false, true, null);
537553
IFile sampleLib3 = libFolder.getFile("sample-lib3.jar");
@@ -543,6 +559,18 @@ public void testProjectClasspath() throws Exception {
543559
// build the project, so that the output folder "bin/" is created
544560
otherProject2.build(IncrementalProjectBuilder.FULL_BUILD, null);
545561

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+
546574
IFile file = this.testProject.getFile(".classpath");
547575
String newClasspathContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
548576
+ "<classpath>\n"
@@ -553,6 +581,7 @@ public void testProjectClasspath() throws Exception {
553581
+ " <classpathentry kind=\"lib\" path=\"" + realSampleLib2.getAbsolutePath() + "\"/>\n"
554582
+ " <classpathentry kind=\"lib\" path=\"lib/sample-lib3.jar\"/>\n"
555583
+ " <classpathentry kind=\"output\" path=\"bin\"/>\n"
584+
+ " <classpathentry kind=\"lib\" path=\"/ExternalProject/sample-lib4.jar\"/>\n"
556585
+ "</classpath>\n";
557586
file.setContents(IOUtils.toInputStream(newClasspathContent, "UTF-8"), 0, null);
558587
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
@@ -563,7 +592,7 @@ public void testProjectClasspath() throws Exception {
563592
urls.add(url.toURI());
564593
}
565594

566-
Assert.assertEquals(5, urls.size());
595+
Assert.assertEquals(6, urls.size());
567596

568597
// own project's output folder
569598
Assert.assertTrue(urls.remove(
@@ -577,6 +606,8 @@ public void testProjectClasspath() throws Exception {
577606
Assert.assertTrue(urls.remove(realSampleLib2.toURI()));
578607
// sample-lib3.jar stored in own project folder lib
579608
Assert.assertTrue(urls.remove(realSampleLib3.toURI()));
609+
// sample-lib4.jar stored in external project folder outside of workspace
610+
Assert.assertTrue(urls.remove(realSampleLib4.toURI()));
580611

581612
// no remaining urls
582613
Assert.assertTrue(urls.isEmpty());

0 commit comments

Comments
 (0)