Java Code Examples for org.sonar.api.batch.sensor.internal.SensorContextTester#create()

The following examples show how to use org.sonar.api.batch.sensor.internal.SensorContextTester#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyntaxHighlighterVisitorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("gherkin")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);
  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new SyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example 2
Source File: RubocopSensorTest.java    From sonar-ruby-plugin with MIT License 6 votes vote down vote up
@Before
public void setUp() throws URISyntaxException {
    MockitoAnnotations.initMocks(this);

    URL filePath = RubocopSensorTest.class.getClassLoader().getResource("./test_controller.rb");
    rubyFile = new File(filePath.toURI());

    context = SensorContextTester.create(rubyFile.getParentFile());
    context.fileSystem().add(new DefaultInputFile("myProjectKey", "test_controller.rb")
        .setLanguage("rb")
        .setOriginalLineOffsets(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
        .setLines(15));
    context.fileSystem().add(new DefaultInputFile("myProjectKey", "unknown_file.rb")
        .setLanguage("rb")
        .setOriginalLineOffsets(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
        .setLines(10));
}
 
Example 3
Source File: PathResolverImplTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    URL filePath = PathResolverImplTest.class.getClassLoader().getResource("./existing.ts");
    existingFile = new File(filePath.toURI());
    String parentPath = existingFile.getParent();

    this.sensorContext = SensorContextTester.create(new File(parentPath));
    this.sensorContext.settings().setProperty("path key", "existing.ts");
    
    DefaultInputFile file = 
            new DefaultInputFile("", "existing.ts")
                .setLanguage(TypeScriptLanguage.LANGUAGE_KEY);
    
    this.sensorContext.fileSystem().add(file);
    
    this.resolver = new PathResolverImpl();
}
 
Example 4
Source File: ScssMetricsVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setUp(String fileName) {
  File moduleBaseDir = new File("src/test/resources/metrics/");
  context = SensorContextTester.create(moduleBaseDir);

  DefaultInputFile inputFile = new DefaultInputFile("moduleKey", fileName)
    .setModuleBaseDir(moduleBaseDir.toPath())
    .setLanguage("scss")
    .setType(InputFile.Type.MAIN);

  context.fileSystem().add(inputFile);
  context.fileSystem().setEncoding(Charsets.UTF_8);

  ScssMetricsVisitor metricsVisitor = new ScssMetricsVisitor(context, mock(NoSonarFilter.class));

  TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
  when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
  when(treeVisitorContext.getTopTree()).thenReturn(ScssParser.createParser(Charsets.UTF_8).parse(inputFile.file()));

  metricsVisitor.scanTree(treeVisitorContext);
}
 
Example 5
Source File: LessMetricsVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setUp(String fileName) {
  File moduleBaseDir = new File("src/test/resources/metrics/");
  context = SensorContextTester.create(moduleBaseDir);

  DefaultInputFile inputFile = new DefaultInputFile("moduleKey", fileName)
    .setModuleBaseDir(moduleBaseDir.toPath())
    .setLanguage("less")
    .setType(InputFile.Type.MAIN);

  context.fileSystem().add(inputFile);
  context.fileSystem().setEncoding(Charsets.UTF_8);

  LessMetricsVisitor metricsVisitor = new LessMetricsVisitor(context, mock(NoSonarFilter.class));

  TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
  when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
  when(treeVisitorContext.getTopTree()).thenReturn(LessParser.createParser(Charsets.UTF_8).parse(inputFile.file()));

  metricsVisitor.scanTree(treeVisitorContext);
}
 
Example 6
Source File: ClojureSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
private SensorContextTester prepareContext() {
    DefaultFileSystem fileSystem = new DefaultFileSystem(temporaryFolder.getRoot());
    SensorContextTester context = SensorContextTester.create(temporaryFolder.getRoot());
    context.setFileSystem(fileSystem);

    DefaultInputFile inputFile1 = TestInputFileBuilder.create("module", "foo.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata("firstLine\nsecondLine")
            .build();
    DefaultInputFile inputFile2 = TestInputFileBuilder.create("module", "bar.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata("firstLine\nsecondLine\nthirdLine")
            .build();
    context.fileSystem().add(inputFile1).add(inputFile2);

    return context;
}
 
Example 7
Source File: CssSyntaxHighlighterVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("css")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new CssSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example 8
Source File: LessSyntaxHighlighterVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("less")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new LessSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example 9
Source File: CloverageSensorTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
private SensorContextTester prepareContext() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("/"));
    File baseDir = new File("src/test/resources/");
    context.settings().appendProperty(REPORT_LOCATION_PROPERTY, "src/test/resources/cloverage-result.json");

    addFileToContext(context, baseDir, FOO_PATH, "foo_in_src_clj.clj");
    addFileToContext(context, baseDir, BAR_PATH, "bar_in_src_cljc.cljc");

    CommandStreamConsumer stdOut = new CommandStreamConsumer();
    stdOut.consumeLine("Cloverage is running just fine - please relax");
    when(commandRunner.run(300L, "lein", "cloverage", "--codecov")).thenReturn(stdOut);
    return context;
}
 
Example 10
Source File: CustomChecksSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSingleFile() throws IOException {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM_RULES, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_MAX_FILE_SIZE, 100);
	String tempName = "test.sql";

	File f = folder.newFile(tempName);

	FileUtils.write(f, "select * from dbo.test;\r\n--test", Charset.defaultCharset());
	DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
			.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
	ctxTester.fileSystem().add(file1);

	CustomChecksSensor sensor = new CustomChecksSensor();
	sensor.execute(ctxTester);
	Collection<Issue> issues = ctxTester.allIssues();
	for (Issue i : issues) {
		System.out.println(i.ruleKey() + " " + i.primaryLocation());
	}

	Assert.assertEquals(1, issues.size());
	Assert.assertEquals(2, ctxTester.cpdTokens(file1.key()).size());
	Assert.assertEquals(1, ctxTester.highlightingTypeAt(file1.key(), 2, 1).size());
	Assert.assertEquals(4, ctxTester.measures(file1.key()).size());
}
 
