aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-04-08 09:13:10 +0200
committersimonbrandhof <simon.brandhof@gmail.com>2011-04-08 09:13:10 +0200
commit7d231a67761c8c5637435d21a7e24364bb00fbe1 (patch)
tree707248a373f86ab868165cd1432eb5bd0f857e86 /plugins
parentaa5de1d8e7f8f6d68f9be98ad9536e4f6db88b6f (diff)
downloadsonarqube-7d231a67761c8c5637435d21a7e24364bb00fbe1.tar.gz
sonarqube-7d231a67761c8c5637435d21a7e24364bb00fbe1.zip
SONAR-2322 Filter unit tests files listed in surefire reports
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java41
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java9
-rw-r--r--plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java21
-rw-r--r--plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java23
4 files changed, 30 insertions, 64 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
index 9627ba5f9f5..e8c0cf381e3 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
@@ -19,9 +19,13 @@
*/
package org.sonar.plugins.squid;
+import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.*;
+import org.sonar.api.batch.DependedUpon;
+import org.sonar.api.batch.Phase;
+import org.sonar.api.batch.Sensor;
+import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.*;
import org.sonar.api.utils.SonarException;
import org.sonar.java.api.JavaUtils;
@@ -31,15 +35,25 @@ import java.nio.charset.Charset;
import java.util.List;
@Phase(name = Phase.Name.PRE)
-@DependsUpon(classes = SquidSensor.class)
-@DependedUpon(JavaUtils.BARRIER_AFTER_SQUID)
+@DependedUpon(JavaUtils.BARRIER_BEFORE_SQUID)
public final class JavaSourceImporter implements Sensor {
+ private boolean importSources = false;
+
+ public JavaSourceImporter(Configuration conf) {
+ this.importSources = conf.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
+ CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
+ }
+
+ JavaSourceImporter(boolean importSources) {
+ this.importSources = importSources;
+ }
+
/**
* {@inheritDoc}
*/
public boolean shouldExecuteOnProject(Project project) {
- return isEnabled(project) && Java.KEY.equals(project.getLanguageKey());
+ return Java.KEY.equals(project.getLanguageKey());
}
/**
@@ -63,14 +77,12 @@ public final class JavaSourceImporter implements Sensor {
void importSource(SensorContext context, JavaFile javaFile, InputFile inputFile, Charset sourcesEncoding) {
try {
- //if (!context.isIndexed(javaFile, true)) {
- // See http://jira.codehaus.org/browse/SONAR-791
- // Squid is the reference plugin to index files. If a file is not indexed,
- // throw new SonarException("Invalid file: " + javaFile + ". Please check that Java source directories match root directories" +
- // " as defined by packages.");
- //}
- String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
- context.saveSource(javaFile, source);
+ context.index(javaFile);
+
+ if (importSources) {
+ String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
+ context.saveSource(javaFile, source);
+ }
} catch (IOException e) {
throw new SonarException("Unable to read and import the source file : '" + inputFile.getFile().getAbsolutePath() + "' with the charset : '"
@@ -78,11 +90,6 @@ public final class JavaSourceImporter implements Sensor {
}
}
- boolean isEnabled(Project project) {
- return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
- CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
- }
-
@Override
public String toString() {
return getClass().getSimpleName();
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
index ec98b96ee15..b6417ce7958 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
@@ -48,7 +48,7 @@ public class JavaSourceImporterTest {
when(inputFile.getRelativePath()).thenReturn("UndocumentedApi.java");
when(inputFile.getFile()).thenReturn(fileToImport);
when(inputFile.getFileBaseDir()).thenReturn(fileToImport.getParentFile());
- importer = new JavaSourceImporter();
+ importer = new JavaSourceImporter(true);
context = mock(SensorContext.class);
}
@@ -61,11 +61,4 @@ public class JavaSourceImporterTest {
verify(context).saveSource(eq(javaFile), anyString());
}
- @Test(expected = SonarException.class)
- @Ignore("see SONAR-791")
- public void shouldFailWhenSquidDidNotIndexFile() throws IOException {
- JavaFile javaFile = new JavaFile("Bar");
- when(context.isIndexed(javaFile, true)).thenReturn(false);
- importer.importSource(context, javaFile, inputFile, Charset.defaultCharset());
- }
} \ No newline at end of file
diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java
index b13e667ebe0..c8d6a5fd372 100644
--- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java
+++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java
@@ -73,7 +73,7 @@ public abstract class AbstractSurefireParser {
private void parseFiles(SensorContext context, File[] reports) {
UnitTestIndex index = new UnitTestIndex();
parseFiles(reports, index);
- sanitize(index, context);
+ sanitize(index);
save(index, context);
}
@@ -90,23 +90,12 @@ public abstract class AbstractSurefireParser {
}
}
- private void sanitize(UnitTestIndex index, SensorContext context) {
+ private void sanitize(UnitTestIndex index) {
for (String classname : index.getClassnames()) {
- Resource resource = getUnitTestResource(classname);
- if (resource != null && context.isIndexed(resource, false)) {
- // ok
-
- } else if (StringUtils.contains(classname, "$")) {
- // Java inner class
+ if (StringUtils.contains(classname, "$")) {
+ // Surefire reports classes whereas sonar supports files
String parentClassName = StringUtils.substringBeforeLast(classname, "$");
- Resource parentResource = getUnitTestResource(parentClassName);
- if (parentResource != null && context.isIndexed(parentResource, false)) {
- index.merge(classname, parentClassName);
- } else {
- index.remove(classname);
- }
- } else {
- index.remove(classname);
+ index.merge(classname, parentClassName);
}
}
}
diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java
index 84fef260f4f..697c94a5750 100644
--- a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java
+++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java
@@ -83,29 +83,6 @@ public class AbstractSurefireParserTest {
verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble());
}
- /**
- * This use-case occurs in projects mixing Groovy and Java tests. All test files are listed in surefire reports,
- * but only Java files are indexed into sonar.
- */
- @Test
- public void shouldIgnoreNonIndexedFiles() throws URISyntaxException {
- AbstractSurefireParser parser = newParser();
-
- SensorContext context = mock(SensorContext.class);
- when(context.isIndexed(argThat(new BaseMatcher<Resource>(){
- public boolean matches(Object o) {
- return ((Resource)o).getName().startsWith("java");
- }
- public void describeTo(Description description) {
- }
- }), eq(false))).thenReturn(true);
-
- parser.collect(new Project("foo"), context, getDir("groovyAndJavaTests"));
-
- verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "java.Foo")), eq(CoreMetrics.TESTS), eq(6.0));
- verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "groovy.Foo")), any(Metric.class), anyDouble());
- }
-
@Test
public void shouldMergeInnerClasses() throws URISyntaxException {
AbstractSurefireParser parser = newParser();