Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pebble template file not found if packaging into jar fix #765 #777

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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