Search in sources :

Example 1 with AbsentMessageResolver

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

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

the class ThymeleafMessageResolverTest method createAbsentMessageRepresentationWithResolver.

@Test
public void createAbsentMessageRepresentationWithResolver() {
    // given
    MessageProvider messageProvider = mock(MessageProvider.class);
    Properties exmessages = new Properties();
    exmessages.setProperty("ex.hello", "Ex Hello");
    when(messageProvider.provide()).thenReturn(Collections.singletonMap("EN", exmessages));
    ThymeleafMessageResolver sut = ThymeleafMessageResolver.builder().messageLocation("unknow").messageProviders(Collections.singletonList(messageProvider)).absentMessageResolver(new TestAbsentMessageResolver()).build();
    ITemplateContext context = mock(ITemplateContext.class);
    when(context.getLocale()).thenReturn(new Locale("unkown"));
    Class<?> origin = getClass();
    // when
    String helloMessage = sut.createAbsentMessageRepresentation(context, origin, "hello", new Object[0]);
    String welcomeMessage = sut.createAbsentMessageRepresentation(context, origin, "home.welcome", new Object[0]);
    // then
    Asserts.assertEquals("Hello", helloMessage);
    Asserts.assertEquals("home.welcome", welcomeMessage);
}
Also used : ThymeleafMessageResolver(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver) Locale(java.util.Locale) MessageProvider(com.tvd12.ezyhttp.server.core.view.MessageProvider) Properties(java.util.Properties) ITemplateContext(org.thymeleaf.context.ITemplateContext) Test(org.testng.annotations.Test)

Example 3 with AbsentMessageResolver

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

the class ThymeleafViewContext method createTemplateEngine.

private TemplateEngine createTemplateEngine() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    TemplateMode templateMode = TemplateMode.valueOf(metadata.getTemplateMode());
    templateResolver.setTemplateMode(templateMode);
    templateResolver.setPrefix(metadata.getPrefix());
    templateResolver.setSuffix(metadata.getSuffix());
    templateResolver.setCacheTTLMs((long) metadata.getCacheTTLMs());
    templateResolver.setCacheable(metadata.isCacheable());
    ThymeleafMessageResolver messageResolver = ThymeleafMessageResolver.builder().messageLocation(metadata.getMessagesLocation()).messageProviders(messageProviders).absentMessageResolver(absentMessageResolver).build();
    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setMessageResolver(messageResolver);
    templateEngine.addDialect(new LayoutDialect());
    for (ViewDialect dialect : viewDialects) {
        if (dialect instanceof IDialect) {
            templateEngine.addDialect((IDialect) dialect);
        }
    }
    return templateEngine;
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateMode(org.thymeleaf.templatemode.TemplateMode) TemplateEngine(org.thymeleaf.TemplateEngine) ViewDialect(com.tvd12.ezyhttp.server.core.view.ViewDialect) IDialect(org.thymeleaf.dialect.IDialect) LayoutDialect(nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect)

Example 4 with AbsentMessageResolver

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

AbsentMessageResolver (com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver)2 MessageProvider (com.tvd12.ezyhttp.server.core.view.MessageProvider)2 TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)2 ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)2 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)2 ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)2 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)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 ThymeleafMessageResolver (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver)1 Locale (java.util.Locale)1 Properties (java.util.Properties)1 LayoutDialect (nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect)1 TemplateEngine (org.thymeleaf.TemplateEngine)1 ITemplateContext (org.thymeleaf.context.ITemplateContext)1 IDialect (org.thymeleaf.dialect.IDialect)1 TemplateMode (org.thymeleaf.templatemode.TemplateMode)1 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)1