Skip to content

Commit

Permalink
Merge pull request #1357 from Bertk/testing
Browse files Browse the repository at this point in the history
more unit tests
  • Loading branch information
guwirth authored Dec 20, 2017
2 parents d397e9d + f9a5236 commit 4aa2f02
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
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;
import static org.mockito.Mockito.when;
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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
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;
import org.sonar.cxx.CxxLanguage;

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
Expand Down Expand Up @@ -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();
}
}
172 changes: 172 additions & 0 deletions cxx-squid/src/test/java/org/sonar/cxx/CxxLanguageTest.java
Original file line number Diff line number Diff line change
@@ -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<Class> 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);
}

}

0 comments on commit 4aa2f02

Please sign in to comment.