From 7ea26df373f7e7aa17c1822f55ae5daec65d5050 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 13 Sep 2018 09:51:56 -0600 Subject: [PATCH] Added unit test for syntax highlighting --- pom.xml | 10 +-- .../plugins/confluence/MarkdownUnitTest.java | 64 ++++++++++++++++--- src/test/resources/jquery-3.3.1.min.js | 2 + 3 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/jquery-3.3.1.min.js diff --git a/pom.xml b/pom.xml index 6805617..f85bcd2 100644 --- a/pom.xml +++ b/pom.xml @@ -91,11 +91,11 @@ 1.9.0 test - - - org.mozilla - rhino - 1.7.10 + + net.sourceforge.htmlunit + htmlunit + 2.32 + test 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 191a422..b6547fe 100644 --- a/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java +++ b/src/test/java/ut/com/atlassian/plugins/confluence/MarkdownUnitTest.java @@ -4,11 +4,18 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + +import net.sourceforge.htmlunit.*; +import com.gargoylesoftware.htmlunit.*; +import com.gargoylesoftware.htmlunit.html.*; import org.mockito.*; import org.mockito.runners.MockitoJUnitRunner; +import java.io.*; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.regex.Pattern; @@ -17,7 +24,6 @@ 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.atlassian.plugins.confluence.markdown.MarkdownMacro; @RunWith (MockitoJUnitRunner.class) @@ -45,14 +51,52 @@ public class MarkdownUnitTest { @Test public void testSyntaxHighlighting() throws MacroExecutionException { /*Test that the correct JavaScript is returned for highlight.js to work*/ - // Run the macro using input of a line of code in a code block, - // then assert that a block is returned. - // Intended only as a temporary test until I can program a better one - @SuppressWarnings({ "rawtypes", "unchecked" }) - String output = markdownMacro.execute(new HashMap(), "`public class JavaClass {}`", conversionContext); - System.out.println(output); - assertTrue(Pattern.matches("[\\S\\s]*public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output)); - assertTrue(Pattern.matches("[\\S\\s]*\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(); + } +// System.out.println(output); +// assertTrue(Pattern.matches("[\\S\\s]*public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output)); +// assertTrue(Pattern.matches("[\\S\\s]*