|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.jte; |
| 7 | + |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.ArgumentMatchers.isA; |
| 10 | +import static org.mockito.Mockito.*; |
| 11 | + |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import gg.jte.TemplateEngine; |
| 18 | +import gg.jte.TemplateOutput; |
| 19 | +import io.jooby.Context; |
| 20 | +import io.jooby.MapModelAndView; |
| 21 | +import io.jooby.ModelAndView; |
| 22 | +import io.jooby.buffer.DataBuffer; |
| 23 | +import io.jooby.buffer.DataBufferFactory; |
| 24 | + |
| 25 | +public class Issue3599 { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void shouldNotCallObjectMethodOnMapModels() { |
| 29 | + var bufferFactory = mock(DataBufferFactory.class); |
| 30 | + var buffer = mock(DataBuffer.class); |
| 31 | + when(bufferFactory.allocateBuffer()).thenReturn(buffer); |
| 32 | + |
| 33 | + var attributes = Map.<String, Object>of("foo", 1); |
| 34 | + var mapModel = new HashMap<String, Object>(); |
| 35 | + mapModel.put("param1", new Issue3599()); |
| 36 | + |
| 37 | + var ctx = mock(Context.class); |
| 38 | + when(ctx.getBufferFactory()).thenReturn(bufferFactory); |
| 39 | + when(ctx.getAttributes()).thenReturn(attributes); |
| 40 | + |
| 41 | + var jteParams = new HashMap<String, Object>(); |
| 42 | + jteParams.putAll(attributes); |
| 43 | + jteParams.putAll(mapModel); |
| 44 | + |
| 45 | + var jte = mock(TemplateEngine.class); |
| 46 | + |
| 47 | + var engine = new JteTemplateEngine(jte); |
| 48 | + var modelAndView = new MapModelAndView("template.jte", mapModel); |
| 49 | + engine.render(ctx, modelAndView); |
| 50 | + |
| 51 | + verify(jte, times(1)).render(eq("template.jte"), eq(jteParams), isA(TemplateOutput.class)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldCallObjectMethodOnObjectModels() { |
| 56 | + var bufferFactory = mock(DataBufferFactory.class); |
| 57 | + var buffer = mock(DataBuffer.class); |
| 58 | + when(bufferFactory.allocateBuffer()).thenReturn(buffer); |
| 59 | + |
| 60 | + var attributes = Map.<String, Object>of("foo", 1); |
| 61 | + var model = new Issue3599(); |
| 62 | + |
| 63 | + var ctx = mock(Context.class); |
| 64 | + when(ctx.getBufferFactory()).thenReturn(bufferFactory); |
| 65 | + when(ctx.getAttributes()).thenReturn(attributes); |
| 66 | + |
| 67 | + var jte = mock(TemplateEngine.class); |
| 68 | + |
| 69 | + var engine = new JteTemplateEngine(jte); |
| 70 | + var modelAndView = new ModelAndView<>("template.jte", model); |
| 71 | + engine.render(ctx, modelAndView); |
| 72 | + |
| 73 | + verify(jte, times(1)).render(eq("template.jte"), eq(model), isA(TemplateOutput.class)); |
| 74 | + } |
| 75 | +} |
0 commit comments