diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-08-06 11:28:40 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-08-06 12:52:33 +0200 |
commit | 8e6415a4c7444f7c6e03b10e8fa8eb535cc7e505 (patch) | |
tree | aacc9e22f21f755e98a9ac2ad945136ee36f0419 /sonar-scanner-engine/src/test | |
parent | e55369090f3be4f1e4baa791b05c3f5cf1cef6e1 (diff) | |
download | sonarqube-8e6415a4c7444f7c6e03b10e8fa8eb535cc7e505.tar.gz sonarqube-8e6415a4c7444f7c6e03b10e8fa8eb535cc7e505.zip |
SONAR-7654 API to propagate props from scanner to CE
Diffstat (limited to 'sonar-scanner-engine/src/test')
3 files changed, 173 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ContextPropertiesPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ContextPropertiesPublisherTest.java new file mode 100644 index 00000000000..337f1b7a924 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ContextPropertiesPublisherTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.scanner.report; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import java.util.Map; +import javax.annotation.Nonnull; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.scanner.repository.ContextPropertiesCache; +import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.scanner.protocol.output.ScannerReportWriter; + +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ContextPropertiesPublisherTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + ContextPropertiesCache cache = new ContextPropertiesCache(); + ContextPropertiesPublisher underTest = new ContextPropertiesPublisher(cache); + + @Test + public void publish_writes_properties_to_report() { + cache.put("foo1", "bar1"); + cache.put("foo2", "bar2"); + + ScannerReportWriter writer = mock(ScannerReportWriter.class); + underTest.publish(writer); + + verify(writer).writeContextProperties(argThat(new TypeSafeMatcher<Iterable<ScannerReport.ContextProperty>>() { + @Override + protected boolean matchesSafely(Iterable<ScannerReport.ContextProperty> props) { + Map<String, ScannerReport.ContextProperty> map = Maps.uniqueIndex(props, ContextPropertyToKey.INSTANCE); + return map.size() == 2 && + map.get("foo1").getValue().equals("bar1") && + map.get("foo2").getValue().equals("bar2"); + } + + @Override + public void describeTo(Description description) { + } + })); + } + + @Test + public void publish_writes_no_properties_to_report() { + ScannerReportWriter writer = mock(ScannerReportWriter.class); + underTest.publish(writer); + + verify(writer).writeContextProperties(argThat(new TypeSafeMatcher<Iterable<ScannerReport.ContextProperty>>() { + @Override + protected boolean matchesSafely(Iterable<ScannerReport.ContextProperty> props) { + return Iterables.isEmpty(props); + } + + @Override + public void describeTo(Description description) { + } + })); + } + + private enum ContextPropertyToKey implements Function<ScannerReport.ContextProperty, String> { + INSTANCE; + @Override + public String apply(@Nonnull ScannerReport.ContextProperty input) { + return input.getKey(); + } + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ContextPropertiesCacheTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ContextPropertiesCacheTest.java new file mode 100644 index 00000000000..94eb23d36d1 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ContextPropertiesCacheTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.scanner.repository; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; + +public class ContextPropertiesCacheTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + ContextPropertiesCache underTest = new ContextPropertiesCache(); + + @Test + public void put_property() { + assertThat(underTest.getAll()).isEmpty(); + + underTest.put("foo", "bar"); + assertThat(underTest.getAll()).containsOnly(entry("foo", "bar")); + } + + @Test + public void put_overrides_existing_value() { + underTest.put("foo", "bar"); + underTest.put("foo", "baz"); + assertThat(underTest.getAll()).containsOnly(entry("foo", "baz")); + } + + @Test + public void put_throws_IAE_if_key_is_null() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Key of context property must not be null"); + + underTest.put(null, "bar"); + } + + @Test + public void put_throws_IAE_if_value_is_null() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Value of context property must not be null"); + + underTest.put("foo", null); + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index 72bbfafac76..c0d8053ece5 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java @@ -43,10 +43,12 @@ import org.sonar.scanner.index.BatchComponentCache; import org.sonar.scanner.issue.ModuleIssues; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.ReportPublisher; +import org.sonar.scanner.repository.ContextPropertiesCache; import org.sonar.scanner.scan.measure.MeasureCache; import org.sonar.scanner.sensor.coverage.CoverageExclusions; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; @@ -65,7 +67,7 @@ public class DefaultSensorStorageTest { private ModuleIssues moduleIssues; private Project project; private MeasureCache measureCache; - + private ContextPropertiesCache contextPropertiesCache = new ContextPropertiesCache(); private BatchComponentCache resourceCache; @Before @@ -83,7 +85,8 @@ public class DefaultSensorStorageTest { ReportPublisher reportPublisher = mock(ReportPublisher.class); when(reportPublisher.getWriter()).thenReturn(new ScannerReportWriter(temp.newFolder())); underTest = new DefaultSensorStorage(metricFinder, - moduleIssues, settings, coverageExclusions, resourceCache, reportPublisher, measureCache, mock(SonarCpdBlockIndex.class)); + moduleIssues, settings, coverageExclusions, resourceCache, reportPublisher, measureCache, + mock(SonarCpdBlockIndex.class), contextPropertiesCache); } @Test @@ -159,4 +162,12 @@ public class DefaultSensorStorageTest { underTest.store(st); } + @Test + public void shouldStoreContextProperty() { + underTest.storeProperty("foo", "bar"); + + assertThat(contextPropertiesCache.getAll()).containsOnly(entry("foo", "bar")); + + } + } |