@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.plugins.core.sensors; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Lists; | |||
import org.junit.Rule; | |||
@@ -57,8 +58,8 @@ public class FileHashSensorTest { | |||
@Test | |||
public void store_file_hashes() throws Exception { | |||
when(fileCache.byModule("struts")).thenReturn(Lists.<InputFile>newArrayList( | |||
DefaultInputFile.create(temp.newFile(), "src/Foo.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "ABC")), | |||
DefaultInputFile.create(temp.newFile(), "src/Bar.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "DEF")) | |||
DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/Foo.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "ABC")), | |||
DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/Bar.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "DEF")) | |||
)); | |||
SensorContext sensorContext = mock(SensorContext.class); | |||
@@ -72,8 +73,8 @@ public class FileHashSensorTest { | |||
public void store_file_hashes_for_branches() throws Exception { | |||
project = new Project("struts", "branch-2.x", "Struts 2.x"); | |||
when(fileCache.byModule("struts:branch-2.x")).thenReturn(Lists.<InputFile>newArrayList( | |||
DefaultInputFile.create(temp.newFile(), "src/Foo.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "ABC")), | |||
DefaultInputFile.create(temp.newFile(), "src/Bar.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "DEF")) | |||
DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/Foo.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "ABC")), | |||
DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/Bar.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "DEF")) | |||
)); | |||
SensorContext sensorContext = mock(SensorContext.class); |
@@ -19,8 +19,6 @@ | |||
*/ | |||
package org.sonar.batch; | |||
import org.sonar.core.measure.MeasurementFilters; | |||
import org.sonar.api.batch.Event; | |||
import org.sonar.api.batch.SensorContext; | |||
import org.sonar.api.batch.SonarIndex; | |||
@@ -32,6 +30,7 @@ import org.sonar.api.resources.Project; | |||
import org.sonar.api.resources.ProjectLink; | |||
import org.sonar.api.resources.Resource; | |||
import org.sonar.api.rules.Violation; | |||
import org.sonar.core.measure.MeasurementFilters; | |||
import java.util.Collection; | |||
import java.util.Date; | |||
@@ -55,11 +54,11 @@ public class DefaultSensorContext implements SensorContext { | |||
} | |||
public boolean index(Resource resource) { | |||
return index.index(resource); | |||
return true; | |||
} | |||
public boolean index(Resource resource, Resource parentReference) { | |||
return index.index(resource, parentReference); | |||
return true; | |||
} | |||
public boolean isExcluded(Resource reference) { | |||
@@ -123,7 +122,7 @@ public class DefaultSensorContext implements SensorContext { | |||
} | |||
public Measure saveMeasure(Resource resource, Measure measure) { | |||
if(filters.accept(resource, measure)) { | |||
if (filters.accept(resource, measure)) { | |||
return index.addMeasure(resourceOrProject(resource), measure); | |||
} else { | |||
return measure; | |||
@@ -166,7 +165,6 @@ public class DefaultSensorContext implements SensorContext { | |||
} | |||
public void saveSource(Resource reference, String source) { | |||
index.setSource(reference, source); | |||
} | |||
public void saveLink(ProjectLink link) { | |||
@@ -190,6 +188,6 @@ public class DefaultSensorContext implements SensorContext { | |||
} | |||
private Resource resourceOrProject(Resource resource) { | |||
return resource!=null ? resource : project; | |||
return resource != null ? resource : project; | |||
} | |||
} |
@@ -19,9 +19,13 @@ | |||
*/ | |||
package org.sonar.batch.phases; | |||
import com.google.common.base.CharMatcher; | |||
import com.google.common.io.Files; | |||
import org.sonar.api.BatchComponent; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.InstantiationStrategy; | |||
import org.sonar.api.batch.SensorContext; | |||
import org.sonar.api.batch.SonarIndex; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.resources.File; | |||
import org.sonar.api.resources.Java; | |||
import org.sonar.api.resources.JavaFile; | |||
@@ -31,6 +35,7 @@ import org.sonar.api.resources.Qualifiers; | |||
import org.sonar.api.resources.Resource; | |||
import org.sonar.api.scan.filesystem.FileQuery; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.utils.SonarException; | |||
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; | |||
/** | |||
@@ -40,24 +45,30 @@ import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; | |||
@InstantiationStrategy(InstantiationStrategy.PER_PROJECT) | |||
public class FileIndexer implements BatchComponent { | |||
private Project module; | |||
private DefaultModuleFileSystem fs; | |||
private final Project module; | |||
private final DefaultModuleFileSystem fs; | |||
private final Languages languages; | |||
private final Settings settings; | |||
private final SonarIndex sonarIndex; | |||
private Languages languages; | |||
private boolean importSource; | |||
public FileIndexer(Project module, DefaultModuleFileSystem fs, Languages languages) { | |||
public FileIndexer(Project module, DefaultModuleFileSystem fs, Languages languages, SonarIndex sonarIndex, Settings settings) { | |||
this.module = module; | |||
this.fs = fs; | |||
this.languages = languages; | |||
this.sonarIndex = sonarIndex; | |||
this.settings = settings; | |||
} | |||
public void execute(SensorContext context) { | |||
public void execute() { | |||
this.importSource = settings.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY); | |||
String languageKey = module.getLanguageKey(); | |||
indexFiles(fs.inputFiles(FileQuery.onSource().onLanguage(languageKey)), false, context, languageKey); | |||
indexFiles(fs.inputFiles(FileQuery.onTest().onLanguage(languageKey)), true, context, languageKey); | |||
indexFiles(fs.inputFiles(FileQuery.onSource().onLanguage(languageKey)), false, languageKey); | |||
indexFiles(fs.inputFiles(FileQuery.onTest().onLanguage(languageKey)), true, languageKey); | |||
} | |||
private void indexFiles(Iterable<InputFile> files, boolean unitTest, SensorContext context, String languageKey) { | |||
private void indexFiles(Iterable<InputFile> files, boolean unitTest, String languageKey) { | |||
for (InputFile inputFile : files) { | |||
Resource sonarFile; | |||
if (Java.KEY.equals(languageKey)) { | |||
@@ -71,7 +82,18 @@ public class FileIndexer implements BatchComponent { | |||
} | |||
if (sonarFile != null) { | |||
sonarFile.setPath(inputFile.path()); | |||
context.index(sonarFile); | |||
sonarIndex.index(sonarFile); | |||
try { | |||
if (importSource) { | |||
String source = Files.toString(inputFile.file(), inputFile.encoding()); | |||
// SONAR-3860 Remove BOM character from source | |||
source = CharMatcher.anyOf("\uFEFF").removeFrom(source); | |||
sonarIndex.setSource(sonarFile, source); | |||
} | |||
} catch (Exception e) { | |||
throw new SonarException("Unable to read and import the source file : '" + inputFile.absolutePath() + "' with the charset : '" | |||
+ inputFile.encoding() + "'.", e); | |||
} | |||
} | |||
} | |||
} |
@@ -65,7 +65,7 @@ public class SensorsExecutor implements BatchComponent { | |||
Collection<Sensor> sensors = selector.select(Sensor.class, project, true, sensorMatcher); | |||
eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), true)); | |||
fileIndexer.execute(context); | |||
fileIndexer.execute(); | |||
for (Sensor sensor : sensors) { | |||
// SONAR-2965 In case the sensor takes too much time we close the session to not face a timeout |
@@ -40,6 +40,7 @@ import org.sonar.api.utils.PathUtils; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import java.io.File; | |||
import java.nio.charset.Charset; | |||
import java.util.Collection; | |||
@@ -77,7 +78,7 @@ public class FileIndex implements BatchComponent { | |||
private final Project project; | |||
public FileIndex(List<InputFileFilter> filters, LanguageRecognizer languageRecognizer, | |||
InputFileCache cache, FileHashes fileHashes, PathResolver pathResolver, Project project) { | |||
InputFileCache cache, FileHashes fileHashes, PathResolver pathResolver, Project project) { | |||
this.filters = filters; | |||
this.languageRecognizer = languageRecognizer; | |||
this.cache = cache; | |||
@@ -127,7 +128,7 @@ public class FileIndex implements BatchComponent { | |||
if (sourceDirPath == null) { | |||
LoggerFactory.getLogger(getClass()).warn(String.format( | |||
"File '%s' is not declared in source directories %s", sourceFile.getAbsoluteFile(), StringUtils.join(sourceDirs, ", ") | |||
)); | |||
)); | |||
} else { | |||
indexFile(fileSystem, progress, sourceDirPath.dir(), sourceFile, type); | |||
} | |||
@@ -183,7 +184,7 @@ public class FileIndex implements BatchComponent { | |||
// hash + status | |||
initStatus(file, fileSystem.sourceCharset(), path, attributes); | |||
return DefaultInputFile.create(file, path, attributes); | |||
return DefaultInputFile.create(file, fileSystem.sourceCharset(), path, attributes); | |||
} | |||
private void initStatus(File file, Charset charset, String baseRelativePath, Map<String, String> attributes) { |
@@ -19,15 +19,26 @@ | |||
*/ | |||
package org.sonar.batch.phases; | |||
import com.google.common.base.Charsets; | |||
import edu.emory.mathcs.backport.java.util.Collections; | |||
import org.apache.commons.io.FileUtils; | |||
import org.apache.commons.lang.CharEncoding; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.batch.SensorContext; | |||
import org.mockito.ArgumentMatcher; | |||
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.SonarIndex; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.resources.AbstractLanguage; | |||
import org.sonar.api.resources.Java; | |||
import org.sonar.api.resources.JavaFile; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.resources.Project; | |||
import org.sonar.api.resources.Qualifiers; | |||
import org.sonar.api.resources.Resource; | |||
import org.sonar.api.scan.filesystem.FileQuery; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFileBuilder; | |||
@@ -35,8 +46,11 @@ import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.nio.charset.Charset; | |||
import java.util.Arrays; | |||
import static org.mockito.Matchers.argThat; | |||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
@@ -45,56 +59,176 @@ public class FileIndexerTest { | |||
@Rule | |||
public TemporaryFolder temp = new TemporaryFolder(); | |||
private File baseDir; | |||
private DefaultModuleFileSystem fs; | |||
private SonarIndex sonarIndex; | |||
private AbstractLanguage cobolLanguage; | |||
private Project project; | |||
private Settings settings; | |||
private String aClaess; | |||
private String explicacao; | |||
@Before | |||
public void prepare() throws IOException { | |||
baseDir = temp.newFolder(); | |||
fs = mock(DefaultModuleFileSystem.class); | |||
sonarIndex = mock(SonarIndex.class); | |||
project = mock(Project.class); | |||
settings = new Settings(); | |||
cobolLanguage = new AbstractLanguage("cobol") { | |||
@Override | |||
public String[] getFileSuffixes() { | |||
return new String[] {"cbl"}; | |||
} | |||
}; | |||
aClaess = new String(new byte[] {65, 67, 108, 97, -61, -88, 115, 115, 40, 41}, CharEncoding.UTF_8); | |||
explicacao = new String(new byte[] {101, 120, 112, 108, 105, 99, 97, -61, -89, -61, -93, 111, 40, 41}, CharEncoding.UTF_8); | |||
} | |||
@Test | |||
public void should_index_java_files() throws IOException { | |||
File baseDir = temp.newFolder(); | |||
DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); | |||
public void should_index_java_files() { | |||
File javaFile1 = new File(baseDir, "src/main/java/foo/bar/Foo.java"); | |||
File javaFile2 = new File(baseDir, "src/main/java2/foo/bar/Foo.java"); | |||
when(fs.inputFiles(FileQuery.onSource().onLanguage(Java.KEY))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(javaFile1, "src/main/java/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build(), | |||
new InputFileBuilder(javaFile2, "src/main/java2/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build())); | |||
new InputFileBuilder(javaFile1, Charsets.UTF_8, "src/main/java/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build(), | |||
new InputFileBuilder(javaFile2, Charsets.UTF_8, "src/main/java2/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build())); | |||
File javaTestFile1 = new File(baseDir, "src/test/java/foo/bar/FooTest.java"); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage(Java.KEY))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(javaTestFile1, "src/test/java/foo/bar/FooTest.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/FooTest.java").build())); | |||
Project project = mock(Project.class); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage(Java.KEY))).thenReturn( | |||
(Iterable) Arrays.asList( | |||
new InputFileBuilder(javaTestFile1, Charsets.UTF_8, "src/test/java/foo/bar/FooTest.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/FooTest.java") | |||
.build())); | |||
when(project.getLanguageKey()).thenReturn(Java.KEY); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(Java.INSTANCE)); | |||
SensorContext sensorContext = mock(SensorContext.class); | |||
indexer.execute(sensorContext); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(Java.INSTANCE), sonarIndex, settings); | |||
indexer.execute(); | |||
verify(sensorContext).index(new JavaFile("foo.bar.Foo", false).setPath("/src/main/java/foo/bar/Foo.java")); | |||
verify(sensorContext).index(new JavaFile("foo.bar.Foo", false).setPath("/src/main/java2/foo/bar/Foo.java")); | |||
verify(sensorContext).index(new JavaFile("foo.bar.FooTest", true).setPath("/src/test/java/foo/bar/FooTest.java")); | |||
verify(sonarIndex).index(new JavaFile("foo.bar.Foo", false).setPath("/src/main/java/foo/bar/Foo.java")); | |||
verify(sonarIndex).index(new JavaFile("foo.bar.Foo", false).setPath("/src/main/java2/foo/bar/Foo.java")); | |||
verify(sonarIndex).index(argThat(new ArgumentMatcher<JavaFile>() { | |||
@Override | |||
public boolean matches(Object arg0) { | |||
JavaFile javaFile = (JavaFile) arg0; | |||
return javaFile.getKey().equals("foo.bar.FooTest") && javaFile.getPath().equals("/src/test/java/foo/bar/FooTest.java") | |||
&& javaFile.getQualifier().equals(Qualifiers.UNIT_TEST_FILE); | |||
} | |||
})); | |||
} | |||
@Test | |||
public void should_index_cobol_files() throws IOException { | |||
File baseDir = temp.newFolder(); | |||
DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); | |||
File cobolFile1 = new File(baseDir, "src/foo/bar/Foo.cbl"); | |||
File cobolFile2 = new File(baseDir, "src2/foo/bar/Foo.cbl"); | |||
when(fs.inputFiles(FileQuery.onSource().onLanguage("cobol"))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(cobolFile1, "src/foo/bar/Foo.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.cbl").build(), | |||
new InputFileBuilder(cobolFile2, "src2/foo/bar/Foo.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.cbl").build())); | |||
new InputFileBuilder(cobolFile1, Charsets.UTF_8, "src/foo/bar/Foo.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.cbl").build(), | |||
new InputFileBuilder(cobolFile2, Charsets.UTF_8, "src2/foo/bar/Foo.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.cbl").build())); | |||
File cobolTestFile1 = new File(baseDir, "src/test/foo/bar/FooTest.cbl"); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage("cobol"))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(cobolTestFile1, "src/test/foo/bar/FooTest.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/FooTest.cbl").build())); | |||
Project project = mock(Project.class); | |||
new InputFileBuilder(cobolTestFile1, Charsets.UTF_8, "src/test/foo/bar/FooTest.cbl").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/FooTest.cbl").build())); | |||
when(project.getLanguageKey()).thenReturn("cobol"); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(new AbstractLanguage("cobol") { | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(cobolLanguage), sonarIndex, settings); | |||
indexer.execute(); | |||
verify(sonarIndex).index(new org.sonar.api.resources.File("foo/bar/Foo.cbl").setPath("/src/foo/bar/Foo.cbl")); | |||
verify(sonarIndex).index(new org.sonar.api.resources.File("foo/bar/Foo.cbl").setPath("/src2/foo/bar/Foo.cbl")); | |||
verify(sonarIndex).index(new org.sonar.api.resources.File("foo/bar/FooTest.cbl").setPath("/src/test/foo/bar/FooTest.cbl")); | |||
} | |||
@Test | |||
public void shouldImportSource() throws IOException { | |||
settings.setProperty(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, "true"); | |||
File javaFile1 = new File(baseDir, "src/main/java/foo/bar/Foo.java"); | |||
FileUtils.write(javaFile1, "sample code"); | |||
when(fs.inputFiles(FileQuery.onSource().onLanguage(Java.KEY))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(javaFile1, Charsets.UTF_8, "src/main/java/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build())); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage(Java.KEY))).thenReturn( | |||
(Iterable) Collections.emptyList()); | |||
when(project.getLanguageKey()).thenReturn(Java.KEY); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(Java.INSTANCE), sonarIndex, settings); | |||
indexer.execute(); | |||
Resource sonarFile = new JavaFile("foo.bar.Foo", false).setPath("/src/main/java/foo/bar/Foo.java"); | |||
verify(sonarIndex).index(sonarFile); | |||
verify(sonarIndex).setSource(sonarFile, "sample code"); | |||
} | |||
@Test | |||
public void should_use_mac_roman_charset_forR_reading_source_files() throws Exception { | |||
String encoding = "MacRoman"; | |||
String testFile = "MacRomanEncoding.java"; | |||
fileEncodingTest(encoding, testFile); | |||
} | |||
@Test | |||
public void should_use_CP1252_charset_for_reading_source_files() throws Exception { | |||
String encoding = "CP1252"; | |||
String testFile = "CP1252Encoding.java"; | |||
fileEncodingTest(encoding, testFile); | |||
} | |||
@Test(expected = ArgumentsAreDifferent.class) | |||
public void should_fail_with_wrong_charset_for_reading_source_files() throws Exception { | |||
String encoding = CharEncoding.UTF_8; | |||
String testFile = "CP1252Encoding.java"; | |||
fileEncodingTest(encoding, testFile); | |||
} | |||
@Test | |||
public void should_remove_byte_order_mark_character() throws Exception { | |||
settings.setProperty(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, "true"); | |||
File javaFile1 = new File(baseDir, "src/main/java/foo/bar/Foo.java"); | |||
FileUtils.write(javaFile1, "\uFEFFpublic class Test", Charsets.UTF_8); | |||
when(fs.inputFiles(FileQuery.onSource().onLanguage(Java.KEY))).thenReturn((Iterable) Arrays.asList( | |||
new InputFileBuilder(javaFile1, Charsets.UTF_8, "src/main/java/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java").build())); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage(Java.KEY))).thenReturn( | |||
(Iterable) Collections.emptyList()); | |||
when(project.getLanguageKey()).thenReturn(Java.KEY); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(Java.INSTANCE), sonarIndex, settings); | |||
indexer.execute(); | |||
Resource sonarFile = new JavaFile("foo.bar.Foo", false).setPath("/src/main/java/foo/bar/Foo.java"); | |||
verify(sonarIndex).setSource(eq(sonarFile), argThat(new ArgumentMatcher<String>() { | |||
@Override | |||
public String[] getFileSuffixes() { | |||
return new String[] {"cbl"}; | |||
public boolean matches(Object arg0) { | |||
String source = (String) arg0; | |||
return !source.contains("\uFEFF"); | |||
} | |||
})); | |||
} | |||
private void fileEncodingTest(String encoding, String testFile) throws Exception { | |||
settings.setProperty(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, "true"); | |||
File javaFile1 = new File(baseDir, "src/main/java/foo/bar/Foo.java"); | |||
FileUtils.copyFile(getFile(testFile), javaFile1); | |||
when(fs.inputFiles(FileQuery.onSource().onLanguage(Java.KEY))) | |||
.thenReturn( | |||
(Iterable) Arrays.asList( | |||
new InputFileBuilder(javaFile1, Charset.forName(encoding), "src/main/java/foo/bar/Foo.java").attribute(InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "foo/bar/Foo.java") | |||
.build())); | |||
when(fs.inputFiles(FileQuery.onTest().onLanguage(Java.KEY))).thenReturn( | |||
(Iterable) Collections.emptyList()); | |||
when(project.getLanguageKey()).thenReturn(Java.KEY); | |||
FileIndexer indexer = new FileIndexer(project, fs, new Languages(Java.INSTANCE), sonarIndex, settings); | |||
indexer.execute(); | |||
Resource sonarFile = new JavaFile("foo.bar.Foo", false).setPath("/src/main/java/foo/bar/Foo.java"); | |||
verify(sonarIndex).setSource(eq(sonarFile), argThat(new ArgumentMatcher<String>() { | |||
@Override | |||
public boolean matches(Object arg0) { | |||
String source = (String) arg0; | |||
return source.contains(aClaess) && source.contains(explicacao); | |||
} | |||
})); | |||
SensorContext sensorContext = mock(SensorContext.class); | |||
indexer.execute(sensorContext); | |||
} | |||
verify(sensorContext).index(new org.sonar.api.resources.File("foo/bar/Foo.cbl").setPath("/src/foo/bar/Foo.cbl")); | |||
verify(sensorContext).index(new org.sonar.api.resources.File("foo/bar/Foo.cbl").setPath("/src2/foo/bar/Foo.cbl")); | |||
verify(sensorContext).index(new org.sonar.api.resources.File("foo/bar/FooTest.cbl").setPath("/src/test/foo/bar/FooTest.cbl")); | |||
private File getFile(String testFile) { | |||
return new File("test-resources/org/sonar/batch/phases/FileIndexerTest/encoding/" + testFile); | |||
} | |||
} |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Lists; | |||
import org.junit.Rule; | |||
@@ -39,9 +40,9 @@ public class AttributeFilterTest { | |||
assertThat(filter.key()).isEqualTo("foo"); | |||
assertThat(filter.values()).containsOnly("one", "two"); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), "Why.java", ImmutableMap.of("foo", "two")))).isTrue(); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), "Where.java", ImmutableMap.of("foo", "three")))).isFalse(); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), "What.java", ImmutableMap.of("bar", "one")))).isFalse(); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "Why.java", ImmutableMap.of("foo", "two")))).isTrue(); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "Where.java", ImmutableMap.of("foo", "three")))).isFalse(); | |||
assertThat(filter.accept(DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "What.java", ImmutableMap.of("bar", "one")))).isFalse(); | |||
} | |||
} |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Lists; | |||
import org.junit.Before; | |||
@@ -160,8 +161,8 @@ public class DefaultModuleFileSystemTest { | |||
DefaultModuleFileSystem fs = new DefaultModuleFileSystem(new Project("foo"), settings, fileIndex, initializer, mode); | |||
File mainFile = temp.newFile(); | |||
InputFile mainInput = DefaultInputFile.create(mainFile, "Main.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE)); | |||
InputFile testInput = DefaultInputFile.create(temp.newFile(), "Test.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST)); | |||
InputFile mainInput = DefaultInputFile.create(mainFile, Charsets.UTF_8, "Main.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE)); | |||
InputFile testInput = DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "Test.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST)); | |||
when(fileIndex.inputFiles("foo")).thenReturn(Lists.newArrayList(mainInput, testInput)); | |||
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Maps; | |||
import org.apache.commons.io.FilenameUtils; | |||
@@ -28,15 +29,17 @@ import org.junit.rules.TemporaryFolder; | |||
import org.mockito.ArgumentCaptor; | |||
import org.sonar.api.scan.filesystem.FileSystemFilter; | |||
import org.sonar.api.scan.filesystem.FileType; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.DefaultInputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import java.io.File; | |||
import static org.fest.assertions.Assertions.assertThat; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.*; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
public class DeprecatedFileFiltersTest { | |||
@@ -49,22 +52,22 @@ public class DeprecatedFileFiltersTest { | |||
public void no_filters() throws Exception { | |||
DeprecatedFileFilters filters = new DeprecatedFileFilters(); | |||
InputFile inputFile = DefaultInputFile.create(temp.newFile(), "src/main/java/Foo.java", Maps.<String, String>newHashMap()); | |||
InputFile inputFile = DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/main/java/Foo.java", Maps.<String, String>newHashMap()); | |||
assertThat(filters.accept(inputFile)).isTrue(); | |||
} | |||
@Test | |||
public void at_least_one_filter() throws Exception { | |||
DeprecatedFileFilters filters = new DeprecatedFileFilters(new FileSystemFilter[]{filter}); | |||
DeprecatedFileFilters filters = new DeprecatedFileFilters(new FileSystemFilter[] {filter}); | |||
File basedir = temp.newFolder(); | |||
File file = temp.newFile(); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/Foo.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_SOURCEDIR_PATH, new File(basedir, "src/main/java").getAbsolutePath(), | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "Foo.java", | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST | |||
)); | |||
)); | |||
when(filter.accept(eq(file), any(DeprecatedFileFilters.DeprecatedContext.class))).thenReturn(false); | |||
assertThat(filters.accept(inputFile)).isFalse(); |
@@ -19,12 +19,13 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.DefaultInputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import java.io.File; | |||
@@ -41,17 +42,17 @@ public class ExclusionFilterTest { | |||
ExclusionFilter absoluteFilter = new ExclusionFilter("file:**/src/main/**Foo.java"); | |||
File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java"); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/org/MyFoo.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/org/MyFoo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "org/MyFoo.java" | |||
)); | |||
)); | |||
assertThat(sourceRelativeFilter.accept(inputFile)).isFalse(); | |||
assertThat(absoluteFilter.accept(inputFile)).isFalse(); | |||
file = new File(temp.newFolder(), "src/main/java/org/Other.java"); | |||
inputFile = DefaultInputFile.create(file, "src/main/java/org/Other.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/org/Other.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "org/Other.java" | |||
)); | |||
)); | |||
assertThat(sourceRelativeFilter.accept(inputFile)).isTrue(); | |||
assertThat(absoluteFilter.accept(inputFile)).isTrue(); | |||
} |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -29,8 +30,8 @@ import org.sonar.api.resources.File; | |||
import org.sonar.api.resources.JavaFile; | |||
import org.sonar.api.resources.Project; | |||
import org.sonar.api.scan.filesystem.FileExclusions; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.DefaultInputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import java.io.IOException; | |||
@@ -48,24 +49,24 @@ public class ExclusionFiltersTest { | |||
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings)); | |||
java.io.File file = temp.newFile(); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/Foo.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isFalse(); | |||
inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/FooDao.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isTrue(); | |||
// source inclusions do not apply to tests | |||
inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/Foo.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isTrue(); | |||
} | |||
@@ -76,17 +77,17 @@ public class ExclusionFiltersTest { | |||
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings)); | |||
java.io.File file = temp.newFile(); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/Foo.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isFalse(); | |||
inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/FooDto.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDto.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/FooDto.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isTrue(); | |||
} | |||
@@ -97,23 +98,23 @@ public class ExclusionFiltersTest { | |||
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings)); | |||
java.io.File file = temp.newFile(); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/FooDao.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isFalse(); | |||
inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/Foo.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isTrue(); | |||
// source exclusions do not apply to tests | |||
inputFile = DefaultInputFile.create(file, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST, | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "com/mycompany/FooDao.java" | |||
)); | |||
)); | |||
assertThat(filter.accept(inputFile)).isTrue(); | |||
} | |||
@@ -126,14 +127,14 @@ public class ExclusionFiltersTest { | |||
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "file:" + excludedFile.getCanonicalPath()); | |||
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings)); | |||
InputFile includedInput = DefaultInputFile.create(includedFile, "src/main/java/org/bar/Foo.java", ImmutableMap.of( | |||
InputFile includedInput = DefaultInputFile.create(includedFile, Charsets.UTF_8, "src/main/java/org/bar/Foo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE | |||
)); | |||
)); | |||
assertThat(filter.accept(includedInput)).isTrue(); | |||
InputFile excludedInput = DefaultInputFile.create(excludedFile, "src/main/java/org/bar/Bar.java", ImmutableMap.of( | |||
InputFile excludedInput = DefaultInputFile.create(excludedFile, Charsets.UTF_8, "src/main/java/org/bar/Bar.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE | |||
)); | |||
)); | |||
assertThat(filter.accept(excludedInput)).isFalse(); | |||
} | |||
@@ -236,5 +237,4 @@ public class ExclusionFiltersTest { | |||
assertThat(filter.sourceExclusions()[0].toString()).isEqualTo("**/*Dao.java"); | |||
} | |||
} |
@@ -19,12 +19,13 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.ImmutableMap; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.DefaultInputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import java.io.File; | |||
@@ -40,17 +41,17 @@ public class InclusionFilterTest { | |||
InclusionFilter absoluteFilter = new InclusionFilter("file:**/src/main/**Foo.java"); | |||
File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java"); | |||
InputFile inputFile = DefaultInputFile.create(file, "src/main/java/org/MyFoo.java", ImmutableMap.of( | |||
InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/org/MyFoo.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "org/MyFoo.java" | |||
)); | |||
)); | |||
assertThat(sourceRelativeFilter.accept(inputFile)).isTrue(); | |||
assertThat(absoluteFilter.accept(inputFile)).isTrue(); | |||
file = new File(temp.newFolder(), "src/main/java/org/Other.java"); | |||
inputFile = DefaultInputFile.create(file, "src/main/java/org/Other.java", ImmutableMap.of( | |||
inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/org/Other.java", ImmutableMap.of( | |||
InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, "org/Other.java" | |||
)); | |||
)); | |||
assertThat(sourceRelativeFilter.accept(inputFile)).isFalse(); | |||
assertThat(absoluteFilter.accept(inputFile)).isFalse(); | |||
} |
@@ -19,14 +19,15 @@ | |||
*/ | |||
package org.sonar.batch.scan.filesystem; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.Maps; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.DefaultInputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.batch.index.Caches; | |||
import org.sonar.batch.index.CachesTest; | |||
@@ -53,8 +54,8 @@ public class InputFileCacheTest { | |||
@Test | |||
public void should_add_input_file() throws Exception { | |||
InputFileCache cache = new InputFileCache(caches); | |||
cache.put("struts", DefaultInputFile.create(temp.newFile(), "src/main/java/Foo.java", Maps.<String, String>newHashMap())); | |||
cache.put("struts-core", DefaultInputFile.create(temp.newFile(), "src/main/java/Foo.java", Maps.<String, String>newHashMap())); | |||
cache.put("struts", DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/main/java/Foo.java", Maps.<String, String>newHashMap())); | |||
cache.put("struts-core", DefaultInputFile.create(temp.newFile(), Charsets.UTF_8, "src/main/java/Foo.java", Maps.<String, String>newHashMap())); | |||
assertThat(cache.byModule("struts")).hasSize(1); | |||
assertThat(cache.byModule("struts-core")).hasSize(1); |
@@ -19,51 +19,36 @@ | |||
*/ | |||
package org.sonar.api.batch; | |||
import com.google.common.base.CharMatcher; | |||
import com.google.common.io.Files; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Project; | |||
import org.sonar.api.resources.ProjectFileSystem; | |||
import org.sonar.api.resources.Qualifiers; | |||
import org.sonar.api.resources.Resource; | |||
import org.sonar.api.utils.SonarException; | |||
import java.io.File; | |||
import java.nio.charset.Charset; | |||
import java.util.List; | |||
/** | |||
* 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 | |||
* @deprecated since 4.2 Resource indexing/source import is done by the core and this extension will not be used. | |||
*/ | |||
@Deprecated | |||
@Phase(name = Phase.Name.PRE) | |||
public abstract class AbstractSourceImporter implements Sensor { | |||
private Language language; | |||
private boolean enabled = false; | |||
public AbstractSourceImporter(Language language) { | |||
this.language = language; | |||
} | |||
/** | |||
* Generally this method should not be overridden in subclasses, but if it is, then it should be executed anyway (see SONAR-3419). | |||
*/ | |||
public boolean shouldExecuteOnProject(Project project) { | |||
enabled = isEnabled(project); | |||
return language.equals(project.getLanguage()); | |||
return false; | |||
} | |||
/** | |||
* {@inheritDoc} | |||
*/ | |||
public void analyse(Project project, SensorContext context) { | |||
analyse(project.getFileSystem(), context); | |||
onFinished(); | |||
// Do not remove for backward compatibility | |||
} | |||
protected void onFinished() { | |||
@@ -71,28 +56,11 @@ public abstract class AbstractSourceImporter implements Sensor { | |||
} | |||
protected void analyse(ProjectFileSystem fileSystem, SensorContext context) { | |||
parseDirs(context, fileSystem.getSourceFiles(language), fileSystem.getSourceDirs(), false, fileSystem.getSourceCharset()); | |||
parseDirs(context, fileSystem.getTestFiles(language), fileSystem.getTestDirs(), true, fileSystem.getSourceCharset()); | |||
// Do not remove for backward compatibility | |||
} | |||
protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) { | |||
for (File file : files) { | |||
Resource resource = createResource(file, sourceDirs, unitTest); | |||
if (resource != null) { | |||
try { | |||
context.index(resource); | |||
if (enabled) { | |||
String source = Files.toString(file, Charset.forName(sourcesEncoding.name())); | |||
// SONAR-3860 Remove BOM character from source | |||
source = CharMatcher.anyOf("\uFEFF").removeFrom(source); | |||
context.saveSource(resource, source); | |||
} | |||
} catch (Exception e) { | |||
throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '" | |||
+ sourcesEncoding.name() + "'.", e); | |||
} | |||
} | |||
} | |||
// Do not remove for backward compatibility | |||
} | |||
protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) { | |||
@@ -107,13 +75,9 @@ public abstract class AbstractSourceImporter implements Sensor { | |||
} | |||
protected boolean isEnabled(Project project) { | |||
return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, | |||
CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE); | |||
return false; | |||
} | |||
/** | |||
* @return the language | |||
*/ | |||
public Language getLanguage() { | |||
return language; | |||
} |
@@ -42,10 +42,11 @@ public interface SensorContext { | |||
* | |||
* @return false if the resource is excluded | |||
* @since 2.6 | |||
* @deprecated since 4.2 Resource indexing is done by the platform | |||
*/ | |||
@Deprecated | |||
boolean index(Resource resource); | |||
/** | |||
* Indexes a resource. This method does nothing if the resource is already indexed. | |||
* | |||
@@ -53,7 +54,9 @@ public interface SensorContext { | |||
* @param parentReference a reference to the parent. If null, the the resource is indexed as a direct child of project. | |||
* @return false if the parent is not indexed or if the resource is excluded | |||
* @since 2.6 | |||
* @deprecated since 4.2 Resource indexing is done by the platform | |||
*/ | |||
@Deprecated | |||
boolean index(Resource resource, Resource parentReference); | |||
/** | |||
@@ -126,7 +129,6 @@ public interface SensorContext { | |||
@Deprecated | |||
String saveResource(Resource resource); | |||
/** | |||
* Find all measures for this project. Never return null. | |||
*/ | |||
@@ -191,7 +193,9 @@ public interface SensorContext { | |||
* @throws org.sonar.api.resources.DuplicatedSourceException | |||
* if the source has already been set on this resource | |||
* @since 1.10. Returns a boolean since 2.6. | |||
* @deprecated since 4.2 Source import is done by the platform | |||
*/ | |||
@Deprecated | |||
void saveSource(Resource reference, String source); | |||
// ----------- LINKS -------------- |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.api.scan.filesystem.internal; | |||
import org.apache.commons.codec.Charsets; | |||
import org.apache.commons.io.FilenameUtils; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.sonar.api.utils.PathUtils; | |||
@@ -26,6 +27,7 @@ import org.sonar.api.utils.PathUtils; | |||
import javax.annotation.CheckForNull; | |||
import java.io.File; | |||
import java.nio.charset.Charset; | |||
import java.util.Map; | |||
/** | |||
@@ -43,8 +45,10 @@ public class DefaultInputFile implements InputFile { | |||
private final String absolutePath; | |||
private final String path; | |||
private final Map<String, String> attributes; | |||
private final String encoding; | |||
private DefaultInputFile(File file, String path, Map<String, String> attributes) { | |||
private DefaultInputFile(File file, Charset encoding, String path, Map<String, String> attributes) { | |||
this.encoding = encoding.name(); | |||
this.absolutePath = PathUtils.canonicalPath(file); | |||
this.path = FilenameUtils.separatorsToUnix(path); | |||
this.attributes = attributes; | |||
@@ -56,8 +60,8 @@ public class DefaultInputFile implements InputFile { | |||
* <p/> | |||
* Usage: <code>InputFile.create(file, "src/main/java/com/Foo.java", attributes)</code> | |||
*/ | |||
public static DefaultInputFile create(File file, String path, Map<String, String> attributes) { | |||
return new DefaultInputFile(file, path, attributes); | |||
public static DefaultInputFile create(File file, Charset encoding, String path, Map<String, String> attributes) { | |||
return new DefaultInputFile(file, encoding, path, attributes); | |||
} | |||
@Override | |||
@@ -75,6 +79,11 @@ public class DefaultInputFile implements InputFile { | |||
return new File(absolutePath); | |||
} | |||
@Override | |||
public Charset encoding() { | |||
return Charsets.toCharset(encoding); | |||
} | |||
@Override | |||
public String name() { | |||
return file().getName(); |
@@ -20,8 +20,10 @@ | |||
package org.sonar.api.scan.filesystem.internal; | |||
import javax.annotation.CheckForNull; | |||
import java.io.File; | |||
import java.io.Serializable; | |||
import java.nio.charset.Charset; | |||
import java.util.Map; | |||
public interface InputFile extends Serializable { | |||
@@ -57,7 +59,6 @@ public interface InputFile extends Serializable { | |||
String ATTRIBUTE_HASH = "HASH"; | |||
/** | |||
* Path is relative from module base directory. Path is unique and identifies file | |||
* within given <code>{@link org.sonar.api.scan.filesystem.ModuleFileSystem}</code>. | |||
@@ -78,6 +79,8 @@ public interface InputFile extends Serializable { | |||
File file(); | |||
Charset encoding(); | |||
/** | |||
* Not-null filename, including extension | |||
*/ |
@@ -22,7 +22,9 @@ package org.sonar.api.scan.filesystem.internal; | |||
import org.sonar.api.utils.PathUtils; | |||
import javax.annotation.Nullable; | |||
import java.io.File; | |||
import java.nio.charset.Charset; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
@@ -36,13 +38,15 @@ public class InputFileBuilder { | |||
private final Map<String, String> attributes = new HashMap<String, String>(); | |||
private final File file; | |||
private final String relativePath; | |||
private Charset encoding; | |||
public static void _FOR_UNIT_TESTING_ONLY_() { | |||
// For those who don't read javadoc | |||
} | |||
public InputFileBuilder(File file, String relativePath) { | |||
public InputFileBuilder(File file, Charset encoding, String relativePath) { | |||
this.file = file; | |||
this.encoding = encoding; | |||
this.relativePath = relativePath; | |||
} | |||
@@ -78,6 +82,6 @@ public class InputFileBuilder { | |||
} | |||
public DefaultInputFile build() { | |||
return DefaultInputFile.create(file, relativePath, attributes); | |||
return DefaultInputFile.create(file, encoding, relativePath, attributes); | |||
} | |||
} |
@@ -1,201 +0,0 @@ | |||
/* | |||
* SonarQube, open source software quality management tool. | |||
* Copyright (C) 2008-2013 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* SonarQube is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* SonarQube is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.api.batch; | |||
import com.google.common.io.Files; | |||
import org.apache.commons.configuration.MapConfiguration; | |||
import org.apache.commons.configuration.PropertiesConfiguration; | |||
import org.apache.commons.lang.CharEncoding; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentMatcher; | |||
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; | |||
import org.sonar.api.resources.Java; | |||
import org.sonar.api.resources.JavaFile; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Project; | |||
import org.sonar.api.resources.ProjectFileSystem; | |||
import org.sonar.api.resources.Resource; | |||
import java.io.File; | |||
import java.io.UnsupportedEncodingException; | |||
import java.nio.charset.Charset; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
import static org.fest.assertions.Assertions.assertThat; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Matchers.anyString; | |||
import static org.mockito.Matchers.argThat; | |||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.never; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
public class AbstractSourceImporterTest { | |||
private String aClaess; | |||
private String explicacao; | |||
private FakeSourceImporter importer; | |||
@Before | |||
public void setup() throws UnsupportedEncodingException { | |||
aClaess = new String(new byte[] {65, 67, 108, 97, -61, -88, 115, 115, 40, 41}, CharEncoding.UTF_8); | |||
explicacao = new String(new byte[] {101, 120, 112, 108, 105, 99, 97, -61, -89, -61, -93, 111, 40, 41}, CharEncoding.UTF_8); | |||
importer = new FakeSourceImporter(); | |||
} | |||
@Test | |||
public void shouldBeEnabledByDefault() { | |||
Project pom = mock(Project.class); | |||
when(pom.getConfiguration()).thenReturn(new PropertiesConfiguration()); | |||
assertThat(importer.isEnabled(pom)).isTrue(); | |||
} | |||
@Test | |||
public void do_not_save_source_if_null_resource() { | |||
AbstractSourceImporter nullImporter = new AbstractSourceImporter(Java.INSTANCE) { | |||
@Override | |||
protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) { | |||
return null; | |||
} | |||
}; | |||
SensorContext context = mock(SensorContext.class); | |||
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); | |||
when(fileSystem.getSourceFiles(Java.INSTANCE)).thenReturn(newArrayList(new File("Foo.java"), new File("Bar.java"))); | |||
nullImporter.analyse(fileSystem, context); | |||
verify(context, never()).saveSource(any(Resource.class), anyString()); | |||
} | |||
@Test | |||
public void should_use_mac_roman_charset_forR_reading_source_files() throws Exception { | |||
Project project = mock(Project.class); | |||
SensorContext context = mock(SensorContext.class); | |||
String encoding = "MacRoman"; | |||
String testFile = "MacRomanEncoding.java"; | |||
fileEncodingTest(project, context, encoding, testFile); | |||
} | |||
@Test | |||
public void should_use_CP1252_charset_for_reading_source_files() throws Exception { | |||
Project project = mock(Project.class); | |||
SensorContext context = mock(SensorContext.class); | |||
String encoding = "CP1252"; | |||
String testFile = "CP1252Encoding.java"; | |||
fileEncodingTest(project, context, encoding, testFile); | |||
} | |||
@Test(expected = ArgumentsAreDifferent.class) | |||
public void should_fail_with_wrong_charset_for_reading_source_files() throws Exception { | |||
Project project = mock(Project.class); | |||
SensorContext context = mock(SensorContext.class); | |||
String encoding = CharEncoding.UTF_8; | |||
String testFile = "CP1252Encoding.java"; | |||
fileEncodingTest(project, context, encoding, testFile); | |||
} | |||
@Test | |||
public void should_remove_byte_order_mark_character() throws Exception { | |||
Project project = mock(Project.class); | |||
SensorContext context = mock(SensorContext.class); | |||
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); | |||
when(project.getFileSystem()).thenReturn(fileSystem); | |||
when(fileSystem.getSourceCharset()).thenReturn(Charset.forName(CharEncoding.UTF_8)); | |||
when(project.getConfiguration()).thenReturn(new MapConfiguration(new HashMap<String, String>())); | |||
File file = new File(Files.createTempDir(), "Test.java"); | |||
Files.write("\uFEFFpublic class Test", file, Charset.defaultCharset()); | |||
when(fileSystem.getSourceFiles(any(Language.class))).thenReturn(newArrayList(file)); | |||
importer.shouldExecuteOnProject(project); | |||
importer.analyse(project, context); | |||
verify(context).saveSource(eq(FakeSourceImporter.TEST_RESOURCE), argThat(new ArgumentMatcher<String>() { | |||
@Override | |||
public boolean matches(Object arg0) { | |||
String source = (String) arg0; | |||
return !source.contains("\uFEFF"); | |||
} | |||
})); | |||
} | |||
private void fileEncodingTest(Project project, SensorContext context, String encoding, String testFile) throws Exception { | |||
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); | |||
when(project.getFileSystem()).thenReturn(fileSystem); | |||
when(fileSystem.getSourceCharset()).thenReturn(Charset.forName(encoding)); | |||
when(project.getConfiguration()).thenReturn(new MapConfiguration(new HashMap<String, String>())); | |||
when(fileSystem.getSourceFiles(any(Language.class))).thenReturn(newArrayList(getFile(testFile))); | |||
importer.shouldExecuteOnProject(project); | |||
importer.analyse(project, context); | |||
verify(context).saveSource(eq(FakeSourceImporter.TEST_RESOURCE), argThat(new ArgumentMatcher<String>() { | |||
@Override | |||
public boolean matches(Object arg0) { | |||
String source = (String) arg0; | |||
return source.contains(aClaess) && source.contains(explicacao); | |||
} | |||
})); | |||
} | |||
@Test | |||
public void test_create_unit_test_resource() { | |||
AbstractSourceImporter importer = new AbstractSourceImporter(Java.INSTANCE) { | |||
}; | |||
File unitTestFile = new File("test/UnitTest.java"); | |||
File unitTestDir = new File("test"); | |||
List<File> unitTestDirs = newArrayList(); | |||
unitTestDirs.add(unitTestDir); | |||
Resource unitTest = importer.createResource(unitTestFile, unitTestDirs, true); | |||
assertThat(unitTest.getQualifier()).isEqualTo("UTS"); | |||
Resource srcTest = importer.createResource(unitTestFile, unitTestDirs, false); | |||
assertThat(srcTest.getQualifier()).isEqualTo("FIL"); | |||
} | |||
private static class FakeSourceImporter extends AbstractSourceImporter { | |||
private final static Resource TEST_RESOURCE = new JavaFile("Test"); | |||
private FakeSourceImporter() { | |||
super(Java.INSTANCE); | |||
} | |||
@Override | |||
protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) { | |||
return TEST_RESOURCE; | |||
} | |||
} | |||
private File getFile(String testFile){ | |||
return new File("test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/" + testFile); | |||
} | |||
} |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.api.scan.filesystem.internal; | |||
import com.google.common.base.Charsets; | |||
import org.apache.commons.io.FilenameUtils; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -38,7 +39,7 @@ public class DefaultInputFileTest { | |||
@Test | |||
public void test_attributes() throws IOException { | |||
File file = temp.newFile(); | |||
InputFile input = new InputFileBuilder(file, "src/main/java/Foo.java") | |||
InputFile input = new InputFileBuilder(file, Charsets.UTF_8, "src/main/java/Foo.java") | |||
.attribute("foo", "bar") | |||
.type(InputFile.TYPE_TEST) | |||
.hash("ABC") | |||
@@ -63,7 +64,7 @@ public class DefaultInputFileTest { | |||
public void test_file() throws Exception { | |||
File sourceDir = temp.newFolder(); | |||
File file = temp.newFile("Foo.java"); | |||
InputFile input = new InputFileBuilder(file, "src/main/java/Foo.java") | |||
InputFile input = new InputFileBuilder(file, Charsets.UTF_8, "src/main/java/Foo.java") | |||
.sourceDir(sourceDir) | |||
.build(); | |||
@@ -77,9 +78,9 @@ public class DefaultInputFileTest { | |||
@Test | |||
public void test_equals_and_hashCode() throws Exception { | |||
File file1 = temp.newFile(); | |||
InputFile input1 = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); | |||
InputFile input1a = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); | |||
InputFile input2 = new InputFileBuilder(temp.newFile(), "src/main/java/Bar.java").build(); | |||
InputFile input1 = new InputFileBuilder(file1, Charsets.UTF_8, "src/main/java/Foo.java").build(); | |||
InputFile input1a = new InputFileBuilder(file1, Charsets.UTF_8, "src/main/java/Foo.java").build(); | |||
InputFile input2 = new InputFileBuilder(temp.newFile(), Charsets.UTF_8, "src/main/java/Bar.java").build(); | |||
assertThat(input1.equals(input1)).isTrue(); | |||
assertThat(input1.equals(input1a)).isTrue(); | |||
@@ -91,7 +92,7 @@ public class DefaultInputFileTest { | |||
@Test | |||
public void test_toString() throws Exception { | |||
File file1 = temp.newFile(); | |||
InputFile input = new InputFileBuilder(file1, "src/main/java/Foo.java").type(InputFile.TYPE_TEST).build(); | |||
InputFile input = new InputFileBuilder(file1, Charsets.UTF_8, "src/main/java/Foo.java").type(InputFile.TYPE_TEST).build(); | |||
assertThat(input.toString()).isEqualTo("[src/main/java/Foo.java,TEST]"); | |||
} | |||
} |
@@ -19,13 +19,11 @@ | |||
*/ | |||
package org.sonar.api.scan.filesystem.internal; | |||
import com.google.common.base.Charsets; | |||
import com.google.common.collect.Lists; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.scan.filesystem.internal.InputFile; | |||
import org.sonar.api.scan.filesystem.internal.InputFileBuilder; | |||
import org.sonar.api.scan.filesystem.internal.InputFiles; | |||
import java.io.File; | |||
@@ -40,8 +38,8 @@ public class InputFilesTest { | |||
public void test_toFiles() throws Exception { | |||
File file1 = temp.newFile(); | |||
File file2 = temp.newFile(); | |||
InputFile input1 = new InputFileBuilder(file1, "src/main/java/Foo.java").build(); | |||
InputFile input2 = new InputFileBuilder(file2, "src/main/java/Bar.java").build(); | |||
InputFile input1 = new InputFileBuilder(file1, Charsets.UTF_8, "src/main/java/Foo.java").build(); | |||
InputFile input2 = new InputFileBuilder(file2, Charsets.UTF_8, "src/main/java/Bar.java").build(); | |||
assertThat(InputFiles.toFiles(Lists.newArrayList(input1, input2))).containsOnly(file1, file2); | |||
} |