PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/mindtouch/LinkParserTest.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 265 lines | 225 code | 38 blank | 2 comment | 0 complexity | 3d0e0c3d275cf1ae1cf7cbb07e375cbc MD5 | raw file
  1. package com.atlassian.uwc.converters.mindtouch;
  2. import java.io.File;
  3. import java.util.Properties;
  4. import junit.framework.TestCase;
  5. import org.apache.log4j.Logger;
  6. import org.apache.log4j.PropertyConfigurator;
  7. import com.atlassian.uwc.converters.xml.DefaultXmlEvents;
  8. import com.atlassian.uwc.converters.xml.XmlConverter;
  9. import com.atlassian.uwc.ui.Page;
  10. public class LinkParserTest extends TestCase {
  11. XmlConverter tester = null;
  12. Logger log = Logger.getLogger(this.getClass());
  13. DefaultXmlEvents events = null;
  14. protected void setUp() throws Exception {
  15. tester = new XmlConverter();
  16. PropertyConfigurator.configure("log4j.properties");
  17. events = new DefaultXmlEvents();
  18. events.clearAll();
  19. events.addEvent("a", "com.atlassian.uwc.converters.mindtouch.LinkParser");
  20. }
  21. public void testConvert_Internal() {
  22. String input = "<content><a rel=\"internal\" href=\"http://192.168.2.247/Sandbox\">Sandbox</a>\n" +
  23. "</content>";
  24. String expected = "[Sandbox]\n";
  25. String actual = parse(input);
  26. assertNotNull(actual);
  27. assertEquals(expected, actual);
  28. }
  29. public void testConvert_External() {
  30. String input = "<content>" +
  31. "<a class=\" external\" title=\"http://google.com/\" rel=\"external nofollow\" href=\"http://google.com\" target=\"_blank\">http://google.com</a>\n" +
  32. "</content>";
  33. String expected = "[http://google.com]\n";
  34. String actual = parse(input);
  35. assertNotNull(actual);
  36. assertEquals(expected, actual);
  37. }
  38. public void testConvert_Alias() {
  39. Properties props = new Properties();
  40. props.setProperty("exportdir", "sampleData/mindtouch/junit_resources/links");
  41. tester.setProperties(props);
  42. String input = "<content><p>" +
  43. "<a rel=\"internal\" href=\"http://192.168.2.247/Sandbox/Test_Comments\">Alias to Comments Page</a>" +
  44. "\n<a title=\"http://abc.com/\" class=\" external\" rel=\"external nofollow\" href=\"http://abc.com\" target=\"_blank\">Alias to external link</a>" +
  45. "</p></content>";
  46. String expected = "[Alias to Comments Page|Test Comments]\n" +
  47. "[Alias to external link|http://abc.com]";
  48. String actual = parse(input);
  49. assertNotNull(actual);
  50. assertEquals(expected, actual);
  51. }
  52. public void testConvert_Transformedchars() { //ws and urlencoding
  53. Properties props = new Properties();
  54. props.setProperty("exportdir", "sampleData/mindtouch/junit_resources/links");
  55. tester.setProperties(props);
  56. String input = "<content><p>" +
  57. "<a rel=\"internal\" href=\"http://192.168.2.247/Sandbox/Test_Comments\">Alias to Comments Page</a>" +
  58. "<a rel=\"internal\" href=\"http://192.168.2.247/Sandbox/Test_XYZ%3b_Bad_Chars\">Test XYZ; Bad Chars</a>" +
  59. "</p></content>";
  60. String expected = "[Alias to Comments Page|Test Comments]" +
  61. "[Test XYZ; Bad Chars|Test XYZ%3b Bad Chars]"; //the url encode will be permanently fixed by the illegal page handler
  62. String actual = parse(input);
  63. assertNotNull(actual);
  64. assertEquals(expected, actual);
  65. }
  66. public void testConvert_Transformedchars_IgnoringMindtouch() { //ws and urlencoding
  67. Properties props = new Properties();
  68. props.setProperty("exportdir", "sampleData/mindtouch/junit_resources/links2");
  69. tester.setProperties(props);
  70. String input = "<content><p>" +
  71. "<a rel=\"internal\" href=\"http://192.168.2.247/Sandbox/Test_Comments\">Alias to Comments Page</a>" +
  72. "<a rel=\"internal\" href=\"http://192.168.2.247/Sandbox/Test_XYZ%3b_Bad_Chars\">Test XYZ; Bad Chars</a>" +
  73. "</p></content>";
  74. String expected = "[Alias to Comments Page|Test Comments]" +
  75. "[Test XYZ; Bad Chars|Test XYZ%3b Bad Chars]"; //the url encode will be permanently fixed by the illegal page handler
  76. String actual = parse(input);
  77. assertNotNull(actual);
  78. assertEquals(expected, actual);
  79. }
  80. public void testConvert_AttachmentLink() {
  81. String input = "<content>" +
  82. "<a rel=\"internal\" href=\"http://192.168.2.247/@api/deki/files/3/=abc.txt\" " +
  83. "class=\"iconitext-16 ext-txt \">abc.txt</a>" +
  84. "</content>";
  85. String expected = "[^abc.txt]";
  86. String actual = parse(input);
  87. assertNotNull(actual);
  88. assertEquals(expected, actual);
  89. }
  90. public void testConvert_AttachmentLinkSameParent() {
  91. String input = "<content>" +
  92. "<a rel=\"internal\" href=\"http://192.168.2.247/@api/deki/files/3/=abc.txt?parent=Test Attachments\" " +
  93. "class=\"iconitext-16 ext-txt \">abc.txt</a>" +
  94. "</content>";
  95. String expected = "[^abc.txt]";
  96. String actual = parse(input, "Test Attachments");
  97. assertNotNull(actual);
  98. assertEquals(expected, actual);
  99. }
  100. public void testConvert_AttachmentLinkOtherParent() {
  101. String input = "<content>" +
  102. "<a rel=\"internal\" href=\"http://192.168.2.247/@api/deki/files/3/=abc.txt?parent=Some Other Page\" " +
  103. "class=\"iconitext-16 ext-txt \">abc.txt</a>" +
  104. "</content>";
  105. String expected = "[Some Other Page^abc.txt]";
  106. String actual = parse(input);
  107. assertNotNull(actual);
  108. assertEquals(expected, actual);
  109. }
  110. public void testConvert_AttachmentLinkOtherParent_Alias() {
  111. String input = "<content>" +
  112. "<a rel=\"internal\" href=\"http://192.168.2.247/@api/deki/files/3/=abc.txt?parent=Some Other Page\" " +
  113. "class=\"iconitext-16 ext-txt \">Alias</a>" +
  114. "</content>";
  115. String expected = "[Alias|Some Other Page^abc.txt]";
  116. String actual = parse(input);
  117. assertNotNull(actual);
  118. assertEquals(expected, actual);
  119. }
  120. public void testIsImage() {
  121. String input, expected, actual;
  122. LinkParser parser = new LinkParser();
  123. input = "http://192.168.2.247/@api/deki/files/3/=abc.txt";
  124. assertTrue(parser.isImage(input));
  125. input = "http://192.168.2.247/Sandbox";
  126. assertFalse(parser.isImage(input));
  127. }
  128. public void testGetImageTarget() {
  129. String input, expected, actual;
  130. LinkParser parser = new LinkParser();
  131. input = "http://192.168.2.247/@api/deki/files/3/=abc.txt";
  132. expected = "abc.txt";
  133. actual = parser.getImageTarget(input);
  134. assertNotNull(actual);
  135. assertEquals(expected, actual);
  136. }
  137. public void testFixUnderscore() {
  138. String input, href, expected, actual;
  139. LinkParser parser = new LinkParser();
  140. Page page = new Page(null);
  141. //no exportdir
  142. input = "Test_Comments";
  143. href = "http://192.168.2.247/Sandbox/Test_Comments";
  144. expected = "Test_Comments";
  145. actual = parser.fixUnderscores(input, href, page);
  146. assertNotNull(actual);
  147. assertEquals(expected, actual);
  148. Properties props = new Properties();
  149. props.setProperty("exportdir", "sampleData/mindtouch/junit_resources/links");
  150. parser.setProperties(props);
  151. input = "Sandbox";
  152. href = "http://192.168.2.247/Sandbox";
  153. expected = "Sandbox";
  154. actual = parser.fixUnderscores(input, href, page);
  155. assertNotNull(actual);
  156. assertEquals(expected, actual);
  157. input = "Test_Comments";
  158. href = "http://192.168.2.247/Sandbox/Test_Comments";
  159. expected = "Test Comments";
  160. actual = parser.fixUnderscores(input, href, page);
  161. assertNotNull(actual);
  162. assertEquals(expected, actual);
  163. input = "Test_Z_underscores";
  164. href = "http://192.168.2.247/Sandbox/Test_Z_underscores";
  165. expected = "Test Z_underscores";
  166. actual = parser.fixUnderscores(input, href, page);
  167. assertNotNull(actual);
  168. assertEquals(expected, actual);
  169. input = "foo_bar"; //doesn't exist
  170. href = "http://192.168.2.247/Sandbox/foo_bar";
  171. expected = input;
  172. actual = parser.fixUnderscores(input, href, page);
  173. assertNotNull(actual);
  174. assertEquals(expected, actual);
  175. input = "Test_XYZ%3b_Bad_Chars";//bad chars
  176. href = "http://192.168.2.247/Sandbox/Test_XYZ%3b_Bad_Chars";
  177. expected = "Test XYZ%3b Bad Chars"; //fixed by illegal page handling framework
  178. actual = parser.fixUnderscores(input, href, page);
  179. assertNotNull(actual);
  180. assertEquals(expected, actual);
  181. //bad exportdir
  182. props.setProperty("exportdir", "sampleData/mindtouch/junit_resources/linkity");
  183. parser.setProperties(props);
  184. input = "Test_Comments";
  185. href = "http://192.168.2.247/Sandbox/Test_Comments";
  186. expected = "Test_Comments";
  187. actual = parser.fixUnderscores(input, href, page);
  188. assertNotNull(actual);
  189. assertEquals(expected, actual);
  190. }
  191. public void testFixUnderscores_StringFile() {
  192. String input, expected, actual;
  193. LinkParser parser = new LinkParser();
  194. File file = new File("sampleData/mindtouch/junit_resources/links/21_Mindtouch_subpages/" +
  195. "39_Sandbox_subpages/42_TestComments.xml");
  196. input = "Test_Comments";
  197. expected = "Test Comments";
  198. actual = parser.fixUnderscores(input, file);
  199. assertNotNull(actual);
  200. assertEquals(expected, actual);
  201. file = new File("sampleData/mindtouch/junit_resources/links/21_Mindtouch_subpages/" +
  202. "39_Sandbox_subpages/54_TestZ_underscores.xml");
  203. input = "Test_Z_underscores";
  204. expected = "Test Z_underscores";
  205. actual = parser.fixUnderscores(input, file);
  206. assertNotNull(actual);
  207. assertEquals(expected, actual);
  208. file = new File("sampleData/mindtouch/junit_resources/links/21_Mindtouch_subpages/" +
  209. "39_Sandbox_subpages/49_TestXYZBadChars.xml");
  210. input = "Test_XYZ%3b_Bad_Chars";
  211. expected = "Test XYZ%3b Bad Chars";
  212. actual = parser.fixUnderscores(input, file);
  213. assertNotNull(actual);
  214. assertEquals(expected, actual);
  215. }
  216. private String parse(String input) {
  217. return parse(input, "");
  218. }
  219. private String parse(String input, String pagename) {
  220. Page page = new Page(null);
  221. page.setName(pagename);
  222. page.setOriginalText(input);
  223. tester.convert(page);
  224. String actual = page.getConvertedText();
  225. return actual;
  226. }
  227. }