diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-07-31 13:12:10 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-04 14:58:16 +0200 |
commit | 75e47ba5a19b3d4ab901d2ed7a180714b141d580 (patch) | |
tree | 9f15f196571ed354b9a17e0a790734813d7493ed /sonar-plugin-api | |
parent | 2183aa17c17e8cb7385bf1662a57368c0b2979fd (diff) | |
download | sonarqube-75e47ba5a19b3d4ab901d2ed7a180714b141d580.tar.gz sonarqube-75e47ba5a19b3d4ab901d2ed7a180714b141d580.zip |
Rework file indexing
* Drop cycle ModuleFileSystem -> FileIndexer -> ModuleFileSystem
* Ensure basedir is initialized once
* Drop useless FileSystemLogger, and log in ModuleFileSystemInitializer
Diffstat (limited to 'sonar-plugin-api')
11 files changed, 106 insertions, 88 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java index f7d2858a7a3..a3922b5ee64 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java @@ -24,7 +24,6 @@ import com.google.common.collect.SetMultimap; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; -import java.nio.file.LinkOption; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -60,23 +59,18 @@ public class DefaultFileSystem implements FileSystem { * Only for testing */ public DefaultFileSystem(Path baseDir) { - this(baseDir.toFile(), new MapCache()); + this(baseDir, new MapCache()); } /** * Only for testing */ public DefaultFileSystem(File baseDir) { - this(baseDir, new MapCache()); + this(baseDir.toPath(), new MapCache()); } - protected DefaultFileSystem(@Nullable File baseDir, Cache cache) { - // Basedir can be null with views - try { - this.baseDir = baseDir != null ? baseDir.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS) : new File(".").toPath().toAbsolutePath().normalize(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + protected DefaultFileSystem(Path baseDir, Cache cache) { + this.baseDir = baseDir; this.cache = cache; this.predicates = new DefaultFilePredicates(this.baseDir); } @@ -90,22 +84,18 @@ public class DefaultFileSystem implements FileSystem { return baseDir.toFile(); } - public DefaultFileSystem setEncoding(@Nullable Charset e) { + public DefaultFileSystem setEncoding(Charset e) { this.encoding = e; return this; } @Override public Charset encoding() { - return encoding == null ? Charset.defaultCharset() : encoding; - } - - public boolean isDefaultJvmEncoding() { - return encoding == null; + return encoding; } - public DefaultFileSystem setWorkDir(File d) { - this.workDir = d.getAbsoluteFile().toPath().normalize(); + public DefaultFileSystem setWorkDir(Path d) { + this.workDir = d; return this; } @@ -150,13 +140,11 @@ public class DefaultFileSystem implements FileSystem { * Default predicate is used when some files/dirs should not be processed by sensors. */ public Iterable<InputFile> inputFiles() { - doPreloadFiles(); return OptimizedFilePredicateAdapter.create(predicates.all()).get(cache); } @Override public Iterable<InputFile> inputFiles(FilePredicate predicate) { - doPreloadFiles(); Iterable<InputFile> iterable = OptimizedFilePredicateAdapter.create(predicate).get(cache); if (defaultPredicate != null) { return StreamSupport.stream(iterable.spliterator(), false) @@ -172,7 +160,6 @@ public class DefaultFileSystem implements FileSystem { @Override public Iterable<File> files(FilePredicate predicate) { - doPreloadFiles(); return () -> StreamSupport.stream(inputFiles(predicate).spliterator(), false) .map(InputFile::file) .iterator(); @@ -180,7 +167,6 @@ public class DefaultFileSystem implements FileSystem { @Override public InputDir inputDir(File dir) { - doPreloadFiles(); String relativePath = PathUtils.sanitize(new PathResolver().relativePath(baseDir.toFile(), dir)); if (relativePath == null) { return null; @@ -200,7 +186,6 @@ public class DefaultFileSystem implements FileSystem { @Override public SortedSet<String> languages() { - doPreloadFiles(); return cache.languages(); } @@ -209,13 +194,6 @@ public class DefaultFileSystem implements FileSystem { return predicates; } - /** - * This method is called before each search of files. - */ - protected void doPreloadFiles() { - // nothing to do by default - } - public abstract static class Cache implements Index { protected abstract void doAdd(InputFile inputFile); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java index 1165e54d494..e5d8a5c060b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java @@ -20,15 +20,17 @@ package org.sonar.api.batch.fs.internal; import java.io.File; +import java.io.IOException; +import java.nio.file.LinkOption; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; - import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; - +import org.apache.commons.io.FileUtils; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputModule; @@ -37,8 +39,8 @@ import org.sonar.api.batch.fs.InputModule; */ @Immutable public class DefaultInputModule extends DefaultInputComponent implements InputModule { - private final File baseDir; - private final File workDir; + private final Path baseDir; + private final Path workDir; private final String name; private final String version; private final String originalName; @@ -56,21 +58,14 @@ public class DefaultInputModule extends DefaultInputComponent implements InputMo /** * For testing only! */ - public DefaultInputModule(String moduleKey) { - this(ProjectDefinition.create().setKey(moduleKey), TestInputFileBuilder.nextBatchId()); - } - - /** - * For testing only! - */ public DefaultInputModule(ProjectDefinition definition) { this(definition, TestInputFileBuilder.nextBatchId()); } public DefaultInputModule(ProjectDefinition definition, int batchId) { super(batchId); - this.baseDir = definition.getBaseDir(); - this.workDir = definition.getWorkDir(); + this.baseDir = initBaseDir(definition); + this.workDir = initWorkingDir(definition); this.name = definition.getName(); this.originalName = definition.getOriginalName(); this.version = definition.getVersion(); @@ -86,6 +81,26 @@ public class DefaultInputModule extends DefaultInputComponent implements InputMo this.moduleKey = definition.getKey(); } + private static Path initBaseDir(ProjectDefinition module) { + Path result; + try { + result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS); + } catch (IOException e) { + throw new IllegalStateException("Unable to resolve module baseDir", e); + } + return result; + } + + private static Path initWorkingDir(ProjectDefinition module) { + File workingDirAsFile = module.getWorkDir(); + try { + FileUtils.forceMkdir(workingDirAsFile); + } catch (Exception e) { + throw new IllegalStateException("Fail to create working dir: " + workingDirAsFile.getAbsolutePath(), e); + } + return workingDirAsFile.getAbsoluteFile().toPath().normalize(); + } + /** * Module key without branch */ @@ -102,12 +117,12 @@ public class DefaultInputModule extends DefaultInputComponent implements InputMo public ProjectDefinition definition() { return definition; } - - public File getBaseDir() { + + public Path getBaseDir() { return baseDir; } - public File getWorkDir() { + public Path getWorkDir() { return workDir; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java index fa86a9e17e6..c06e1ec4c30 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java @@ -26,9 +26,7 @@ import java.nio.charset.Charset; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; - import javax.annotation.Nullable; - import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.utils.PathUtils; @@ -205,7 +203,7 @@ public class TestInputFileBuilder { } public static DefaultInputModule newDefaultInputModule(String moduleKey, File baseDir) { - ProjectDefinition definition = ProjectDefinition.create().setKey(moduleKey).setBaseDir(baseDir); + ProjectDefinition definition = ProjectDefinition.create().setKey(moduleKey).setBaseDir(baseDir).setWorkDir(new File(baseDir, ".sonar")); return newDefaultInputModule(definition); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 711821e7909..edfa3645112 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -21,6 +21,7 @@ package org.sonar.api.batch.sensor.internal; import java.io.File; import java.io.Serializable; +import java.nio.charset.Charset; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; @@ -34,6 +35,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.TextRange; @@ -104,10 +106,10 @@ public class SensorContextTester implements SensorContext { private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); - this.fs = new DefaultFileSystem(moduleBaseDir); + this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); - this.module = new DefaultInputModule("projectKey"); + this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java index 17698835e8e..692fd5427da 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java @@ -26,7 +26,6 @@ import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; - import org.apache.commons.io.FilenameUtils; import org.sonar.api.batch.ScannerSide; import org.sonar.api.utils.PathUtils; @@ -40,7 +39,10 @@ import static java.util.stream.Collectors.joining; @Immutable public class PathResolver { - public File relativeFile(File dir, String path) { + /** + * Static since 6.6 + */ + public static File relativeFile(File dir, String path) { return dir.toPath().resolve(path).normalize().toFile(); } @@ -77,10 +79,11 @@ public class PathResolver { * <li>null is returned if file is not a child of dir * <li>the resulting path is converted to use Unix separators * </ul> + * Static since 6.6 * @since 6.0 */ @CheckForNull - public String relativePath(Path dir, Path file) { + public static String relativePath(Path dir, Path file) { Path baseDir = dir.normalize(); Path path = file.normalize(); if (!path.startsWith(baseDir)) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java index 1fcc28e0322..d3ba5dbbf86 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java @@ -19,16 +19,15 @@ */ package org.sonar.api.batch.fs.internal; +import java.io.File; +import java.nio.charset.Charset; +import java.util.Iterator; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import java.io.File; -import java.nio.charset.Charset; -import java.util.Iterator; - import static org.assertj.core.api.Assertions.assertThat; public class DefaultFileSystemTest { @@ -55,19 +54,15 @@ public class DefaultFileSystemTest { assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath()); File workdir = temp.newFolder(); - fs.setWorkDir(workdir); + fs.setWorkDir(workdir.toPath()); assertThat(fs.workDir()).isAbsolute().isDirectory().exists(); assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workdir.getCanonicalPath()); } @Test public void test_encoding() throws Exception { - assertThat(fs.isDefaultJvmEncoding()).isTrue(); - assertThat(fs.encoding()).isEqualTo(Charset.defaultCharset()); - fs.setEncoding(Charset.forName("ISO-8859-1")); assertThat(fs.encoding()).isEqualTo(Charset.forName("ISO-8859-1")); - assertThat(fs.isDefaultJvmEncoding()).isFalse(); } @Test diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java index 34fe36b38a0..bb67cdc92d8 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java @@ -19,25 +19,32 @@ */ package org.sonar.api.batch.fs.internal; -import static org.assertj.core.api.Assertions.assertThat; - import java.io.File; +import java.io.IOException; import java.util.Collections; - +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.bootstrap.ProjectDefinition; +import static org.assertj.core.api.Assertions.assertThat; + public class DefaultInputModuleTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test - public void testGetters() { + public void testGetters() throws IOException { ProjectDefinition def = ProjectDefinition.create(); def.setKey("projectKey"); def.setName("projectName"); - def.setBaseDir(new File("baseDir")); + File baseDir = temp.newFolder(); + def.setBaseDir(baseDir); def.setVersion("version"); def.setDescription("desc"); - def.setWorkDir(new File("workDir")); + File workDir = temp.newFolder(); + def.setWorkDir(workDir); def.setSources("file1"); def.setTests("test1"); DefaultInputModule module = new DefaultInputModule(def); @@ -47,12 +54,12 @@ public class DefaultInputModuleTest { assertThat(module.getOriginalName()).isEqualTo("projectName"); assertThat(module.definition()).isEqualTo(def); assertThat(module.getBranch()).isNull(); - assertThat(module.getBaseDir()).isEqualTo(new File("baseDir")); + assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); assertThat(module.getKeyWithBranch()).isEqualTo("projectKey"); assertThat(module.getVersion()).isEqualTo("version"); assertThat(module.getOriginalVersion()).isEqualTo("version"); assertThat(module.getDescription()).isEqualTo("desc"); - assertThat(module.getWorkDir()).isEqualTo(new File("workDir")); + assertThat(module.getWorkDir()).isEqualTo(workDir.toPath()); assertThat(module.sources()).isEqualTo(Collections.singletonList("file1")); assertThat(module.tests()).isEqualTo(Collections.singletonList("test1")); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java index 6f12bf92ccb..b6dc6a38d65 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java @@ -19,19 +19,23 @@ */ package org.sonar.api.batch.fs.internal; -import static org.assertj.core.api.Assertions.assertThat; - import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; - import org.apache.commons.io.IOUtils; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.InputFile.Status; import org.sonar.api.batch.fs.InputFile.Type; +import static org.assertj.core.api.Assertions.assertThat; + public class TestInputFileBuilderTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void setContent() throws IOException { DefaultInputFile file = TestInputFileBuilder.create("module", "invalidPath") @@ -59,9 +63,10 @@ public class TestInputFileBuilderTest { } @Test - public void testCreateInputModule() { - DefaultInputModule module = TestInputFileBuilder.newDefaultInputModule("key", new File("baseDir")); + public void testCreateInputModule() throws IOException { + File baseDir = temp.newFolder(); + DefaultInputModule module = TestInputFileBuilder.newDefaultInputModule("key", baseDir); assertThat(module.key()).isEqualTo("key"); - assertThat(module.getBaseDir()).isEqualTo(new File("baseDir")); + assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 566facb4a1a..7f801cd641b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -20,11 +20,13 @@ package org.sonar.api.batch.sensor.internal; import java.io.File; +import java.io.IOException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; @@ -124,7 +126,7 @@ public class SensorContextTesterTest { } @Test - public void testMeasures() { + public void testMeasures() throws IOException { assertThat(tester.measures("foo:src/Foo.java")).isEmpty(); assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNull(); tester.<Integer>newMeasure() @@ -143,7 +145,7 @@ public class SensorContextTesterTest { assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull(); assertThat(tester.measure("foo:src/Foo.java", "lines")).isNotNull(); tester.<Integer>newMeasure() - .on(new DefaultInputModule("foo")) + .on(new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()))) .forMetric(CoreMetrics.DIRECTORIES) .withValue(4) .save(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java index c88e06b0b48..30e35f36a28 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java @@ -19,7 +19,11 @@ */ package org.sonar.api.batch.sensor.issue.internal; +import java.io.IOException; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -34,6 +38,9 @@ import static org.mockito.Mockito.verify; public class DefaultIssueTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php") .initMetadata("Foo\nBar\n") .build(); @@ -82,16 +89,17 @@ public class DefaultIssueTest { } @Test - public void build_project_issue() { + public void build_project_issue() throws IOException { SensorStorage storage = mock(SensorStorage.class); + DefaultInputModule inputModule = new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultIssue issue = new DefaultIssue(storage) .at(new DefaultIssueLocation() - .on(new DefaultInputModule("foo")) + .on(inputModule) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .effortToFix(10.0); - assertThat(issue.primaryLocation().inputComponent()).isEqualTo(new DefaultInputModule("foo")); + assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputModule); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.primaryLocation().textRange()).isNull(); assertThat(issue.effortToFix()).isEqualTo(10.0); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java index 3419141324a..afa9490368e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java @@ -19,10 +19,12 @@ */ package org.sonar.api.batch.sensor.measure.internal; +import java.io.IOException; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.internal.SensorStorage; @@ -37,6 +39,9 @@ public class DefaultMeasureTest { @Rule public ExpectedException thrown = ExpectedException.none(); + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void build_file_measure() { SensorStorage storage = mock(SensorStorage.class); @@ -55,15 +60,15 @@ public class DefaultMeasureTest { } @Test - public void build_project_measure() { + public void build_project_measure() throws IOException { SensorStorage storage = mock(SensorStorage.class); - DefaultInputModule module = new DefaultInputModule("foo"); + DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage) .forMetric(CoreMetrics.LINES) .on(module) .withValue(3); - assertThat(newMeasure.inputComponent()).isEqualTo(new DefaultInputModule("foo")); + assertThat(newMeasure.inputComponent()).isEqualTo(module); assertThat(newMeasure.metric()).isEqualTo(CoreMetrics.LINES); assertThat(newMeasure.value()).isEqualTo(3); @@ -73,11 +78,11 @@ public class DefaultMeasureTest { } @Test - public void not_allowed_to_call_on_twice() { + public void not_allowed_to_call_on_twice() throws IOException { thrown.expect(IllegalStateException.class); thrown.expectMessage("on() already called"); new DefaultMeasure<Integer>() - .on(new DefaultInputModule("foo")) + .on(new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()))) .on(new TestInputFileBuilder("foo", "src/Foo.php").build()) .withValue(3) .save(); |