|
| 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.internal.thymeleaf; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | +import static org.mockito.ArgumentMatchers.eq; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import java.io.Writer; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | +import org.thymeleaf.TemplateEngine; |
| 29 | +import org.thymeleaf.context.IContext; |
| 30 | + |
| 31 | +import io.jooby.Context; |
| 32 | +import io.jooby.MapModelAndView; |
| 33 | +import io.jooby.ModelAndView; |
| 34 | +import io.jooby.output.BufferedOutput; |
| 35 | +import io.jooby.output.Output; |
| 36 | +import io.jooby.output.OutputFactory; |
| 37 | + |
| 38 | +@ExtendWith(MockitoExtension.class) |
| 39 | +class ThymeleafTemplateEngineTest { |
| 40 | + |
| 41 | + @Mock TemplateEngine templateEngine; |
| 42 | + @Mock Context ctx; |
| 43 | + @Mock OutputFactory outputFactory; |
| 44 | + @Mock BufferedOutput outputBuffer; |
| 45 | + @Mock Writer writer; |
| 46 | + |
| 47 | + private ThymeleafTemplateEngine engine; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setup() { |
| 51 | + engine = new ThymeleafTemplateEngine(templateEngine, List.of(".thl", ".html")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testExtensions_AreAssignedAndUnmodifiable() { |
| 56 | + assertEquals(List.of(".thl", ".html"), engine.extensions()); |
| 57 | + assertThrows(UnsupportedOperationException.class, () -> engine.extensions().add(".bad")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testRender_UnsupportedModelAndView() { |
| 62 | + ModelAndView<?> badModel = mock(ModelAndView.class); |
| 63 | + |
| 64 | + assertThrows(ModelAndView.UnsupportedModelAndView.class, () -> engine.render(ctx, badModel)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testRender_MapModelAndView_ViewWithoutSlash_LocaleFromContext() { |
| 69 | + MapModelAndView modelAndView = new MapModelAndView("index.html"); |
| 70 | + modelAndView.put("user", "edgar"); |
| 71 | + |
| 72 | + Map<String, Object> ctxAttributes = new HashMap<>(); |
| 73 | + ctxAttributes.put("flash", "success"); |
| 74 | + |
| 75 | + when(ctx.getAttributes()).thenReturn(ctxAttributes); |
| 76 | + when(ctx.locale()).thenReturn(Locale.UK); |
| 77 | + when(ctx.getOutputFactory()).thenReturn(outputFactory); |
| 78 | + when(outputFactory.allocate()).thenReturn(outputBuffer); |
| 79 | + when(outputBuffer.asWriter()).thenReturn(writer); |
| 80 | + |
| 81 | + Output result = engine.render(ctx, modelAndView); |
| 82 | + |
| 83 | + assertEquals(outputBuffer, result); |
| 84 | + |
| 85 | + ArgumentCaptor<IContext> contextCaptor = ArgumentCaptor.forClass(IContext.class); |
| 86 | + |
| 87 | + // Verifies the engine processed a sanitized path (prepended slash) |
| 88 | + verify(templateEngine).process(eq("/index.html"), contextCaptor.capture(), eq(writer)); |
| 89 | + |
| 90 | + IContext thymeleafContext = contextCaptor.getValue(); |
| 91 | + |
| 92 | + // Verifies the locale fell back to the context locale |
| 93 | + assertEquals(Locale.UK, thymeleafContext.getLocale()); |
| 94 | + |
| 95 | + // Verifies model attributes and context attributes were successfully merged |
| 96 | + assertTrue(thymeleafContext.containsVariable("user")); |
| 97 | + assertEquals("edgar", thymeleafContext.getVariable("user")); |
| 98 | + assertTrue(thymeleafContext.containsVariable("flash")); |
| 99 | + assertEquals("success", thymeleafContext.getVariable("flash")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testRender_MapModelAndView_ViewWithSlash_LocaleFromModel() { |
| 104 | + MapModelAndView modelAndView = new MapModelAndView("/admin/dashboard.html"); |
| 105 | + modelAndView.setLocale(Locale.CANADA); |
| 106 | + |
| 107 | + when(ctx.getAttributes()).thenReturn(new HashMap<>()); |
| 108 | + when(ctx.getOutputFactory()).thenReturn(outputFactory); |
| 109 | + when(outputFactory.allocate()).thenReturn(outputBuffer); |
| 110 | + when(outputBuffer.asWriter()).thenReturn(writer); |
| 111 | + |
| 112 | + engine.render(ctx, modelAndView); |
| 113 | + |
| 114 | + ArgumentCaptor<IContext> contextCaptor = ArgumentCaptor.forClass(IContext.class); |
| 115 | + |
| 116 | + // Verifies the engine processed the path as-is (slash already present) |
| 117 | + verify(templateEngine) |
| 118 | + .process(eq("/admin/dashboard.html"), contextCaptor.capture(), eq(writer)); |
| 119 | + |
| 120 | + IContext thymeleafContext = contextCaptor.getValue(); |
| 121 | + |
| 122 | + // Verifies the explicit model locale takes precedence |
| 123 | + assertEquals(Locale.CANADA, thymeleafContext.getLocale()); |
| 124 | + } |
| 125 | +} |
0 commit comments