Java Code Examples for org.sonar.api.batch.fs.internal.DefaultInputFile

The following examples show how to use org.sonar.api.batch.fs.internal.DefaultInputFile. These examples are extracted from open source projects. 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 Project: sonar-p3c-pmd   Source File: PmdViolationRecorderTest.java    License: MIT License 6 votes vote down vote up
@Test
public void should_convert_pmd_violation_to_sonar_violation() {
  org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE");
  File file1 = new File("src/source.java");
  DefaultInputFile inputFile1 = addToFileSystem(file1);
  Issuable issuable = createIssuable(inputFile1);
  RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "RULE");

  Issue issue = mock(Issue.class);
  IssueBuilder issueBuilder = mock(IssueBuilder.class);
  when(issuable.newIssueBuilder()).thenReturn(issueBuilder);
  when(issueBuilder.ruleKey(sonarRule.ruleKey())).thenReturn(issueBuilder);
  when(issueBuilder.message("Description")).thenReturn(issueBuilder);
  when(issueBuilder.line(42)).thenReturn(issueBuilder);
  when(issueBuilder.build()).thenReturn(issue);

  pmdViolationRecorder.saveViolation(pmdViolation);
  verify(issuable).addIssue(issue);
  verify(issueBuilder).ruleKey(sonarRule.ruleKey());
  verify(issueBuilder).message("Description");
  verify(issueBuilder).line(42);
  verify(sonarRule, atLeastOnce()).ruleKey();
}
 
Example 2
Source Project: sonar-clojure   Source File: LeinNvdSensorTest.java    License: 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 Project: sonar-clojure   Source File: ClojureSensorTest.java    License: 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
@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 5
@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("scss")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

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

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new ScssSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example 6
@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 7
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("css")
    .setType(InputFile.Type.MAIN);

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

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

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

  metricsVisitor.scanTree(treeVisitorContext);
}
 
Example 8
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 9
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 10
@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);
  cpdVisitor = new CpdVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example 11
Source Project: sonar-ruby-plugin   Source File: SimpleCovSensorTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws URISyntaxException {
    URL filePath = SimpleCovSensorTest.class.getClassLoader().getResource("./test_controller.rb");
    rubyFile = new File(filePath.toURI());

    context = SensorContextTester.create(rubyFile.getParentFile());
    context.settings().setProperty(RubyPlugin.TEST_FRAMEWORK, "RSpec");
    context.fileSystem().add(new DefaultInputFile("myProjectKey", "test_controller.rb")
            .setLanguage("rb")
            .setLines(15));
    context.fileSystem().add(new DefaultInputFile("myProjectKey", "unknown_file.rb")
            .setLanguage("rb")
            .setLines(10));

    TestUtils.buildResultSetFile(rubyFile);
}
 
Example 12
Source Project: sonar-ruby-plugin   Source File: PathResolverTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    URL filePath = PathResolverTest.class.getClassLoader().getResource("./test_controller.rb");
    existingFile = new File(filePath.toURI());
    String parentPath = existingFile.getParent();

    this.sensorContext = SensorContextTester.create(new File(parentPath));
    this.sensorContext.settings().setProperty("path key", "test_controller.rb");

    DefaultInputFile file =
        new DefaultInputFile("", "test_controller.rb")
            .setLanguage(Ruby.LANGUAGE_KEY);

    this.sensorContext.fileSystem().add(file);

    this.resolver = new PathResolver();
}
 
Example 13
Source Project: sonar-ruby-plugin   Source File: RubocopSensorTest.java    License: 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 14
private static CheckstyleConfiguration mockConf() throws CheckstyleException {
    final CheckstyleConfiguration conf = mock(CheckstyleConfiguration.class);
    when(conf.getCharset()).thenReturn(Charset.defaultCharset());
    when(conf.getCheckstyleConfiguration()).thenReturn(
            CheckstyleConfiguration.toCheckstyleConfiguration(
                    new File("test-resources/checkstyle-conf.xml")));

    final File file = new File("test-resources/Hello.java");
    final File file2 = new File("test-resources/World.java");
    when(conf.getSourceFiles()).thenReturn(Arrays.asList(
            new DefaultInputFile(
                    new DefaultIndexedFile("",
                            file.getParentFile().toPath(),
                            file.getName(),
                            "java"),
                    DefaultInputFile::checkMetadata),
            new DefaultInputFile(
                    new DefaultIndexedFile("",
                            file2.getParentFile().toPath(),
                            file2.getName(),
                            "java"),
                    DefaultInputFile::checkMetadata)
    ));

    return conf;
}
 
