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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations