Copied syntax highlighting test to markdown from url unit test, cleaned up code
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package ut.com.atlassian.plugins.confluence;
|
package ut.com.atlassian.plugins.confluence;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@@ -11,8 +10,9 @@ import org.mockito.*;
|
|||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
|
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.PageBuilderService;
|
||||||
import com.atlassian.webresource.api.assembler.RequiredResources;
|
import com.atlassian.webresource.api.assembler.RequiredResources;
|
||||||
import com.atlassian.webresource.api.assembler.WebResourceAssembler;
|
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;
|
import com.atlassian.plugins.confluence.markdown.MarkdownFromURLMacro;
|
||||||
|
|
||||||
@RunWith (MockitoJUnitRunner.class)
|
@RunWith (MockitoJUnitRunner.class)
|
||||||
@@ -44,7 +47,7 @@ public class MarkdownFromURLUnitTest {
|
|||||||
String file = new File("src/test/resources/testMarkdown.md").toURI().toURL().toString();
|
String file = new File("src/test/resources/testMarkdown.md").toURI().toURL().toString();
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
String output = markdownMacro.execute(new HashMap(), file, conversionContext);
|
String output = markdownMacro.execute(new HashMap(), file, conversionContext);
|
||||||
assertThat(Pattern.matches("[\\S\\s]*<em>Italic</em>[\\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("<em>Italic</em>"));
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testErrorHandling() throws MacroExecutionException, MalformedURLException {
|
public void testErrorHandling() throws MacroExecutionException, MalformedURLException {
|
||||||
@@ -78,6 +81,55 @@ public class MarkdownFromURLUnitTest {
|
|||||||
String output3 = markdownMacro.execute(new HashMap(), input3, conversionContext);
|
String output3 = markdownMacro.execute(new HashMap(), input3, conversionContext);
|
||||||
assertThat(output3, is(not("")));
|
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 = "<!DOCTYPE html>\r\n" +
|
||||||
|
"<html>\r\n" +
|
||||||
|
"<head>\r\n" +
|
||||||
|
" <title>Syntax Highlighting Page</title>\r\n" +
|
||||||
|
" <script src=\"./jquery-3.3.1.min.js\"></script>\r\n" +
|
||||||
|
" <script src=\"../../main/resources/js/highlight.min.js\"></script>\r\n" +
|
||||||
|
" <link href=\"../../main/resources/css/highlight.min.css\" rel=\"stylesheet\" />\r\n" +
|
||||||
|
"</head>\r\n" +
|
||||||
|
"<body>\r\n" +
|
||||||
|
" <script>\r\n" +
|
||||||
|
" var AJS = {\r\n" +
|
||||||
|
" $: $\r\n" +
|
||||||
|
" };\r\n" +
|
||||||
|
" </script>\r\n" +
|
||||||
|
"<div data-macro-name='markdown'>" +
|
||||||
|
output +
|
||||||
|
"</div>" +
|
||||||
|
"</body>\r\n" +
|
||||||
|
"</html>";
|
||||||
|
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
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
//Mock methods for pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs");
|
//Mock methods for pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs");
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
package ut.com.atlassian.plugins.confluence;
|
package ut.com.atlassian.plugins.confluence;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
|
||||||
|
|
||||||
import net.sourceforge.htmlunit.*;
|
|
||||||
import com.gargoylesoftware.htmlunit.*;
|
import com.gargoylesoftware.htmlunit.*;
|
||||||
import com.gargoylesoftware.htmlunit.html.*;
|
import com.gargoylesoftware.htmlunit.html.*;
|
||||||
|
|
||||||
@@ -17,7 +14,6 @@ import org.mockito.runners.MockitoJUnitRunner;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
|
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
|
||||||
import com.atlassian.confluence.macro.MacroExecutionException;
|
import com.atlassian.confluence.macro.MacroExecutionException;
|
||||||
@@ -46,15 +42,16 @@ public class MarkdownUnitTest {
|
|||||||
// then assert that *Italic* was correctly rendered into <em>Italic</em>
|
// then assert that *Italic* was correctly rendered into <em>Italic</em>
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
String output = markdownMacro.execute(new HashMap(), "*Italic*", conversionContext);
|
String output = markdownMacro.execute(new HashMap(), "*Italic*", conversionContext);
|
||||||
assertTrue(Pattern.matches("[\\S\\s]*<em>Italic</em>[\\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("<em>Italic</em>"));
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testSyntaxHighlighting() throws MacroExecutionException {
|
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
|
// Run the macro with an input of a line of code
|
||||||
// Create a temporary HTML file containing the output
|
// Create a temporary HTML file containing the output
|
||||||
// Parse the HTML file with htmlunit
|
// Parse the HTML file with htmlunit
|
||||||
// Assert that the page contains three spans with the correct classes
|
// 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()) {
|
try (final WebClient webClient = new WebClient()) {
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
String output = markdownMacro.execute(new HashMap(), "`class className() {}`", conversionContext);
|
String output = markdownMacro.execute(new HashMap(), "`class className() {}`", conversionContext);
|
||||||
@@ -94,9 +91,6 @@ public class MarkdownUnitTest {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// System.out.println(output);
|
|
||||||
// assertTrue(Pattern.matches("[\\S\\s]*<code.*>public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output));
|
|
||||||
// assertTrue(Pattern.matches("[\\S\\s]*<script>\\sAJS\\.\\$\\('\\[data\\-macro\\-name=\"markdown\"\\] code'\\)\\.each\\(function\\(i, block\\) \\{\\s hljs\\.highlightBlock\\(block\\);\\s \\}\\);\\s<\\/script>[\\S\\s]*", output));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|||||||
1
src/test/resources/testSyntaxHighlighting.md
Normal file
1
src/test/resources/testSyntaxHighlighting.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
`class className() {}`
|
||||||
Reference in New Issue
Block a user