Example 15
@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 16
@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 17
@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 18
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.file = new DefaultInputFile("", "src/test/existing.ts").setLanguage(TypeScriptLanguage.LANGUAGE_KEY);
    this.file.setLines(5);
    
    this.settings = mock(Settings.class);
    when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn("lcovpath");

    this.parser = mock(LCOVParser.class);

    this.sensor = spy(new TsCoverageSensorImpl());
    this.context = SensorContextTester.create(new File(""));

    this.context.fileSystem().add(this.file);
    this.context.setSettings(this.settings);
    
    this.lcovFile = mock(File.class);
    when(this.lcovFile.isFile()).thenReturn(true);
    doReturn(this.lcovFile).when(this.sensor).getIOFile(any(File.class), eq("lcovpath"));
    doReturn(this.parser).when(this.sensor).getParser(eq(this.context), any(File[].class));
}
 
Example 19
Source Project: SonarTsPlugin   Source File: PathResolverImplTest.java    License: 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 20
@Test
public void analyse2() throws FileNotFoundException {
  DefaultFileSystem fs = new DefaultFileSystem(TEST_DIR);
  tester.setFileSystem(fs);
  DefaultInputFile inputFile = new DefaultInputFile("key", "timeFormatter.lua")
    .setType(InputFile.Type.MAIN)
    .setLanguage(Lua.KEY)
    .initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "timeFormatter.lua"))));
  fs.add(inputFile);

  sensor.execute(tester);

  String componentKey = inputFile.key();
  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY_IN_CLASSES).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(59);
  assertThat(tester.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTIONS).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0");



  assertThat(tester.allIssues()).hasSize(0);
}
 
Example 21
Source Project: sonar-p3c-pmd   Source File: PmdViolationRecorderTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_ignore_violation_on_unknown_rule() {
  File file1 = new File("test/source.java");
  DefaultInputFile inputFile1 = addToFileSystem(file1);
  Issuable issuable = createIssuable(inputFile1);
  RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "UNKNOWN");

  pmdViolationRecorder.saveViolation(pmdViolation);
  verifyZeroInteractions(issuable);
}
 
Example 22
Source Project: sonar-clojure   Source File: CloverageSensorTest.java    License: 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 23
Source Project: sonar-clojure   Source File: CloverageMetricParserTest.java    License: 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 24
Source Project: sonar-clojure   Source File: EastwoodSensorTest.java    License: 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 25
Source Project: sonar-clojure   Source File: AncientSensorTest.java    License: 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 26
Source Project: sonar-clojure   Source File: KibitSensorTest.java    License: 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 27
Source Project: sonar-ruby-plugin   Source File: RubyMetricsSensorTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws URISyntaxException {
    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));
}
 
Example 28
Source Project: sonar-ruby-plugin   Source File: SimpleCovParserTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws URISyntaxException {
    URL filePath = SimpleCovParserTest.class.getClassLoader().getResource("./test_controller.rb");
    rubyFile = new File(filePath.toURI());

    context = SensorContextTester.create(rubyFile.getParentFile());
    context.settings().setProperty(RubyPlugin.TEST_FRAMEWORK, "RSpec");
    context.fileSystem().add(new DefaultInputFile("myProjectKey", "test_controller.rb")
        .setLanguage("rb")
        .setLines(15));

    TestUtils.buildResultSetFile(rubyFile);
}
 
Example 29
@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 30
Source Project: enforce-sonarqube-plugin   Source File: ApexSquidSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testExecuteOnApexProject() {
    Project project = new Project("cls");
    assertThat(squidSensor.toString(), equalTo("ApexSquidSensor"));
    assertThat(squidSensor.shouldExecuteOnProject(project), is(Boolean.FALSE));
    fileSystem.add(new DefaultInputFile("Book.cls").setLanguage(Apex.KEY));
    assertThat(squidSensor.shouldExecuteOnProject(project), is(Boolean.TRUE));
}