Search in sources :

Example 16 with TemplateEngine

use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.

the class ThymeleafTemplateTest method testFragmentedTemplates.

@Test
public void testFragmentedTemplates(TestContext should) {
    TemplateEngine engine = ThymeleafTemplateEngine.create(vertx);
    final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox").put("context", new JsonObject().put("path", "/test-thymeleaf-template2.html"));
    engine.render(context, "somedir/test-thymeleaf-fragmented.html", should.asyncAssertSuccess(render -> {
        final String expected = "<!doctype html>\n" + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" + "<head>\n" + "  <meta charset=\"utf-8\">\n" + "  <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/resources/images/favicon.png\">\n" + "</head>\n" + "<body>\n" + "<p>badger</p>\n" + "<p>fox</p>\n" + "<p>/test-thymeleaf-template2.html</p>\n" + "</body>\n" + "</html>\n";
        should.assertEquals(expected, normalizeCRLF(render.toString()));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) PrintWriter(java.io.PrintWriter) ThymeleafTemplateEngine(io.vertx.ext.web.templ.thymeleaf.ThymeleafTemplateEngine) BeforeClass(org.junit.BeforeClass) FileSystemOptions(io.vertx.core.file.FileSystemOptions) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) JsonObject(io.vertx.core.json.JsonObject) ThymeleafTemplateEngine(io.vertx.ext.web.templ.thymeleaf.ThymeleafTemplateEngine) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 17 with TemplateEngine

use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.

the class MVELTemplateNoCacheTest method testCachingDisabled.

@Test
public void testCachingDisabled(TestContext should) throws IOException {
    final Async test = should.async();
    System.setProperty("vertxweb.environment", "development");
    TemplateEngine engine = MVELTemplateEngine.create(vertx);
    PrintWriter out;
    File temp = File.createTempFile("template", ".templ", new File("target/classes"));
    temp.deleteOnExit();
    out = new PrintWriter(temp);
    out.print("before");
    out.flush();
    out.close();
    engine.render(new JsonObject(), temp.getParent() + "/" + temp.getName(), render -> {
        should.assertTrue(render.succeeded());
        should.assertEquals("before", render.result().toString());
        try {
            PrintWriter out2 = new PrintWriter(temp);
            out2.print("after");
            out2.flush();
            out2.close();
        } catch (IOException e) {
            should.fail(e);
        }
        engine.render(new JsonObject(), temp.getParent() + "/" + temp.getName(), render2 -> {
            should.assertTrue(render2.succeeded());
            should.assertEquals("after", render2.result().toString());
            test.complete();
        });
    });
    test.await();
}
Also used : MVELTemplateEngine(io.vertx.ext.web.templ.mvel.MVELTemplateEngine) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 18 with TemplateEngine

use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.

the class FreeMarkerTemplateTest method testCachingEnabled.

@Test
public void testCachingEnabled(TestContext should) throws IOException {
    System.setProperty("vertxweb.environment", "production");
    TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);
    File temp = File.createTempFile("template", ".ftl", new File("target/classes"));
    temp.deleteOnExit();
    try (PrintWriter out = new PrintWriter(temp)) {
        out.print("before");
        out.flush();
    }
    engine.render(new JsonObject(), temp.getName(), should.asyncAssertSuccess(render -> {
        should.assertEquals("before", normalizeCRLF(render.toString()));
        try (PrintWriter out2 = new PrintWriter(temp)) {
            out2.print("after");
            out2.flush();
        } catch (IOException e) {
            should.fail(e);
        }
        engine.render(new JsonObject(), temp.getName(), should.asyncAssertSuccess(render2 -> {
            should.assertEquals("before", normalizeCRLF(render2.toString()));
        }));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) PrintWriter(java.io.PrintWriter) BeforeClass(org.junit.BeforeClass) FileSystemOptions(io.vertx.core.file.FileSystemOptions) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 19 with TemplateEngine

use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.

the class FreeMarkerTemplateTest method testTemplateHandlerOnFileSystem.

@Test
public void testTemplateHandlerOnFileSystem(TestContext should) {
    TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);
    final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox");
    context.put("context", new JsonObject().put("path", "/test-freemarker-template3.ftl"));
    engine.render(context, "src/test/filesystemtemplates/test-freemarker-template3.ftl", should.asyncAssertSuccess(render -> {
        should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template3.ftl\n", normalizeCRLF(render.toString()));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) PrintWriter(java.io.PrintWriter) BeforeClass(org.junit.BeforeClass) FileSystemOptions(io.vertx.core.file.FileSystemOptions) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 20 with TemplateEngine

use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.

the class FreeMarkerTemplateTest method testTemplateHandlerIncludes.

@Test
public void testTemplateHandlerIncludes(TestContext should) {
    TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);
    final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox");
    context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));
    engine.render(context, "somedir/base", should.asyncAssertSuccess(render -> {
        should.assertEquals("Vert.x rules", normalizeCRLF(render.toString()));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) PrintWriter(java.io.PrintWriter) BeforeClass(org.junit.BeforeClass) FileSystemOptions(io.vertx.core.file.FileSystemOptions) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

TemplateEngine (io.vertx.ext.web.common.template.TemplateEngine)75 Test (org.junit.Test)74 JsonObject (io.vertx.core.json.JsonObject)70 TestContext (io.vertx.ext.unit.TestContext)57 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)57 RunWith (org.junit.runner.RunWith)57 File (java.io.File)53 IOException (java.io.IOException)53 PrintWriter (java.io.PrintWriter)53 Vertx (io.vertx.core.Vertx)52 VertxOptions (io.vertx.core.VertxOptions)52 FileSystemOptions (io.vertx.core.file.FileSystemOptions)52 BeforeClass (org.junit.BeforeClass)52 JsonArray (io.vertx.core.json.JsonArray)16 PebbleTemplateEngine (io.vertx.ext.web.templ.pebble.PebbleTemplateEngine)13 PebbleEngine (com.mitchellbosecke.pebble.PebbleEngine)11 TestExtension (io.vertx.ext.web.templ.extension.TestExtension)11 PebbleVertxLoader (io.vertx.ext.web.templ.pebble.impl.PebbleVertxLoader)11 ArrayList (java.util.ArrayList)11 Assert.assertNotNull (org.junit.Assert.assertNotNull)10