org.sonar.api.batch.fs.internal.TestInputFileBuilder Java Examples

The following examples show how to use org.sonar.api.batch.fs.internal.TestInputFileBuilder. 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: CloverSensorTest.java    From sonar-clover with Apache License 2.0 6 votes vote down vote up
@Test
public void should_process_file() throws Exception {
  final String cloverFilePath = "org/sonar/plugins/clover/CloverXmlReportParserTest/clover_2_6_0.xml";
  final File cloverFile = TestUtils.getResource(cloverFilePath);
  final MapSettings settings = new MapSettings();
  settings.setProperty(CloverSensor.REPORT_PATH_PROPERTY, cloverFile.getAbsolutePath());

  final DefaultFileSystem fs = context.fileSystem();
  fs.add(new TestInputFileBuilder("", cloverFile.getAbsolutePath()).build());

  final Configuration configuration = new ConfigurationBridge(settings);
  final CloverSensor sensor = new CloverSensor(configuration, fs, new PathResolver());

  sensor.execute(context);

  assertThat(logTester.logs(LoggerLevel.INFO)).contains("Parsing " + cloverFile.getCanonicalPath());
  assertThat(logTester.logs(LoggerLevel.WARN).stream().anyMatch(s -> s.contains("14 files in Clover report did not match any file in SonarQube Index"))).isEqualTo(true);
}
 
Example #2
Source File: LeinNvdSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
private SensorContextTester prepareContext() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("/"));

    context.settings().appendProperty(NvdProperties.REPORT_LOCATION_PROPERTY, "src/test/resources/nvd-report.json");
    File baseDir = new File("src/test/resources/");
    File project = new File(baseDir, "project.clj");

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

    return context;
}
 
Example #3
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 #4
Source File: CoverageSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void test() throws Throwable {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());
	String tempName = "GetStatusMessage.sql";
	String covReport = "test.xml";
	File f = folder.newFile(tempName);
	File coverage = folder.newFile(covReport);

	FileUtils.copyInputStreamToFile(this.getClass().getResourceAsStream("/coverage/Coverage.opencoverxml"),
			coverage);

	FileUtils.copyInputStreamToFile(this.getClass().getResourceAsStream("/coverage/TestCode.sql"), f);
	DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
			.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
	ctxTester.fileSystem().add(file1);
	ctxTester.settings().setProperty(Constants.COVERAGE_FILE, coverage.getAbsolutePath());
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_COVERAGE, false);
	CoverageSensor sut = new CoverageSensor(new SqlCoverCoverageProvider(ctxTester.settings(), ctxTester.fileSystem()));
	sut.execute(ctxTester);
	Assert.assertEquals((int) 2, (int) ctxTester.lineHits(file1.key(), 7));

}
 
Example #5
Source File: AntlrContextTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void compareWithSonarInputFile() throws IOException {
	String s = "select " + "*" + "from dbo.test\r\nselect 1";
	File ff = folder.newFile("test.sql");
	FileUtils.write(ff, s);
	DefaultInputFile f = new TestInputFileBuilder("", "test.sql").setLanguage(TSQLLanguage.KEY).initMetadata(s)
			.build();
	AntlrContext result = AntlrUtils.getRequest(s);

	for (Token t : result.getStream().getTokens()) {
		if (t.getType() == Token.EOF) {
			continue;
		}
		int[] start = result.getLineAndColumn(t.getStartIndex());
		int[] end = result.getLineAndColumn(t.getStopIndex());
		TextPointer p1 = f.newPointer(t.getStartIndex());
		TextPointer p2 = f.newPointer(t.getStopIndex());
		Assert.assertNotNull(start);
		Assert.assertNotNull(end);
		Assert.assertEquals(p1.line(), start[0]);
		Assert.assertEquals(p1.lineOffset(), start[1]);
		Assert.assertEquals(p2.line(), end[0]);
		Assert.assertEquals(p2.lineOffset(), end[1]);
	}
}
 
Example #6
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 #7
Source File: TestUtils.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
public static DefaultInputFile createTestInputFile(String baseDir, String relativePath) {
  final DefaultInputFile inputFile = new TestInputFileBuilder("module1", relativePath)
    .setModuleBaseDir(Paths.get(baseDir))
    .setLanguage("esql")
    .setCharset(StandardCharsets.UTF_8)
    .setType(InputFile.Type.MAIN)
    .build();
  return inputFile;
}
 
