diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-09 18:19:01 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-09 18:21:36 +0200 |
commit | e8cbc899adb18eec17aa5df5db11eea06c46c009 (patch) | |
tree | d2f7693fcbacc03a42182b98c314dcaae3e39316 /plugins | |
parent | 2557634d1700e935047ecb190681a6cd418cc2b1 (diff) | |
download | sonarqube-e8cbc899adb18eec17aa5df5db11eea06c46c009.tar.gz sonarqube-e8cbc899adb18eec17aa5df5db11eea06c46c009.zip |
SONAR-3677 enhanced file system
Diffstat (limited to 'plugins')
3 files changed, 51 insertions, 94 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FileHashSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FileHashSensor.java index df5cd76ade5..74af986d814 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FileHashSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FileHashSensor.java @@ -19,39 +19,32 @@ */ package org.sonar.plugins.core.sensors; +import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.FileType; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.api.scan.filesystem.InputFile; +import org.sonar.api.utils.KeyValueFormat; import org.sonar.batch.index.ComponentDataCache; -import org.sonar.batch.scan.filesystem.FileHashCache; +import org.sonar.batch.scan.filesystem.InputFileCache; import org.sonar.core.source.SnapshotDataType; -import java.io.File; -import java.util.List; +import java.util.Map; /** * This sensor will retrieve hash of each file of the current module and store it in DB * in order to compare it during next analysis and see if the file was modified. * This is used by the incremental preview mode. - * @see org.sonar.plugins.core.batch.IncrementalPreviewFilter * @since 4.0 */ public final class FileHashSensor implements Sensor { - private ModuleFileSystem moduleFileSystem; - private PathResolver pathResolver; - private ComponentDataCache componentDataCache; - private FileHashCache fileHashCache; + private final InputFileCache fileCache; + private final ComponentDataCache componentDataCache; - public FileHashSensor(FileHashCache fileHashCache, ModuleFileSystem moduleFileSystem, PathResolver pathResolver, ComponentDataCache componentDataCache) { - this.fileHashCache = fileHashCache; - this.moduleFileSystem = moduleFileSystem; - this.pathResolver = pathResolver; + public FileHashSensor(InputFileCache fileCache, ComponentDataCache componentDataCache) { + this.fileCache = fileCache; this.componentDataCache = componentDataCache; } @@ -61,20 +54,17 @@ public final class FileHashSensor implements Sensor { @Override public void analyse(Project project, SensorContext context) { - StringBuilder fileHashMap = new StringBuilder(); - analyse(fileHashMap, project, FileType.SOURCE); - analyse(fileHashMap, project, FileType.TEST); - String fileHashes = fileHashMap.toString(); - if (StringUtils.isNotBlank(fileHashes)) { - componentDataCache.setStringData(project.getKey(), SnapshotDataType.FILE_HASH.getValue(), fileHashes); + Map<String, String> map = Maps.newHashMap(); + for (InputFile inputFile : fileCache.byModule(project.key())) { + String baseRelativePath = inputFile.attribute(InputFile.ATTRIBUTE_BASE_RELATIVE_PATH); + String hash = inputFile.attribute(InputFile.ATTRIBUTE_HASH); + if (StringUtils.isNotEmpty(baseRelativePath) && StringUtils.isNotEmpty(hash)) { + map.put(baseRelativePath, hash); + } } - } - - private void analyse(StringBuilder fileHashMap, Project project, FileType fileType) { - List<File> files = moduleFileSystem.files(FileQuery.on(fileType).onLanguage(project.getLanguageKey())); - for (File file : files) { - String hash = fileHashCache.getCurrentHash(file, moduleFileSystem.sourceCharset()); - fileHashMap.append(pathResolver.relativePath(moduleFileSystem.baseDir(), file)).append("=").append(hash).append("\n"); + if (!map.isEmpty()) { + String data = KeyValueFormat.format(map); + componentDataCache.setStringData(project.getKey(), SnapshotDataType.FILE_HASH.getValue(), data); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FileHashSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FileHashSensorTest.java index b46519c27d3..7e11a61698c 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FileHashSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FileHashSensorTest.java @@ -19,96 +19,63 @@ */ package org.sonar.plugins.core.sensors; -import com.google.common.base.Charsets; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.commons.io.FileUtils; -import org.junit.Before; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import edu.emory.mathcs.backport.java.util.Collections; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.api.scan.filesystem.InputFile; import org.sonar.batch.index.ComponentDataCache; -import org.sonar.batch.scan.filesystem.FileHashCache; - -import java.io.File; -import java.util.Arrays; -import java.util.Collections; +import org.sonar.batch.scan.filesystem.InputFileCache; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class FileHashSensorTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private FileHashSensor sensor; - @Rule public ExpectedException thrown = ExpectedException.none(); - private ModuleFileSystem fileSystem; + Project project = new Project("struts"); + InputFileCache fileCache = mock(InputFileCache.class); + ComponentDataCache componentDataCache = mock(ComponentDataCache.class); + FileHashSensor sensor = new FileHashSensor(fileCache, componentDataCache); - private ComponentDataCache componentDataCache; - - private Project project; + @Test + public void store_file_hashes() throws Exception { + when(fileCache.byModule("struts")).thenReturn(Lists.<InputFile>newArrayList( + InputFile.create(temp.newFile(), "src/Foo.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "ABC")), + InputFile.create(temp.newFile(), "src/Bar.java", ImmutableMap.of(InputFile.ATTRIBUTE_HASH, "DEF")) + )); - private FileHashCache fileHashCache; + SensorContext sensorContext = mock(SensorContext.class); + sensor.analyse(project, sensorContext); - @Before - public void prepare() { - fileSystem = mock(ModuleFileSystem.class); - when(fileSystem.sourceCharset()).thenReturn(Charsets.UTF_8); - componentDataCache = mock(ComponentDataCache.class); - fileHashCache = mock(FileHashCache.class); - sensor = new FileHashSensor(fileHashCache, fileSystem, new PathResolver(), componentDataCache); - PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty("sonar.language", "java"); - project = new Project("java_project").setConfiguration(conf).setLanguage(Java.INSTANCE); + verify(componentDataCache).setStringData("struts", "file_hash", "src/Foo.java=ABC;src/Bar.java=DEF"); + verifyZeroInteractions(sensorContext); } @Test - public void improve_code_coverage() throws Exception { + public void various_tests() throws Exception { assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); assertThat(sensor.toString()).isEqualTo("FileHashSensor"); } @Test - public void computeHashes() throws Exception { - File baseDir = temp.newFolder(); - File file1 = new File(baseDir, "src/com/foo/Bar.java"); - FileUtils.write(file1, "Bar", Charsets.UTF_8); - when(fileHashCache.getCurrentHash(file1, Charsets.UTF_8)).thenReturn("barhash"); - File file2 = new File(baseDir, "src/com/foo/Foo.java"); - FileUtils.write(file2, "Foo", Charsets.UTF_8); - when(fileHashCache.getCurrentHash(file2, Charsets.UTF_8)).thenReturn("foohash"); - when(fileSystem.baseDir()).thenReturn(baseDir); - when(fileSystem.files(any(FileQuery.class))).thenReturn(Arrays.asList(file1, file2)).thenReturn(Collections.<File> emptyList()); - sensor.analyse(project, mock(SensorContext.class)); + public void dont_save_hashes_if_no_files() throws Exception { + when(fileCache.byModule("struts")).thenReturn(Collections.<InputFile>emptyList()); - verify(componentDataCache).setStringData("java_project", "hash", - "src/com/foo/Bar.java=barhash\n" - + "src/com/foo/Foo.java=foohash\n"); - } - - @Test - public void dont_save_hashes_if_no_file() throws Exception { - File baseDir = temp.newFolder(); - when(fileSystem.baseDir()).thenReturn(baseDir); - when(fileSystem.files(any(FileQuery.class))).thenReturn(Collections.<File> emptyList()); - sensor.analyse(project, mock(SensorContext.class)); + SensorContext sensorContext = mock(SensorContext.class); + sensor.analyse(project, sensorContext); - verify(componentDataCache, never()).setStringData(anyString(), anyString(), anyString()); + verifyZeroInteractions(componentDataCache); + verifyZeroInteractions(sensorContext); } } diff --git a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java index 3b877f3d479..6b883439371 100644 --- a/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java +++ b/plugins/sonar-maven-batch-plugin/src/main/java/org/sonar/plugins/maven/MavenProjectConverter.java @@ -226,11 +226,11 @@ public class MavenProjectConverter implements TaskExtension { public static void synchronizeFileSystem(MavenProject pom, DefaultModuleFileSystem into) { into.resetDirs( - pom.getBasedir(), - getBuildDir(pom), - resolvePaths(pom.getCompileSourceRoots(), pom.getBasedir()), - resolvePaths(pom.getTestCompileSourceRoots(), pom.getBasedir()), - Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()))); + pom.getBasedir(), + getBuildDir(pom), + resolvePaths(pom.getCompileSourceRoots(), pom.getBasedir()), + resolvePaths(pom.getTestCompileSourceRoots(), pom.getBasedir()), + Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()))); } static File resolvePath(@Nullable String path, File basedir) { |