Search in sources :

Example 21 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class XmlFileSensorTest method test_issues_creation.

@Test
public void test_issues_creation() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    final File file = new File("src/test/files/maven/pom.xml");
    DefaultInputFile inputFile = new TestInputFileBuilder("", "pom.xml").setModuleBaseDir(fs.baseDirPath()).setPublish(false).build();
    fs.add(inputFile);
    SonarComponents sonarComponents = createSonarComponentsMock(fs, file);
    XmlFileSensor sensor = new XmlFileSensor(sonarComponents, fs);
    assertThat(inputFile.isPublished()).isFalse();
    sensor.execute(context);
    assertThat(inputFile.isPublished()).isTrue();
    verify(sonarComponents, times(1)).reportIssue(Mockito.argThat(argument -> file.getAbsolutePath().equals(argument.getFile().getAbsolutePath())));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) Files(com.google.common.io.Files) JavaCheck(org.sonar.plugins.java.api.JavaCheck) SonarComponents(org.sonar.java.SonarComponents) Checks(org.sonar.api.batch.rule.Checks) Nullable(javax.annotation.Nullable) Before(org.junit.Before) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) RuleAnnotationUtils(org.sonar.api.rules.RuleAnnotationUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PomElementOrderCheck(org.sonar.java.checks.xml.maven.PomElementOrderCheck) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 22 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class XmlFileSensorTest method not_executed_without_xml_files_in_file_system.

@Test
public void not_executed_without_xml_files_in_file_system() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven/"));
    DefaultFileSystem fs = context.fileSystem();
    SonarComponents sonarComponents = createSonarComponentsMock(fs);
    XmlFileSensor sensor = new XmlFileSensor(sonarComponents, fs);
    sensor.execute(context);
    verify(sonarComponents, Mockito.never()).reportIssue(any());
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 23 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class XmlFileSensorTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock(DefaultFileSystem fs, @Nullable File file) throws IOException {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.checkClasses()).thenReturn(new JavaCheck[] { new PomElementOrderCheck() });
    when(sonarComponents.getFileSystem()).thenReturn(fs);
    if (file != null) {
        when(sonarComponents.fileLines(any(File.class))).thenReturn(Files.readLines(file, StandardCharsets.UTF_8));
    }
    Checks<JavaCheck> checks = mock(Checks.class);
    when(checks.ruleKey(any(JavaCheck.class))).thenReturn(RuleKey.of("squid", RuleAnnotationUtils.getRuleKey(PomElementOrderCheck.class)));
    when(sonarComponents.checks()).thenReturn(Lists.<Checks<JavaCheck>>newArrayList(checks));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaCheck(org.sonar.plugins.java.api.JavaCheck) PomElementOrderCheck(org.sonar.java.checks.xml.maven.PomElementOrderCheck) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File)

Example 24 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class SonarSymbolTableVisitorTest method setUp.

@Before
public void setUp() {
    context = SensorContextTester.create(temp.getRoot());
    fs = context.fileSystem();
    sonarComponents = new SonarComponents(mock(FileLinesContextFactory.class), fs, mock(JavaClasspath.class), mock(JavaTestClasspath.class), mock(CheckFactory.class));
    sonarComponents.setSensorContext(context);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) Before(org.junit.Before)

Example 25 with SonarComponents

use of org.sonar.java.SonarComponents in project sonar-java by SonarSource.

the class VisitorsBridgeForTestsTest method test_semantic_disabled.

@Test
public void test_semantic_disabled() {
    SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null, null);
    sonarComponents.setSensorContext(context);
    Tree parse = JavaParser.createParser().parse("class A{}");
    VisitorsBridgeForTests visitorsBridgeForTests = new VisitorsBridgeForTests(Collections.singletonList(new DummyVisitor()), sonarComponents);
    visitorsBridgeForTests.setCurrentFile(new File("dummy.java"));
    visitorsBridgeForTests.visitFile(parse);
    assertThat(visitorsBridgeForTests.lastCreatedTestContext().getSemanticModel()).isNull();
    parse = JavaParser.createParser().parse("class A{}");
    visitorsBridgeForTests = new VisitorsBridgeForTests(new DummyVisitor(), sonarComponents);
    visitorsBridgeForTests.setCurrentFile(new File("dummy.java"));
    visitorsBridgeForTests.visitFile(parse);
    assertThat(visitorsBridgeForTests.lastCreatedTestContext().getSemanticModel()).isNotNull();
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Tree(org.sonar.plugins.java.api.tree.Tree) File(java.io.File) Test(org.junit.Test)

Aggregations

SonarComponents (org.sonar.java.SonarComponents)36 File (java.io.File)27 Test (org.junit.Test)21 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)17 AnalyzerMessage (org.sonar.java.AnalyzerMessage)14 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)13 JavaCheck (org.sonar.plugins.java.api.JavaCheck)11 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)10 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)7 Before (org.junit.Before)6 Lists (com.google.common.collect.Lists)3 RecognitionException (com.sonar.sslr.api.RecognitionException)3 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 VisitorsBridge (org.sonar.java.model.VisitorsBridge)3 Files (com.google.common.io.Files)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2