SourcePrinter { public String render(final Stream<String> lines, final Issue issue, final String description, final String iconUrl) { try (LookaheadStream stream = new LookaheadStream(lines)) { int start = issue.getLineStart(); int end = issue.getLineEnd(); StringBuilder before = readBlockUntilLine(stream, start - 1); StringBuilder marked = readBlockUntilLine(stream, end); StringBuilder after = readBlockUntilLine(stream, Integer.MAX_VALUE); String language = selectLanguageClass(issue); String code = asCode(before, language, "line-numbers") + asCode(marked, language, "highlight") + createInfoPanel(issue, description, iconUrl) + asCode(after, language); return pre().with(new UnescapedText(code)).renderFormatted(); } } SourcePrinter(); @VisibleForTesting  SourcePrinter(final JenkinsFacade jenkinsFacade); String render(final Stream<String> lines, final Issue issue, final String description,
            final String iconUrl);  }
@Test void shouldCreateSourceWithoutLineNumber() { SourcePrinter printer = new SourcePrinter(); IssueBuilder builder = new IssueBuilder(); Issue issue = builder.build(); Document document = Jsoup.parse(printer.render(asStream("format-java.txt"), issue, NO_DESCRIPTION, ICON_URL)); String expectedFile = toString("format-java.txt"); assertThat(document.text()).isEqualToIgnoringWhitespace(expectedFile); Elements pre = document.getElementsByTag("pre"); assertThat(pre.text()).isEqualToIgnoringWhitespace(expectedFile); }
@Test void shouldCreateSourceWithLineNumber() { IssueBuilder builder = new IssueBuilder(); Issue issue = builder.setLineStart(7).setMessage(MESSAGE).build(); SourcePrinter printer = new SourcePrinter(createJenkinsFacade()); Document document = Jsoup.parse(printer.render(asStream("format-java.txt"), issue, DESCRIPTION, ICON_URL)); assertThatCodeIsEqualToSourceText(document); assertThat(document.getElementsByClass("analysis-warning-title").text()) .isEqualTo(MESSAGE); assertThat(document.getElementsByClass("analysis-detail").text()) .isEqualTo(DESCRIPTION); }
@Test void shouldCreateSourceWithoutDescription() { IssueBuilder builder = new IssueBuilder(); Issue issue = builder.setLineStart(7).setMessage("Hello Message").build(); SourcePrinter printer = new SourcePrinter(); Document document = Jsoup.parse(printer.render(asStream("format-java.txt"), issue, NO_DESCRIPTION, ICON_URL)); assertThatCodeIsEqualToSourceText(document); assertThat(document.getElementsByClass("analysis-warning-title").text()) .isEqualTo(MESSAGE); assertThat(document.getElementsByClass("analysis-detail")).isEmpty(); assertThat(document.getElementsByClass("collapse-panel")).isEmpty(); }
@Test void shouldFilterTagsInCode() { IssueBuilder builder = new IssueBuilder(); Issue issue = builder.setLineStart(2).build(); SourcePrinter printer = new SourcePrinter(); Document document = Jsoup.parse(printer.render(asStream("format-jelly.txt"), issue, NO_DESCRIPTION, ICON_URL)); assertThat(document.getElementsByTag("code").html()) .isEqualTo("&lt;l:main-panel&gt;Before&lt;script&gt;execute&lt;/script&gt; Text&lt;/l:main-panel&gt;\n" + "&lt;l:main-panel&gt;Warning&lt;script&gt;execute&lt;/script&gt; Text&lt;/l:main-panel&gt;\n" + "&lt;l:main-panel&gt;After&lt;script&gt;execute&lt;/script&gt; Text&lt;/l:main-panel&gt;"); }
@Test void shouldFilterTagsInMessageAndDescription() { IssueBuilder builder = new IssueBuilder(); Issue issue = builder.setLineStart(7).setMessage("Hello <b>Message</b> <script>execute</script>").build(); SourcePrinter printer = new SourcePrinter(createJenkinsFacade()); Document document = Jsoup.parse(printer.render(asStream("format-java.txt"), issue, "Hello <b>Description</b> <script>execute</script>", ICON_URL)); assertThatCodeIsEqualToSourceText(document); assertThat(document.getElementsByClass("analysis-warning-title").html()) .isEqualToIgnoringWhitespace("Hello <b>Message</b>"); assertThat(document.getElementsByClass("analysis-detail").html()) .isEqualToIgnoringWhitespace("Hello <b>Description</b>"); }
@Test void shouldAddBreakOnNewLine() { IssueBuilder builder = new IssueBuilder(); Issue issue = builder.setLineStart(7).setMessage("Hello <b>MessageLine1\nLine2\nLine3</b>").build(); SourcePrinter printer = new SourcePrinter(); Document document = Jsoup.parse(printer.render(asStream("format-java.txt"), issue, NO_DESCRIPTION, ICON_URL)); assertThatCodeIsEqualToSourceText(document); assertThat(document.getElementsByClass("analysis-warning-title").html()) .isEqualTo("Hello <b>MessageLine1<br>Line2<br>Line3</b>"); assertThat(document.getElementsByClass("analysis-detail")).isEmpty(); assertThat(document.getElementsByClass("collapse-panel")).isEmpty(); }
@Test @org.jvnet.hudson.test.Issue("JENKINS-55679") void shouldRenderXmlFiles() { SourcePrinter printer = new SourcePrinter(); IssueBuilder builder = new IssueBuilder(); Issue issue = builder.build(); Document document = Jsoup.parse(printer.render(asStream("format.xml"), issue, NO_DESCRIPTION, ICON_URL)); String expectedFile = toString("format.xml"); assertThat(document.text()).isEqualToIgnoringWhitespace(expectedFile); Elements pre = document.getElementsByTag("pre"); assertThat(pre.text()).isEqualToIgnoringWhitespace(expectedFile); }