Example 11
Source File: LuaSquidSensorTest.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  NewActiveRule ar = new ActiveRulesBuilder().create(RuleKey.of("lua", "S1125")).setSeverity("BLOCKER");
  ActiveRules activeRules = new DefaultActiveRules(Collections.singletonList(ar));
  CheckFactory checkFactory = new CheckFactory(activeRules);
  FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
  when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
  sensor = new LuaSquidSensor(checkFactory, fileLinesContextFactory);
  tester = SensorContextTester.create(TEST_DIR);
}
 
Example 12
Source File: CpdVisitorTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private void scan(String source) throws IOException {
  inputFile = TestUtils.createTestInputFile(tempFolder.newFile(), source, CHARSET);

  sensorContext = SensorContextTester.create(tempFolder.getRoot().toPath());
  CpdVisitor cpdVisitor = new CpdVisitor(sensorContext);
  ProgramTree tree = (ProgramTree) p.parse(source);
  TreeVisitorContext visitorContext = new EsqlVisitorContext(tree, inputFile, null);
  cpdVisitor.scanTree(visitorContext);
}
 
Example 13
Source File: BaseTsqlSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAddDescNull() throws Throwable {

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	File baseFile = folder.newFile("test.sql");

	FileUtils.copyURLToFile(getClass().getResource("/testFiles/TestTable.sql"), baseFile);

	InputFile ti = new TestInputFileBuilder("test", "test.sql")
			.initMetadata(new String(Files.readAllBytes(baseFile.toPath()))).build();

	ctxTester.fileSystem().add(ti);

	final TsqlIssue issue = new TsqlIssue();
	issue.setFilePath(ti.absolutePath());
	issue.setLine(1);
	issue.setType("test");
	IIssuesProvider provider = new IIssuesProvider() {

		@Override
		public TsqlIssue[] getIssues(String baseDir) {
			return new TsqlIssue[] {

					issue };
		}

	};
	BaseTsqlExternalSensor sensor = new BaseTsqlExternalSensor(provider, "test", "test");
	sensor.execute(ctxTester);
	Assert.assertEquals(1, ctxTester.allIssues().size());
}
 
Example 14
Source File: AncientSensorTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
private SensorContextTester prepareContext() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("/"));

    File baseDir = new File("src/test/resources/");
    File file = new File(baseDir, "project.clj");

    DefaultInputFile inputFile = TestInputFileBuilder.create("moduleKey", "project.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8))
            .setContents(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8))
            .build();
    context.fileSystem().add(inputFile);

    return context;
}
 
Example 15
Source File: CustomChecksSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testTSQLGrammarFiles() throws IOException {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();
	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM_RULES, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_MAX_FILE_SIZE, 100);
	String dirPath = "..\\grammars\\tsql";
	File dir = new File(dirPath);
	Collection<File> files = FileUtils.listFiles(dir, new String[] { "sql" }, true);
	for (File f : files) {
		String tempName = f.getName() + System.nanoTime();
		File dest = folder.newFile(tempName);
		FileUtils.copyFile(f, dest);

		DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
				.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
		ctxTester.fileSystem().add(file1);

	}
	CustomChecksSensor sensor = new CustomChecksSensor();
	sensor.execute(ctxTester);
	Collection<Issue> issues = ctxTester.allIssues();
	Assert.assertEquals(183, issues.size());

}
 
Example 16
Source File: BaseTsqlSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSkipSensor() throws Throwable {

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	File baseFile = folder.newFile("test.sql");

	FileUtils.copyURLToFile(getClass().getResource("/testFiles/TestTable.sql"), baseFile);

	InputFile ti = new TestInputFileBuilder("test", "test.sql")
			.initMetadata(new String(Files.readAllBytes(baseFile.toPath()))).build();

	ctxTester.fileSystem().add(ti);
	ctxTester.settings().setProperty("test", true);
	final TsqlIssue issue = new TsqlIssue();
	issue.setFilePath(ti.absolutePath());
	issue.setLine(1);
	issue.setType("test");
	IIssuesProvider provider = new IIssuesProvider() {

		@Override
		public TsqlIssue[] getIssues(String baseDir) {
			return new TsqlIssue[] {

					issue };
		}

	};
	BaseTsqlExternalSensor sensor = new BaseTsqlExternalSensor(provider, "test", "test");
	sensor.execute(ctxTester);
	Assert.assertEquals(0, ctxTester.allIssues().size());
}
 
Example 17
Source File: LOCSensorImplTest.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.sensorContext = SensorContextTester.create(new File(""));
    this.sensor = new LOCSensorImpl();
}
 
Example 18
Source File: Utils.java    From sonar-ansible with Apache License 2.0 4 votes vote down vote up
public static SensorContextTester getSensorContext() {
    return SensorContextTester.create(BASE_DIR);
}
 
Example 19
Source File: CoberturaSensorTest.java    From sonar-lua with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  sensor = new CoberturaSensor();
  tester = SensorContextTester.create(new File(TEST_DIR));
}
 
Example 20
Source File: RubocopParserTest.java    From sonar-ruby-plugin with MIT License 4 votes vote down vote up
@Before
public void setUp() throws URISyntaxException {
    context = SensorContextTester.create(Paths.get("."));
}