PageRenderTime 140ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/pirka/src/test/java/org/pirkaengine/core/parser/StAXXmlParserTest.java

http://pirka.googlecode.com/
Java | 637 lines | 531 code | 56 blank | 50 comment | 1 complexity | 481a1836c34df6802bc3ebf01bc34f3f MD5 | raw file
  1. package org.pirkaengine.core.parser;
  2. import static org.hamcrest.CoreMatchers.*;
  3. import static org.junit.Assert.*;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.StringReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.charset.Charset;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.pirkaengine.core.PrkAttribute;
  13. import org.pirkaengine.core.PrkElement;
  14. /**
  15. * ??????.
  16. *
  17. * @author shuji
  18. */
  19. public class StAXXmlParserTest {
  20. private StAXXmlParser target;
  21. @Before
  22. public void setup() {
  23. target = new StAXXmlParser();
  24. }
  25. @Test
  26. public void parse_Text() throws ParseException {
  27. String text = "<xhtml><body>Text ????</body></xhtml>";
  28. XhtmlStruct actual = target.parse(getInput(text));
  29. assertThat(actual.getFragments(), is(expected(Fragment.create(0, Fragment.Type.TEXT).build())));
  30. assertThat(actual.getText().toString(), is(text));
  31. }
  32. @Test
  33. public void parse_Text_ShiftJIS() throws ParseException, UnsupportedEncodingException {
  34. Charset charset = Charset.forName("Windows-31J");
  35. String text = "<xhtml><body>Text ????</body></xhtml>";
  36. ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes(charset));
  37. XhtmlStruct actual = target.parse(input, charset);
  38. assertThat(actual.getFragments(), is(expected(Fragment.create(0, Fragment.Type.TEXT).build())));
  39. assertThat(actual.getText().toString(), is(text));
  40. }
  41. @Test
  42. public void parse_Expression() throws ParseException {
  43. String text = "<xhtml><body>${hoge}</body></xhtml>";
  44. XhtmlStruct actual = target.parse(getInput(text));
  45. assertThat(
  46. actual.getFragments(),
  47. is(expected(Fragment.create(20, Fragment.Type.TEXT).build(),
  48. Fragment.create(13, Fragment.Type.EXPRESSION).build(), Fragment.create(0, Fragment.Type.TEXT)
  49. .build())));
  50. assertThat(actual.getText().toString(), is(text));
  51. }
  52. @Test
  53. public void parse_Expression_mix() throws ParseException {
  54. String text = "<xhtml><body>xxx<a>${hoge}</a></body></xhtml>";
  55. XhtmlStruct actual = target.parse(getInput(text));
  56. assertThat(
  57. actual.getFragments(),
  58. is(expected(Fragment.create(26, Fragment.Type.TEXT).build(),
  59. Fragment.create(19, Fragment.Type.EXPRESSION).build(), Fragment.create(0, Fragment.Type.TEXT)
  60. .build())));
  61. assertThat(actual.getText().toString(), is(text));
  62. }
  63. @Test
  64. public void parse_Expression_mix2() throws ParseException {
  65. String text = "<xhtml><body>\r\n01234<a href=\"aa\">aaa</a>${hoge}</body></xhtml>";
  66. XhtmlStruct actual = target.parse(getInput(text));
  67. assertThat(
  68. actual.getFragments(),
  69. is(expected(Fragment.create(47, Fragment.Type.TEXT).build(),
  70. Fragment.create(40, Fragment.Type.EXPRESSION).build(), Fragment.create(0, Fragment.Type.TEXT)
  71. .build())));
  72. assertThat(actual.getText().toString(), is(text));
  73. }
  74. @Test
  75. public void parse_Expression_mix3() throws ParseException {
  76. String text = "<xhtml><body>\r\n${hoge}<a>aaa</a>ccc</body></xhtml>";
  77. XhtmlStruct actual = target.parse(getInput(text));
  78. assertThat(
  79. actual.getFragments(),
  80. is(expected(Fragment.create(22, Fragment.Type.TEXT).build(),
  81. Fragment.create(15, Fragment.Type.EXPRESSION).build(), Fragment.create(0, Fragment.Type.TEXT)
  82. .build())));
  83. assertThat(actual.getText().toString(), is(text));
  84. }
  85. @Test
  86. public void parse_Content() throws ParseException {
  87. String text = "<html xmlns:prk=\"http://www.hoge/prk\"><body><div prk:content=\"text\">aaaaaaaaaaa</div></body></html>";
  88. XhtmlStruct actual = target.parse(getInput(text));
  89. assertThat(
  90. actual.getFragments(),
  91. is(expected(Fragment.create(85, Fragment.Type.TEXT).build(), Fragment.create(79, Fragment.Type.TAG_END)
  92. .build(), Fragment.create(68, Fragment.Type.TAG_BODY).build(),
  93. Fragment.create(44, Fragment.Type.TAG_START).append("content", "text").build(), Fragment
  94. .create(0, Fragment.Type.TEXT).build())));
  95. assertThat(actual.getText().toString(), is(text));
  96. }
  97. @Test
  98. public void parse_If() throws ParseException {
  99. String text = "<html xmlns:prk=\"http://www.hoge/prk\"><body><p prk:if=\"foo\">bar</p></body></html>";
  100. XhtmlStruct actual = target.parse(getInput(text));
  101. assertThat(
  102. actual.getFragments(),
  103. is(expected(Fragment.create(67, Fragment.Type.TEXT).build(), Fragment.create(63, Fragment.Type.TAG_END)
  104. .build(), Fragment.create(60, Fragment.Type.TAG_BODY).build(),
  105. Fragment.create(44, Fragment.Type.TAG_START).append("if", "foo").build(),
  106. Fragment.create(0, Fragment.Type.TEXT).build())));
  107. assertThat(actual.getText().toString(), is(text));
  108. }
  109. @Test
  110. public void parse_If_Expression() throws ParseException {
  111. String text = "<html xmlns:prk=\"http://www.hoge/prk\"><body><p prk:if=\"foo\">${bar}</p></body></html>";
  112. XhtmlStruct actual = target.parse(getInput(text));
  113. assertThat(
  114. actual.getFragments(),
  115. is(expected(Fragment.create(70, Fragment.Type.TEXT).build(), Fragment.create(66, Fragment.Type.TAG_END)
  116. .build(), Fragment.create(60, Fragment.Type.EXPRESSION).build(),
  117. Fragment.create(44, Fragment.Type.TAG_START).append("if", "foo").build(),
  118. Fragment.create(0, Fragment.Type.TEXT).build())));
  119. assertThat(actual.getText().toString(), is(text));
  120. }
  121. @Test
  122. public void parse_For_Expression() throws ParseException {
  123. String text = "<html xmlns:prk=\"http://www.hoge/prk\"><body><p prk:for=\"item in items\">${item}</p></body></html>";
  124. XhtmlStruct actual = target.parse(getInput(text));
  125. assertThat(
  126. actual.getFragments(),
  127. is(expected(Fragment.create(82, Fragment.Type.TEXT).build(), Fragment.create(78, Fragment.Type.TAG_END)
  128. .build(), Fragment.create(71, Fragment.Type.EXPRESSION).build(),
  129. Fragment.create(44, Fragment.Type.TAG_START).append("for", "item in items").build(), Fragment
  130. .create(0, Fragment.Type.TEXT).build())));
  131. assertThat(actual.getText().toString(), is(text));
  132. }
  133. @Test
  134. public void parse_For2_Expression() throws ParseException {
  135. String text = "<html xmlns:prk=\"http://www.hoge/prk\"><body><p prk:for=\"item in items\"><span>${item.name}</span><span>${item.price}</span></p></body></html>";
  136. XhtmlStruct actual = target.parse(getInput(text));
  137. assertThat(
  138. actual.getFragments(),
  139. is(expected(Fragment.create(126, Fragment.Type.TEXT).build(),
  140. Fragment.create(122, Fragment.Type.TAG_END).build(), Fragment.create(115, Fragment.Type.TEXT)
  141. .build(), Fragment.create(102, Fragment.Type.EXPRESSION).build(),
  142. Fragment.create(89, Fragment.Type.TEXT).build(), Fragment.create(77, Fragment.Type.EXPRESSION)
  143. .build(), Fragment.create(71, Fragment.Type.TEXT).build(),
  144. Fragment.create(44, Fragment.Type.TAG_START).append("for", "item in items").build(), Fragment
  145. .create(0, Fragment.Type.TEXT).build())));
  146. assertThat(actual.getText().toString(), is(text));
  147. }
  148. @Test
  149. public void parse_Escape() throws ParseException {
  150. String text = "<html><body>AA$${name}AA</body></html>";
  151. XhtmlStruct actual = target.parse(getInput(text));
  152. assertThat(
  153. actual.getFragments(),
  154. is(expected(Fragment.create(16, Fragment.Type.TEXT).build(), Fragment.create(14, Fragment.Type.ESCAPE)
  155. .build(), Fragment.create(0, Fragment.Type.TEXT).build())));
  156. assertThat(actual.getText().toString(), is(text));
  157. }
  158. @Test
  159. public void parse_Escape_WithHTMLEscape() throws ParseException {
  160. String text = "<html><body>&lt;p&gt;$${name}&lt;/p&gt;</body></html>";
  161. XhtmlStruct actual = target.parse(getInput(text));
  162. assertThat(
  163. actual.getFragments(),
  164. is(expected(Fragment.create(23, Fragment.Type.TEXT).build(), Fragment.create(21, Fragment.Type.ESCAPE)
  165. .build(), Fragment.create(0, Fragment.Type.TEXT).build())));
  166. assertThat(actual.getText().toString(), is(text));
  167. }
  168. @Test
  169. public void parse_EmptyTag() throws ParseException {
  170. String text = "<html><body><br/>${test}</body></html>";
  171. XhtmlStruct actual = target.parse(getInput(text));
  172. assertThat(
  173. actual.getFragments(),
  174. is(expected(Fragment.create(24, Fragment.Type.TEXT).build(),
  175. Fragment.create(17, Fragment.Type.EXPRESSION).build(), Fragment.create(0, Fragment.Type.TEXT)
  176. .build())));
  177. assertThat(actual.getText().toString(), is(text));
  178. }
  179. @Test(expected = ParseException.class)
  180. public void parse_InvalidXhtml() throws ParseException {
  181. String text = "<xhtml><body>Text ????<body></xhtml>";
  182. target.parse(getInput(text));
  183. }
  184. @Test(expected = ParseException.class)
  185. public void parse_InvalidprkDef() throws ParseException {
  186. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:def /></html>";
  187. target.parse(getInput(text));
  188. }
  189. @Test(expected = ParseException.class)
  190. public void parse_InvalidprkDef_language??() throws ParseException {
  191. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:def name=\"hoge\"><![CDATA[//]]></prk:def></html>";
  192. target.parse(getInput(text));
  193. }
  194. @Test(expected = ParseException.class)
  195. public void parse_InvalidprkDef_name??() throws ParseException {
  196. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:def language=\"hoge\"><![CDATA[//]]></prk:def></html>";
  197. target.parse(getInput(text));
  198. }
  199. @Test(expected = ParseException.class)
  200. public void parse_InvalidprkDef_?????() throws ParseException {
  201. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:def language=\"hoge\" name=\"hoge\"><b></b></prk:def></html>";
  202. target.parse(getInput(text));
  203. }
  204. /**
  205. * ?????for
  206. *
  207. * @throws ParseException
  208. */
  209. @Test
  210. public void parse_ForNest() throws ParseException {
  211. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><body><p prk:for=\"item in items\"><span prk:content=\"item\">xx</span></p></body></html>";
  212. XhtmlStruct actual = target.parse(getInput(text));
  213. assertThat(
  214. actual.getFragments(),
  215. is(expected(Fragment.create(116, Fragment.Type.TEXT).build(),
  216. Fragment.create(112, Fragment.Type.TAG_END).build(), Fragment
  217. .create(105, Fragment.Type.TAG_END).build(),
  218. Fragment.create(103, Fragment.Type.TAG_BODY).build(),
  219. Fragment.create(78, Fragment.Type.TAG_START).append("content", "item").build(), Fragment
  220. .create(51, Fragment.Type.TAG_START).append("for", "item in items").build(), Fragment
  221. .create(0, Fragment.Type.TEXT).build())));
  222. assertThat(actual.getText().toString(), is(text));
  223. }
  224. /**
  225. * ???????
  226. *
  227. * @throws ParseException
  228. */
  229. @Test
  230. public void parse_NestAttrs() throws ParseException {
  231. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><body><p prk:if=\"is_login\" prk:attr.class=\"welcome_class\">????</p></body></html>";
  232. XhtmlStruct actual = target.parse(getInput(text));
  233. assertThat(
  234. actual.getFragments(),
  235. is(expected(
  236. Fragment.create(111, Fragment.Type.TEXT).build(),
  237. Fragment.create(107, Fragment.Type.TAG_END).build(),
  238. Fragment.create(103, Fragment.Type.TAG_BODY).build(),
  239. Fragment.create(51, Fragment.Type.TAG_START).append("if", "is_login")
  240. .appendPrkAttrs(Fragment.attr("attr.class", "welcome_class")).build(),
  241. Fragment.create(0, Fragment.Type.TEXT).build())));
  242. assertThat(actual.getText().toString(), is(text));
  243. }
  244. /**
  245. * ????
  246. *
  247. * @throws ParseException
  248. */
  249. @Test
  250. public void parse_MultiAttrs() throws ParseException {
  251. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><a prk:attr.href=\"url\" prk:attr.title=\"title\"></a></html>";
  252. XhtmlStruct actual = target.parse(getInput(text));
  253. assertThat(
  254. actual.getFragments(),
  255. is(expected(
  256. Fragment.create(95, Fragment.Type.TEXT).build(),
  257. Fragment.create(91, Fragment.Type.TAG_END).build(),
  258. Fragment.create(45, Fragment.Type.TAG_START)
  259. .append(PrkAttribute.ATTR.name, PrkAttribute.ATTR.name)
  260. .appendPrkAttrs(Fragment.attr("attr.href", "url"), Fragment.attr("attr.title", "title"))
  261. .build(), Fragment.create(0, Fragment.Type.TEXT).build())));
  262. assertThat(actual.getText().toString(), is(text));
  263. }
  264. /**
  265. * path??
  266. *
  267. * @throws ParseException
  268. */
  269. @Test
  270. public void parse_Path() throws ParseException {
  271. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><a prk:path.href=\"index.html\">xxx</a></html>";
  272. XhtmlStruct actual = target.parse(getInput(text));
  273. assertThat(
  274. actual.getFragments(),
  275. is(expected(
  276. Fragment.create(82, Fragment.Type.TEXT).build(),
  277. Fragment.create(78, Fragment.Type.TAG_END).build(),
  278. Fragment.create(75, Fragment.Type.TAG_BODY).build(),
  279. Fragment.create(45, Fragment.Type.TAG_START)
  280. .append(PrkAttribute.ATTR.name, PrkAttribute.ATTR.name)
  281. .appendPathAttrs(Fragment.attr("path.href", "index.html")).build(),
  282. Fragment.create(0, Fragment.Type.TEXT).build())));
  283. assertThat(actual.getText().toString(), is(text));
  284. }
  285. /**
  286. * ???
  287. *
  288. * @throws ParseException
  289. */
  290. @Test
  291. public void parse_empty_element() throws ParseException {
  292. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><img prk:attr.src=\"img_src\" /></html>";
  293. XhtmlStruct actual = target.parse(getInput(text));
  294. assertThat(
  295. actual.getFragments(),
  296. is(expected(
  297. Fragment.create(75, Fragment.Type.TEXT).build(),
  298. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  299. .append(PrkAttribute.ATTR.name, PrkAttribute.ATTR.name)
  300. .appendPrkAttrs(Fragment.attr("attr.src", "img_src")).build(),
  301. Fragment.create(0, Fragment.Type.TEXT).build())));
  302. assertThat(actual.getText().toString(), is(text));
  303. }
  304. /**
  305. * ???prk??
  306. *
  307. * @throws ParseException
  308. */
  309. @Test(expected = ParseException.class)
  310. public void parse_Invalid_prkTag() throws ParseException {
  311. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><body><prk:poo></prk:poo></body></html>";
  312. target.parse(getInput(text));
  313. }
  314. @Test
  315. public void parse_complexAttr() throws ParseException {
  316. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><a prk:attr.href=\"update/${value}\"></a></html>";
  317. XhtmlStruct actual = target.parse(getInput(text));
  318. assertThat(
  319. actual.getFragments(),
  320. is(expected(
  321. Fragment.create(84, Fragment.Type.TEXT).build(),
  322. Fragment.create(80, Fragment.Type.TAG_END).build(),
  323. Fragment.create(45, Fragment.Type.TAG_START)
  324. .append(PrkAttribute.ATTR.name, PrkAttribute.ATTR.name)
  325. .appendPrkAttrs(Fragment.attr("attr.href", "update/${value}")).build(), Fragment
  326. .create(0, Fragment.Type.TEXT).build())));
  327. assertThat(actual.getText().toString(), is(text));
  328. }
  329. @Test
  330. public void parse_attr_when() throws ParseException {
  331. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><option selected=\"selected\" prk:attr.selected.when=\"cond\" /></html>";
  332. XhtmlStruct actual = target.parse(getInput(text));
  333. assertThat(
  334. actual.getFragments(),
  335. is(expected(
  336. Fragment.create(105, Fragment.Type.TEXT).build(),
  337. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  338. .append(PrkAttribute.ATTR.name, PrkAttribute.ATTR.name)
  339. .appendPrkAttrs(Fragment.attr("attr.selected.when", "cond"))
  340. .appendAttrs(Fragment.attr("selected", "selected")).build(),
  341. Fragment.create(0, Fragment.Type.TEXT).build())));
  342. assertThat(actual.getText().toString(), is(text));
  343. }
  344. @Test
  345. public void parse_prk_component() throws ParseException {
  346. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" /></html>";
  347. XhtmlStruct actual = target.parse(getInput(text));
  348. assertThat(
  349. actual.getFragments(),
  350. is(expected(
  351. Fragment.create(76, Fragment.Type.TEXT).build(),
  352. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  353. .append(PrkElement.COMPONENT.name, "plugin").build(),
  354. Fragment.create(0, Fragment.Type.TEXT).build())));
  355. assertThat(actual.getText().toString(), is(text));
  356. }
  357. @Test
  358. public void parse_prk_component_withParam() throws ParseException {
  359. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" ><prk:param key=\"name\" value=\"FooPlugin\" /></prk:component></html>";
  360. XhtmlStruct actual = target.parse(getInput(text));
  361. assertThat(
  362. actual.getFragments(),
  363. is(expected(
  364. Fragment.create(133, Fragment.Type.TEXT).build(),
  365. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  366. .append(PrkElement.COMPONENT.name, "plugin")
  367. .appendAttrs(Fragment.attr("name", "FooPlugin")).build(),
  368. Fragment.create(0, Fragment.Type.TEXT).build())));
  369. assertThat(actual.getText().toString(), is(text));
  370. }
  371. @Test
  372. public void parse_prk_component_withParams() throws ParseException {
  373. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" ><prk:param key=\"name\" value=\"FooPlugin\" /><prk:param key=\"var\" value=\"foo\" /></prk:component></html>";
  374. XhtmlStruct actual = target.parse(getInput(text));
  375. assertThat(
  376. actual.getFragments(),
  377. is(expected(
  378. Fragment.create(168, Fragment.Type.TEXT).build(),
  379. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  380. .append(PrkElement.COMPONENT.name, "plugin")
  381. .appendAttrs(Fragment.attr("name", "FooPlugin"), Fragment.attr("var", "foo")).build(),
  382. Fragment.create(0, Fragment.Type.TEXT).build())));
  383. assertThat(actual.getText().toString(), is(text));
  384. }
  385. @Test
  386. public void parse_prk_component_withParams2() throws ParseException {
  387. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" > <prk:param key=\"name\" value=\"FooPlugin\" /> <prk:param key=\"var\" value=\"foo\" /></prk:component></html>";
  388. XhtmlStruct actual = target.parse(getInput(text));
  389. assertThat(
  390. actual.getFragments(),
  391. is(expected(
  392. Fragment.create(172, Fragment.Type.TEXT).build(),
  393. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  394. .append(PrkElement.COMPONENT.name, "plugin")
  395. .appendAttrs(Fragment.attr("name", "FooPlugin"), Fragment.attr("var", "foo")).build(),
  396. Fragment.create(0, Fragment.Type.TEXT).build())));
  397. assertThat(actual.getText().toString(), is(text));
  398. }
  399. @Test
  400. public void parse_prk_component_withParams3() throws ParseException {
  401. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" name=\"FooPlugin\" var=\"foo\" /></html>";
  402. XhtmlStruct actual = target.parse(getInput(text));
  403. assertThat(
  404. actual.getFragments(),
  405. is(expected(
  406. Fragment.create(103, Fragment.Type.TEXT).build(),
  407. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  408. .append(PrkElement.COMPONENT.name, "plugin")
  409. .appendAttrs(Fragment.attr("name", "FooPlugin"), Fragment.attr("var", "foo")).build(),
  410. Fragment.create(0, Fragment.Type.TEXT).build())));
  411. assertThat(actual.getText().toString(), is(text));
  412. }
  413. @Test
  414. public void parse_prk_component_withParams_and_for() throws ParseException {
  415. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:component type=\"plugin\" ><prk:param key=\"name\" value=\"FooPlugin\" /></prk:component><p>AAA</p><p prk:for=\"item in items\">${item}</p></html>";
  416. XhtmlStruct actual = target.parse(getInput(text));
  417. assertThat(
  418. actual.getFragments(),
  419. is(expected(
  420. Fragment.create(181, Fragment.Type.TEXT).build(),
  421. Fragment.create(177, Fragment.Type.TAG_END).build(),
  422. Fragment.create(170, Fragment.Type.EXPRESSION).build(),
  423. Fragment.create(143, Fragment.Type.TAG_START).append("for", "item in items").build(),
  424. Fragment.create(133, Fragment.Type.TEXT).build(),
  425. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  426. .append(PrkElement.COMPONENT.name, "plugin")
  427. .appendAttrs(Fragment.attr("name", "FooPlugin")).build(),
  428. Fragment.create(0, Fragment.Type.TEXT).build())));
  429. assertThat(actual.getText().toString(), is(text));
  430. }
  431. @Test
  432. public void parse_prk_include() throws ParseException {
  433. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:include file=\"foo.html\" /></html>";
  434. XhtmlStruct actual = target.parse(getInput(text));
  435. assertThat(
  436. actual.getFragments(),
  437. is(expected(
  438. Fragment.create(76, Fragment.Type.TEXT).build(),
  439. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS)
  440. .append(PrkElement.INCLUDE.name, "foo.html").build(),
  441. Fragment.create(0, Fragment.Type.TEXT).build())));
  442. assertThat(actual.getText().toString(), is(text));
  443. }
  444. @Test(expected = ParseException.class)
  445. public void parse_prk_include_without_file() throws ParseException {
  446. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:include /></html>";
  447. target.parse(getInput(text));
  448. }
  449. @Test
  450. public void parse_prk_extends() throws ParseException {
  451. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\" prk:extends=\"foo.html\" >aaa</html>";
  452. XhtmlStruct actual = target.parse(getInput(text));
  453. assertThat(
  454. actual.getFragments(),
  455. is(expected(Fragment.create(72, Fragment.Type.TAG_END).build(),
  456. Fragment.create(69, Fragment.Type.TAG_BODY).build(), Fragment
  457. .create(0, Fragment.Type.TAG_START).append(PrkAttribute.EXTENDS.name, "foo.html")
  458. .build())));
  459. assertThat(actual.getText().toString(), is(text));
  460. }
  461. /**
  462. * prk:functions???????
  463. *
  464. * @throws ParseException
  465. */
  466. @Test
  467. public void parse_prk_functions() throws ParseException {
  468. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:functions class=\"foo.bar.Poo\" /></html>";
  469. XhtmlStruct actual = target.parse(getInput(text));
  470. assertThat(
  471. actual.getFragments(),
  472. is(expected(
  473. Fragment.create(82, Fragment.Type.TEXT).build(),
  474. Fragment.create(45, Fragment.Type.FUNCTIONS)
  475. .appendPrkAttrs(Fragment.attr("class", "foo.bar.Poo"), Fragment.attr("name", ""))
  476. .build(), Fragment.create(0, Fragment.Type.TEXT).build())));
  477. assertThat(actual.getText().toString(), is(text));
  478. }
  479. /**
  480. * prk:functions???????
  481. *
  482. * @throws ParseException
  483. */
  484. @Test(expected = ParseException.class)
  485. public void parse_prk_functions_noClass() throws ParseException {
  486. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:functions /></html>";
  487. target.parse(getInput(text));
  488. }
  489. /**
  490. * prk:functions???????
  491. *
  492. * @throws ParseException
  493. */
  494. @Test(expected = ParseException.class)
  495. public void parse_prk_functions_hasElements() throws ParseException {
  496. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:functions class=\"foo.bar.Poo\" >aaa</prk:functions></html>";
  497. target.parse(getInput(text));
  498. }
  499. @Test
  500. public void parse_prk_block() throws ParseException {
  501. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><div prk:block=\"header\"></div></html>";
  502. XhtmlStruct actual = target.parse(getInput(text));
  503. assertThat(
  504. actual.getFragments(),
  505. is(expected(Fragment.create(75, Fragment.Type.TEXT).build(), Fragment.create(69, Fragment.Type.TAG_END)
  506. .build(), Fragment.create(45, Fragment.Type.TAG_START)
  507. .append(PrkAttribute.BLOCK.name, "header").build(), Fragment.create(0, Fragment.Type.TEXT)
  508. .build())));
  509. assertThat(actual.getText().toString(), is(text));
  510. }
  511. @Test
  512. public void parse_Expression_and_prk_block() throws ParseException {
  513. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><body prk:block=\"id\"><p>${hoge}</p></body></html>";
  514. XhtmlStruct actual = target.parse(getInput(text));
  515. assertThat(
  516. actual.getFragments(),
  517. is(expected(Fragment.create(87, Fragment.Type.TEXT).build(), Fragment.create(80, Fragment.Type.TAG_END)
  518. .build(), Fragment.create(76, Fragment.Type.TEXT).build(),
  519. Fragment.create(69, Fragment.Type.EXPRESSION).build(), Fragment.create(66, Fragment.Type.TEXT)
  520. .build(),
  521. Fragment.create(45, Fragment.Type.TAG_START).append(PrkAttribute.BLOCK.name, "id").build(),
  522. Fragment.create(0, Fragment.Type.TEXT).build())));
  523. assertThat(actual.getText().toString(), is(text));
  524. }
  525. @Test
  526. public void parse_prk_for() throws ParseException {
  527. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:for loop=\"item in items\"><span>XXX</span></prk:for></html>";
  528. XhtmlStruct actual = target.parse(getInput(text));
  529. assertThat(
  530. actual.getFragments(),
  531. is(expected(Fragment.create(101, Fragment.Type.TEXT).build(), Fragment
  532. .create(91, Fragment.Type.TAG_END).build(), Fragment.create(75, Fragment.Type.TEXT).build(),
  533. Fragment.create(45, Fragment.Type.TAG_START).append(PrkAttribute.FOR.name, "item in items")
  534. .build(), Fragment.create(0, Fragment.Type.TEXT).build())));
  535. assertThat(actual.getText().toString(), is(text));
  536. }
  537. @Test
  538. public void parse_prk_val() throws ParseException {
  539. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:val name=\"foo\" value=\"xxx\" /></html>";
  540. XhtmlStruct actual = target.parse(getInput(text));
  541. assertThat(
  542. actual.getFragments(),
  543. is(expected(Fragment.create(79, Fragment.Type.TEXT).build(),
  544. Fragment.create(45, Fragment.Type.TAG_EMPTY_ELEMENTS).append(PrkElement.VAL.name, "foo")
  545. .appendAttrs(Fragment.attr("value", "xxx")).build(),
  546. Fragment.create(0, Fragment.Type.TEXT).build())));
  547. assertThat(actual.getText().toString(), is(text));
  548. }
  549. @Test(expected = ParseException.class)
  550. public void parse_prk_val_without_name() throws ParseException {
  551. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:val value=\"xxx\" /></html>";
  552. target.parse(getInput(text));
  553. }
  554. @Test(expected = ParseException.class)
  555. public void parse_prk_val_without_value() throws ParseException {
  556. String text = "<html xmlns:prk=\"http://www.pirkaengine.org\"><prk:val name=\"foo\" /></html>";
  557. target.parse(getInput(text));
  558. }
  559. @Test(expected = IllegalArgumentException.class)
  560. public void parse_null() throws ParseException {
  561. target.parse(null);
  562. }
  563. @Test(expected = IllegalArgumentException.class)
  564. public void parse_null_reader() throws ParseException {
  565. target.parse(null, Charset.forName("Windows-31J"));
  566. }
  567. @Test(expected = IllegalArgumentException.class)
  568. public void parse_null_charset() throws ParseException {
  569. target.parse(new ByteArrayInputStream(new byte[0]), null);
  570. }
  571. private StringReader getInput(String text) {
  572. return new StringReader(text);
  573. }
  574. private List<Fragment> expected(Fragment... fragments) {
  575. LinkedList<Fragment> list = new LinkedList<Fragment>();
  576. for (Fragment fragment : fragments) {
  577. list.add(fragment);
  578. }
  579. return list;
  580. }
  581. }