/hutool-extra/src/test/java/cn/hutool/extra/template/ThymeleafTest.java

https://github.com/liaoze2010/hutool · Java · 99 lines · 68 code · 20 blank · 11 comment · 0 complexity · 061636944ab2f5f5fe9d83e0b253db8d MD5 · raw file

  1. package cn.hutool.extra.template;
  2. import java.io.StringWriter;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import java.util.Map;
  9. import org.junit.Assert;
  10. import org.junit.Test;
  11. import org.thymeleaf.context.Context;
  12. import org.thymeleaf.templateresolver.StringTemplateResolver;
  13. import cn.hutool.core.date.DateUtil;
  14. import cn.hutool.core.lang.Dict;
  15. import cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine;
  16. /**
  17. * Thymeleaf单元测试
  18. *
  19. * @author looly
  20. *
  21. */
  22. public class ThymeleafTest {
  23. @Test
  24. public void thymeleafEngineTest() {
  25. Map<String, Object> map1 = new HashMap<>();
  26. map1.put("name", "a");
  27. Map<String, Object> map2 = new HashMap<>();
  28. map2.put("name", "b");
  29. // 日期测试
  30. Map<String, Object> map3 = new HashMap<>();
  31. map3.put("name", DateUtil.parse("2019-01-01"));
  32. List<Map<String, Object>> list = new ArrayList<>();
  33. list.add(map1);
  34. list.add(map2);
  35. list.add(map3);
  36. // 字符串模板
  37. TemplateEngine engine = new ThymeleafEngine(new TemplateConfig());
  38. Template template = engine.getTemplate("<h3 th:each=\"item : ${list}\" th:text=\"${item.name}\"></h3>");
  39. String render = template.render(Dict.create().set("list", list));
  40. Assert.assertEquals("<h3>a</h3><h3>b</h3><h3>2019-01-01 00:00:00</h3>", render);
  41. }
  42. @Test
  43. public void thymeleafEngineTest2() {
  44. Map<String, Object> map1 = new HashMap<>();
  45. map1.put("name", "a");
  46. Map<String, Object> map2 = new HashMap<>();
  47. map2.put("name", "b");
  48. // 日期测试
  49. Map<String, Object> map3 = new HashMap<>();
  50. map3.put("name", DateUtil.parse("2019-01-01"));
  51. List<Map<String, Object>> list = new ArrayList<>();
  52. list.add(map1);
  53. list.add(map2);
  54. list.add(map3);
  55. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  56. map.put("list", list);
  57. hutoolApi(map);
  58. thymeleaf(map);
  59. }
  60. @SuppressWarnings({ "rawtypes", "unchecked" })
  61. private static void thymeleaf(Map map) {
  62. org.thymeleaf.TemplateEngine templateEngine = new org.thymeleaf.TemplateEngine();
  63. StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
  64. templateEngine.addTemplateResolver(stringTemplateResolver);
  65. StringWriter writer = new StringWriter();
  66. Context context = new Context(Locale.getDefault(), map);
  67. templateEngine.process("<h3 th:each=\"item : ${list}\" th:text=\"${item.name}\"></h3>", context, writer);
  68. Assert.assertEquals("<h3>a</h3><h3>b</h3><h3>2019-01-01 00:00:00</h3>", writer.toString());
  69. }
  70. @SuppressWarnings("rawtypes")
  71. private static void hutoolApi(Map map) {
  72. // 字符串模板
  73. TemplateEngine engine = new ThymeleafEngine(new TemplateConfig());
  74. Template template = engine.getTemplate("<h3 th:each=\"item : ${list}\" th:text=\"${item.name}\"></h3>");
  75. // "<h3 th:text=\"${nestMap.nestKey}\"></h3>"
  76. String render = template.render(map);
  77. Assert.assertEquals("<h3>a</h3><h3>b</h3><h3>2019-01-01 00:00:00</h3>", render);
  78. }
  79. }