MessagesImpl implements Messages { @Override public Optional<String> get(String key, Context context, Optional<Result> result, Object... parameter) { Optional<String> language = lang.getLanguage(context, result); return get(key, language, parameter); } @Inject  MessagesImpl(NinjaProperties ninjaProperties,
                        Lang lang); @Override Optional<String> get(String key,
                      Context context,
                      Optional<Result> result,
                      Object... parameter); @Override Optional<String> get(String key, Optional<String> language, Object... params); @Override Map<Object, Object> getAll(Context context, Optional<Result> result); @Override Map<Object, Object> getAll(Optional<String> language); @Override String getWithDefault(String key,
                                 String defaultMessage,
                                 Context context,
                                 Optional<Result> result,
                                 Object... params); @Override String getWithDefault(String key,
                                 String defaultMessage,
                                 Optional<String> language,
                                 Object... params);  }
@Test public void testGetWithDefaultAndContextAndResult() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); result = Results.ok(); when(context.getAcceptLanguage()).thenReturn("en-US"); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); when(context.getAcceptLanguage()).thenReturn("en-CA"); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); when(context.getAcceptLanguage()).thenReturn("en-UK"); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); lang.setLanguage("de", result); assertEquals("Toröööö - das ist der platzhalter: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); lang.setLanguage("de-DE", result); assertEquals("Toröööö - das ist der platzhalter: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); result = Results.ok(); when(context.getCookie(Mockito.anyString())).thenReturn( Cookie.builder("name", "fr-FR").build()); assertEquals("c'est le placeholder: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); result = Results.ok(); lang.setLanguage("de-DE", result); assertEquals("Toröööö - das ist der platzhalter: test_parameter", messages.get("message_with_placeholder", context, Optional.of(result), "test_parameter").get()); }
@Test public void testGetWithSpecialI18nPlaceholder() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Date DATE_1970_JAN = new DateTime(1970, 1, 1, 1, 1).toDate(); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); Optional<String> language = Optional.empty(); Optional<String> result = messages.get("message_with_placeholder_date", language, DATE_1970_JAN); assertEquals("that's a date: Jan 1, 1970", result.get()); language = Optional.of("de"); result = messages.get("message_with_placeholder_date", language, DATE_1970_JAN); assertEquals("das ist ein datum: 01.01.1970", result.get()); language = Optional.of("fr-FR"); result = messages.get("message_with_placeholder_date", language, DATE_1970_JAN); assertEquals("c'est la date: 1 janv. 1970", result.get()); language = Optional.of("en"); result = messages.get("message_with_placeholder_date", language, DATE_1970_JAN); assertEquals("that's a date: Jan 1, 1970", result.get()); }
@Test public void testCorrectParsingOfDelimitersInPropertiesFiles() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); assertEquals("prop1, prop2, prop3", messages.get("a_propert_with_commas", Optional.of("en-US")).get()); }
@Test public void testGetWithLanguage() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); assertEquals("english", messages.get("language", Optional.of("en-US")).get()); assertEquals("english", messages.get("language", Optional.of("en-CA")).get()); assertEquals("english", messages.get("language", Optional.of("en-UK")).get()); assertEquals("deutsch", messages.get("language", Optional.of("de")).get()); assertEquals("deutsch", messages.get("language", Optional.of("de-DE")).get()); assertEquals("français", messages.get("language", Optional.of("fr-FR")).get()); assertEquals("français", messages.get("language", Optional.of("da,fr-FR;q=0.8")).get()); assertEquals("français", messages.get("language", Optional.of("da;q=0.9, fr-FR; q=0.8")).get()); assertEquals("deutsch", messages.get("language", Optional.of("de,fr-FR;q=0.8")).get()); assertEquals("deutsch", messages.get("language", Optional.of("de;q=0.9, fr-FR; q=0.8")).get()); assertEquals("defaultlanguage", messages.get("language", Optional.of("fr")).get()); assertEquals(Optional.empty(), messages.get("a_non_existing_key", Optional.of("fr"))); }
@Test public void testGetWithContextAndResult() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); result = Results.ok(); when(context.getAcceptLanguage()).thenReturn("en-US"); assertEquals("english", messages.get("language", context, Optional.of(result)).get()); when(context.getAcceptLanguage()).thenReturn("en-CA"); assertEquals("english", messages.get("language", context, Optional.of(result)).get()); when(context.getAcceptLanguage()).thenReturn("en-UK"); assertEquals("english", messages.get("language", context, Optional.of(result)).get()); lang.setLanguage("de", result); assertEquals("deutsch", messages.get("language", context, Optional.of(result)).get()); result = Results.ok(); lang.setLanguage("de-DE", result); assertEquals("deutsch", messages.get("language", context, Optional.of(result)).get()); result = Results.ok(); when(context.getCookie(Mockito.anyString())).thenReturn( Cookie.builder("name", "fr-FR").build()); assertEquals("français", messages.get("language", context, Optional.of(result)).get()); result = Results.ok(); lang.setLanguage("de-DE", result); assertEquals("deutsch", messages.get("language", context, Optional.of(result)).get()); }
@Test public void testGetWithLanguageAndParameters() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)) .thenReturn(new String[] { "en", "de", "fr-FR" }); Lang lang = new LangImpl(ninjaProperties); Messages messages = new MessagesImpl(ninjaProperties, lang); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", Optional.of("en-US"), "test_parameter").get()); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", Optional.of("en-CA"), "test_parameter").get()); assertEquals("this is the placeholder: test_parameter", messages.get("message_with_placeholder", Optional.of("en-UK"), "test_parameter").get()); assertEquals("Toröööö - das ist der platzhalter: test_parameter", messages.get("message_with_placeholder", Optional.of("de"), "test_parameter").get()); assertEquals("Toröööö - das ist der platzhalter: test_parameter", messages.get("message_with_placeholder", Optional.of("de-DE"), "test_parameter").get()); }