PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/converters/smf/FixNestingTest.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 511 lines | 478 code | 31 blank | 2 comment | 2 complexity | 43c1a97098949b19d78a842e3a69202f MD5 | raw file
  1. package com.atlassian.uwc.converters.smf;
  2. import java.util.TreeMap;
  3. import java.util.TreeSet;
  4. import junit.framework.TestCase;
  5. import org.apache.log4j.Logger;
  6. import org.apache.log4j.PropertyConfigurator;
  7. public class FixNestingTest extends TestCase {
  8. FixNesting tester = null;
  9. Logger log = Logger.getLogger(this.getClass());
  10. protected void setUp() throws Exception {
  11. tester = new FixNesting();
  12. PropertyConfigurator.configure("log4j.properties");
  13. }
  14. public void testConvertNesting() {
  15. String input, expected, actual;
  16. input = "<ul>\n" +
  17. "\n" +
  18. "<li>a\n" +
  19. "\n" +
  20. "\n" +
  21. "<ul><li>\n" +
  22. "a</li>\n" +
  23. "\n" +
  24. "<li>b</li>\n" +
  25. "</ul>\n" +
  26. "";
  27. expected = "<ul>\n" +
  28. "\n" +
  29. "<li>a\n" +
  30. "\n" +
  31. "\n" +
  32. "<ul><li>\n" +
  33. "a</li>\n" +
  34. "\n" +
  35. "<li>b</li>\n" +
  36. "</ul>\n" +
  37. "</li>" +
  38. "</ul>";
  39. actual = tester.convertNesting(input);
  40. assertNotNull(actual);
  41. assertEquals(expected, actual);
  42. }
  43. public void testGetIndexes() {
  44. String input, expected, actual;
  45. input = "<a>testing" +
  46. " <a>123</a>" +
  47. "</a>";
  48. int[] expStart = {0, 11};
  49. int[] expEnd = {input.length() - 8, input.length() - 4};
  50. TreeMap<Integer, String> actStart = tester.getIndexes("<a>", input);
  51. TreeMap<Integer, String> actEnd = tester.getIndexes("<\\/a>", input);
  52. assertNotNull(actStart);
  53. assertNotNull(actEnd);
  54. assertTrue(actStart.containsKey(expStart[0]));
  55. assertTrue(actStart.containsKey(expStart[1]));
  56. assertTrue(actEnd.containsKey(expEnd[0]));
  57. assertTrue(actEnd.containsKey(expEnd[1]));
  58. input = "<a>testing" +
  59. " <b>123</b>" +
  60. "</a>";
  61. actStart = tester.getIndexes("<[ab]>", input);
  62. actEnd = tester.getIndexes("<\\/[ab]>", input);
  63. assertNotNull(actStart);
  64. assertNotNull(actEnd);
  65. assertTrue(actStart.containsKey(expStart[0]));
  66. assertTrue(actStart.containsKey(expStart[1]));
  67. assertTrue(actEnd.containsKey(expEnd[0]));
  68. assertTrue(actEnd.containsKey(expEnd[1]));
  69. }
  70. public void testAddClosing() {
  71. String input, expected, actual;
  72. input = "<ol><li>test</ol>";
  73. expected = "<ol><li>test</li></ol>";
  74. TreeMap<Integer, String> start = tester.getIndexes("<((ol)|(li))>", input);
  75. TreeMap<Integer, String> end = tester.getIndexes("<\\/((ol)|(li))>", input);
  76. actual = tester.addClosing(start, end, input);
  77. assertNotNull(actual);
  78. assertEquals(expected, actual);
  79. input = "<ol>test<li>ing"; //use tags that are represented in NestingOrderComparator
  80. expected = "<ol>test<li>ing</li></ol>";
  81. TreeMap<Integer, String> start2 = tester.getIndexes("<((ol)|(li))>", input);
  82. TreeMap<Integer, String> end2 = tester.getIndexes("<\\/((ol)|(li))>", input);
  83. actual = tester.addClosing(start2, end2, input);
  84. assertNotNull(actual);
  85. assertEquals(expected, actual);
  86. input = "<ol>abc</ol></ol>";
  87. expected = "<ol>abc</ol>";
  88. TreeMap<Integer, String> start3 = tester.getIndexes("<((ol)|(li))>", input);
  89. TreeMap<Integer, String> end3 = tester.getIndexes("<\\/((ol)|(li))>", input);
  90. actual = tester.addClosing(start3, end3, input);
  91. assertNotNull(actual);
  92. assertEquals(expected, actual);
  93. }
  94. public void testSame() {
  95. TreeMap<Integer, String> a = new TreeMap<Integer, String>();
  96. TreeMap<Integer, String> b = new TreeMap<Integer, String>();
  97. a.put(1, "<a>");
  98. a.put(4, "<b>");
  99. a.put(12, "<a>");
  100. b.put(7, "</a>");
  101. b.put(33, "</b>");
  102. assertFalse(tester.same(a,b));
  103. b.put(43, "</a>");
  104. assertTrue(tester.same(a,b));
  105. }
  106. public void testFixNesting_ex1() {
  107. String input, expected, actual;
  108. input = "abcdef:\n" +
  109. "<ul>\n" +
  110. "\n" +
  111. "<li>foobar\n" +
  112. "abcdef\n" +
  113. "\n" +
  114. "<ul><li>foobar\n" +
  115. "lorem</li>\n" +
  116. "\n" +
  117. "<li>ipsum</li>\n" +
  118. "</ul>\n" +
  119. "\n" +
  120. "testing\n" +
  121. "";
  122. expected = "abcdef:\n" +
  123. "<ul>\n" +
  124. "\n" +
  125. "<li>foobar\n" +
  126. "abcdef\n" +
  127. "\n" +
  128. // "</li><ul><li>foobar\n" + //option 1
  129. "<ul><li>foobar\n" + //option 2
  130. "lorem</li>\n" +
  131. "\n" +
  132. "<li>ipsum</li>\n" +
  133. "</ul>\n" +
  134. "\n" +
  135. // "testing\n</ul>" + //option 1
  136. "testing\n</li></ul>" + //option 2
  137. "";
  138. actual = tester.convertNesting(input);
  139. assertNotNull(actual);
  140. assertEquals(expected, actual);
  141. }
  142. public void testFixNesting_ex2() {
  143. String input, expected, actual;
  144. input = "abc\n" +
  145. "<ul><li>abcdef</li></ul>\n" +
  146. "<b>bold</b>\n" +
  147. "</li>\n" +//XXX Problem starts here?
  148. "</ul>\n" +
  149. "\n" +
  150. "";
  151. expected = "abc\n" +
  152. "<ul><li>abcdef</li></ul>\n" +
  153. "<b>bold</b>\n" +
  154. "\n\n\n" + //removed last two lines
  155. "";
  156. actual = tester.convertNesting(input);
  157. assertNotNull(actual);
  158. assertEquals(expected, actual);
  159. }
  160. public void testFixNesting_ex3() {
  161. String input, expected, actual;
  162. input =
  163. "<ul>\n" +
  164. "<li>abc</li>\n" +
  165. "</ul> \n" +
  166. "<ul>\n" +
  167. "<li>lorem - ipsum \n" +
  168. "</li><li>lorem @ ipsum </li>\n" +
  169. "<li>foobar </li>\n" +
  170. "</ul> \n" +
  171. "<ul>\n" +
  172. "<li>tada </li>\n" +
  173. "</ul> \n" +
  174. "<ul>\n" +
  175. "</li>\n" + //XXX This is the problem
  176. "</ul> \n" +
  177. "<ul>\n" +
  178. "<li>abc </li>\n" +
  179. "<li>def. </li>\n" +
  180. "</ul> \n" +
  181. "";
  182. expected = "<ul>\n" +
  183. "<li>abc</li>\n" +
  184. "</ul> \n" +
  185. "<ul>\n" +
  186. "<li>lorem - ipsum \n" +
  187. "</li><li>lorem @ ipsum </li>\n" +
  188. "<li>foobar </li>\n" +
  189. "</ul> \n" +
  190. "<ul>\n" +
  191. "<li>tada </li>\n" +
  192. "</ul> \n" +
  193. "<ul>" +
  194. "<li>\n" +
  195. "</li>\n" +
  196. "</ul> \n" +
  197. "<ul>\n" +
  198. "<li>abc </li>\n" +
  199. "<li>def. </li>\n" +
  200. "</ul> \n" +
  201. "";
  202. actual = tester.convertNesting(input);
  203. assertNotNull(actual);
  204. assertEquals(expected, actual);
  205. }
  206. public void testFixNesting_nochanges1() {
  207. String input, expected, actual;
  208. input = "<ul>\n" +
  209. "<li>abc</li>\n" +
  210. "<li>\n" +
  211. "def</li>\n" +
  212. "<li> efg</li>\n" +
  213. "<li>\n" +
  214. "hig</li>\n" +
  215. "</ul>\n" +
  216. "\n" +
  217. "<ul>\n" +
  218. "<li> \n" +
  219. " .</li>\n" +
  220. "<li>testing</li>\n" +
  221. "<li>\n" +
  222. " ( 123, 456, 789,\n" +
  223. "000)\n" +
  224. "</li>\n" +
  225. "</ul>\n" +
  226. "";
  227. expected = input;
  228. actual = tester.convertNesting(input);
  229. assertNotNull(actual);
  230. assertEquals(expected, actual);
  231. }
  232. public void testFixNesting_nochange2() {
  233. String input, expected, actual;
  234. input = "*Original Poster:* [~admin]\n" +
  235. "*Original Timestamp:* 2009-07-02 15:06:22\n" +
  236. "\n" +
  237. "Simple list:\n" +
  238. "<ul>\n" +
  239. "<li>Testing</li>\n" +
  240. "<li>123</li>\n" +
  241. "<li>abc</li>\n" +
  242. "<li>def</li>\n" +
  243. "</ul>\n" +
  244. "\n" +
  245. "Nesting:\n" +
  246. "<ul>\n" +
  247. "<li>level1\n" +
  248. " <ul>\n" +
  249. " <li>level 2</li>\n" +
  250. " </ul>\n" +
  251. "</li>\n" +
  252. "<li>foobar</li>\n" +
  253. "</ul>\n" +
  254. "\n" +
  255. "\n" +
  256. "{gallery:title=Attached Images}\n" +
  257. "";
  258. expected = input;
  259. actual = tester.convertNesting(input);
  260. assertNotNull(actual);
  261. assertEquals(expected, actual);
  262. }
  263. public void testFixNesting_nochange3() {
  264. String input, expected, actual;
  265. input = "*Original Poster:* [~admin]\n" +
  266. "*Original Timestamp:* 2009-07-02 15:06:22\n" +
  267. "\n" +
  268. "Simple list:\n" +
  269. "<ul>\n" +
  270. "<li>Testing</li>\n" +
  271. "<li>123</li>\n" +
  272. "<li>abc</li>\n" +
  273. "<li>def</li>\n" +
  274. "</ul>\n" +
  275. "\n" +
  276. "Nesting:\n" +
  277. "<ul>\n" +
  278. "<li>level1\n" +
  279. " <ul>\n" +
  280. " <li>level 2</li>\n" +
  281. " </ul>\n" +
  282. "</li>\n" +
  283. "<li>foobar</li>\n" +
  284. "</ul>\n" +
  285. "No closing delimiter\n" +
  286. "<ul>\n" +
  287. "<li>\n" +
  288. "no delim\n" +
  289. "</li>\n" +
  290. "</ul>\n" +
  291. "With Links:\n" +
  292. "\n" +
  293. "<ul>\n" +
  294. "<li>\n" +
  295. "test <a href=\"http://abc.com\">http://abc.com</a>\n" +
  296. "</li>\n" +
  297. "</ul>\n" +
  298. "No LIs\n" +
  299. "<ul>\n" +
  300. "<li>* circle\n" +
  301. "* circle\n" +
  302. "* circle\n" +
  303. "* disc\n" +
  304. "* disc\n" +
  305. "# square\n" +
  306. "# square\n" +
  307. "# square\n" +
  308. "</li>\n" +
  309. "</ul>\n" +
  310. "\n" +
  311. "\n" +
  312. "{gallery:title=Attached Images}\n" +
  313. "";
  314. expected = input;
  315. actual = tester.convertNesting(input);
  316. assertNotNull(actual);
  317. assertEquals(expected, actual);
  318. }
  319. public void testFixNesting_nochange4() {
  320. String input, expected, actual;
  321. input = "*Original Poster:* [~admin]\n" +
  322. "*Original Timestamp:* 2009-07-02 16:11:24\n" +
  323. "\n" +
  324. "Probably do:\n" +
  325. "[s]strike[/s]\n" +
  326. "[pre]pre\n" +
  327. " testing\n" +
  328. "[/pre]\n" +
  329. "[hr]\n" +
  330. "{color:red}color{color}\n" +
  331. "[sup]sup[/sup]\n" +
  332. "[sub]sub[/sub]\n" +
  333. "[tt]mono[/tt]\n" +
  334. "[code]code[/code]\n" +
  335. "{quote}quote{quote}\n" +
  336. "[quote=Author link=http://somesite/]text{quote}\n" +
  337. "[nobbc] [/nobbc]\n" +
  338. "\n" +
  339. "Probably don&#039;t:\n" +
  340. "[glow=red,2,300]test[/glow]\n" +
  341. "[shadow=red,left]test[/shadow]\n" +
  342. "[move]test[/move]\n" +
  343. "[left]left[/left]\n" +
  344. "[center]center[/center]\n" +
  345. "[right]right[/right]\n" +
  346. "[size=10pt]fontsize[/size]\n" +
  347. "[flash=200,200][/flash]\n" +
  348. "[font=Verdana]fontface[/font]\n" +
  349. "<ul>\n" +
  350. "<li>* circle\n" +
  351. "* circle\n" +
  352. "* circle\n" +
  353. "* disc\n" +
  354. "* disc\n" +
  355. "# square\n" +
  356. "# square\n" +
  357. "# square\n" +
  358. "</li>\n" +
  359. "</ul>\n" +
  360. "[abbr=exemlpi gratia]eg[/abbr]\n" +
  361. "[acronym=Simple Machines Forum]SMF[/acronym]\n" +
  362. "[html]\n" +
  363. "[/html]\n" +
  364. "[time]1132812640[/time]\n" +
  365. "\n" +
  366. "{gallery:title=Attached Images}\n" +
  367. "";
  368. expected = input;
  369. actual = tester.convertNesting(input);
  370. assertNotNull(actual);
  371. assertEquals(expected, actual);
  372. }
  373. public void testFixNesting_nochange5() {
  374. String input, expected, actual;
  375. input = "{color:red}Red{color}\n" +
  376. "\n" +
  377. "<ul>\n" +
  378. "<li>\n" +
  379. "abc\n" +
  380. "<a href=\"http://www.abc.com/\">abc\n.com</a>\n" +
  381. "{quote}loremipsum{quote}\n" +
  382. "\n" +
  383. "foobar\n" +
  384. "<a href=\"http://www.foobar.com\">http://www.foobar.com</a>\n" +
  385. "{quote}testing{quote}\n" +
  386. "</li>\n" +
  387. "</ul>";
  388. expected = input;
  389. actual = tester.convertNesting(input);
  390. assertNotNull(actual);
  391. assertEquals(expected, actual);
  392. }
  393. public void testNestingOrderComparator() {
  394. TreeSet<String> sorted = new TreeSet<String>(tester.new NestingOrderComparator());
  395. sorted.add("<ul>");
  396. sorted.add("<ol>");
  397. sorted.add("<li>");
  398. sorted.add("</ul>");
  399. sorted.add("</li>");
  400. int index = 0;
  401. for (String s : sorted) {
  402. assertNotNull(s);
  403. switch (index) {
  404. case 0:
  405. assertEquals("</li>", s);
  406. break;
  407. case 1:
  408. assertEquals("<li>", s);
  409. break;
  410. case 2:
  411. assertEquals("<ul>", s);
  412. break;
  413. case 3:
  414. assertEquals("<ol>", s);
  415. break;
  416. case 4:
  417. assertEquals("</ul>", s);
  418. break;
  419. }
  420. index++;
  421. }
  422. }
  423. public void testFixNesting_AddMissingOpeningLi() {
  424. String input, expected, actual;
  425. input = "<ul>\n" +
  426. "\n" +
  427. "a\n" +
  428. "\n" +
  429. "<ul><li>b</li>\n" +
  430. "\n" +
  431. "<li>c</li>\n" +
  432. "</ul>\n" +
  433. "\n" +
  434. "d\n" +
  435. "\n" +
  436. "</ul>";
  437. expected = "<ul><li>\n" +
  438. "\n" +
  439. "a\n" +
  440. "\n" +
  441. "<ul><li>b</li>\n" +
  442. "\n" +
  443. "<li>c</li>\n" +
  444. "</ul>\n" +
  445. "\n" +
  446. "d\n" +
  447. "\n" +
  448. "</ul>";
  449. actual = tester.addMissingLi(input);
  450. assertNotNull(actual);
  451. assertEquals(expected, actual);
  452. expected = "<ul><li>\n" +
  453. "\n" +
  454. "a\n" +
  455. "\n" +
  456. "<ul><li>b</li>\n" +
  457. "\n" +
  458. "<li>c</li>\n" +
  459. "</ul>\n" +
  460. "\n" +
  461. "d\n" +
  462. "\n" +
  463. "</li></ul>";
  464. actual = tester.convertNesting(input);
  465. assertNotNull(actual);
  466. assertEquals(expected, actual);
  467. }
  468. public void testIsJustAdd() {
  469. TreeSet<String> input = new TreeSet<String>(tester.new NestingOrderComparator());
  470. input.add("li");
  471. assertFalse(tester.isJustAdd(input));
  472. input.clear();
  473. input.add("ul");
  474. assertTrue(tester.isJustAdd(input));
  475. input.clear();
  476. input.add("li");
  477. input.add("ul");
  478. assertTrue(tester.isJustAdd(input));
  479. }
  480. }