From 147d4f4ac0b266848042b3355de753eff8c6b314 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 13 Sep 2018 12:02:27 -0600 Subject: [PATCH] Copied syntax highlighting test to markdown from url unit test, cleaned up code --- .../confluence/MarkdownFromURLUnitTest.java | 60 +++++++++++++++++-- .../plugins/confluence/MarkdownUnitTest.java | 12 +--- src/test/resources/testSyntaxHighlighting.md | 1 + 3 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/testSyntaxHighlighting.md diff --git a/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownFromURLUnitTest.java b/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownFromURLUnitTest.java index fa299b2..df01135 100644 --- a/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownFromURLUnitTest.java +++ b/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownFromURLUnitTest.java @@ -1,7 +1,6 @@ package ut.com.atlassian.plugins.confluence; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.*; @@ -11,8 +10,9 @@ import org.mockito.*; import org.mockito.runners.MockitoJUnitRunner; import java.util.HashMap; -import java.util.regex.Pattern; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.net.*; import com.atlassian.confluence.content.render.xhtml.ConversionContext; @@ -20,7 +20,10 @@ import com.atlassian.confluence.macro.MacroExecutionException; import com.atlassian.webresource.api.assembler.PageBuilderService; import com.atlassian.webresource.api.assembler.RequiredResources; import com.atlassian.webresource.api.assembler.WebResourceAssembler; - +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlElement; +import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.atlassian.plugins.confluence.markdown.MarkdownFromURLMacro; @RunWith (MockitoJUnitRunner.class) @@ -44,7 +47,7 @@ public class MarkdownFromURLUnitTest { String file = new File("src/test/resources/testMarkdown.md").toURI().toURL().toString(); @SuppressWarnings({ "rawtypes", "unchecked" }) String output = markdownMacro.execute(new HashMap(), file, conversionContext); - assertThat(Pattern.matches("[\\S\\s]*Italic[\\S\\s]*", output), is(true)); //Uses [\S\s] (anything that is either whitespace or not whitespace) instead of . (any character) because . does not match newline characters. + assertTrue(output.contains("Italic")); } @Test public void testErrorHandling() throws MacroExecutionException, MalformedURLException { @@ -78,6 +81,55 @@ public class MarkdownFromURLUnitTest { String output3 = markdownMacro.execute(new HashMap(), input3, conversionContext); assertThat(output3, is(not(""))); } + @Test + public void testSyntaxHighlighting() throws MacroExecutionException { + /*Test that the javascript for syntax highlighting works*/ + // Run the macro with an input of a line of code + // Create a temporary HTML file containing the output + // Parse the HTML file with htmlunit + // Assert that the page contains three spans with the correct classes + // Note: Does not test if highlight.js and highlight.css are correctly included in the page + try (final WebClient webClient = new WebClient()) { + String file = new File("src/test/resources/testSyntaxHighlighting.md").toURI().toURL().toString(); + @SuppressWarnings({ "rawtypes", "unchecked" }) + String output = markdownMacro.execute(new HashMap(), file, conversionContext); + String toWrite = "\r\n" + + "\r\n" + + "\r\n" + + " Syntax Highlighting Page\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + "\r\n" + + "\r\n" + + " \r\n" + + "
" + + output + + "
" + + "\r\n" + + ""; + File tmpHTMLFile = File.createTempFile("syntax-highlighting-", ".html", new File("src/test/resources")); + FileWriter writer = new FileWriter(tmpHTMLFile); + writer.write(toWrite); + writer.close(); + final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString()); + HtmlElement document = page.getDocumentElement(); + assertTrue(document.getElementsByAttribute("span", "class", "hljs-class").size() > 0); + assertTrue(document.getElementsByAttribute("span", "class", "hljs-keyword").size() > 0); + assertTrue(document.getElementsByAttribute("span", "class", "hljs-title").size() > 0); + tmpHTMLFile.delete(); + } catch (FailingHttpStatusCodeException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } @Before public void setup() { //Mock methods for pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); diff --git a/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java b/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java index b6547fe..d914b6a 100644 --- a/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java +++ b/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java @@ -1,13 +1,10 @@ package ut.com.atlassian.plugins.confluence; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import net.sourceforge.htmlunit.*; import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.html.*; @@ -17,7 +14,6 @@ import org.mockito.runners.MockitoJUnitRunner; import java.io.*; import java.net.MalformedURLException; import java.util.HashMap; -import java.util.regex.Pattern; import com.atlassian.confluence.content.render.xhtml.ConversionContext; import com.atlassian.confluence.macro.MacroExecutionException; @@ -46,15 +42,16 @@ public class MarkdownUnitTest { // then assert that *Italic* was correctly rendered into Italic @SuppressWarnings({ "rawtypes", "unchecked" }) String output = markdownMacro.execute(new HashMap(), "*Italic*", conversionContext); - assertTrue(Pattern.matches("[\\S\\s]*Italic[\\S\\s]*", output)); //Uses [\S\s] (anything that is either whitespace or not whitespace) instead of . (any character) because . does not match newline characters. + assertTrue(output.contains("Italic")); } @Test public void testSyntaxHighlighting() throws MacroExecutionException { - /*Test that the correct JavaScript is returned for highlight.js to work*/ + /*Test that the javascript for syntax highlighting works*/ // Run the macro with an input of a line of code // Create a temporary HTML file containing the output // Parse the HTML file with htmlunit // Assert that the page contains three spans with the correct classes + // Note: Does not test if highlight.js and highlight.css are correctly included in the page try (final WebClient webClient = new WebClient()) { @SuppressWarnings({ "rawtypes", "unchecked" }) String output = markdownMacro.execute(new HashMap(), "`class className() {}`", conversionContext); @@ -94,9 +91,6 @@ public class MarkdownUnitTest { } catch (IOException e) { e.printStackTrace(); } -// System.out.println(output); -// assertTrue(Pattern.matches("[\\S\\s]*public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output)); -// assertTrue(Pattern.matches("[\\S\\s]*