Example #8
Source File: TestUtils.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
public static DefaultInputFile createTestInputFile(File file, String contents, Charset encoding) {
  final DefaultInputFile inputFile = new TestInputFileBuilder("module1", file.getName()).setCharset(encoding).build();
  try {
    Files.write(file.toPath(), contents.getBytes(encoding));
    inputFile.setMetadata(new FileMetadata().readMetadata(new FileInputStream(file), encoding, file.getAbsolutePath()));
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
  return inputFile;
}
 
Example #9
Source File: HighlightSymbolTableBuilderTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private NewSymbolTable newSymbolTable(String filename) throws FileNotFoundException  {
  File moduleBaseDir = new File("src/test/resources/highlighter/");
  sensorContext = SensorContextTester.create(moduleBaseDir);
  inputFile = new TestInputFileBuilder("moduleKey", filename)
    .setModuleBaseDir(moduleBaseDir.toPath())
    .setCharset(StandardCharsets.UTF_8)
    .build();
  inputFile.setMetadata(new FileMetadata().readMetadata(new FileInputStream(inputFile.file()), inputFile.charset(), inputFile.absolutePath()));

  return sensorContext.newSymbolTable().onFile(inputFile);
}
 
Example #10
Source File: HighlighterVisitorTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private void initFile(String text) throws IOException {
	File file = tempFolder.newFile();
	inputFile = new TestInputFileBuilder("moduleKey", file.getName())
			.setLanguage("esql")
			.setType(Type.MAIN)
			.setCharset(CHARSET)
			.initMetadata(text)
			.build();

	when(visitorContext.getEsqlFile()).thenReturn(new EsqlFileImpl(inputFile));
}
 
Example #11
Source File: AbstractBaseTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private static HtmlSourceCode createSourceCode(File file) {
    return new HtmlSourceCode(
        new TestInputFileBuilder("key", file.getPath())
            .setLanguage(HtmlConstants.LANGUAGE_KEY)
            .setType(InputFile.Type.MAIN)
            .setModuleBaseDir(new File(".").toPath())
            .setCharset(StandardCharsets.UTF_8)
            .build()
    );
}
 
Example #12
Source File: HtlScannerTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private HtmlSourceCode createHtmlSourceCode(String relativePath) {
    return new HtmlSourceCode(new TestInputFileBuilder("key", relativePath)
        .setLanguage(HtmlConstants.LANGUAGE_KEY)
        .setModuleBaseDir(new File(".").toPath())
        .build()
    );
}
 
Example #13
Source File: HtlSensorTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private DefaultInputFile createInputFile(File dir, String fileName) throws IOException {
    return new TestInputFileBuilder("key", fileName)
        .setModuleBaseDir(dir.toPath())
        .setLanguage(HtmlConstants.LANGUAGE_KEY)
        .setType(InputFile.Type.MAIN)
        .initMetadata(Files.readString(new File(dir, fileName).toPath(), StandardCharsets.UTF_8))
        .setCharset(StandardCharsets.UTF_8)
        .build();
}
 
Example #14
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 #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 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 #16
Source File: BaseTsqlSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
public void testSkipPlugin() 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(Constants.PLUGIN_SKIP, 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: AnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintStdOutput() throws IOException {
    Path baseDir = context.fileSystem().baseDirPath();
    InputFile playbook = TestInputFileBuilder.create("moduleKey", baseDir.resolve("playbooks/playbook1.yml").toString())
            .setModuleBaseDir(Paths.get("."))
            .setContents(new String(Files.readAllBytes(baseDir.resolve("playbooks/playbook1.yml"))))
            .setLanguage(YamlLanguage.KEY)
            .setCharset(StandardCharsets.UTF_8)
            .build();
    context.fileSystem().add(playbook);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/ansible-lint.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/ansible-lint.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        Set<PosixFilePermission> perms = new HashSet<>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_EXECUTE);
        perms.add(PosixFilePermission.OTHERS_READ);
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.setPosixFilePermissions(Paths.get(path), perms);
    }

    sensor.execute(context);
    assertEquals(1, sensor.scannedFiles.size());
    assertTrue(sensor.scannedFiles.contains(playbook));

    Collection<Issue> issues = context.allIssues();
    assertEquals(3, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook, 1, "A first error"));
    assertTrue(issueExists(issues, ruleKey2, playbook, 3, "An error -p"));
    assertTrue(issueExists(issues, ruleKey3, playbook, 5, "Another error --nocolor"));
}
 
