diff --git a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/tests/xunit/CxxXunitSensorTest.java b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/tests/xunit/CxxXunitSensorTest.java index 384c5289bc..6424510874 100644 --- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/tests/xunit/CxxXunitSensorTest.java +++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/tests/xunit/CxxXunitSensorTest.java @@ -24,6 +24,7 @@ import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; import org.junit.Before; import org.junit.Test; @@ -31,6 +32,7 @@ import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.measures.CoreMetrics; import org.sonar.cxx.CxxLanguage; import org.sonar.cxx.sensors.utils.TestUtils; @@ -63,6 +65,29 @@ public void shouldReportNothingWhenNoReportFound() { assertThat(context.measures(context.module().key())).hasSize(0); } + @Test + public void shouldReadXunitReport() { + SensorContextTester context = SensorContextTester.create(fs.baseDir()); + + settings.setProperty(language.getPluginProperty(CxxXunitSensor.REPORT_PATH_KEY), "xunit-reports/xunit-result-SAMPLE_with_fileName.xml"); + context.setSettings(settings); + + CxxXunitSensor sensor = new CxxXunitSensor(language); + + sensor.execute(context); + + assertThat(context.measures(context.module().key())).hasSize(6); + assertThat(context.measures(context.module().key())) + .extracting("metric.key", "value") + .containsOnly( + tuple(CoreMetrics.TESTS_KEY, 3), + tuple(CoreMetrics.SKIPPED_TESTS_KEY, 0), + tuple(CoreMetrics.TEST_FAILURES_KEY, 0), + tuple(CoreMetrics.TEST_ERRORS_KEY, 0), + tuple(CoreMetrics.TEST_SUCCESS_DENSITY_KEY, 100.0), + tuple(CoreMetrics.TEST_EXECUTION_TIME_KEY, 0L)); + } + @Test(expected = IllegalStateException.class) public void shouldThrowWhenGivenInvalidTime() { SensorContextTester context = SensorContextTester.create(fs.baseDir()); @@ -95,8 +120,7 @@ public void transformReport_shouldTransformCppunitReport() File reportBefore = cppunitReport(); File reportAfter = sensor.transformReport(reportBefore); - - assert (reportAfter != reportBefore); + assertThat(reportAfter).isNotSameAs(reportBefore); } File cppunitReport() { diff --git a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxMetricsTest.java b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxMetricsTest.java index c30410954c..23325c1b70 100644 --- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxMetricsTest.java +++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxMetricsTest.java @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.List; import org.junit.Before; +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.measures.Metric; @@ -29,13 +31,14 @@ public class CxxMetricsTest { + private MapSettings settings; private CxxLanguage language; private CxxMetrics metrics; public class CxxLanguageImpl extends CxxLanguage { - public CxxLanguageImpl(MapSettings settings) { - super("c++", "C++", (Configuration) settings); + public CxxLanguageImpl(Configuration settings) { + super("c++", "C++", settings); } @Override @@ -72,25 +75,26 @@ public String getRepositoryKey() { @Before public void setUp() { - language = TestUtils.mockCxxLanguage(); + settings = new MapSettings(); + language = new CxxLanguageImpl(settings.asConfig()); metrics = new CxxMetrics(language); } -// @Test + @Test public void getMetricsTest() { List list = metrics.getMetrics(); - assert (list.size() == 14); + assertThat(list.size()).isEqualTo(14); } -// @Test + @Test public void getMetricTest() { Metric metric = language.getMetric(CxxMetrics.PUBLIC_API_KEY); - assert (metric != null); + assertThat(metric).isNotNull(); metric = language.getMetric(CxxMetrics.PUBLIC_UNDOCUMENTED_API_KEY); - assert (metric != null); + assertThat(metric).isNotNull(); metric = language.getMetric(CxxMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY); - assert (metric != null); + assertThat(metric).isNotNull(); } } diff --git a/cxx-squid/src/test/java/org/sonar/cxx/CxxLanguageTest.java b/cxx-squid/src/test/java/org/sonar/cxx/CxxLanguageTest.java new file mode 100644 index 0000000000..0a465c05af --- /dev/null +++ b/cxx-squid/src/test/java/org/sonar/cxx/CxxLanguageTest.java @@ -0,0 +1,172 @@ +/* + * Sonar C++ Plugin (Community) + * Copyright (C) 2010-2017 SonarOpenCommunity + * http://github.com/SonarOpenCommunity/sonar-cxx + * + * 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.cxx; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import org.sonar.api.config.Configuration; +import org.sonar.api.config.internal.MapSettings; + +public class CxxLanguageTest { + + private MapSettings settings; + + private static final String KEY = "c++"; + private static final String NAME = "C++"; + private static final String PLUGIN_ID = "cxx"; + private static final String SOURCE_SUFFIXES = ".cxx,.cpp,.cc,.c"; + private static final String HEADER_SUFFIXES = ".hxx,.hpp,.hh,.h"; + + /** + * Default cxx header files suffixes + */ + public static final String DEFAULT_HEADER_SUFFIXES = ".hxx,.hpp,.hh,.h"; + + @Before + public void setUp() { + settings = new MapSettings(); + } + + private class CppLanguage extends CxxLanguage { + private final String[] sourceSuffixes; + private final String[] headerSuffixes; + private final String[] fileSuffixes; + + public CppLanguage(Configuration settings) { + super(KEY, NAME, settings); + + sourceSuffixes = createStringArray(settings.getStringArray("sonar.cxx.suffixes.sources"), SOURCE_SUFFIXES); + headerSuffixes = createStringArray(settings.getStringArray("sonar.cxx.suffixes.headers"), HEADER_SUFFIXES); + fileSuffixes = mergeArrays(sourceSuffixes, headerSuffixes); + } + + @Override + public String[] getFileSuffixes() { + return fileSuffixes.clone(); + } + + @Override + public String[] getSourceFileSuffixes() { + return sourceSuffixes.clone(); + } + + @Override + public String[] getHeaderFileSuffixes() { + return headerSuffixes.clone(); + } + + @Override + public List getChecks() { + return new ArrayList<>(); + } + + private String[] createStringArray(String[] values, String defaultValues) { + if (values.length == 0) { + return defaultValues.split(","); + } + return values; + } + + private String[] mergeArrays(String[] array1, String[] array2) { + String[] result = new String[array1.length + array2.length]; + System.arraycopy(sourceSuffixes, 0, result, 0, array1.length); + System.arraycopy(headerSuffixes, 0, result, array1.length, array2.length); + return result; + } + + @Override + public String getPropertiesKey() { + return PLUGIN_ID; + } + + @Override + public String getRepositoryKey() { + return PLUGIN_ID; + + } + } + + @Test + public void testCxxLanguageStringConfiguration() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getKey()).isEqualTo(KEY); + } + + @Test + public void testGetSourceFileSuffixes() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getSourceFileSuffixes()).isEqualTo(SOURCE_SUFFIXES.split(",")); + } + + @Test + public void testGetHeaderFileSuffixes() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getHeaderFileSuffixes()).isEqualTo(HEADER_SUFFIXES.split(",")); + } + + @Test + public void testGetPropertiesKey() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getPropertiesKey()).isEqualTo(PLUGIN_ID); + } + + @Test + public void testGetChecks() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getChecks()).isEmpty(); + } + + @Test + public void testGetRepositoryKey() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getPropertiesKey()).isEqualTo(PLUGIN_ID); + } + + @Test + public void testGetRepositorySuffix() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getRepositorySuffix()).isEqualTo(""); + } + + @Test + public void testGetPluginProperty() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getPropertiesKey()).isEqualTo(PLUGIN_ID); + } + + @Test + public void testIsRecoveryEnabled() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.getBooleanOption("sonar.cxx.errorRecoveryEnabled")).isEqualTo(Optional.empty()); + } + + @Test + public void testHasKey() throws Exception { + CppLanguage language = new CppLanguage(settings.asConfig()); + assertThat(language.hasKey("sonar.cxx.errorRecoveryEnabled")).isEqualTo(Boolean.FALSE); + } + +}