XmlFileSensor implements Sensor { @Override public void execute(SensorContext context) { FileSystem fs = context.fileSystem(); FilePredicate xmlFilesPredicate = fs.predicates().matchesPathPattern("**/*.xml"); List<InputFile> inputFiles = new ArrayList<>(); fs.inputFiles(xmlFilesPredicate).forEach(inputFile1 -> { context.markForPublishing(inputFile1); inputFiles.add(inputFile1); }); if (inputFiles.isEmpty()) { return; } ProgressReport progressReport = new ProgressReport("Report about progress of Java XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean successfullyCompleted = false; boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile); progressReport.nextFile(); } successfullyCompleted = !cancelled; } finally { if (successfullyCompleted) { progressReport.stop(); } else { progressReport.cancel(); } } } XmlFileSensor(CheckFactory checkFactory); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }
@Test void test() throws Exception { CheckFactory checkFactory = new CheckFactory(activeRules()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); DefaultInputFile xml = (DefaultInputFile) addFileWithIssue("xml"); sensor.execute(context); assertThat(xml.isPublished()).isTrue(); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); Issue issue = issues.iterator().next(); assertThat(issue.ruleKey()).isEqualTo(XML_RULE_KEY); assertThat(issue.primaryLocation().message()).isEqualTo("Move this default interceptor to \"ejb-jar.xml\""); assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(5); }
@Test void testDoNothingIfNoXmlFile() throws Exception { CheckFactory checkFactory = new CheckFactory(activeRules()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); addFileWithIssue("foo"); sensor.execute(context); assertThat(context.allIssues()).isEmpty(); }
@Test void testDoNothingIfNoXmlRule() throws Exception { CheckFactory checkFactory = new CheckFactory(new ActiveRulesBuilder().build()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); DefaultInputFile xml = (DefaultInputFile) addFileWithIssue("xml"); sensor.execute(context); assertThat(xml.isPublished()).isTrue(); assertThat(context.allIssues()).isEmpty(); }
@Test void testHandleAnalysisCancellation() throws Exception { CheckFactory checkFactory = new CheckFactory(new ActiveRulesBuilder().build()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); context.setCancelled(true); DefaultInputFile xml = (DefaultInputFile) addFileWithIssue("xml"); sensor.execute(context); assertThat(xml.isPublished()).isTrue(); assertThat(context.allIssues()).isEmpty(); }
@Test void testDoNothingIfParsingError() throws Exception { CheckFactory checkFactory = new CheckFactory(activeRules()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); DefaultInputFile notXml = TestInputFileBuilder.create("moduleKey", "test.xml") .setCharset(StandardCharsets.UTF_8) .setContents("<ejb-jar") .build(); context.fileSystem().add(notXml); sensor.execute(context); assertThat(context.allIssues()).isEmpty(); assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).isEqualTo("Skipped 'test.xml' due to parsing error"); }
@Test void testDoNotFailAnalysisIfUnexpectedIssue() throws Exception { CheckFactory checkFactory = new CheckFactory(activeRules()); XmlFileSensor sensor = new XmlFileSensor(checkFactory); DefaultInputFile inputFile = TestInputFileBuilder.create("moduleKey", "test.xml") .setCharset(StandardCharsets.UTF_8) .setContents( "<a>\n" + " <b />\n" + "</a>\n") .build(); DefaultInputFile mocked = Mockito.spy(inputFile); Mockito.when(mocked.contents()).thenThrow(new IllegalStateException("This should have been caught.")); context.fileSystem().add(mocked); sensor.execute(context); assertThat(context.allIssues()).isEmpty(); assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty(); assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1); assertThat(logTester.logs(LoggerLevel.WARN).get(0)).startsWith("Unable to analyse file 'test.xml'."); }