|
| 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