Skip to content

Commit

Permalink
Pebble updates
Browse files Browse the repository at this point in the history
- Pebble template files not found when running integration test fix #936
- pebble 2.4.0 fix #940
  • Loading branch information
jknack committed Nov 1, 2017
1 parent 775e9a4 commit 45ff7b0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
Expand Up @@ -212,6 +212,7 @@
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -257,25 +258,26 @@ public AbstractRendererContext(final List<Renderer> renderers,

public void render(final Object value) throws Exception {
int i = 0;
List<String> notFound = new ArrayList<>();
FileNotFoundException notFound = null;
while (!committed && i < rsize) {
Renderer next = renderers.get(i);
try {
next.render(value, this);
} catch (FileNotFoundException ex) {
} catch (FileNotFoundException x) {
// view engine should recover from a template not found
if (next instanceof View.Engine) {
notFound.add(next.toString());
if (notFound == null) {
notFound = x;
}
} else {
throw ex;
throw x;
}
}
i += 1;
}
if (!committed) {
if (notFound.size() > 0) {
throw new FileNotFoundException("Template not found: " + ((View) value).name() + " in "
+ notFound);
if (notFound != null) {
throw notFound;
}
throw new Err(Status.NOT_ACCEPTABLE, Joiner.on(", ").join(produces));
}
Expand Down
Expand Up @@ -203,6 +203,7 @@
*/
package org.jooby.pebble;

import com.google.common.base.Strings;
import static java.util.Objects.requireNonNull;

import java.util.function.BiConsumer;
Expand Down Expand Up @@ -428,9 +429,9 @@ private static Loader<String> loader(final String prefix, final String suffix) {
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 Strings.emptyToNull(prefix.substring(1));
}
return prefix;
}
return null;
}
Expand Down
Expand Up @@ -209,9 +209,11 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import org.jooby.MediaType;
import org.jooby.Renderer;
import org.jooby.Route;
import org.jooby.View;

import com.mitchellbosecke.pebble.PebbleEngine;
Expand All @@ -232,6 +234,9 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
try {
Map<String, Object> locals = ctx.locals();

if (vname.charAt(0) == '/') {
vname = vname.substring(1);
}
PebbleTemplate template = pebble.getTemplate(vname);
Writer writer = new StringWriter();
Map<String, Object> model = new HashMap<>();
Expand All @@ -253,7 +258,9 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
ctx.type(MediaType.html)
.send(writer.toString());
} catch (LoaderException x) {
throw new FileNotFoundException(vname);
FileNotFoundException fnf = new FileNotFoundException(x.getMessage().replace("Could not find template", "").trim());
fnf.initCause(x);
throw fnf;
}
}

Expand Down
Expand Up @@ -64,4 +64,45 @@ public void render() throws Exception {
assertEquals("pebble", engine.toString());
});
}

@SuppressWarnings({"rawtypes", "unchecked" })
@Test
public void renderWithLeadingSlash() throws Exception {
new MockUnit(PebbleEngine.class, View.class, Renderer.Context.class)
.expect(unit -> {
Locale locale = Locale.UK;
Map vmodel = unit.mock(Map.class);
Map<String, Object> locals = unit.mock(Map.class);
expect(locals.getOrDefault("locale", locale)).andReturn(locale);

Map model = unit.constructor(HashMap.class).build();
model.putAll(locals);
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
expect(model.putIfAbsent("locale", locale)).andReturn(null);
model.putAll(vmodel);

View view = unit.get(View.class);
expect(view.name()).andReturn("/vname");
expect(view.model()).andReturn(vmodel);

StringWriter writer = unit.constructor(StringWriter.class).build();

Renderer.Context ctx = unit.get(Renderer.Context.class);
expect(ctx.locale()).andReturn(locale);
expect(ctx.locals()).andReturn(locals);
expect(ctx.type(MediaType.html)).andReturn(ctx);
ctx.send(writer.toString());

PebbleTemplate template = unit.mock(PebbleTemplate.class);
template.evaluate(writer, model, locale);

PebbleEngine pebble = unit.get(PebbleEngine.class);
expect(pebble.getTemplate("vname")).andReturn(template);
})
.run(unit -> {
PebbleRenderer engine = new PebbleRenderer(unit.get(PebbleEngine.class));
engine.render(unit.get(View.class), unit.get(Renderer.Context.class));
assertEquals("pebble", engine.toString());
});
}
}
Expand Up @@ -178,6 +178,33 @@ public void noEmptyPrefix() throws Exception {
});
}

@Test
public void prefixNoLeadingSlash() 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
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3037,7 +3037,7 @@ org.eclipse.jdt.apt.processorOptions/defaultOverwrite=true
<jruby.version>9.0.1.0</jruby.version>
<j2v8.version>4.6.0</j2v8.version>
<metrics.version>3.1.2</metrics.version>
<pebble.version>2.2.1</pebble.version>
<pebble.version>2.4.0</pebble.version>
<jade4j.version>1.1.4</jade4j.version>
<jsoup.version>1.8.3</jsoup.version>
<rxjava.version>1.1.5</rxjava.version>
Expand Down

0 comments on commit 45ff7b0

Please sign in to comment.