Example #18
Source File: CloverageMetricParserTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
@Test
public void testParse() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("/"));
    // Adding file to Sonar Context
    File baseDir = new File("src/test/resources/");

    File fooSource = new File(baseDir, "foo_in_src_clj.clj");
    DefaultInputFile fooFile = TestInputFileBuilder.create("", "src/clj/foo.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(fooSource.toPath()), StandardCharsets.UTF_8))
            .setContents(new String(Files.readAllBytes(fooSource.toPath()), StandardCharsets.UTF_8))
            .build();
    context.fileSystem().add(fooFile);

    File barSource = new File(baseDir, "bar_in_src_cljc.cljc");

    DefaultInputFile barFile = TestInputFileBuilder.create("", "src/cljc/bar.cljc")
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(barSource.toPath()), StandardCharsets.UTF_8))
            .setContents(new String(Files.readAllBytes(barSource.toPath()), StandardCharsets.UTF_8))
            .build();

    context.fileSystem().add(barFile);

    String json = new String(Files.readAllBytes(Paths.get("src/test/resources/cloverage-result.json")),Charset.forName("UTF-8"));

    CoverageReport c = CloverageMetricParser.parse(context, json);
    assertThat(c.filesCount(), is(2));
    FileAnalysis e = c.getFileEntries().get(0);

    List<LineAnalysis> entries = e.getEntries();
    List<LineAnalysis> expected = new ArrayList<>();
    expected.addAll(asList(new LineAnalysis().setLineNumber(1).setHits(1),

            new LineAnalysis().setLineNumber(3).setHits(1),
            new LineAnalysis().setLineNumber(5).setHits(0),
            new LineAnalysis().setLineNumber(6).setHits(1)));

    entries.stream().forEach(entry -> assertTrue(expected.contains(entry)));
}
 
Example #19
Source File: EastwoodSensorTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
private SensorContextTester prepareContext() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/resources/"));

    context.settings().appendProperty(EASTWOOD_OPTIONS, "eastwood-option");

    File baseDir = new File("src/test/resources/");
    File file = new File(baseDir, "file.clj");
    DefaultInputFile inputFile = TestInputFileBuilder.create("", "file.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8))
            .build();
    context.fileSystem().add(inputFile);

    return context;
}
 
Example #20
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 #21
Source File: KibitSensorTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
private SensorContextTester prepareContext() throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/resources/"));

    File baseDir = new File("src/test/resources/");
    File file = new File(baseDir, "kibit.clj");
    DefaultInputFile inputFile = TestInputFileBuilder.create("", "kibit.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8))
            .build();

    context.fileSystem().add(inputFile);

    return context;
}
 
Example #22
Source File: Utils.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
public static InputFile getInputFile(String relativePath) throws IOException {
    return TestInputFileBuilder.create(MODULE_KEY, BASE_DIR.resolve(relativePath).toString())
            .setModuleBaseDir(Paths.get("."))
            .setContents(new String(Files.readAllBytes(BASE_DIR.resolve(relativePath))))
            .setLanguage(YamlLanguage.KEY)
            .setCharset(StandardCharsets.UTF_8)
            .build();
}
 
Example #23
Source File: CloverageSensorTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
private void addFileToContext(SensorContextTester context, File baseDir, String fooPath, String s) throws IOException {
    File fooSource1 = new File(baseDir, s);
    DefaultInputFile fooFile1 = TestInputFileBuilder.create(MODULE_KEY, fooPath)
            .setLanguage(Clojure.KEY)
            .initMetadata(new String(Files.readAllBytes(fooSource1.toPath()), StandardCharsets.UTF_8))
            .setContents(new String(Files.readAllBytes(fooSource1.toPath()), StandardCharsets.UTF_8))
            .build();
    context.fileSystem().add(fooFile1);
}
 
