/vertx-template-engines/vertx-web-templ-rocker/src/test/java/io/vertx/ext/web/templ/RockerTemplateEngineTest.java

https://github.com/vert-x3/vertx-web · Java · 139 lines · 99 code · 22 blank · 18 comment · 0 complexity · 9d2f2a5d3b18894b494faa2ad4837876 MD5 · raw file

  1. /*
  2. * Copyright 2014 Red Hat, Inc.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Apache License v2.0 which accompanies this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. *
  11. * The Apache License v2.0 is available at
  12. * http://www.opensource.org/licenses/apache2.0.php
  13. *
  14. * You may elect to redistribute this code under either of these licenses.
  15. */
  16. package io.vertx.ext.web.templ;
  17. import io.vertx.core.json.JsonObject;
  18. import io.vertx.ext.unit.Async;
  19. import io.vertx.ext.unit.TestContext;
  20. import io.vertx.ext.unit.junit.VertxUnitRunner;
  21. import io.vertx.ext.web.common.template.TemplateEngine;
  22. import org.junit.Test;
  23. import io.vertx.ext.web.templ.rocker.RockerTemplateEngine;
  24. import org.junit.runner.RunWith;
  25. /**
  26. * @author <a href="mailto:xianguang.zhou@outlook.com">Xianguang Zhou</a>
  27. */
  28. @RunWith(VertxUnitRunner.class)
  29. public class RockerTemplateEngineTest {
  30. @Test
  31. public void testTemplateHandler(TestContext should) {
  32. final Async test = should.async();
  33. TemplateEngine engine = RockerTemplateEngine.create();
  34. final JsonObject context = new JsonObject()
  35. .put("foo", "badger")
  36. .put("bar", "fox")
  37. .put("context", new JsonObject().put("path", "/TestRockerTemplate2.rocker.html"));
  38. engine.render(context, "somedir/TestRockerTemplate2.rocker.html", render -> {
  39. should.assertTrue(render.succeeded());
  40. should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2.rocker.html\n", render.result().toString());
  41. test.complete();
  42. });
  43. test.await();
  44. }
  45. @Test
  46. public void testTemplateHandlerNoExtension(TestContext should) {
  47. final Async test = should.async();
  48. TemplateEngine engine = RockerTemplateEngine.create();
  49. final JsonObject context = new JsonObject()
  50. .put("foo", "badger")
  51. .put("bar", "fox")
  52. .put("context", new JsonObject().put("path", "/TestRockerTemplate2"));
  53. engine.render(context, "somedir/TestRockerTemplate2", render -> {
  54. should.assertTrue(render.succeeded());
  55. should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2\n", render.result().toString());
  56. test.complete();
  57. });
  58. test.await();
  59. }
  60. @Test
  61. public void testTemplateHandlerChangeExtension(TestContext should) {
  62. final Async test = should.async();
  63. TemplateEngine engine = RockerTemplateEngine.create("rocker.raw");
  64. final JsonObject context = new JsonObject()
  65. .put("foo", "badger")
  66. .put("bar", "fox")
  67. .put("context", new JsonObject().put("path", "/TestRockerTemplate3"));
  68. engine.render(context, "somedir/TestRockerTemplate3", render -> {
  69. should.assertTrue(render.succeeded());
  70. should.assertEquals("\nCheerio badger and fox\nRequest path is /TestRockerTemplate3\n", render.result().toString());
  71. test.complete();
  72. });
  73. test.await();
  74. }
  75. @Test
  76. public void testTemplateHandlerIncludes(TestContext should) {
  77. final Async test = should.async();
  78. TemplateEngine engine = RockerTemplateEngine.create();
  79. final JsonObject context = new JsonObject()
  80. .put("foo", "badger")
  81. .put("bar", "fox")
  82. .put("context", new JsonObject().put("path", "/TestRockerTemplate3"));
  83. engine.render(context, "somedir/Base", render -> {
  84. should.assertTrue(render.succeeded());
  85. should.assertEquals("Vert.x rules\n", render.result().toString());
  86. test.complete();
  87. });
  88. test.await();
  89. }
  90. @Test
  91. public void testNoSuchTemplate(TestContext should) {
  92. final Async test = should.async();
  93. TemplateEngine engine = RockerTemplateEngine.create();
  94. final JsonObject context = new JsonObject();
  95. engine.render(context, "nosuchtemplate.rocker.html", render -> {
  96. should.assertFalse(render.succeeded());
  97. test.complete();
  98. });
  99. test.await();
  100. }
  101. @Test
  102. public void testTemplateWithUndrescoreKeysHandler(TestContext should) {
  103. final Async test = should.async();
  104. TemplateEngine engine = RockerTemplateEngine.create();
  105. final JsonObject context = new JsonObject()
  106. .put("foo", "badger")
  107. .put("bar", "fox")
  108. .put("context", new JsonObject().put("path", "/TestRockerTemplate2.rocker.html"))
  109. .put("__body-handled", true);
  110. engine.render(context, "somedir/TestRockerTemplate2.rocker.html", render -> {
  111. should.assertTrue(render.succeeded());
  112. should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2.rocker.html\n", render.result().toString());
  113. test.complete();
  114. });
  115. test.await();
  116. }
  117. }