Skip to content

Commit 2f08e38

Browse files
committed
Fix project includes/excludes under Windows
1 parent 3a7436f commit 2f08e38

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/MultipleRulesetsTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.eclipse.core.runtime.CoreException;
1818
import org.eclipse.core.runtime.NullProgressMonitor;
1919
import org.eclipse.core.runtime.Path;
20-
import org.eclipse.core.runtime.jobs.Job;
2120
import org.eclipse.jdt.core.IClasspathEntry;
2221
import org.eclipse.jdt.core.IJavaProject;
2322
import org.eclipse.jdt.core.JavaCore;
@@ -119,7 +118,7 @@ public void done() {
119118
}
120119
});
121120
latch.await(30, TimeUnit.SECONDS);
122-
waitForJobs();
121+
EclipseUtils.waitForJobs();
123122

124123
final ReviewCodeCmd cmd = new ReviewCodeCmd();
125124
cmd.addResource(this.testProject);
@@ -143,13 +142,6 @@ public void done() {
143142
assertHasRuleViolation(markersThird, "UseUtilityClass");
144143
}
145144

146-
private void waitForJobs() throws InterruptedException {
147-
long start = System.currentTimeMillis();
148-
while (!Job.getJobManager().isIdle() && System.currentTimeMillis() - start < TimeUnit.SECONDS.toMillis(30)) {
149-
Thread.sleep(500);
150-
}
151-
}
152-
153145
private void assertHasRuleViolation(IMarker[] markers, String rulename) throws CoreException {
154146
boolean found = false;
155147
for (IMarker marker : markers) {

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,26 @@ public void projectWithIncludesExcludes() throws Exception {
7575
newClasspath.add(entry);
7676
}
7777
}
78+
// excludes all *.java files, but includes Test.java
7879
newClasspath.add(JavaCore.newSourceEntry(new Path("/ProjectPropertiesImplTest/src/main/java"),
79-
new Path[] {new Path("**/*.java")},
80-
new Path[0], null));
80+
new Path[] {new Path("**/Test.java")},
81+
new Path[] {new Path("**/*.java")}, null));
8182
newClasspath.add(JavaCore.newSourceEntry(new Path("/ProjectPropertiesImplTest/src/main/resources"),
8283
new Path[0],
8384
new Path[] {new Path("**")}, null));
8485
javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), new NullProgressMonitor());
8586

86-
String expectedPattern = testProject.getFolder("/src/main/java").getLocation().toPortableString();
87-
expectedPattern += "/.*/[^/]*\\.java";
88-
8987
IProjectProperties projectProperties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
90-
Set<String> includePatterns = projectProperties.getBuildPathIncludePatterns();
91-
Collection<Pattern> patterns = InternalRuleSetUtil.convertStringPatterns(includePatterns);
9288

89+
Set<String> includeStringPatterns = projectProperties.getBuildPathIncludePatterns();
90+
Collection<Pattern> includePatterns = InternalRuleSetUtil.convertStringPatterns(includeStringPatterns);
91+
Assert.assertEquals(1, includeStringPatterns.size());
9392
Assert.assertEquals(1, includePatterns.size());
94-
Assert.assertEquals(1, patterns.size());
95-
Assert.assertEquals(includePatterns.toString(), patterns.toString());
96-
Assert.assertEquals("[" + expectedPattern + "]", patterns.toString());
93+
94+
Set<String> excludeStringPatterns = projectProperties.getBuildPathExcludePatterns();
95+
Collection<Pattern> excludePatterns = InternalRuleSetUtil.convertStringPatterns(excludeStringPatterns);
96+
Assert.assertEquals(1, excludeStringPatterns.size());
97+
Assert.assertEquals(1, excludePatterns.size());
9798

