Search in sources :

Example 21 with TemplateEngine

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

the class HandlebarsTemplateNoCacheTest method testCachingDisabled.

@Test
public void testCachingDisabled(TestContext should) throws IOException {
    System.setProperty("vertxweb.environment", "development");
    TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);
    File temp = File.createTempFile("template", ".hbs", 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(), render -> {
        should.assertTrue(render.succeeded());
        should.assertEquals("before", render.result().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", 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) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) 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) HandlebarsTemplateTest.normalizeCRLF(io.vertx.ext.web.templ.HandlebarsTemplateTest.normalizeCRLF) JsonObject(io.vertx.core.json.JsonObject) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 22 with TemplateEngine

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

the class HandlebarsTemplateTest method testTemplateOnClasspath.

@Test
public void testTemplateOnClasspath(TestContext should) {
    TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);
    final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox");
    engine.render(context, "somedir/test-handlebars-template2.hbs", should.asyncAssertSuccess(render -> {
        should.assertEquals("Hello badger and fox", 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) Assert.assertNotNull(org.junit.Assert.assertNotNull) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Entry(java.util.Map.Entry) JsonObject(io.vertx.core.json.JsonObject) Collections(java.util.Collections) ValueResolver(com.github.jknack.handlebars.ValueResolver) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 23 with TemplateEngine

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

the class HandlebarsTemplateTest method testTemplateJsonObjectResolver.

@Test
public void testTemplateJsonObjectResolver(TestContext should) {
    TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);
    JsonObject json = new JsonObject();
    json.put("bar", new JsonObject().put("one", "badger").put("two", "fox"));
    engine.render(new JsonObject().put("foo", json), "src/test/filesystemtemplates/test-handlebars-template4.hbs", should.asyncAssertSuccess(render -> {
        should.assertEquals("Goodbye badger and fox", 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) Assert.assertNotNull(org.junit.Assert.assertNotNull) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Entry(java.util.Map.Entry) JsonObject(io.vertx.core.json.JsonObject) Collections(java.util.Collections) ValueResolver(com.github.jknack.handlebars.ValueResolver) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 24 with TemplateEngine

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

the class HandlebarsTemplateTest method testCachingEnabled.

@Test
public void testCachingEnabled(TestContext should) throws IOException {
    System.setProperty("vertxweb.environment", "production");
    TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);
    File temp = File.createTempFile("template", ".hbs", 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", 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("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) Assert.assertNotNull(org.junit.Assert.assertNotNull) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Entry(java.util.Map.Entry) JsonObject(io.vertx.core.json.JsonObject) Collections(java.util.Collections) ValueResolver(com.github.jknack.handlebars.ValueResolver) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HandlebarsTemplateEngine(io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 25 with TemplateEngine

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

the class HTTLTemplateEngineTest method testNoSuchTemplate.

@Test
public void testNoSuchTemplate(TestContext should) {
    TemplateEngine engine = HTTLTemplateEngine.create(vertx);
    engine.render(new JsonObject(), "not-found", should.asyncAssertFailure());
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HTTLTemplateEngine(io.vertx.ext.web.templ.httl.HTTLTemplateEngine) 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