use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JasperTest method test_exclude_all_jsp.
@Test
void test_exclude_all_jsp() throws Exception {
SensorContextTester ctx = jspContext(JSP_SOURCE);
ctx.setSettings(new MapSettings().setProperty("sonar.exclusions", "**/*_jsp.java"));
Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList());
assertThat(generatedFiles).isEmpty();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JasperTest method should_log_warning_when_jasper_fails.
@Test
void should_log_warning_when_jasper_fails() throws Exception {
SensorContextTester ctx = jspContext(JSP_SOURCE);
Jasper jasper = spy(new Jasper());
// we make Jasper#getJasperOptions blowup
doThrow(new IllegalStateException()).when(jasper).getJasperOptions(any(), any());
Collection<GeneratedFile> generatedFiles = jasper.generateFiles(ctx, emptyList());
assertThat(generatedFiles).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Failed to transpile JSP files.");
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JasperTest method test_compile_custom_tag.
@Test
void test_compile_custom_tag() throws Exception {
String tagLib = "<%@ taglib prefix=\"t\" tagdir=\"/WEB-INF/tags\" %>";
SensorContextTester ctx = jspContext(tagLib + "<t:mytag />");
// spring tag library is used to complete coverage in Jasper classloader - it has resource which is on the project's
// classpath
createJspFile(tagLib + SPRING_TLD + "<spring:url value=\"/url/path\" />\n" + "<h2>Hello World!</h2>", webInf.resolve("tags/mytag.tag"));
Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, singletonList(springJar));
assertThat(generatedFiles).hasSize(1);
GeneratedFile testJspFile = generatedFiles.iterator().next();
List<String> testJsp = Files.readAllLines(testJspFile.path());
assertThat(testJsp).contains(" org.apache.jsp.tag.web.mytag_tag _jspx_th_t_005fmytag_005f0 = new org.apache.jsp.tag.web.mytag_tag();");
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JasperTest method jspContext.
private SensorContextTester jspContext(String jspSource, Path path) throws IOException {
jspFile = createJspFile(jspSource, path);
SensorContextTester ctx = SensorContextTester.create(tempFolder);
DefaultInputFile inputFile = TestInputFileBuilder.create("", tempFolder.toFile(), jspFile.toFile()).setLanguage("jsp").setContents(jspSource).build();
ctx.fileSystem().add(inputFile);
ctx.fileSystem().setWorkDir(workDir);
return ctx;
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class VisitorsBridgeForTestsTest method test_report_with_analysis_message.
@Test
void test_report_with_analysis_message() {
SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null);
sonarComponents.setSensorContext(context);
Tree parse = JParserTestUtils.parse("class A{}");
DummyVisitor javaCheck = new DummyVisitor();
VisitorsBridgeForTests visitorsBridgeForTests = new VisitorsBridgeForTests(Collections.singletonList(javaCheck), sonarComponents, new JavaVersionImpl());
visitorsBridgeForTests.setCurrentFile(TestUtils.emptyInputFile("dummy.java"));
visitorsBridgeForTests.visitFile(parse, false);
JavaFileScannerContextForTests lastContext = visitorsBridgeForTests.lastCreatedTestContext();
assertThat(lastContext.getIssues()).isEmpty();
AnalyzerMessage message = lastContext.createAnalyzerMessage(javaCheck, parse, "test");
lastContext.addIssue(-1, javaCheck, "test");
lastContext.addIssue(-1, javaCheck, "test", 15);
lastContext.addIssueOnFile(javaCheck, "test");
lastContext.addIssueOnProject(javaCheck, "test");
lastContext.reportIssue(message);
assertThat(message.getMessage()).isEqualTo("test");
assertThat(lastContext.getIssues()).hasSize(5);
}
Aggregations