Skip to content

Commit

Permalink
Merge pull request #777 from jooby-project/765
Browse files Browse the repository at this point in the history
Pebble template file not found if packaging into jar fix #765
  • Loading branch information
jknack authored May 21, 2017
2 parents 0e2f414 + 00c5f00 commit 7a288e0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
12 changes: 11 additions & 1 deletion jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -240,4 +240,14 @@ private static Loader<String> 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;
}

}
71 changes: 71 additions & 0 deletions jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7a288e0

Please sign in to comment.