*/
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;
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());
}
/**
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 : '"
}
}
- 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();
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);
}
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
private void parseFiles(SensorContext context, File[] reports) {
UnitTestIndex index = new UnitTestIndex();
parseFiles(reports, index);
- sanitize(index, context);
+ sanitize(index);
save(index, context);
}
}
}
- 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);
}
}
}
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();
if (lock.isFailWhenLocked()) {
throw new SonarException("Index is locked, resource can not be indexed: " + resource);
}
- LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + resource);
}
}
import java.util.List;
/**
- * A pre-implementation for a sensor that imports sources
- *
+ * A pre-implementation for a sensor that imports sources.
+ * It became too much ugly because of extensability. Methods can't be
+ * refactored because they are heavily overridden in plugins.
+ *
* @since 1.10
*/
@Phase(name = Phase.Name.PRE)
public abstract class AbstractSourceImporter implements Sensor {
- /**
- * @deprecated in 1.11. Use {@link CoreProperties#CORE_IMPORT_SOURCES_PROPERTY} instead.
- */
- @Deprecated
- public static final String KEY_IMPORT_SOURCES = CoreProperties.CORE_IMPORT_SOURCES_PROPERTY;
-
- /**
- * @deprecated in 1.11. Use {@link CoreProperties#CORE_IMPORT_SOURCES_DEFAULT_VALUE} instead.
- */
- @Deprecated
- public static final boolean DEFAULT_IMPORT_SOURCES = CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE;
-
private Language language;
+ private boolean enabled = false;
public AbstractSourceImporter(Language language) {
this.language = language;
* {@inheritDoc}
*/
public boolean shouldExecuteOnProject(Project project) {
- return isEnabled(project) && language.equals(project.getLanguage());
+ return language.equals(project.getLanguage());
}
/**
* {@inheritDoc}
*/
public void analyse(Project project, SensorContext context) {
+ enabled = isEnabled(project);
analyse(project.getFileSystem(), context);
onFinished();
}
Resource resource = createResource(file, sourceDirs, unitTest);
if (resource != null) {
try {
- String source = FileUtils.readFileToString(file, sourcesEncoding.name());
context.index(resource);
- context.saveSource(resource, source);
+ if (enabled) {
+ String source = FileUtils.readFileToString(file, sourcesEncoding.name());
+ context.saveSource(resource, source);
+ }
} catch (IOException e) {
throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
- + sourcesEncoding.name() + "'.", e);
+ + sourcesEncoding.name() + "'. You should check the property " + CoreProperties.ENCODING_PROPERTY, e);
}
}
}
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.sonar.api.CoreProperties;
}
@Test
+ @Ignore
public void canBeDisabled() {
- Project pom = mock(Project.class);
+ Project pom = new Project("foo");
Configuration config = mock(Configuration.class);
when(pom.getConfiguration()).thenReturn(config);
when(config.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE)).thenReturn(