diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-03-03 08:51:43 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-03-03 08:51:43 +0100 |
commit | 2d898468dc31c97922f1757a308bb891bece4140 (patch) | |
tree | e4afb2e3b81754102825f4e724ed6a583eec0075 /sonar-scanner-engine/src/test | |
parent | c946c2174c56c7c0463292c5a94e1d542950e028 (diff) | |
parent | ea8d74d2617e334ffbf827ec29a6accb8a7c1cfc (diff) | |
download | sonarqube-2d898468dc31c97922f1757a308bb891bece4140.tar.gz sonarqube-2d898468dc31c97922f1757a308bb891bece4140.zip |
Automatic merge from branch-6.3
* origin/branch-6.3:
SONAR-8883 Fix Server::getVersion() on scanner side, and reenable IssueJsonReportTest IT
SONAR-8622 Fix preview mode/JSON report on branches
Remove javaProperties plugin from PluginsTest
Remove JSON plugin
SONAR-8460 Do not fail to write response when profile not found
SONAR-8460 Remove Errors
SONAR-8460 Remove creation BadRequestException with Errors
SONAR-8460 Fix bundled error messages
SONAR-8460 Simplify creation of BadRequestException with one message
SONAR-8460 Remove isEmpty from Errors
SONAR-8460 Remove check from Errors
SONAR-8460 Remove writeJson from Errors
fix quality flaw
Diffstat (limited to 'sonar-scanner-engine/src/test')
3 files changed, 47 insertions, 18 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java index b7605bff482..f2f783a6bb3 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java @@ -19,18 +19,19 @@ */ package org.sonar.scanner.issue.tracking; +import java.io.File; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.scanner.issue.tracking.ServerLineHashesLoader; -import org.sonar.scanner.issue.tracking.SourceHashHolder; -import java.io.File; -import java.nio.charset.StandardCharsets; +import org.sonar.api.batch.fs.internal.DefaultInputModule; import static org.apache.commons.codec.digest.DigestUtils.md5Hex; import static org.assertj.core.api.Assertions.assertThat; @@ -49,6 +50,7 @@ public class SourceHashHolderTest { DefaultInputFile file; private File ioFile; + private ProjectDefinition def = ProjectDefinition.create(); @Before public void setUp() throws Exception { @@ -60,7 +62,7 @@ public class SourceHashHolderTest { when(file.lines()).thenReturn(1); when(file.charset()).thenReturn(StandardCharsets.UTF_8); - sourceHashHolder = new SourceHashHolder(file, lastSnapshots); + sourceHashHolder = new SourceHashHolder(new DefaultInputModule(def, 1), file, lastSnapshots); } @Test @@ -71,8 +73,6 @@ public class SourceHashHolderTest { assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); assertThat(sourceHashHolder.getHashedSource().getHash(2)).isEqualTo(""); - verify(file).key(); - verify(file).status(); assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); } @@ -80,9 +80,28 @@ public class SourceHashHolderTest { @Test public void should_lazy_load_reference_hashes_when_status_changed() throws Exception { final String source = "source"; + FileUtils.write(ioFile, source, StandardCharsets.UTF_8); + def.setKey("foo"); + when(file.relativePath()).thenReturn("src/Foo.java"); String key = "foo:src/Foo.java"; + when(file.status()).thenReturn(InputFile.Status.CHANGED); + when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); + + assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); + verify(lastSnapshots).getLineHashes(key); + + assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); + Mockito.verifyNoMoreInteractions(lastSnapshots); + } + + @Test + public void should_lazy_load_reference_hashes_when_status_changed_on_branch() throws Exception { + final String source = "source"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); - when(file.key()).thenReturn(key); + def.setKey("foo"); + def.properties().put(CoreProperties.PROJECT_BRANCH_PROPERTY, "myBranch"); + when(file.relativePath()).thenReturn("src/Foo.java"); + String key = "foo:myBranch:src/Foo.java"; when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/platform/DefaultServerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/platform/DefaultServerTest.java index 3c6df76e498..47a09228471 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/platform/DefaultServerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/platform/DefaultServerTest.java @@ -21,8 +21,11 @@ package org.sonar.scanner.platform; import org.junit.Test; import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; +import org.sonar.api.SonarQubeSide; import org.sonar.api.config.MapSettings; +import org.sonar.api.config.Settings; +import org.sonar.api.internal.SonarRuntimeImpl; +import org.sonar.api.utils.Version; import org.sonar.scanner.bootstrap.ScannerWsClient; import static org.assertj.core.api.Assertions.assertThat; @@ -35,13 +38,12 @@ public class DefaultServerTest { public void shouldLoadServerProperties() { Settings settings = new MapSettings(); settings.setProperty(CoreProperties.SERVER_ID, "123"); - settings.setProperty(CoreProperties.SERVER_VERSION, "2.2"); settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000"); settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde"); ScannerWsClient client = mock(ScannerWsClient.class); when(client.baseUrl()).thenReturn("http://foo.com"); - DefaultServer metadata = new DefaultServer(settings, client); + DefaultServer metadata = new DefaultServer(settings, client, SonarRuntimeImpl.forSonarQube(Version.parse("2.2"), SonarQubeSide.SCANNER)); assertThat(metadata.getId()).isEqualTo("123"); assertThat(metadata.getVersion()).isEqualTo("2.2"); @@ -61,7 +63,7 @@ public class DefaultServerTest { Settings settings = new MapSettings(); ScannerWsClient client = mock(ScannerWsClient.class); when(client.baseUrl()).thenReturn("http://foo.com/"); - DefaultServer metadata = new DefaultServer(settings, client); + DefaultServer metadata = new DefaultServer(settings, client, null); settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://server.com/"); assertThat(metadata.getPublicRootUrl()).isEqualTo("http://server.com"); @@ -75,7 +77,7 @@ public class DefaultServerTest { Settings settings = new MapSettings(); settings.setProperty(CoreProperties.SERVER_STARTTIME, "invalid"); ScannerWsClient client = mock(ScannerWsClient.class); - DefaultServer metadata = new DefaultServer(settings, client); + DefaultServer metadata = new DefaultServer(settings, client, null); metadata.getStartedAt(); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java index 25fd1ca01fd..59ce9e71958 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java @@ -39,8 +39,8 @@ import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.rule.Rules; import org.sonar.api.batch.rule.internal.RulesBuilder; -import org.sonar.api.config.Settings; import org.sonar.api.config.MapSettings; +import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import org.sonar.api.platform.Server; import org.sonar.api.rule.RuleKey; @@ -48,6 +48,7 @@ import org.sonar.scanner.issue.IssueCache; import org.sonar.scanner.issue.tracking.TrackedIssue; import org.sonar.scanner.protocol.input.ScannerInput; import org.sonar.scanner.repository.user.UserRepositoryLoader; +import org.sonar.scanner.scan.DefaultComponentTree; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson; @@ -83,13 +84,20 @@ public class JSONReportTest { DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts", TestInputFileBuilder.nextBatchId()); DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java").build(); inputFile.setStatus(InputFile.Status.CHANGED); - InputComponentStore fileCache = mock(InputComponentStore.class); - when(fileCache.allFilesToPublish()).thenReturn(Collections.singleton(inputFile)); - when(fileCache.allDirs()).thenReturn(Collections.singleton(inputDir)); + inputFile.setPublish(true); + InputComponentStore fileCache = new InputComponentStore(); + fileCache.put(inputFile); + fileCache.put(inputDir); + DefaultComponentTree inputComponentTree = new DefaultComponentTree(); DefaultInputModule rootModule = new DefaultInputModule("struts"); DefaultInputModule moduleA = new DefaultInputModule("struts-core"); + inputComponentTree.index(moduleA, rootModule); DefaultInputModule moduleB = new DefaultInputModule("struts-ui"); + inputComponentTree.index(moduleB, rootModule); + + inputComponentTree.index(inputDir, rootModule); + inputComponentTree.index(inputFile, inputDir); when(moduleHierarchy.children(rootModule)).thenReturn(Arrays.asList(moduleA, moduleB)); when(moduleHierarchy.parent(moduleA)).thenReturn(rootModule); @@ -100,7 +108,7 @@ public class JSONReportTest { RulesBuilder builder = new RulesBuilder(); builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles"); rules = builder.build(); - jsonReport = new JSONReport(moduleHierarchy, settings, fs, server, rules, issueCache, rootModule, fileCache, userRepository); + jsonReport = new JSONReport(moduleHierarchy, settings, fs, server, rules, issueCache, rootModule, fileCache, userRepository, inputComponentTree); } @Test |