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.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;
FileUtils.write(sourceFile, content);
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo")
.setLanguage("xoo")
- .initMetadata(new FileMetadata().readMetadata(new StringReader(content)));
+ .initMetadata(content);
context.fileSystem().add(inputFile);
}
*/
package org.sonar.xoo.lang;
+import java.io.File;
+import java.io.IOException;
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.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
-import java.io.File;
-import java.io.IOException;
-import java.io.StringReader;
-
import static org.assertj.core.api.Assertions.assertThat;
public class SyntaxHighlightingSensorTest {
File symbol = new File(baseDir, "src/foo.xoo.highlighting");
FileUtils.write(symbol, "1:4:k\n12:15:cppd\n\n#comment");
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo")
- .initMetadata(new FileMetadata().readMetadata(new StringReader(" xoo\nazertyazer\nfoo")));
+ .initMetadata(" xoo\nazertyazer\nfoo");
context.fileSystem().add(inputFile);
sensor.execute(context);
package org.sonar.xoo.rule;
import java.io.IOException;
-import java.io.StringReader;
import org.junit.Rule;
import org.junit.Test;
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.rule.Severity;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testRule() throws IOException {
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testForceSeverity() throws IOException {
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testProvideGap() throws IOException {
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testProvideGap_before_5_5() throws IOException {
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n")));
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Arrays;
return this;
}
+ /**
+ * For testing purpose
+ */
+ public DefaultInputFile initMetadata(String content) {
+ return initMetadata(new FileMetadata().readMetadata(new StringReader(content)));
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.BatchSide;
+import org.sonar.api.batch.sensor.internal.SensorContextTester;
/**
* <p>
* used to add measure and issues at file level.
* <p>
* For example the Cobertura Sensor parses Cobertura report and saves the first-level of measures on files.
+ *
+ * For testing purpose you can use {@link SensorContextTester}
* @since 5.1
*/
@BatchSide
/**
* This builder is used to define code coverage by tests of a given type (UT/IT/Overall) on files.
+ *
+ * Example:
+ *
+ * <pre>
+ * sensorContext.newCoverage().onFile(file)
+ .ofType(UNIT)
+ .lineHits(1, 2)
+ .lineHits(2, 5)
+ .lineHits(3, 0)
+ . ...
+ .conditions(3, 4, 2)
+ .conditions(12, 2, 2)
+ . ...
+ .save();
+ *
+ * </pre>
* @since 5.2
*/
public interface NewCoverage {
/**
* This builder is used to define tokens used by CPD algorithm on files.
+ *
+ * Example:
+ *
+ * <pre>
+ * sensorContext.newCpdTokens().onFile(inputFile)
+ * .addToken(1, 10, 1, 15, "class")
+ * .addToken(1, 16, 1, 18, "IDENTIFIER")
+ * // Add more tokens
+ * .save;
+ *
+ * </pre>
* @since 5.5
*/
public interface NewCpdTokens {
*/
NewCpdTokens addToken(TextRange range, String image);
+ /**
+ * Shortcut to avoid calling {@link InputFile#newRange(int, int, int, int)}
+ * @since 5.6
+ */
+ NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image);
+
/**
* Call this method only once when your are done with defining tokens of the file.
*/
return inputFile;
}
+ @Override
+ public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) {
+ checkInputFileNotNull();
+ TextRange newRange;
+ try {
+ newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unable to register token in file " + inputFile, e);
+ }
+ return addToken(newRange, image);
+ }
+
@Override
public DefaultCpdTokens addToken(TextRange range, String image) {
Preconditions.checkNotNull(range, "Range should not be null");
Preconditions.checkNotNull(image, "Image should not be null");
- Preconditions.checkState(inputFile != null, "Call onFile() first");
+ checkInputFileNotNull();
if (excluded) {
return this;
}
addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
storage.store(this);
}
+
+ private void checkInputFileNotNull() {
+ Preconditions.checkState(inputFile != null, "Call onFile() first");
+ }
}
/**
* This builder is used to define syntax highlighting (aka code coloration) on files.
+ *
+ * Example:
+ *
+ * <pre>
+ * sensorContext.newHighlighting().onFile(inputFile)
+ * .highlight(1, 10, 1, 15, KEYWORD)
+ * .highlight(1, 16, 1, 18, STRING)
+ * // Add more highlight if needed
+ * .save;
+ *
+ * </pre>
+ *
* @since 5.1
*/
public interface NewHighlighting {
/**
* Utility class to help testing {@link Sensor}.
*
- * Usage: call {@link #create(File)} to create an "in memory" implementation of {@link SensorContext} then
- * pass it to your {@link Sensor}. You can then query elements provided by your sensor using methods {@link #allIssues()}, ...
+ * Usage: call {@link #create(File)} to create an "in memory" implementation of {@link SensorContext} with a filesystem initialized with provided baseDir.
+ * <p>
+ * You have to manually register inputFiles using:
+ * <pre>
+ * sensorContextTester.fileSystem().add(new DefaultInputFile("myProjectKey", "src/Foo.java")
+ .setLanguage("java")
+ .initMetadata("public class Foo {\n}"));
+ * </pre>
+ * <p>
+ * Then pass it to your {@link Sensor}. You can then query elements provided by your sensor using methods {@link #allIssues()}, ...
*
* @since 5.1
*/
/**
* This builder is used to define symbol references on files.
+ *
+ * Example:
+ *
+ * <pre>
+ * NewSymbolTable symbolTable = sensorContext.newSymbolTable().onFile(inputFile);
+ * symbolTable.newSymbol(1, 10, 1, 15)
+ * .newReference(10, 12, 10, 17)
+ * .newReference(11, 11, 11, 16);
+ *
+ * // Add more symbols if needed
+ *
+ * symbolTable.save();
+ *
+ * </pre>
+ *
* @since 5.6
*/
public interface NewSymbolTable {
// Do nothing
return this;
}
+
+ @Override
+ public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) {
+ // Do nothing
+ return this;
+ }
}
*/
package org.sonar.batch.issue;
-import java.io.StringReader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
ModuleIssues moduleIssues;
BatchComponentCache componentCache = new BatchComponentCache();
- InputFile file = new DefaultInputFile("foo", "src/Foo.php").initMetadata(new FileMetadata().readMetadata(new StringReader("Foo\nBar\nBiz\n")));
+ InputFile file = new DefaultInputFile("foo", "src/Foo.php").initMetadata("Foo\nBar\nBiz\n");
ReportPublisher reportPublisher = mock(ReportPublisher.class, RETURNS_DEEP_STUBS);
@Before
*/
package org.sonar.batch.source;
-import java.io.StringReader;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.internal.SensorStorage;
public void should_store_highlighting_rules() {
SensorStorage sensorStorage = mock(SensorStorage.class);
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata(new FileMetadata().readMetadata(new StringReader("azerty\nbla bla")));
+ .initMetadata("azerty\nbla bla");
DefaultHighlightable highlightablePerspective = new DefaultHighlightable(inputFile, sensorStorage, mock(AnalysisMode.class));
highlightablePerspective.newHighlighting().highlight(0, 6, "k").highlight(7, 10, "cppd").done();
package org.sonar.batch.source;
import com.google.common.base.Strings;
-import java.io.StringReader;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class);
DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata(new FileMetadata().readMetadata(new StringReader(Strings.repeat("azerty\n", 20))));
+ .initMetadata(Strings.repeat("azerty\n", 20));
DefaultSymbolizable symbolPerspective = new DefaultSymbolizable(inputFile, sensorStorage, mock(AnalysisMode.class));
Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolPerspective.newSymbolTableBuilder();
package org.sonar.batch.source;
import com.google.common.base.Strings;
-import java.io.StringReader;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
@Before
public void prepare() {
inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata(new FileMetadata().readMetadata(new StringReader(Strings.repeat("azerty\n", 20))));
+ .initMetadata(Strings.repeat("azerty\n", 20));
}
@Test