Browse Source

SONAR-7389 Improve testability

tags/5.5-M11
Julien HENRY 8 years ago
parent
commit
1447a88043

+ 87
- 0
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java View File

@@ -0,0 +1,87 @@
/*
* 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.xoo.lang;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

public class CpdTokenizerSensorTest {

private CpdTokenizerSensor sensor;
private SensorContextTester context;

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Rule
public ExpectedException thrown = ExpectedException.none();

private File baseDir;

@Before
public void prepare() throws IOException {
baseDir = temp.newFolder();
sensor = new CpdTokenizerSensor();
context = SensorContextTester.create(baseDir);
}

@Test
public void testDescriptor() {
sensor.describe(new DefaultSensorDescriptor());
}

@Test
public void testExecution() throws IOException {
String content = "public class Foo {\n\n}";

createSourceFile(content);

sensor.execute(context);

assertThat(context.cpdTokens("foo:src/foo.xoo")).extracting("value", "startLine", "startUnit", "endUnit")
.containsExactly(
tuple("publicclassFoo{", 1, 1, 4),
tuple("}", 3, 5, 5));
}

private void createSourceFile(String content) throws IOException {
File sourceFile = new File(baseDir, "src/foo.xoo");
FileUtils.write(sourceFile, content);
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo")
.setLanguage("xoo")
.initMetadata(new FileMetadata().readMetadata(new StringReader(content)));
context.fileSystem().add(inputFile);
}

}

+ 7
- 0
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java View File

@@ -58,6 +58,7 @@ import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.Metric;
import org.sonar.duplications.internal.pmd.TokensLine;

/**
* Utility class to help testing {@link Sensor}.
@@ -182,6 +183,12 @@ public class SensorContextTester implements SensorContext {
return null;
}

@CheckForNull
public List<TokensLine> cpdTokens(String componentKey) {
DefaultCpdTokens defaultCpdTokens = sensorStorage.cpdTokensByComponent.get(componentKey);
return defaultCpdTokens != null ? defaultCpdTokens.getTokenLines() : null;
}

@Override
public NewHighlighting newHighlighting() {
return new DefaultHighlighting(sensorStorage);

+ 20
- 0
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java View File

@@ -41,6 +41,7 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.RuleKey;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

public class SensorContextTesterTest {

@@ -192,4 +193,23 @@ public class SensorContextTesterTest {
assertThat(tester.conditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull();
assertThat(tester.coveredConditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull();
}

@Test
public void testCpdTokens() {
assertThat(tester.cpdTokens("foo:src/Foo.java")).isNull();
DefaultInputFile inputFile =
new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("public class Foo {\n\n}")));
tester.newCpdTokens()
.onFile(inputFile)
.addToken(inputFile.newRange(0, 6), "public")
.addToken(inputFile.newRange(7, 12), "class")
.addToken(inputFile.newRange(13, 16), "$IDENTIFIER")
.addToken(inputFile.newRange(17, 18), "{")
.addToken(inputFile.newRange(3, 0, 3, 1), "}")
.save();
assertThat(tester.cpdTokens("foo:src/Foo.java")).extracting("value", "startLine", "startUnit", "endUnit")
.containsExactly(
tuple("publicclass$IDENTIFIER{", 1, 1, 4),
tuple("}", 3, 5, 5));
}
}

Loading…
Cancel
Save