Search in sources :

Example 1 with ThymeleafMessageResolver

use of com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver in project ezyhttp by youngmonkeys.

the class ThymeleafMessageResolverTest method resolveMessageDefault.

@Test
public void resolveMessageDefault() {
    // 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("messages").messageProviders(Collections.singletonList(messageProvider)).build();
    ITemplateContext context = mock(ITemplateContext.class);
    when(context.getLocale()).thenReturn(new Locale("unkown"));
    Class<?> origin = getClass();
    // when
    String welcomeMessage = sut.resolveMessage(context, origin, "home.welcome", new Object[0]);
    String greetTitle = sut.resolveMessage(context, origin, "greet.title", new Object[] { "Monkey" });
    String greetMessage = sut.resolveMessage(context, origin, "greet.message", null);
    String unkown = sut.resolveMessage(context, origin, "unknown", new Object[0]);
    String foo = sut.resolveMessage(context, origin, "foo", new Object[0]);
    // then
    Asserts.assertEquals("Welcome to EzyHTTP", welcomeMessage);
    Asserts.assertEquals("Greet Monkey", greetTitle);
    Asserts.assertEquals("Great {0}!", greetMessage, false);
    Asserts.assertNull(unkown);
    Asserts.assertEquals("Bar", foo);
}
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 2 with ThymeleafMessageResolver

use of com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver in project ezyhttp by youngmonkeys.

the class ThymeleafMessageResolverTest method createAbsentMessageRepresentationOK.

@Test
public void createAbsentMessageRepresentationOK() {
    // 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)).build();
    ITemplateContext context = mock(ITemplateContext.class);
    when(context.getLocale()).thenReturn(new Locale("unkown"));
    Class<?> origin = getClass();
    // when
    String welcomeMessage = sut.createAbsentMessageRepresentation(context, origin, "home.welcome", new Object[0]);
    // then
    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 ThymeleafMessageResolver

use of com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver 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 4 with ThymeleafMessageResolver

use of com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver in project ezyhttp by youngmonkeys.

the class ThymeleafMessageResolverTest method resolveMessageEn.

@Test
public void resolveMessageEn() {
    // 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("messages").messageProviders(Collections.singletonList(messageProvider)).build();
    ITemplateContext context = mock(ITemplateContext.class);
    when(context.getLocale()).thenReturn(new Locale("en", "US"));
    Class<?> origin = getClass();
    // when
    String welcomeMessage = sut.resolveMessage(context, origin, "home.welcome", new Object[0]);
    String greetTitle = sut.resolveMessage(context, origin, "greet.title", new Object[] { "Monkey" });
    String greetMessage = sut.resolveMessage(context, origin, "greet.message", new Object[] { "Monkey" });
    String unkown = sut.resolveMessage(context, origin, "unknown", new Object[0]);
    String foo = sut.resolveMessage(context, origin, "foo", new Object[0]);
    // then
    Asserts.assertEquals("DEFAULT", sut.getName());
    Asserts.assertEquals(0, sut.getOrder());
    Asserts.assertEquals("Welcome to EzyHTTP en_US", welcomeMessage);
    Asserts.assertEquals("Greet Monkey", greetTitle);
    Asserts.assertEquals("Great Monkey!", greetMessage, false);
    Asserts.assertNull(unkown);
    Asserts.assertEquals("Bar", foo);
}
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 5 with ThymeleafMessageResolver

use of com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver in project ezyhttp by youngmonkeys.

the class ThymeleafMessageResolverTest method createAbsentMessageRepresentation.

@Test
public void createAbsentMessageRepresentation() {
    // 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("messages").messageProviders(Collections.singletonList(messageProvider)).build();
    ITemplateContext context = mock(ITemplateContext.class);
    when(context.getLocale()).thenReturn(new Locale("unkown"));
    Class<?> origin = getClass();
    // when
    String welcomeMessage = sut.createAbsentMessageRepresentation(context, origin, "home.welcome", new Object[0]);
    // then
    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)

Aggregations

MessageProvider (com.tvd12.ezyhttp.server.core.view.MessageProvider)5 ThymeleafMessageResolver (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafMessageResolver)5 Locale (java.util.Locale)5 Properties (java.util.Properties)5 Test (org.testng.annotations.Test)5 ITemplateContext (org.thymeleaf.context.ITemplateContext)5 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)1 LayoutDialect (nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect)1 TemplateEngine (org.thymeleaf.TemplateEngine)1 IDialect (org.thymeleaf.dialect.IDialect)1 TemplateMode (org.thymeleaf.templatemode.TemplateMode)1 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)1