MessageSourceHelper implements Helper<String> { @Override public Object apply(final String code, final Options options) throws IOException { Object[] args = options.params; String defaultMessage = options.hash("default"); return messageSource.getMessage(code, args, defaultMessage, currentLocale()); } MessageSourceHelper(final MessageSource messageSource); @Override Object apply(final String code, final Options options);  }
@Test public void messageSource() throws IOException { String message = "Hola"; String code = "sayHi"; String defaultMessage = null; Object[] params = {}; @SuppressWarnings("unchecked") Map<String, Object> hash = createMock(Map.class); expect(hash.get("default")).andReturn(defaultMessage); Handlebars hbs = createMock(Handlebars.class); Context ctx = createMock(Context.class); Template fn = createMock(Template.class); Options options = new Options.Builder(hbs, "messageSource", TagType.VAR, ctx, fn) .setParams(params) .setHash(hash) .build(); MessageSource messageSource = createMock(MessageSource.class); expect(messageSource.getMessage(eq(code), eq(params), eq(defaultMessage), isA(Locale.class))).andReturn(message); replay(messageSource, hash); Object result = new MessageSourceHelper(messageSource).apply(code, options); assertEquals(message, result); verify(messageSource, hash); }
@Test public void messageSourceWithParams() throws IOException { String message = "Hola"; String code = "sayHi"; String defaultMessage = null; Object[] params = {1, 2, 3 }; @SuppressWarnings("unchecked") Map<String, Object> hash = createMock(Map.class); expect(hash.get("default")).andReturn(defaultMessage); Handlebars hbs = createMock(Handlebars.class); Context ctx = createMock(Context.class); Template fn = createMock(Template.class); Options options = new Options.Builder(hbs, "messageSource", TagType.VAR, ctx, fn) .setParams(params) .setHash(hash) .build(); MessageSource messageSource = createMock(MessageSource.class); expect(messageSource.getMessage(eq(code), eq(params), eq(defaultMessage), isA(Locale.class))).andReturn(message); replay(messageSource, hash); Object result = new MessageSourceHelper(messageSource).apply(code, options); assertEquals(message, result); verify(messageSource, hash); }
@Test public void messageSourceWithDefaulMessage() throws IOException { String message = "Hola"; String code = "sayHi"; String defaultMessage = "Aca viene el 3"; Object[] params = {1, 2, 3 }; @SuppressWarnings("unchecked") Map<String, Object> hash = createMock(Map.class); expect(hash.get("default")).andReturn(defaultMessage); Handlebars hbs = createMock(Handlebars.class); Context ctx = createMock(Context.class); Template fn = createMock(Template.class); Options options = new Options.Builder(hbs, "messageSource", TagType.VAR, ctx, fn) .setParams(params) .setHash(hash) .build(); MessageSource messageSource = createMock(MessageSource.class); expect(messageSource.getMessage(eq(code), eq(params), eq(defaultMessage), isA(Locale.class))).andReturn(message); replay(messageSource, hash); Object result = new MessageSourceHelper(messageSource).apply(code, options); assertEquals(message, result); verify(messageSource, hash); }