Search in sources :

Example 1 with SonarComponents

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

the class VisitorsBridgeTest method rethrow_exception_when_hidden_property_set_to_true.

@Test
public void rethrow_exception_when_hidden_property_set_to_true() {
    NullPointerException npe = new NullPointerException("BimBadaboum");
    JavaFileScanner visitor = c -> {
        throw npe;
    };
    File currentFile = new File("");
    SensorContextTester sensorContextTester = SensorContextTester.create(currentFile);
    SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null);
    sonarComponents.setSensorContext(sensorContextTester);
    VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singleton(visitor), new ArrayList<>(), sonarComponents);
    visitorsBridge.setCurrentFile(currentFile);
    try {
        visitorsBridge.visitFile(null);
        assertThat(sonarComponents.analysisErrors).hasSize(1);
    } catch (Exception e) {
        e.printStackTrace();
        Fail.fail("Exception should be swallowed when property is not set");
    }
    sensorContextTester.settings().appendProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, "true");
    try {
        visitorsBridge.visitFile(null);
        Fail.fail("scanning of file should have raise an exception");
    } catch (Exception e) {
        assertThat(e).isSameAs(npe);
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) RecognitionException(com.sonar.sslr.api.RecognitionException) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SonarComponents(org.sonar.java.SonarComponents) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) JavaParser(org.sonar.java.ast.parser.JavaParser) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) DecimalFormat(java.text.DecimalFormat) Test(org.junit.Test) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) File(java.io.File) Fail(org.assertj.core.api.Fail) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) Collections(java.util.Collections) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 2 with SonarComponents

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

the class XmlAnalyzerTest method should_interrupt_analysis_when_InterruptedException_is_thrown.

@Test
public void should_interrupt_analysis_when_InterruptedException_is_thrown() {
    DefaultFileSystem fs = new DefaultFileSystem(new File(""));
    File pomFile = new File(VALID_POM);
    fs.add(new TestInputFileBuilder("", pomFile.getAbsolutePath()).build());
    XmlCheckThrowingException check = new XmlCheckThrowingException(new RuntimeException("Analysis cancelled"));
    SonarComponents sonarComponents = createSonarComponentsMock(fs, check);
    thrown.expectMessage("Analysis cancelled");
    thrown.expect(RuntimeException.class);
    XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, check);
    analyzer.scan(Lists.newArrayList(pomFile));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 3 with SonarComponents

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

the class XmlAnalyzerTest method should_not_run_xml_check_when_no_xml_file_provided.

@Test
public void should_not_run_xml_check_when_no_xml_file_provided() {
    DefaultFileSystem fs = new DefaultFileSystem(new File(""));
    SonarComponents sonarComponents = createSonarComponentsMock(fs, XML_CHECK, POM_CHECK);
    XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, XML_CHECK, POM_CHECK);
    analyzer.scan(Lists.<File>newArrayList());
    verify(sonarComponents, never()).addIssue(any(File.class), eq(POM_CHECK), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, never()).addIssue(any(File.class), eq(XML_CHECK), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, never()).reportIssue(any(AnalyzerMessage.class));
}
Also used : SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 4 with SonarComponents

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

the class XmlAnalyzerTest method should_scan_pom_file_with_xml_check.

@Test
public void should_scan_pom_file_with_xml_check() {
    DefaultFileSystem fs = new DefaultFileSystem(new File(""));
    File xmlFile = new File(VALID_POM);
    fs.add(new TestInputFileBuilder("", xmlFile.getAbsolutePath()).build());
    SonarComponents sonarComponents = createSonarComponentsMock(fs, XML_CHECK);
    XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, XML_CHECK);
    analyzer.scan(Lists.newArrayList(xmlFile));
    verify(sonarComponents, times(1)).addIssue(any(File.class), any(JavaCheck.class), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, never()).reportIssue(any(AnalyzerMessage.class));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) JavaCheck(org.sonar.plugins.java.api.JavaCheck) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 5 with SonarComponents

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

the class XmlAnalyzerTest method should_scan_pom_file_with_pom_check.

@Test
public void should_scan_pom_file_with_pom_check() {
    DefaultFileSystem fs = new DefaultFileSystem(new File(""));
    File xmlFile = new File(VALID_POM);
    fs.add(new TestInputFileBuilder("", xmlFile.getAbsolutePath()).build());
    SonarComponents sonarComponents = createSonarComponentsMock(fs, POM_CHECK);
    XmlAnalyzer analyzer = new XmlAnalyzer(sonarComponents, POM_CHECK);
    analyzer.scan(Lists.newArrayList(xmlFile));
    verify(sonarComponents, times(1)).addIssue(any(File.class), any(JavaCheck.class), any(Integer.class), anyString(), isNull());
    verify(sonarComponents, never()).reportIssue(any(AnalyzerMessage.class));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) JavaCheck(org.sonar.plugins.java.api.JavaCheck) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) 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