Skip to content

Commit a4af7af

Browse files
authored
Merge pull request #726 from jooby-project/725
default error page doesn't work fix #725
2 parents 87f03fc + fc82d05 commit a4af7af

2 files changed

Lines changed: 85 additions & 12 deletions

File tree

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.jooby.pebble;
2020

21+
import java.io.FileNotFoundException;
2122
import java.io.StringWriter;
2223
import java.io.Writer;
2324
import java.util.HashMap;
@@ -28,6 +29,7 @@
2829
import org.jooby.View;
2930

3031
import com.mitchellbosecke.pebble.PebbleEngine;
32+
import com.mitchellbosecke.pebble.error.LoaderException;
3133
import com.mitchellbosecke.pebble.template.PebbleTemplate;;
3234

3335
class PebbleRenderer implements View.Engine {
@@ -41,20 +43,24 @@ public PebbleRenderer(final PebbleEngine pebble) {
4143
@Override
4244
public void render(final View view, final Renderer.Context ctx) throws Exception {
4345
String vname = view.name();
44-
PebbleTemplate template = pebble.getTemplate(vname);
45-
Writer writer = new StringWriter();
46-
Map<String, Object> model = new HashMap<>();
47-
// push locals
48-
model.putAll(ctx.locals());
49-
model.putIfAbsent("_vname", vname);
46+
try {
47+
PebbleTemplate template = pebble.getTemplate(vname);
48+
Writer writer = new StringWriter();
49+
Map<String, Object> model = new HashMap<>();
50+
// push locals
51+
model.putAll(ctx.locals());
52+
model.putIfAbsent("_vname", vname);
5053

51-
// put model
52-
model.putAll(view.model());
54+
// put model
55+
model.putAll(view.model());
5356

54-
// render and send
55-
template.evaluate(writer, model);
56-
ctx.type(MediaType.html)
57-
.send(writer.toString());
57+
// render and send
58+
template.evaluate(writer, model);
59+
ctx.type(MediaType.html)
60+
.send(writer.toString());
61+
} catch (LoaderException x) {
62+
throw new FileNotFoundException(vname);
63+
}
5864
}
5965

6066
@Override
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.jooby.pebble;
2+
3+
import static org.easymock.EasyMock.expect;
4+
import static org.easymock.EasyMock.expectLastCall;
5+
import static org.junit.Assert.assertEquals;
6+
7+
import java.io.FileNotFoundException;
8+
import java.io.StringWriter;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import org.jooby.MediaType;
13+
import org.jooby.Renderer;
14+
import org.jooby.View;
15+
import org.jooby.test.MockUnit;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.powermock.core.classloader.annotations.PrepareForTest;
19+
import org.powermock.modules.junit4.PowerMockRunner;
20+
21+
import com.mitchellbosecke.pebble.PebbleEngine;
22+
import com.mitchellbosecke.pebble.error.LoaderException;
23+
import com.mitchellbosecke.pebble.template.PebbleTemplate;
24+
25+
@RunWith(PowerMockRunner.class)
26+
@PrepareForTest({PebbleRenderer.class, HashMap.class })
27+
public class Issue725 {
28+
29+
@SuppressWarnings({"rawtypes", "unchecked" })
30+
@Test(expected = FileNotFoundException.class)
31+
public void templateNotFound() throws Exception {
32+
new MockUnit(PebbleEngine.class, View.class, Renderer.Context.class)
33+
.expect(unit -> {
34+
Map vmodel = unit.mock(Map.class);
35+
Map<String, Object> locals = unit.mock(Map.class);
36+
37+
Map model = unit.constructor(HashMap.class).build();
38+
model.putAll(locals);
39+
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
40+
model.putAll(vmodel);
41+
42+
View view = unit.get(View.class);
43+
expect(view.name()).andReturn("vname");
44+
expect(view.model()).andReturn(vmodel);
45+
46+
StringWriter writer = unit.constructor(StringWriter.class).build();
47+
48+
Renderer.Context ctx = unit.get(Renderer.Context.class);
49+
expect(ctx.locals()).andReturn(locals);
50+
expect(ctx.type(MediaType.html)).andReturn(ctx);
51+
ctx.send(writer.toString());
52+
53+
PebbleTemplate template = unit.mock(PebbleTemplate.class);
54+
template.evaluate(writer, model);
55+
LoaderException x = new LoaderException(null, "template not found");
56+
expectLastCall().andThrow(x);
57+
58+
PebbleEngine pebble = unit.get(PebbleEngine.class);
59+
expect(pebble.getTemplate("vname")).andReturn(template);
60+
})
61+
.run(unit -> {
62+
PebbleRenderer engine = new PebbleRenderer(unit.get(PebbleEngine.class));
63+
engine.render(unit.get(View.class), unit.get(Renderer.Context.class));
64+
assertEquals("pebble", engine.toString());
65+
});
66+
}
67+
}

0 commit comments

Comments
 (0)