diff options
2 files changed, 42 insertions, 36 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index d14f205ed8a..f7860cd3db0 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -60,18 +60,18 @@ public class FileSystemMediumTest { public ExpectedException thrown = ExpectedException.none(); private LogOutputRecorder logs = new LogOutputRecorder(); - - public ScannerMediumTester tester = ScannerMediumTester.builder() - .registerPlugin("xoo", new XooPlugin()) - .addDefaultQProfile("xoo", "Sonar Way") - .setLogOutput(logs) - .build(); + private ScannerMediumTester tester; private File baseDir; private ImmutableMap.Builder<String, String> builder; @Before public void prepare() throws IOException { + tester = ScannerMediumTester.builder() + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .setLogOutput(logs) + .build(); tester.start(); baseDir = temp.getRoot(); @@ -87,7 +87,10 @@ public class FileSystemMediumTest { @After public void stop() { - tester.stop(); + if (tester != null) { + tester.stop(); + tester = null; + } logs = new LogOutputRecorder(); } @@ -284,14 +287,15 @@ public class FileSystemMediumTest { @Test public void createIssueOnAnyFile() throws IOException { LogOutputRecorder logs = new LogOutputRecorder(); - ScannerMediumTester tester2 = ScannerMediumTester.builder() + stop(); + tester = ScannerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") .addRules(new XooRulesDefinition()) .setLogOutput(logs) .addActiveRule("xoo", "OneIssuePerUnknownFile", null, "OneIssuePerUnknownFile", "MAJOR", null, "xoo") .build(); - tester2.start(); + tester.start(); builder = createBuilder(); @@ -301,7 +305,7 @@ public class FileSystemMediumTest { File xooFile = new File(srcDir, "sample.unknown"); FileUtils.write(xooFile, "Sample xoo\ncontent"); - TaskResult result = tester2.newTask() + TaskResult result = tester.newTask() .properties(builder .put("sonar.sources", "src") .build()) @@ -312,21 +316,20 @@ public class FileSystemMediumTest { assertThat(logs.getAllAsString()).contains("'src/sample.unknown' generated metadata"); DefaultInputFile inputFile = (DefaultInputFile) result.inputFile("src/sample.unknown"); assertThat(result.getReportComponent(inputFile.key())).isNotNull(); - - tester2.stop(); } @Test public void lazyIssueExclusion() throws IOException { + tester.stop(); LogOutputRecorder logs = new LogOutputRecorder(); - ScannerMediumTester tester2 = ScannerMediumTester.builder() + tester = ScannerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") .addRules(new XooRulesDefinition()) .setLogOutput(logs) .addActiveRule("xoo", "OneIssuePerFile", null, "OneIssuePerFile", "MAJOR", null, "xoo") .build(); - tester2.start(); + tester.start(); builder = createBuilder(); builder.put("sonar.issue.ignore.allfile", "1") @@ -342,7 +345,7 @@ public class FileSystemMediumTest { new Random().nextBytes(b); FileUtils.writeByteArrayToFile(unknownFile, b); - tester2.newTask() + tester.newTask() .properties(builder .put("sonar.sources", "src") .build()) @@ -351,9 +354,6 @@ public class FileSystemMediumTest { assertThat(logs.getAllAsString()).containsOnlyOnce("'src/myfile.binary' indexed with language 'null'"); assertThat(logs.getAllAsString()).doesNotContain("'src/myfile.binary' generating issue exclusions"); assertThat(logs.getAllAsString()).containsOnlyOnce("'src/sample.xoo' generating issue exclusions"); - - tester2.stop(); - } @Test @@ -383,13 +383,14 @@ public class FileSystemMediumTest { @Test public void publishFilesWithIssues() throws IOException { - ScannerMediumTester tester2 = ScannerMediumTester.builder() + stop(); + tester = ScannerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") .addRules(new XooRulesDefinition()) .addActiveRule("xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo") .build(); - tester2.start(); + tester.start(); builder = createBuilder(); @@ -399,7 +400,7 @@ public class FileSystemMediumTest { File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent"); - TaskResult result = tester2.newTask() + TaskResult result = tester.newTask() .properties(builder .put("sonar.sources", "src") .build()) @@ -411,19 +412,18 @@ public class FileSystemMediumTest { assertThat(file.publish()).isTrue(); assertThat(result.getReportComponent(dir.key())).isNotNull(); assertThat(result.getReportComponent(file.key())).isNotNull(); - - tester2.stop(); } @Test public void publishDirsWithIssues() throws IOException { - ScannerMediumTester tester2 = ScannerMediumTester.builder() + stop(); + tester = ScannerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") .addRules(new XooRulesDefinition()) .addActiveRule("xoo", "OneIssuePerDirectory", null, "OneIssuePerDirectory", "MAJOR", null, "xoo") .build(); - tester2.start(); + tester.start(); builder = ImmutableMap.<String, String>builder() .put("sonar.task", "scan") @@ -433,22 +433,22 @@ public class FileSystemMediumTest { .put("sonar.projectVersion", "1.0-SNAPSHOT") .put("sonar.projectDescription", "Description of Foo Project"); - Path unknownRelative = Paths.get("src/unknown/file.notanalyzed"); + Path unknownRelative = Paths.get("src", "unknown", "file.notanalyzed"); Path unknown = baseDir.toPath().resolve(unknownRelative); Files.createDirectories(unknown.getParent()); Files.write(unknown, "dummy content".getBytes()); - Path emptyDirRelative = Paths.get("src/emptydir"); + Path emptyDirRelative = Paths.get("src", "emptydir"); Files.createDirectories(emptyDirRelative); - TaskResult result = tester2.newTask() + TaskResult result = tester.newTask() .properties(builder .put("sonar.sources", "src") .build()) .start(); - DefaultInputFile unknownInputFile = (DefaultInputFile) result.inputFile(unknownRelative.toString()); - InputDir unknownInputDir = result.inputDir(unknownRelative.getParent().toString()); + DefaultInputFile unknownInputFile = (DefaultInputFile) result.inputFile("src/unknown/file.notanalyzed"); + InputDir unknownInputDir = result.inputDir("src/unknown"); assertThat(unknownInputFile.publish()).isFalse(); assertThat(result.getReportComponent(unknownInputDir.key())).isNotNull(); @@ -459,8 +459,6 @@ public class FileSystemMediumTest { // no issues on parent dir InputDir parentInputDir = result.inputDir(unknownRelative.getParent().getParent().toString()); assertThat(parentInputDir).isNull(); - - tester2.stop(); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/ScanOnlyChangedTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/ScanOnlyChangedTest.java index 8f080f277c0..7d8ebd5331d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/ScanOnlyChangedTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/ScanOnlyChangedTest.java @@ -23,6 +23,11 @@ import com.google.common.collect.ImmutableMap; import com.google.common.io.Resources; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -35,6 +40,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; +import org.sonar.api.batch.fs.internal.FileMetadata; import org.sonar.api.utils.log.LogTester; import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.scanner.issue.tracking.TrackedIssue; @@ -67,10 +73,12 @@ public class ScanOnlyChangedTest { } @Before - public void prepare() throws IOException { + public void prepare() throws IOException, URISyntaxException { String filePath = "xources/hello/HelloJava.xoo"; - String md5sum = DigestUtils.md5Hex(FileUtils.readFileToString(new File( - Resources.getResource("mediumtest/xoo/sample/" + filePath).getPath()))); + Path path = Paths.get(Resources.getResource("mediumtest/xoo/sample/" + filePath).toURI()); + String hash = new FileMetadata() + .readMetadata(Files.newInputStream(path), StandardCharsets.UTF_8, filePath) + .hash(); tester = ScannerMediumTester.builder() .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES)) @@ -81,7 +89,7 @@ public class ScanOnlyChangedTest { .addActiveRule("xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo") .addActiveRule("xoo", "OneIssuePerModule", null, "OneIssuePerModule", "MAJOR", null, "xoo") // this will cause the file to have status==SAME - .addFileData("sample", filePath, new FileData(md5sum, null)) + .addFileData("sample", filePath, new FileData(hash, null)) .setPreviousAnalysisDate(new Date()) // Existing issue that is copied .mockServerIssue(ServerIssue.newBuilder().setKey("xyz") |