Example #24
Source File: CheckstyleConfigurationTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void beforeClass() {
    fileSystem = new DefaultFileSystem(new File(""));
    fileSystem.setWorkDir(new File(".").toPath());

    final File file = new File("mainFile");
    final DefaultInputFile inputFile = new DefaultInputFile(
            new DefaultIndexedFile("",
                    new File("").toPath(),
                    file.getName(),
                    "java"),
            DefaultInputFile::checkMetadata);
    fileSystem.add(inputFile);

    final File testFile = new File("testFile");
    final DefaultInputFile testInputFile = new DefaultInputFile(
            new DefaultIndexedFile(testFile.toPath(),
                    "",
                    "testFile",
                    "testFile",
                    InputFile.Type.TEST,
                    "java",
                    TestInputFileBuilder.nextBatchId(),
                    new SensorStrategy()),
            DefaultInputFile::checkMetadata);
    fileSystem.add(testInputFile);
}
 
Example #25
Source File: ColdfusionSensorTest.java    From sonar-coldfusion with Apache License 2.0 5 votes vote down vote up
private void addFilesToFs() {
    DefaultInputFile inputFileMetrics1 = new TestInputFileBuilder(context.module().key(), baseDir.getAbsoluteFile(), new File("src/test/resources/testmetrics1.cfm").getAbsoluteFile()).setLanguage(ColdFusionPlugin.LANGUAGE_KEY).build();
    context.fileSystem().add(inputFileMetrics1);
    DefaultInputFile inputFileMetrics2 = new TestInputFileBuilder(context.module().key(), baseDir.getAbsoluteFile(), new File("src/test/resources/testmetrics2.cfm").getAbsoluteFile()).setLanguage(ColdFusionPlugin.LANGUAGE_KEY).build();
    context.fileSystem().add(inputFileMetrics2);
    DefaultInputFile inputFileMetrics3 = new TestInputFileBuilder(context.module().key(), baseDir.getAbsoluteFile(), new File("src/test/resources/EpisodeClaim.cfc").getAbsoluteFile()).setLanguage(ColdFusionPlugin.LANGUAGE_KEY).build();
    context.fileSystem().add(inputFileMetrics3);
}
 
Example #26
Source File: FlowIssueTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testIssueCreation() {
  TestInputFileBuilder tifb = new TestInputFileBuilder("module", "relPath");
  tifb.setLines(3);
  tifb.setOriginalLineOffsets(new int[] { 0, 10, 15 });
  tifb.setLastValidOffset(25);
  DefaultInputFile file = tifb.build();
  RuleKey ruleKey = RuleKey.of("squid", "ruleKey");
  SensorContext sensorContext = mock(SensorContext.class);
  SensorStorage storage = mock(SensorStorage.class);
  DefaultIssue newIssueOnFile = new DefaultIssue(storage);
  DefaultIssue newIssueOnLine = new DefaultIssue(storage);
  Mockito.when(sensorContext.newIssue()).thenReturn(newIssueOnFile, newIssueOnLine);

  // issue on file
  FlowIssue flowIssue = FlowIssue.create(sensorContext, ruleKey, null);
  flowIssue.setPrimaryLocationOnFile(file, "file message");
  flowIssue.save();

  Mockito.verify(storage, Mockito.times(1)).store(newIssueOnFile);
  assertThat(newIssueOnFile.ruleKey(),is(equalTo(ruleKey)));
  IssueLocation location = newIssueOnFile.primaryLocation();
  assertThat(location.inputComponent(),is(equalTo(file)));
  assertThat(location.textRange(),is(nullValue()));
  assertThat(location.message(),is(equalTo("file message")));

  // issue on entire line
  flowIssue = FlowIssue.create(sensorContext, ruleKey, null);
  flowIssue.setPrimaryLocation(file, "line message", 2, -1, 2, -1);
  flowIssue.save();

  Mockito.verify(storage, Mockito.times(1)).store(newIssueOnLine);
  assertLocation(newIssueOnLine.primaryLocation(), file, "line message", 2, 0, 2, 4);
}
 
Example #27
Source File: BaseTsqlSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAdd() 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(2);
	issue.setType("test");
	issue.setDescription("test123");
	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 #28
Source File: BaseTsqlSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAddLine0() 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(0);
	issue.setType("test");
	issue.setDescription("test123");
	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 #29
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 #30
Source File: AbstractHtlCheckTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private static HtmlSourceCode createHtmlSourceCode(String relativePath) {
    return new HtmlSourceCode(new TestInputFileBuilder("key", relativePath).setModuleBaseDir(new File(".").toPath()).build());
}