Skip to content

Commit 45ff7b0

Browse files
committed
Pebble updates
- Pebble template files not found when running integration test fix #936 - pebble 2.4.0 fix #940
1 parent 775e9a4 commit 45ff7b0

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed

jooby/src/main/java/org/jooby/internal/AbstractRendererContext.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
import java.nio.channels.FileChannel;
213213
import java.nio.charset.Charset;
214214
import java.util.ArrayList;
215+
import java.util.LinkedList;
215216
import java.util.List;
216217
import java.util.Locale;
217218
import java.util.Map;
@@ -257,25 +258,26 @@ public AbstractRendererContext(final List<Renderer> renderers,
257258

258259
public void render(final Object value) throws Exception {
259260
int i = 0;
260-
List<String> notFound = new ArrayList<>();
261+
FileNotFoundException notFound = null;
261262
while (!committed && i < rsize) {
262263
Renderer next = renderers.get(i);
263264
try {
264265
next.render(value, this);
265-
} catch (FileNotFoundException ex) {
266+
} catch (FileNotFoundException x) {
266267
// view engine should recover from a template not found
267268
if (next instanceof View.Engine) {
268-
notFound.add(next.toString());
269+
if (notFound == null) {
270+
notFound = x;
271+
}
269272
} else {
270-
throw ex;
273+
throw x;
271274
}
272275
}
273276
i += 1;
274277
}
275278
if (!committed) {
276-
if (notFound.size() > 0) {
277-
throw new FileNotFoundException("Template not found: " + ((View) value).name() + " in "
278-
+ notFound);
279+
if (notFound != null) {
280+
throw notFound;
279281
}
280282
throw new Err(Status.NOT_ACCEPTABLE, Joiner.on(", ").join(produces));
281283
}

modules/jooby-pebble/src/main/java/org/jooby/pebble/Pebble.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
*/
204204
package org.jooby.pebble;
205205

206+
import com.google.common.base.Strings;
206207
import static java.util.Objects.requireNonNull;
207208

208209
import java.util.function.BiConsumer;
@@ -428,9 +429,9 @@ private static Loader<String> loader(final String prefix, final String suffix) {
428429
private static String safePrefix(final String prefix) {
429430
if (prefix != null && prefix.length() > 0) {
430431
if (prefix.startsWith("/")) {
431-
String rewrite = prefix.substring(1);
432-
return rewrite.length() == 0 ? null : rewrite;
432+
return Strings.emptyToNull(prefix.substring(1));
433433
}
434+
return prefix;
434435
}
435436
return null;
436437
}

modules/jooby-pebble/src/main/java/org/jooby/pebble/PebbleRenderer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,11 @@
209209
import java.util.HashMap;
210210
import java.util.Locale;
211211
import java.util.Map;
212+
import java.util.Optional;
212213

213214
import org.jooby.MediaType;
214215
import org.jooby.Renderer;
216+
import org.jooby.Route;
215217
import org.jooby.View;
216218

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

237+
if (vname.charAt(0) == '/') {
238+
vname = vname.substring(1);
239+
}
235240
PebbleTemplate template = pebble.getTemplate(vname);
236241
Writer writer = new StringWriter();
237242
Map<String, Object> model = new HashMap<>();
@@ -253,7 +258,9 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
253258
ctx.type(MediaType.html)
254259
.send(writer.toString());
255260
} catch (LoaderException x) {
256-
throw new FileNotFoundException(vname);
261+
FileNotFoundException fnf = new FileNotFoundException(x.getMessage().replace("Could not find template", "").trim());
262+
fnf.initCause(x);
263+
throw fnf;
257264
}
258265
}
259266

modules/jooby-pebble/src/test/java/org/jooby/pebble/PebbleRendererTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,45 @@ public void render() throws Exception {
6464
assertEquals("pebble", engine.toString());
6565
});
6666
}
67+
68+
@SuppressWarnings({"rawtypes", "unchecked" })
69+
@Test
70+
public void renderWithLeadingSlash() throws Exception {
71+
new MockUnit(PebbleEngine.class, View.class, Renderer.Context.class)
72+
.expect(unit -> {
73+
Locale locale = Locale.UK;
74+
Map vmodel = unit.mock(Map.class);
75+
Map<String, Object> locals = unit.mock(Map.class);
76+
expect(locals.getOrDefault("locale", locale)).andReturn(locale);
77+
78+
Map model = unit.constructor(HashMap.class).build();
79+
model.putAll(locals);
80+
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
81+
expect(model.putIfAbsent("locale", locale)).andReturn(null);
82+
model.putAll(vmodel);
83+
84+
View view = unit.get(View.class);
85+
expect(view.name()).andReturn("/vname");
86+
expect(view.model()).andReturn(vmodel);
87+
88+
StringWriter writer = unit.constructor(StringWriter.class).build();
89+
90+
Renderer.Context ctx = unit.get(Renderer.Context.class);
91+
expect(ctx.locale()).andReturn(locale);
92+
expect(ctx.locals()).andReturn(locals);
93+
expect(ctx.type(MediaType.html)).andReturn(ctx);
94+
ctx.send(writer.toString());
95+
96+
PebbleTemplate template = unit.mock(PebbleTemplate.class);
97+
template.evaluate(writer, model, locale);
98+
99+
PebbleEngine pebble = unit.get(PebbleEngine.class);
100+
expect(pebble.getTemplate("vname")).andReturn(template);
101+
})
102+
.run(unit -> {
103+
PebbleRenderer engine = new PebbleRenderer(unit.get(PebbleEngine.class));
104+
engine.render(unit.get(View.class), unit.get(Renderer.Context.class));
105+
assertEquals("pebble", engine.toString());
106+
});
107+
}
67108
}

modules/jooby-pebble/src/test/java/org/jooby/pebble/PebbleTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ public void noEmptyPrefix() throws Exception {
178178
});
179179
}
180180

181+
@Test
182+
public void prefixNoLeadingSlash() throws Exception {
183+
Locale locale = Locale.getDefault();
184+
new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class)
185+
.expect(unit -> {
186+
ClasspathLoader loader = unit.constructor(ClasspathLoader.class).build();
187+
loader.setPrefix("views");
188+
loader.setSuffix(".html");
189+
unit.registerMock(ClasspathLoader.class, loader);
190+
})
191+
.expect(newEngine)
192+
.expect(env("dev", locale))
193+
.expect(cacheStatic)
194+
.expect(cache("pebble.cache", null))
195+
.expect(cache(0))
196+
.expect(cache("pebble.tagCache", null))
197+
.expect(tagCache(0))
198+
.expect(locale(locale))
199+
.expect(build)
200+
.expect(bindEngine)
201+
.expect(renderer)
202+
.run(unit -> {
203+
new Pebble("views", ".html")
204+
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
205+
});
206+
}
207+
181208
@Test
182209
public void proddef() throws Exception {
183210
Locale locale = Locale.getDefault();

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,7 @@ org.eclipse.jdt.apt.processorOptions/defaultOverwrite=true
30373037
<jruby.version>9.0.1.0</jruby.version>
30383038
<j2v8.version>4.6.0</j2v8.version>
30393039
<metrics.version>3.1.2</metrics.version>
3040-
<pebble.version>2.2.1</pebble.version>
3040+
<pebble.version>2.4.0</pebble.version>
30413041
<jade4j.version>1.1.4</jade4j.version>
30423042
<jsoup.version>1.8.3</jsoup.version>
30433043
<rxjava.version>1.1.5</rxjava.version>

0 commit comments

Comments
 (0)