Search in sources :

Example 1 with ViewDecorator

use of com.tvd12.ezyhttp.server.core.view.ViewDecorator in project ezyhttp by youngmonkeys.

the class ThymeleafViewContext method render.

@Override
public void render(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, View view) throws IOException {
    for (ViewDecorator viewDecorator : viewDecorators) {
        viewDecorator.decorate(request, view);
    }
    WebContext ctx = new WebContext(request, response, servletContext, view.getLocale());
    ctx.setVariables(view.getVariables());
    templateEngine.process(view.getTemplate(), ctx, response.getWriter());
}
Also used : WebContext(org.thymeleaf.context.WebContext) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator)

Example 2 with ViewDecorator

use of com.tvd12.ezyhttp.server.core.view.ViewDecorator in project ezyhttp by youngmonkeys.

the class ThymeleafViewContextTest method renderWithViewDecorator.

@Test
public void renderWithViewDecorator() throws Exception {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewDecorator viewDecorator = mock(ViewDecorator.class);
    ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).viewDecorators(Collections.singletonList(viewDecorator)).build();
    ServletContext servletContext = mock(ServletContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    PrintWriter writer = mock(PrintWriter.class);
    when(response.getWriter()).thenReturn(writer);
    View view = View.builder().template("index.html").build();
    // when
    viewContext.render(servletContext, request, response, view);
    // then
    Asserts.assertNotNull(viewContext);
    verify(viewDecorator, times(1)).decorate(request, view);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 3 with ViewDecorator

use of com.tvd12.ezyhttp.server.core.view.ViewDecorator in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method buildViewContextTemplateResolverNotNullIsNull.

@Test
public void buildViewContextTemplateResolverNotNullIsNull() {
    // given
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    ViewContextBuilder viewContextBuilder = mock(ViewContextBuilder.class);
    when(beanContext.getSingleton(ViewContextBuilder.class)).thenReturn(viewContextBuilder);
    TemplateResolver templateResolver = mock(TemplateResolver.class);
    when(beanContext.getSingleton(TemplateResolver.class)).thenReturn(templateResolver);
    when(viewContextBuilder.templateResolver(templateResolver)).thenReturn(viewContextBuilder);
    ViewContext viewContext = mock(ViewContext.class);
    when(viewContextBuilder.build()).thenReturn(viewContext);
    EzySingletonFactory singletonFactory = mock(EzySingletonFactory.class);
    when(beanContext.getSingletonFactory()).thenReturn(singletonFactory);
    ViewDialect viewDialect = mock(ViewDialect.class);
    List<ViewDialect> viewDialects = Collections.singletonList(viewDialect);
    when(beanContext.getSingletonsOf(ViewDialect.class)).thenReturn(viewDialects);
    when(viewContextBuilder.viewDialects(viewDialects)).thenReturn(viewContextBuilder);
    ViewDecorator viewDecorator = mock(ViewDecorator.class);
    List<ViewDecorator> viewDecorators = Collections.singletonList(viewDecorator);
    when(beanContext.getSingletonsOf(ViewDecorator.class)).thenReturn(viewDecorators);
    when(viewContextBuilder.viewDecorators(viewDecorators)).thenReturn(viewContextBuilder);
    MessageProvider messageProvider = mock(MessageProvider.class);
    List<MessageProvider> messageProviders = Collections.singletonList(messageProvider);
    when(beanContext.getSingletonsOf(MessageProvider.class)).thenReturn(messageProviders);
    when(viewContextBuilder.messageProviders(messageProviders)).thenReturn(viewContextBuilder);
    AbsentMessageResolver absentMessageResolver = mock(AbsentMessageResolver.class);
    when(beanContext.getSingleton(AbsentMessageResolver.class)).thenReturn(absentMessageResolver);
    when(viewContextBuilder.absentMessageResolver(absentMessageResolver)).thenReturn(viewContextBuilder);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    ViewContext actual = MethodInvoker.create().object(sut).method("buildViewContext").param(EzyBeanContext.class, beanContext).invoke(ViewContext.class);
    // then
    Asserts.assertEquals(viewContext, actual);
    verify(beanContext, times(1)).getSingleton(ViewContext.class);
    verify(beanContext, times(1)).getSingleton(ViewContextBuilder.class);
    verify(beanContext, times(1)).getSingleton(TemplateResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDialect.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDecorator.class);
    verify(beanContext, times(1)).getSingleton(AbsentMessageResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(MessageProvider.class);
    verify(viewContextBuilder, times(1)).templateResolver(templateResolver);
    verify(viewContextBuilder, times(1)).viewDialects(viewDialects);
    verify(viewContextBuilder, times(1)).viewDecorators(viewDecorators);
    verify(viewContextBuilder, times(1)).messageProviders(messageProviders);
    verify(viewContextBuilder, times(1)).absentMessageResolver(absentMessageResolver);
    verify(viewContextBuilder, times(1)).build();
    verify(singletonFactory, times(1)).addSingleton(viewContext);
}
Also used : MessageProvider(com.tvd12.ezyhttp.server.core.view.MessageProvider) AbsentMessageResolver(com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) EzySingletonFactory(com.tvd12.ezyfox.bean.EzySingletonFactory) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewDialect(com.tvd12.ezyhttp.server.core.view.ViewDialect) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Aggregations

ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)3 TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)2 ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)2 Test (org.testng.annotations.Test)2 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)1 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)1 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)1 AbsentMessageResolver (com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver)1 MessageProvider (com.tvd12.ezyhttp.server.core.view.MessageProvider)1 View (com.tvd12.ezyhttp.server.core.view.View)1 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)1 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)1 ThymeleafViewContextBuilder (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder)1 PrintWriter (java.io.PrintWriter)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 WebContext (org.thymeleaf.context.WebContext)1