TemplateEngineFreemarkerI18nMethod implements
        TemplateMethodModelEx { public TemplateModel exec(List args) throws TemplateModelException { if (args.size() == 1 && args.get(0) instanceof StringModel && ((StringModel) args.get(0)).getWrappedObject() instanceof ConstraintViolation) { ConstraintViolation violation = (ConstraintViolation) ((StringModel) args.get(0)).getWrappedObject(); String messageValue = messages .get(violation.getMessageKey(), context, result, violation.getMessageParams()) .orElse(violation.getDefaultMessage()); return new SimpleScalar(messageValue); } else if (args.size() == 1) { String messageKey = ((SimpleScalar) args.get(0)).getAsString(); String messageValue = messages .get(messageKey, context, result) .orElse(messageKey); logIfMessageKeyIsMissing(messageKey, messageValue); return new SimpleScalar(messageValue); } else if (args.size() > 1) { List<String> strings = Lists.newArrayList(); for (Object o : args) { if (o instanceof SimpleScalar) { strings.add(((SimpleScalar) o).getAsString()); } else if (o instanceof SimpleNumber) { strings.add(((SimpleNumber) o).toString()); } } String messageKey = strings.get(0); String messageValue = messages.get( messageKey, context, result, strings.subList(1, strings.size()).toArray()) .orElse(messageKey); logIfMessageKeyIsMissing(messageKey, messageValue); return new SimpleScalar(messageValue); } else { throw new TemplateModelException( "Using i18n without any key is not possible."); } } TemplateEngineFreemarkerI18nMethod(Messages messages,
                                              Context context,
                                              Result result); TemplateModel exec(List args); void logIfMessageKeyIsMissing(
            String messageKey,
            String messageValue); final static Logger logger; }
@Test public void testThatNoKeyYieldsException() throws Exception { List args = Collections.EMPTY_LIST; thrown.expect(TemplateModelException.class); templateEngineFreemarkerI18nMethod.exec(args); }
@Test public void testThatSingleKeyWithValueWorks() throws Exception { Optional<Result> resultOptional = Optional.of(result); Mockito.when( messages.get("my.message.key", context, resultOptional)) .thenReturn(Optional.of("This simulates the translated message!")); List args = new ArrayList(); args.add(new SimpleScalar("my.message.key")); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message!")); Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject()); }
@Test public void testThatSingleKeyWithMissingValueReturnsDefaultKey() throws Exception { Optional<Result> resultOptional = Optional.of(result); Mockito.when( messages.get("my.message.key", context, resultOptional)) .thenReturn(Optional.<String>empty()); List args = new ArrayList(); args.add(new SimpleScalar("my.message.key")); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("my.message.key")); Mockito.verify(mockAppender).doAppend(Matchers.anyObject()); }
@Test public void testThatKeyWithPlaceholderWorks() throws Exception { Optional<Result> resultOptional = Optional.of(result); List args = new ArrayList(); args.add(new SimpleScalar("my.message.key")); args.add(new SimpleScalar("1000")); Mockito.when( messages.get( Matchers.eq("my.message.key"), Matchers.eq(context), Matchers.eq(resultOptional), Matchers.any(Object.class))) .thenReturn(Optional.of("This simulates the translated message number 1000!")); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message number 1000!")); Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject()); }
@Test public void testThatKeyWithPlaceholderReturnsDefaultKeyWhenKeyCannotBeFound() throws Exception { Optional<Result> resultOptional = Optional.of(result); List args = new ArrayList(); args.add(new SimpleScalar("my.message.key")); args.add(new SimpleScalar("1000")); Mockito.when( messages.get( Matchers.eq("my.message.key"), Matchers.eq(context), Matchers.eq(resultOptional), Matchers.any(Object.class))) .thenReturn(Optional.<String>empty()); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("my.message.key")); Mockito.verify(mockAppender).doAppend(Matchers.anyObject()); }
@Test public void testThatConstraintViolationWorks() throws Exception { Optional<Result> resultOptional = Optional.of(result); Mockito.when( messages.get(IsInteger.KEY, context, resultOptional)) .thenReturn(Optional.of("This simulates the translated message!")); ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE); List args = new ArrayList(); args.add(new StringModel(violation, new BeansWrapper())); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message!")); Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject()); }
@Test public void testThatConstraintViolationWorksWithDefault() throws Exception { Optional<Result> resultOptional = Optional.of(result); Mockito.when( messages.get(IsInteger.KEY, context, resultOptional)) .thenReturn(Optional.empty()); ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE); List args = new ArrayList(); args.add(new StringModel(violation, new BeansWrapper())); TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args); assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("theField must be an integer")); Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject()); }