From 00c5f00417269b51778aa36e5eddfa4d63033d09 Mon Sep 17 00:00:00 2001 From: Edgar Espina Date: Sun, 21 May 2017 15:16:59 -0300 Subject: [PATCH] Pebble template file not found if packaging into jar fix #765 --- .../main/java/org/jooby/pebble/Pebble.java | 12 +++- .../java/org/jooby/pebble/PebbleTest.java | 71 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java b/jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java index 67b9992d30..970991a26c 100644 --- a/jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java +++ b/jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java @@ -142,7 +142,7 @@ public class Pebble implements Jooby.Module { * @param suffix Template extension. */ public Pebble(final String prefix, final String suffix) { - this.pebble = new PebbleEngine.Builder().loader(loader(prefix, suffix)); + this.pebble = new PebbleEngine.Builder().loader(loader(safePrefix(prefix), suffix)); } /** @@ -240,4 +240,14 @@ private static Loader loader(final String prefix, final String suffix) { return loader; } + private static String safePrefix(final String prefix) { + if (prefix != null && prefix.length() > 0) { + if (prefix.startsWith("/")) { + String rewrite = prefix.substring(1); + return rewrite.length() == 0 ? null : rewrite; + } + } + return null; + } + } diff --git a/jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java b/jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java index 1845b90452..9f79b74f6a 100644 --- a/jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java +++ b/jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java @@ -107,6 +107,77 @@ public void basic() throws Exception { }); } + @Test + public void prefixAsRoot() throws Exception { + Locale locale = Locale.getDefault(); + new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class) + .expect(defLoader) + .expect(newEngine) + .expect(env("dev", locale)) + .expect(cacheStatic) + .expect(cache("pebble.cache", null)) + .expect(cache(0)) + .expect(cache("pebble.tagCache", null)) + .expect(tagCache(0)) + .expect(locale(locale)) + .expect(build) + .expect(bindEngine) + .expect(renderer) + .run(unit -> { + new Pebble("/", ".html") + .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); + }); + } + + @Test + public void emptyPrefix() throws Exception { + Locale locale = Locale.getDefault(); + new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class) + .expect(defLoader) + .expect(newEngine) + .expect(env("dev", locale)) + .expect(cacheStatic) + .expect(cache("pebble.cache", null)) + .expect(cache(0)) + .expect(cache("pebble.tagCache", null)) + .expect(tagCache(0)) + .expect(locale(locale)) + .expect(build) + .expect(bindEngine) + .expect(renderer) + .run(unit -> { + new Pebble("", ".html") + .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); + }); + } + + @Test + public void noEmptyPrefix() throws Exception { + Locale locale = Locale.getDefault(); + new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class) + .expect(unit -> { + ClasspathLoader loader = unit.constructor(ClasspathLoader.class).build(); + loader.setPrefix("views"); + loader.setSuffix(".html"); + unit.registerMock(ClasspathLoader.class, loader); + }) + .expect(newEngine) + .expect(env("dev", locale)) + .expect(cacheStatic) + .expect(cache("pebble.cache", null)) + .expect(cache(0)) + .expect(cache("pebble.tagCache", null)) + .expect(tagCache(0)) + .expect(locale(locale)) + .expect(build) + .expect(bindEngine) + .expect(renderer) + .run(unit -> { + new Pebble("/views", ".html") + .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); + }); + } + @Test public void proddef() throws Exception { Locale locale = Locale.getDefault();