9899
// now run a review
99100
projectProperties.setPmdEnabled(true);
@@ -111,5 +112,16 @@ public void projectWithIncludesExcludes() throws Exception {
111112
}
112113
}
113114
Assert.assertTrue("EmptyCatchBlock marker is missing", found);
115+
116+
// patterns should be correct
117+
Assert.assertEquals(includeStringPatterns.toString(), includePatterns.toString());
118+
String expectedIncludePattern = testProject.getFolder("/src/main/java").getLocation().toPortableString();
119+
expectedIncludePattern += "/.*Test\\.java";
120+
Assert.assertEquals("[" + expectedIncludePattern + "]", includePatterns.toString());
121+
122+
String expectedExcludePattern = testProject.getFolder("/src/main/java").getLocation().toPortableString();
123+
expectedExcludePattern += "/.*[^/]*\\.java";
124+
Assert.assertEquals(excludeStringPatterns.toString(), excludePatterns.toString());
125+
Assert.assertEquals("[" + expectedExcludePattern + "]", excludePatterns.toString());
114126
}
115127
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,22 @@ public ProjectPropertiesImpl(final IProject project, IProjectPropertiesManager p
8989
private void determineBuildPathIncludesExcludes() {
9090
IClasspathEntry source = PMDPlugin.buildSourceClassPathEntryFor(project);
9191
if (source != null) {
92-
try {
93-
String basePath = new File(project.getWorkspace().getRoot()
94-
.getFolder(source.getPath()).getLocation().toOSString()).getCanonicalPath();
95-
if (!basePath.endsWith(File.separator)) {
96-
basePath += File.separator;
97-
}
98-
if (source.getExclusionPatterns() != null) {
99-
for (IPath path : source.getExclusionPatterns()) {
100-
String pathString = path.toOSString();
101-
buildPathExcludePatterns.add(basePath + convertPatternToRegex(pathString));
102-
}
92+
String basePath = project.getWorkspace().getRoot()
93+
.getFolder(source.getPath()).getLocation().toPortableString();
94+
if (!basePath.endsWith(String.valueOf(IPath.SEPARATOR))) {
95+
basePath += IPath.SEPARATOR;
96+
}
97+
if (source.getExclusionPatterns() != null) {
98+
for (IPath path : source.getExclusionPatterns()) {
99+
String pathString = path.toPortableString();
100+
buildPathExcludePatterns.add(basePath + convertPatternToRegex(pathString));
103101
}
104-
if (source.getInclusionPatterns() != null) {
105-
for (IPath path : source.getInclusionPatterns()) {
106-
String pathString = path.toOSString();
107-
buildPathIncludePatterns.add(basePath + convertPatternToRegex(pathString));
108-
}
102+
}
103+
if (source.getInclusionPatterns() != null) {
104+
for (IPath path : source.getInclusionPatterns()) {
105+
String pathString = path.toPortableString();
106+
buildPathIncludePatterns.add(basePath + convertPatternToRegex(pathString));
109107
}
110-
} catch (IOException e) {
111-
LOG.error("Couldn't determine build class path", e);
112108
}
113109
}
114110
}
@@ -117,9 +113,11 @@ private void determineBuildPathIncludesExcludes() {
117113
* Simple conversion from the Ant-like pattern to regex pattern.
118114
*/
119115
private String convertPatternToRegex(String pattern) {
120-
String regex = pattern.replaceAll("\\.", "\\\\."); // replace "." with "\\."
116+
String regex = pattern;
117+
regex = regex.replaceAll("\\.", "\\\\."); // replace "." with "\\."
121118
regex = regex.replaceAll("\\*\\*", ".*"); // replace "**" with ".*"
122-
regex = regex.replaceAll("/\\*", "/[^/]*"); // replace "/*" with "/[^/]"
119+
regex = regex.replaceAll("/\\*([^\\*])", "/[^/]*$1"); // replace "/*" with "/[^/]*"
120+
regex = regex.replaceAll("\\.\\*/", ".*"); // replace ".*/" with ".*"
123121
regex = regex.replaceAll("\\?", "."); // replace "?" with "."
124122
return regex;
125123
}

0 commit comments

Comments
 (0)