Search in sources :

Example 1 with TemplateResolver

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

the class ApplicationContextBuilder method buildViewContext.

protected ViewContext buildViewContext(EzyBeanContext beanContext) {
    ViewContext viewContext = beanContext.getSingleton(ViewContext.class);
    if (viewContext == null) {
        ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
        if (viewContextBuilder != null) {
            TemplateResolver templateResolver = beanContext.getSingleton(TemplateResolver.class);
            if (templateResolver == null) {
                templateResolver = TemplateResolver.of(beanContext);
            }
            viewContext = viewContextBuilder.templateResolver(templateResolver).viewDialects(beanContext.getSingletonsOf(ViewDialect.class)).viewDecorators(beanContext.getSingletonsOf(ViewDecorator.class)).messageProviders(beanContext.getSingletonsOf(MessageProvider.class)).absentMessageResolver(beanContext.getSingleton(AbsentMessageResolver.class)).build();
        }
    }
    if (viewContext != null) {
        beanContext.getSingletonFactory().addSingleton(viewContext);
    }
    return viewContext;
}
Also used : TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) AbsentMessageResolver(com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator)

Example 2 with TemplateResolver

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

the class TemplateResolverTest method test.

@Test
public void test() {
    // given
    String prefix = RandomUtil.randomShortAlphabetString();
    String suffix = RandomUtil.randomShortAlphabetString();
    int cacheTTLMs = RandomUtil.randomInt();
    boolean cacheable = RandomUtil.randomBoolean();
    String templateMode = RandomUtil.randomShortAlphabetString();
    String messagesLocation = RandomUtil.randomShortAlphabetString();
    // when
    TemplateResolver sut = TemplateResolver.builder().prefix(prefix).suffix(suffix).cacheTTLMs(cacheTTLMs).cacheable(cacheable).templateMode(templateMode).messagesLocation(messagesLocation).build();
    // then
    Asserts.assertEquals(prefix, sut.getPrefix());
    Asserts.assertEquals(suffix, sut.getSuffix());
    Asserts.assertEquals(cacheTTLMs, sut.getCacheTTLMs());
    Asserts.assertEquals(cacheable, sut.isCacheable());
    Asserts.assertEquals(templateMode, sut.getTemplateMode());
    Asserts.assertEquals(messagesLocation, sut.getMessagesLocation());
}
Also used : TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) Test(org.testng.annotations.Test)

Example 3 with TemplateResolver

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

the class ThymeleafViewContextTest method test.

@Test
public void test() throws Exception {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).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);
}
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) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 4 with TemplateResolver

use of com.tvd12.ezyhttp.server.core.view.TemplateResolver 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 5 with TemplateResolver

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

the class ThymeleafViewContextBuilderTest method test.

@Test
public void test() {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewContextBuilder sut = new ThymeleafViewContextBuilder().templateResolver(resolver);
    // when
    ViewContext viewContext = sut.build();
    // then
    Asserts.assertNotNull(viewContext);
}
Also used : TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) Test(org.testng.annotations.Test)

Aggregations

TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)8 ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)7 Test (org.testng.annotations.Test)7 ThymeleafViewContextBuilder (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder)5 View (com.tvd12.ezyhttp.server.core.view.View)4 PrintWriter (java.io.PrintWriter)4 ServletContext (javax.servlet.ServletContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)3 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)3 AbsentMessageResolver (com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver)2 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)1 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)1 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)1 MessageProvider (com.tvd12.ezyhttp.server.core.view.MessageProvider)1 LayoutDialect (nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect)1 TemplateEngine (org.thymeleaf.TemplateEngine)1 IDialect (org.thymeleaf.dialect.IDialect)1