Skip to content

Commit 1b8709b

Browse files
committed
Add unit test for ProjectClasspath (#135)
1 parent 3ce05d8 commit 1b8709b

File tree

10 files changed

+146
-1
lines changed

10 files changed

+146
-1
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66

77
import java.io.ByteArrayInputStream;
88
import java.io.ByteArrayOutputStream;
9+
import java.io.File;
910
import java.io.InputStream;
1011
import java.io.PrintStream;
12+
import java.net.URI;
13+
import java.net.URL;
14+
import java.net.URLClassLoader;
1115
import java.nio.charset.StandardCharsets;
1216
import java.util.Collection;
17+
import java.util.HashSet;
1318
import java.util.Iterator;
19+
import java.util.LinkedList;
20+
import java.util.List;
21+
import java.util.Set;
1422

23+
import org.apache.commons.io.IOUtils;
1524
import org.eclipse.core.resources.IFile;
25+
import org.eclipse.core.resources.IFolder;
1626
import org.eclipse.core.resources.IProject;
27+
import org.eclipse.core.resources.IncrementalProjectBuilder;
1728
import org.eclipse.core.runtime.CoreException;
1829
import org.eclipse.ui.IWorkingSet;
1930
import org.junit.After;
@@ -43,6 +54,7 @@
4354
public class ProjectPropertiesModelTest {
4455
private IProject testProject;
4556
private RuleSet initialPluginRuleSet;
57+
private List<IProject> additionalProjects = new LinkedList<>();
4658

4759
@Before
4860
public void setUp() throws Exception {
@@ -88,6 +100,13 @@ public void tearDown() throws Exception {
88100

89101
// 2. Restore the plugin initial rule set
90102
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+
}
91110
}
92111

93112
public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) {
@@ -503,4 +522,63 @@ private void dumpRuleSet(final RuleSet ruleSet) {
503522
System.out.println();
504523
}
505524

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+
}
506584
}

test-projects/project-with-libs/project6/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
</classpathentry>
1010
<classpathentry kind="lib" path="/sample-lib1/sample-lib1-v1.jar"/>
1111
<classpathentry kind="lib" path="/tmp/sample-lib3-v1.jar"/>
12+
<classpathentry kind="lib" path="lib/sample-lib4.jar"/>
1213
<classpathentry kind="output" path="bin"/>
1314
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/bin/
2+
lib/sample-lib4.jar
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package project6;
2+
3+
import sample.lib4.MyInterface;
4+
5+
// MyInterface comes in through a jar dependency in project relativ path /lib/sample-lib4.jar.
6+
// The jar file can be created in project sample-lib4
7+
// The jar file is stored in this project.
8+
public class Sample4 implements MyInterface {
9+
10+
// missing override
11+
public void bar() {
12+
}
13+
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>sample-lib4</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<jardesc>
3+
<jar path="project6/lib/sample-lib4.jar"/>
4+
<options buildIfNeeded="true" compress="true" descriptionLocation="/sample-lib4/create-jar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
5+
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
6+
<selectedProjects/>
7+
<manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
8+
<sealing sealJar="false">
9+
<packagesToSeal/>
10+
<packagesToUnSeal/>
11+
</sealing>
12+
</manifest>
13+
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
14+
<javaElement handleIdentifier="=sample-lib4/src"/>
15+
</selectedElements>
16+
</jardesc>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sample.lib4;
2+
3+
public interface MyInterface {
4+
void bar();
5+
}

test-projects/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@
4343
special to do after import.
4444
* sample-lib3 - lib project, that produces a jar at the absolute location `/tmp/sample-lib3-v1.jar`.
4545
Use "create-jar.jardesc" to create the jar in "/tmp/sample-lib3-v1.jar"
46+
* sample-lib4 - lib project, that produces a jar, that is stored in folder "lib" in project6.
47+
This lib is on the classpath of project6. Use "create-jar.jardesc" to create the jar.
4648
* then execute PMD on project6. It should create a "MissingOverride" violation in
47-
each of the classes "Sample1", "Sample2", and "Sample3".
49+
each of the classes "Sample1", "Sample2", "Sample3", and "Sample4".

0 commit comments

Comments
 (0)