PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/parser/TagParserTest.java

#
Java | 613 lines | 457 code | 114 blank | 42 comment | 0 complexity | 82c8c0f54910eabf2bfbdca922482c35 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * TagParserTest.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2010 Eric Le Lay
  6. *
  7. * The XML plugin is licensed under the GNU General Public License, with
  8. * the following exception:
  9. *
  10. * "Permission is granted to link this code with software released under
  11. * the Apache license version 1.1, for example used by the Xerces XML
  12. * parser package."
  13. */
  14. package xml.parser;
  15. // {{{ jUnit imports
  16. import java.util.concurrent.TimeUnit;
  17. import org.junit.*;
  18. import static org.junit.Assert.*;
  19. import static org.junit.Assume.*;
  20. import org.fest.swing.fixture.*;
  21. import org.fest.swing.core.*;
  22. import org.fest.swing.data.TableCell;
  23. import org.fest.swing.finder.*;
  24. import org.fest.swing.edt.*;
  25. import org.fest.swing.timing.*;
  26. import org.fest.swing.core.matcher.JButtonMatcher;
  27. import static org.fest.assertions.Assertions.*;
  28. import org.gjt.sp.jedit.testframework.Log;
  29. import static org.gjt.sp.jedit.testframework.TestUtils.*;
  30. import static org.gjt.sp.jedit.testframework.EBFixture.*;
  31. import org.gjt.sp.jedit.testframework.PluginOptionsFixture;
  32. import org.gjt.sp.jedit.testframework.TestUtils;
  33. import static xml.XMLTestUtils.*;
  34. // }}}
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.EBMessage;
  37. import org.gjt.sp.jedit.textarea.JEditTextArea;
  38. import org.gjt.sp.jedit.Buffer;
  39. import java.io.*;
  40. import java.util.regex.Pattern;
  41. import javax.swing.text.*;
  42. import javax.swing.*;
  43. import java.util.List;
  44. import java.awt.event.KeyEvent;
  45. import java.awt.event.InputEvent;
  46. import org.gjt.sp.jedit.gui.CompletionPopup;
  47. import static xml.parser.TagParser.*;
  48. import xml.XmlParsedData;
  49. import sidekick.SideKickParsedData;
  50. /**
  51. * unit tests for TagParser
  52. * $Id: TagParserTest.java 21248 2012-03-05 19:37:01Z kerik-sf $
  53. */
  54. public class TagParserTest{
  55. private static File testData;
  56. @BeforeClass
  57. public static void setUpjEdit() throws IOException{
  58. TestUtils.beforeClass();
  59. testData = new File(System.getProperty("test_data")).getCanonicalFile();
  60. assertTrue(testData.exists());
  61. }
  62. @AfterClass
  63. public static void tearDownjEdit() {
  64. TestUtils.afterClass();
  65. }
  66. @Test
  67. public void testIsInsideTagNoStart(){
  68. String text = "1 > 0";
  69. assertFalse(TagParser.isInsideTag(text,0)); // |1 > 0
  70. assertFalse(TagParser.isInsideTag(text,2)); // 1 |> 0
  71. assertFalse(TagParser.isInsideTag(text,3)); // 1 >| 0
  72. assertFalse(TagParser.isInsideTag(text,4)); // 1 > |0
  73. assertFalse(TagParser.isInsideTag(text,5)); // 1 > 0|
  74. }
  75. @Test
  76. public void testIsInsideTagSimple(){
  77. String text = "<a>b <c>";
  78. assertFalse(TagParser.isInsideTag(text,0)); // |<a>b <c>
  79. assertTrue( TagParser.isInsideTag(text,1)); // <|a>b <c>
  80. assertTrue( TagParser.isInsideTag(text,2)); // <a|>b <c>
  81. assertFalse(TagParser.isInsideTag(text,3)); // <a>|b <c>
  82. assertFalse(TagParser.isInsideTag(text,4)); // <a>b| <c>
  83. assertTrue(TagParser.isInsideTag(text,6)); // <a>b <|c>
  84. }
  85. @Test
  86. public void testIsInsideTagGtInAttr(){
  87. String text = "<a b='>'>";
  88. assertTrue( TagParser.isInsideTag(text,1)); // <|a b='>'>
  89. assertTrue( TagParser.isInsideTag(text,6)); // <a b='|>'>
  90. assertTrue(TagParser.isInsideTag(text,7)); // <a b='>|'>
  91. assertTrue(TagParser.isInsideTag(text,8)); // <a b='>'|>
  92. assertFalse(TagParser.isInsideTag(text,9)); // <a b='>'>|
  93. }
  94. /* currently, TagParser does not distinguish between end of tag and greater than,
  95. * so the test fails
  96. */
  97. @Test
  98. public void testGetTagAtOffsetGtInAttr(){
  99. String text = "<a b='>'> ";
  100. assertNotNull( TagParser.getTagAtOffset(text,1)); // <|a b='>'>
  101. assertNotNull( TagParser.getTagAtOffset(text,6)); // <a b='|>'>
  102. assertNotNull( TagParser.getTagAtOffset(text,7)); // <a b='>|'>
  103. assertNotNull( TagParser.getTagAtOffset(text,8)); // <a b='>'|>
  104. assertNotNull( TagParser.getTagAtOffset(text,9)); // <a b='>'>|
  105. assertNull( TagParser.getTagAtOffset(text,10)); // <a b='>'> |
  106. }
  107. /* success */
  108. @Test
  109. public void testGetTagAtOffsetSimple(){
  110. String text = "<a> </a> <b c='d'/><br/>";
  111. Tag t;
  112. assertNull(TagParser.getTagAtOffset(text,-1));
  113. assertNull(TagParser.getTagAtOffset(text,text.length()+1));
  114. assertNull(TagParser.getTagAtOffset("short <",7));
  115. t = TagParser.getTagAtOffset(text,1); // <|a> </a> <b c='d'/><br/>
  116. assertNotNull(t);
  117. assertEquals(0,t.start);
  118. assertEquals(3,t.end);
  119. assertEquals("a",t.tag);
  120. assertEquals(T_START_TAG,t.type);
  121. t = TagParser.getTagAtOffset(text,2); // <a|> </a> <b c='d'/><br/>
  122. assertNotNull(t);
  123. assertEquals(0,t.start);
  124. assertEquals(3,t.end);
  125. assertEquals("a",t.tag);
  126. assertEquals(T_START_TAG,t.type);
  127. t = TagParser.getTagAtOffset(text,2); // <a>| </a> <b c='d'/><br/>
  128. assertNotNull(t);
  129. assertEquals(0,t.start);
  130. assertEquals(3,t.end);
  131. assertEquals("a",t.tag);
  132. assertEquals(T_START_TAG,t.type);
  133. t = TagParser.getTagAtOffset(text,4); // <a> |</a> <b c='d'/><br/>
  134. assertNull(t);
  135. t = TagParser.getTagAtOffset(text,5); // <a> <|/a> <b c='d'/><br/>
  136. assertNotNull(t);
  137. assertEquals(4,t.start);
  138. assertEquals(8,t.end);
  139. assertEquals("a",t.tag);
  140. assertEquals(T_END_TAG,t.type);
  141. t = TagParser.getTagAtOffset(text,8); // <a> </a>| <b c='d'/><br/>
  142. assertNotNull(t);
  143. assertEquals(4,t.start);
  144. assertEquals(8,t.end);
  145. assertEquals("a",t.tag);
  146. assertEquals(T_END_TAG,t.type);
  147. t = TagParser.getTagAtOffset(text,12); // <a> </a> <b |c='d'/><br/>
  148. assertNotNull(t);
  149. assertEquals(9,t.start);
  150. assertEquals(19,t.end);
  151. assertEquals("b",t.tag);
  152. assertEquals(T_STANDALONE_TAG,t.type);
  153. t = TagParser.getTagAtOffset(text,19); // <a> </a> <b c='d'/>|<br/>
  154. assertNotNull(t);
  155. assertEquals(9,t.start);
  156. assertEquals(19,t.end);
  157. assertEquals("b",t.tag);
  158. assertEquals(T_STANDALONE_TAG,t.type);
  159. t = TagParser.getTagAtOffset(text,22); // <a> </a> <b c='d'/><br|/>
  160. assertNotNull(t);
  161. assertEquals(19,t.start);
  162. assertEquals(24,t.end);
  163. assertEquals("br",t.tag);
  164. assertEquals(T_STANDALONE_TAG,t.type);
  165. }
  166. /* success */
  167. @Test
  168. public void testGetTagAtOffsetComment(){
  169. String text = "<a> <!-- comment --> </a>";
  170. Tag t;
  171. t = TagParser.getTagAtOffset(text,1); // <|a> <!-- comment --> </a>";
  172. assertNotNull(t);
  173. assertNull(TagParser.getTagAtOffset(text,5)); // <a> <|!-- comment --> </a>";
  174. assertNull(TagParser.getTagAtOffset(text,6)); // <a> <!|-- comment --> </a>";
  175. assertNull(TagParser.getTagAtOffset(text,10)); // <a> <!-- c|omment --> </a>";
  176. assertNull(TagParser.getTagAtOffset(text,19)); // <a> <!-- comment --> </a>";
  177. assertNull(TagParser.getTagAtOffset(text,20)); // <a> <!-- comment --> </a>";
  178. }
  179. /* success */
  180. @Test
  181. public void testGetTagAtOffsetNotClosed(){
  182. String text = "<a <b> <c d='>";
  183. Tag t;
  184. t = TagParser.getTagAtOffset(text,1); // <|a <b> <c d='>
  185. assertNull(t);
  186. t = TagParser.getTagAtOffset(text,4); // <a <|b> <c d='>
  187. assertNotNull(t);
  188. assertEquals(3,t.start);
  189. assertEquals(6,t.end);
  190. assertEquals("b",t.tag);
  191. assertEquals(T_START_TAG,t.type);
  192. t = TagParser.getTagAtOffset(text,8); // <a <b> <|c d='>
  193. assertNull(t);
  194. }
  195. @Test
  196. public void testGetMatchingTagSimple(){
  197. String text = "<a> <b/> <c d='toto'> titi </c> </a>";
  198. Tag f;
  199. Tag t;
  200. // find closing tag for <a>
  201. f = new Tag(0,3);
  202. f.tag = "a";
  203. f.type = T_START_TAG;
  204. t = TagParser.getMatchingTag(text, f); // <|a> <b/> <c d='toto'> titi </c> </a>
  205. assertNotNull(t);
  206. assertEquals(32,t.start);
  207. assertEquals(36,t.end);
  208. assertEquals("a",t.tag);
  209. assertEquals(T_END_TAG,t.type);
  210. // find opening tag for </a>
  211. t = TagParser.getMatchingTag(text, t); // <a> <b/> <c d='toto'> titi </c> </|a>
  212. assertNotNull(t);
  213. assertEquals(0,t.start);
  214. assertEquals(3,t.end);
  215. assertEquals("a",t.tag);
  216. assertEquals(T_START_TAG,t.type);
  217. // no matching tag for standalone
  218. f = new Tag(4,8);
  219. f.tag = "b";
  220. f.type = T_STANDALONE_TAG;
  221. t = TagParser.getMatchingTag(text, f); // <a> <|b/> <c d='toto'> titi </c> </a>
  222. assertNull(t);
  223. }
  224. @Test
  225. public void testGetMatchingTagComment(){
  226. String text = "<a> <!-- </a> <a> --> </a>";
  227. Tag f;
  228. Tag t;
  229. // find closing tag for <a>, ignoring the commented one
  230. f = new Tag(0,3);
  231. f.tag = "a";
  232. f.type = T_START_TAG;
  233. t = TagParser.getMatchingTag(text, f);
  234. assertNotNull(t);
  235. assertEquals(22,t.start);
  236. assertEquals(26,t.end);
  237. assertEquals("a",t.tag);
  238. assertEquals(T_END_TAG,t.type);
  239. // find opening tag for </a>, ignoring the commented one
  240. t = TagParser.getMatchingTag(text, t);
  241. assertNotNull(t);
  242. assertEquals(0,t.start);
  243. assertEquals(3,t.end);
  244. assertEquals("a",t.tag);
  245. assertEquals(T_START_TAG,t.type);
  246. }
  247. @Test
  248. public void testGetMatchingTagRecurse(){
  249. String text = "<a> <a d='toto'> </a> </a>";
  250. Tag f;
  251. Tag t;
  252. // find closing tag for <a>
  253. f = new Tag(0,3);
  254. f.tag = "a";
  255. f.type = T_START_TAG;
  256. t = TagParser.getMatchingTag(text, f);
  257. assertNotNull(t);
  258. assertEquals(22,t.start);
  259. assertEquals(26,t.end);
  260. assertEquals("a",t.tag);
  261. assertEquals(T_END_TAG,t.type);
  262. t = TagParser.getMatchingTag(text, t);
  263. assertNotNull(t);
  264. assertEquals(0,t.start);
  265. assertEquals(3,t.end);
  266. assertEquals("a",t.tag);
  267. assertEquals(T_START_TAG,t.type);
  268. }
  269. @Test
  270. public void testGetMatchingTagNoClose(){
  271. String text = "<a> </b>";
  272. Tag f;
  273. Tag t;
  274. // find closing tag for <a> (not there)
  275. f = new Tag(0,3);
  276. f.tag = "a";
  277. f.type = T_START_TAG;
  278. t = TagParser.getMatchingTag(text, f);
  279. assertNull(t);
  280. // find opening tag for </b> (not there)
  281. f = new Tag(5,9);
  282. f.tag = "b";
  283. f.type = T_END_TAG;
  284. t = TagParser.getMatchingTag(text, f);
  285. assertNull(t);
  286. }
  287. @Test
  288. public void testGetMatchingTagGt(){
  289. String text = "<a> a>b </a>";
  290. Tag f;
  291. Tag t;
  292. // find closing tag for <a> (ignoring a>b)
  293. f = new Tag(0,3);
  294. f.tag = "a";
  295. f.type = T_START_TAG;
  296. t = TagParser.getMatchingTag(text, f);
  297. assertNotNull(t);
  298. assertEquals(8,t.start);
  299. assertEquals(12,t.end);
  300. assertEquals("a",t.tag);
  301. assertEquals(T_END_TAG,t.type);
  302. // find opening tag for </a> (ignoring a>b)
  303. t = TagParser.getMatchingTag(text, t);
  304. assertNotNull(t);
  305. assertEquals(0,t.start);
  306. assertEquals(3,t.end);
  307. assertEquals("a",t.tag);
  308. assertEquals(T_START_TAG,t.type);
  309. }
  310. @Test
  311. public void testFindLastOpenTagXML(){
  312. final File in = new File(testData,"tagparser/test.xml");
  313. Buffer b = openFile(in.getPath());
  314. // wait for end of parsing
  315. doInBetween(new Runnable(){
  316. public void run(){
  317. action("sidekick-parse");
  318. }},
  319. messageOfClassCondition(sidekick.SideKickUpdate.class),
  320. 10000);
  321. XmlParsedData data = getXmlParsedData();
  322. String text = b.getText(0, b.getLength());
  323. Tag t;
  324. t = TagParser.findLastOpenTag(text, 27 , data); //line 2: <p:p| xmlns:p="urn:p:">
  325. assertNull(t);
  326. t = TagParser.findLastOpenTag(text, 45 , data); //line 2: <p:p xmlns:p="urn:p:">|
  327. assertNotNull(t);
  328. assertEquals(23,t.start);
  329. assertEquals(45,t.end);
  330. assertEquals("p:p",t.tag);
  331. assertEquals(T_START_TAG,t.type);
  332. t = TagParser.findLastOpenTag(text, 49 , data); //line 3: <p:|eqn>a>b</p:eqn>
  333. assertNotNull(t);
  334. assertEquals(23,t.start);
  335. assertEquals(45,t.end);
  336. assertEquals("p:p",t.tag);
  337. assertEquals(T_START_TAG,t.type);
  338. t = TagParser.findLastOpenTag(text, 55 , data); //line 3: <p:eqn>a>|b</p:eqn>
  339. assertNotNull(t);
  340. assertEquals(46,t.start);
  341. assertEquals(53,t.end);
  342. assertEquals("p:eqn",t.tag);
  343. assertEquals(T_START_TAG,t.type);
  344. t = TagParser.findLastOpenTag(text, 84 , null); //line 4: <!-- some commented| <open>tag -->
  345. assertNotNull(t);
  346. assertEquals(23,t.start);
  347. assertEquals(45,t.end);
  348. assertEquals("p:p",t.tag);
  349. assertEquals(T_START_TAG,t.type);
  350. // inside comments, matching still works but not after the comment
  351. t = TagParser.findLastOpenTag(text, 94 , data); //line 4: <!-- some commented <open>tag| -->
  352. assertNotNull(t);
  353. assertEquals(85,t.start);
  354. assertEquals(91,t.end);
  355. assertEquals("open",t.tag);
  356. assertEquals(T_START_TAG,t.type);
  357. t = TagParser.findLastOpenTag(text, 114 , null); //line 6: |<attr c="a>d"/>
  358. assertNotNull(t);
  359. assertEquals(23,t.start);
  360. assertEquals(45,t.end);
  361. assertEquals("p:p",t.tag);
  362. assertEquals(T_START_TAG,t.type);
  363. // FAILS because of the GT !
  364. t = TagParser.findLastOpenTag(text, 125 , data); //line 6: <attr c="a>|d"/>
  365. assertNotNull(t);
  366. assertEquals(23,t.start);
  367. assertEquals(45,t.end);
  368. assertEquals("p:p",t.tag);
  369. assertEquals(T_START_TAG,t.type);
  370. close(view(),b);
  371. }
  372. @Test
  373. public void testFindLastOpenTagHTML(){
  374. final File in = new File(testData,"tagparser/test.html");
  375. Buffer b = openParseAndWait(in.getPath());
  376. XmlParsedData data = getXmlParsedData();
  377. String text = b.getText(0, b.getLength());
  378. Tag t;
  379. // after the img (standalone)
  380. t = TagParser.findLastOpenTag(text, 56 , data); //line 5: na|vig
  381. assertNotNull(t);
  382. assertEquals(22,t.start);
  383. assertEquals(37,t.end);
  384. assertEquals("DIV",t.tag);
  385. assertEquals(T_START_TAG,t.type);
  386. // after the embedded div (recursion)
  387. t = TagParser.findLastOpenTag(text, 67 , data); //line 7: |
  388. assertNotNull(t);
  389. assertEquals(7,t.start);
  390. assertEquals(21,t.end);
  391. assertEquals("div",t.tag);
  392. assertEquals(T_START_TAG,t.type);
  393. // in HTML mode, tags are lower-cased for comparison, but not when returned
  394. t = TagParser.findLastOpenTag(text, 85 , data); //line 9: <p>hello <br>|</br>
  395. assertNotNull(t);
  396. assertEquals(68,t.start);
  397. assertEquals(71,t.end);
  398. assertEquals("P",t.tag);
  399. assertEquals(T_START_TAG,t.type);
  400. close(view(),b);
  401. }
  402. @Test
  403. public void testFindLastOpenTagMalformed(){
  404. String text = "<html><p> not closed</html> ";
  405. Tag t;
  406. // after the closing html, to trigger the if(unwindIndex == -1) return tag; line 190 of TagParser
  407. t = TagParser.findLastOpenTag(text, 28 , null); // <html><p> not closed</html>|
  408. assertNotNull(t);
  409. assertEquals(6,t.start);
  410. assertEquals(9,t.end);
  411. assertEquals("p",t.tag);
  412. assertEquals(T_START_TAG,t.type);
  413. }
  414. @Test
  415. public void testGetAttrsContents()
  416. {
  417. String text = "<a title= 'a \"nice\" title' a:href=\"../test\" xmlns:a\n= 'urn:a&lt;b'>";
  418. Tag t;
  419. t = new Tag(0,text.length());
  420. t.tag="a";
  421. t.type=T_START_TAG;
  422. List<Attr> attrs = TagParser.getAttrs(text,t);
  423. assertEquals(3,attrs.size());
  424. Attr a;
  425. a = attrs.get(0);
  426. assertEquals("title",a.name);
  427. assertEquals("'a \"nice\" title'",a.val);
  428. a = attrs.get(1);
  429. assertEquals("a:href",a.name);
  430. assertEquals("\"../test\"",a.val);
  431. a = attrs.get(2);
  432. assertEquals("xmlns:a",a.name);
  433. assertEquals("'urn:a&lt;b'",a.val);
  434. }
  435. @Test
  436. public void testGetAttrsStandaloneAndEmpty()
  437. {
  438. String text = "<a><img src='i.png'/></a>";
  439. Tag t;
  440. t = new Tag(0,3);
  441. t.tag="a";
  442. t.type=T_START_TAG;
  443. List<Attr> attrs;
  444. Attr a;
  445. attrs = TagParser.getAttrs(text,t);
  446. assertEquals(0,attrs.size());
  447. t = new Tag(3,21);
  448. t.tag="img";
  449. t.type=T_STANDALONE_TAG;
  450. attrs = TagParser.getAttrs(text,t);
  451. assertEquals(1,attrs.size());
  452. a = attrs.get(0);
  453. assertEquals("src",a.name);
  454. assertEquals("'i.png'",a.val);
  455. t = new Tag(21,25);
  456. t.tag="a";
  457. t.type=T_END_TAG;
  458. attrs = TagParser.getAttrs(text,t);
  459. assertEquals(0,attrs.size());
  460. }
  461. @Test
  462. public void testGetAttrsMalformed()
  463. {
  464. String text = "<i a=='b'>";
  465. Tag t;
  466. t = new Tag(0,10);
  467. t.tag="i";
  468. t.type=T_START_TAG;
  469. List<Attr> attrs;
  470. Attr a;
  471. attrs = TagParser.getAttrs(text,t);
  472. assertEquals(1,attrs.size());
  473. a = attrs.get(0);
  474. assertEquals("a",a.name);
  475. assertEquals("'b'",a.val);
  476. text = "<j 'no name'>";
  477. t = new Tag(0,13);
  478. t.tag="j";
  479. t.type=T_START_TAG;
  480. attrs = TagParser.getAttrs(text,t);
  481. System.err.println(attrs);
  482. assertEquals(0,attrs.size());
  483. text= "<k l='1' 'not closed>";
  484. t = new Tag(0,21);
  485. t.tag="k";
  486. t.type=T_START_TAG;
  487. attrs = TagParser.getAttrs(text,t);
  488. assertEquals(1,attrs.size());
  489. a = attrs.get(0);
  490. assertEquals("l",a.name);
  491. assertEquals("'1'",a.val);
  492. text = "<l &attrRef;>";
  493. t = new Tag(0,13);
  494. t.tag="l";
  495. t.type=T_START_TAG;
  496. attrs = TagParser.getAttrs(text,t);
  497. assertEquals(0,attrs.size());
  498. }
  499. }