Search in sources :

Example 71 with TemplateEngine

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

the class FreeMarkerTemplateTest method testTemplateHandlerOnClasspath.

@Test
public void testTemplateHandlerOnClasspath(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/test-freemarker-template2.ftl", should.asyncAssertSuccess(render -> {
        should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.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 72 with TemplateEngine

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

the class JadeTemplateNoCacheTest method testCachingDisabled.

@Test
public void testCachingDisabled(TestContext should) throws IOException {
    System.setProperty("vertxweb.environment", "development");
    TemplateEngine engine = JadeTemplateEngine.create(vertx);
    File temp = File.createTempFile("template", ".jade", new File("target/classes"));
    temp.deleteOnExit();
    try (PrintWriter out = new PrintWriter(temp)) {
        out.print("before");
        out.flush();
    }
    engine.render(new JsonObject(), temp.getParent() + "/" + temp.getName(), should.asyncAssertSuccess(render -> {
        should.assertEquals("<before></before>", JadeTemplateTest.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.getParent() + "/" + temp.getName(), should.asyncAssertSuccess(render2 -> {
            should.assertEquals("<after></after>", JadeTemplateTest.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) JadeTemplateEngine(io.vertx.ext.web.templ.jade.JadeTemplateEngine) File(java.io.File) JsonObject(io.vertx.core.json.JsonObject) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) JadeTemplateEngine(io.vertx.ext.web.templ.jade.JadeTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 73 with TemplateEngine

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

the class TemplateHandlerImplTest method testSetIndex.

@Test
public void testSetIndex() {
    TemplateEngine templateEngine = mock(TemplateEngine.class);
    RoutingContext routingContext = mock(RoutingContext.class);
    when(routingContext.normalizedPath()).thenReturn("/");
    Route currentRoute = mock(Route.class);
    when(currentRoute.getPath()).thenReturn("/");
    when(routingContext.currentRoute()).thenReturn(currentRoute);
    TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
    templateHandler.setIndexTemplate("home");
    templateHandler.handle(routingContext);
    verify(templateEngine).render(any(JsonObject.class), eq("templates/home"), any());
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) TemplateHandlerImpl(io.vertx.ext.web.handler.impl.TemplateHandlerImpl) RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) Route(io.vertx.ext.web.Route) Test(org.junit.Test)

Example 74 with TemplateEngine

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

the class TemplateTest method testTemplateEngineFail.

@Test
public void testTemplateEngineFail() throws Exception {
    TemplateEngine engine = new TestEngine(true);
    router.route().handler(TemplateHandler.create(engine, "somedir", "text/html"));
    router.errorHandler(500, ctx -> {
        Throwable t = ctx.failure();
        assertEquals("eek", t.getMessage());
        testComplete();
    });
    testRequest(HttpMethod.GET, "/foo.html", 500, "Internal Server Error");
    await();
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Test(org.junit.Test)

Example 75 with TemplateEngine

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

the class TemplateTest method testRenderDirectly.

@Test
public void testRenderDirectly() throws Exception {
    TemplateEngine engine = new TestEngine(false);
    router.route().handler(context -> {
        context.put("foo", "badger");
        context.put("bar", "fox");
        engine.render(context.data(), "somedir/test-template.html", res -> {
            if (res.succeeded()) {
                context.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/html").end(res.result());
            } else {
                context.fail(res.cause());
            }
        });
    });
    String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>";
    testRequest(HttpMethod.GET, "/", 200, "OK", expected);
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) 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