aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-03-20 23:56:08 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-03-20 23:56:08 +0100
commit5e2e55ddee5a9524d0e2369d6c05edb2b3fad225 (patch)
tree8b47a6f885ccd7766a3fd20a0e5cd5c3742558f1
parent484f40b61f29ff0357a0834b3213d2a5e81e20bd (diff)
downloadsonarqube-5e2e55ddee5a9524d0e2369d6c05edb2b3fad225.tar.gz
sonarqube-5e2e55ddee5a9524d0e2369d6c05edb2b3fad225.zip
SONAR-3229 PMD fails when inspecting Java files suffixed with .jav
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java9
-rw-r--r--plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java83
2 files changed, 49 insertions, 43 deletions
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java
index 4f087e253c0..794bbf0c306 100644
--- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java
+++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java
@@ -19,10 +19,6 @@
*/
package org.sonar.plugins.pmd;
-import java.io.File;
-
-import javax.xml.stream.XMLStreamException;
-
import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
@@ -36,6 +32,9 @@ import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.StaxParser;
+import javax.xml.stream.XMLStreamException;
+import java.io.File;
+
class PmdViolationsXmlParser {
private Project project;
@@ -65,6 +64,8 @@ class PmdViolationsXmlParser {
// Save violations only for existing resources
if (context.getResource(resource) != null) {
streamViolations(fileCursor, resource);
+ } else {
+ fileCursor.advance();
}
}
}
diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java
index d63dd05969e..f6aa0b670ca 100644
--- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java
+++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java
@@ -19,37 +19,31 @@
*/
package org.sonar.plugins.pmd;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.File;
-import java.net.URISyntaxException;
-import java.util.Arrays;
-
-import javax.xml.stream.XMLStreamException;
-
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.batch.SensorContext;
-import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
import org.sonar.api.test.IsViolation;
+import java.io.File;
+import java.util.Arrays;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.*;
+
public class PmdViolationsXmlParserTest {
- private void parse(SensorContext context, String xmlPath) throws URISyntaxException, XMLStreamException {
+ private void parse(SensorContext context, String xmlPath, boolean useIndexedResources) throws Exception {
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
when(fileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/test/src/main/java")));
@@ -64,7 +58,11 @@ public class PmdViolationsXmlParserTest {
}
});
- when(context.getResource((JavaFile) any())).thenReturn(new JavaFile(""));
+ if (useIndexedResources) {
+ when(context.getResource((JavaFile) any())).thenReturn(new JavaFile(""));
+ } else {
+ when(context.getResource((JavaFile) any())).thenReturn(null);
+ }
PmdViolationsXmlParser parser = new PmdViolationsXmlParser(project, ruleFinder, context);
@@ -73,56 +71,63 @@ public class PmdViolationsXmlParserTest {
}
@Test
- public void shouldSaveViolationsOnClasses() throws URISyntaxException, XMLStreamException {
+ public void shouldSaveViolationsOnFiles() throws Exception {
SensorContext context = mock(SensorContext.class);
- parse(context, "/org/sonar/plugins/pmd/pmd-result.xml");
+ parse(context, "/org/sonar/plugins/pmd/pmd-result.xml", true);
- verify(context, times(30)).saveViolation(argThat(new IsViolationOnJavaClass()));
- verify(context, times(4)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))));
+ verify(context, times(30)).saveViolation(argThat(new IsViolationOnJavaFile()));
+ verify(context, times(4)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))));
Violation wanted = Violation.create((Rule) null, new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))
- .setMessage("Avoid unused local variables such as 'toto'.")
- .setLineId(22);
+ .setMessage("Avoid unused local variables such as 'toto'.")
+ .setLineId(22);
verify(context, times(1)).saveViolation(argThat(new IsViolation(wanted)));
}
@Test
- public void defaultPackageShouldBeSetOnclassWithoutPackage() throws URISyntaxException, XMLStreamException {
+ public void shouldIgnoreNonIndexedResources() throws Exception {
SensorContext context = mock(SensorContext.class);
- parse(context, "/org/sonar/plugins/pmd/pmd-class-without-package.xml");
- verify(context, times(3)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("ClassOnDefaultPackage"))));
+ parse(context, "/org/sonar/plugins/pmd/pmd-result.xml", false);
+
+ verify(context, never()).saveViolation(argThat(new IsViolationOnJavaFile()));
}
@Test
- public void unknownXMLEntity() throws URISyntaxException, XMLStreamException {
+ public void defaultPackageShouldBeSetOnClassWithoutPackage() throws Exception {
SensorContext context = mock(SensorContext.class);
- parse(context, "/org/sonar/plugins/pmd/pmd-result-with-unknown-entity.xml");
- verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
+ parse(context, "/org/sonar/plugins/pmd/pmd-class-without-package.xml", true);
+ verify(context, times(3)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("ClassOnDefaultPackage"))));
}
@Test
- public void ISOControlCharsXMLFile() throws URISyntaxException, XMLStreamException {
+ public void unknownXMLEntity() throws Exception {
SensorContext context = mock(SensorContext.class);
- parse(context, "/org/sonar/plugins/pmd/pmd-result-with-control-char.xml");
- verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
+ parse(context, "/org/sonar/plugins/pmd/pmd-result-with-unknown-entity.xml", true);
+ verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("test.Test"))));
}
- private class IsViolationOnJavaClass extends BaseMatcher<Violation> {
+ @Test
+ public void ISOControlCharsXMLFile() throws Exception {
+ SensorContext context = mock(SensorContext.class);
+ parse(context, "/org/sonar/plugins/pmd/pmd-result-with-control-char.xml", true);
+ verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("test.Test"))));
+ }
- private JavaFile javaClass;
+ private class IsViolationOnJavaFile extends BaseMatcher<Violation> {
+ private JavaFile javaFile;
- private IsViolationOnJavaClass(JavaFile javaClass) {
- this.javaClass = javaClass;
+ private IsViolationOnJavaFile(JavaFile javaFile) {
+ this.javaFile = javaFile;
}
- private IsViolationOnJavaClass() {
+ private IsViolationOnJavaFile() {
}
public boolean matches(Object o) {
Violation v = (Violation) o;
boolean ok = (v.getResource() != null) && (v.getResource() instanceof JavaFile);
- if (ok && javaClass != null) {
- ok = javaClass.equals(v.getResource());
+ if (ok && javaFile != null) {
+ ok = javaFile.equals(v.getResource());
}
return ok;
}