public DummyThemeSource() { this.messageSource = new StaticMessageSource(); this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message"); this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}"); }
@Override @Nullable protected MessageFormat resolveCode(String code, Locale locale) { String key = code + '_' + locale.toString(); String msg = this.messages.get(key); if (msg == null) { return null; } synchronized (this.cachedMessageFormats) { MessageFormat messageFormat = this.cachedMessageFormats.get(key); if (messageFormat == null) { messageFormat = createMessageFormat(msg, locale); this.cachedMessageFormats.put(key, messageFormat); } return messageFormat; } }
@Test public void nestedMessageSourceWithParamInChild() { StaticMessageSource source = new StaticMessageSource(); StaticMessageSource parent = new StaticMessageSource(); source.setParentMessageSource(parent); source.addMessage("param", Locale.ENGLISH, "value"); parent.addMessage("with.param", Locale.ENGLISH, "put {0} here"); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable( new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")}); assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH)); }
@Test public void testRejectWithoutDefaultMessage() { TestBean tb = new TestBean(); tb.setName("myName"); tb.setAge(99); BeanPropertyBindingResult ex = new BeanPropertyBindingResult(tb, "tb"); ex.reject("invalid"); ex.rejectValue("age", "invalidField"); StaticMessageSource ms = new StaticMessageSource(); ms.addMessage("invalid", Locale.US, "general error"); ms.addMessage("invalidField", Locale.US, "invalid field"); assertEquals("general error", ms.getMessage(ex.getGlobalError(), Locale.US)); assertEquals("invalid field", ms.getMessage(ex.getFieldError("age"), Locale.US)); }
/** * Create a new StaticApplicationContext with the given parent. * @see #registerSingleton * @see #registerPrototype * @see #registerBeanDefinition * @see #refresh */ public StaticApplicationContext(@Nullable ApplicationContext parent) throws BeansException { super(parent); // Initialize and register a StaticMessageSource. this.staticMessageSource = new StaticMessageSource(); getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.staticMessageSource); }
/** * Associate the given message values with the given keys as codes. * @param messages the messages to register, with messages codes * as keys and message texts as values * @param locale the locale that the messages should be found within */ public void addMessages(Map<String, String> messages, Locale locale) { Assert.notNull(messages, "Messages Map must not be null"); messages.forEach((code, msg) -> addMessage(code, locale, msg)); }
@Test public void testRequiredFieldsValidation() { TargetWithValidatedMap target = new TargetWithValidatedMap(); BindingResult result = bind(target, "info:\n foo: bar"); assertEquals(2, result.getErrorCount()); for (FieldError error : result.getFieldErrors()) { System.err.println(new StaticMessageSource().getMessage(error, Locale.getDefault())); } }
private MessageSource buildMessageSource(Map<String,String> messages, Set<Locale> availableLocales) { StaticMessageSource themeMessageSource = new StaticMessageSource(); availableLocales.forEach(l -> themeMessageSource.addMessages(messages, l)); return themeMessageSource; }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); m.put("name", "Roderick"); parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); StaticMessageSource messageSource = sac.getStaticMessageSource(); Map<String, String> usMessages = new HashMap<>(3); usMessages.put("message.format.example1", MSG_TXT1_US); usMessages.put("message.format.example2", MSG_TXT2_US); usMessages.put("message.format.example3", MSG_TXT3_US); messageSource.addMessages(usMessages, Locale.US); messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK); return sac; }
@Test // SPR-13406 public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedLogicalFieldName() { TestBean testBean = new TestBean(); testBean.setEmail("test@example.com"); testBean.setConfirmEmail("TEST@EXAMPLE.IO"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("email"), is(1)); assertThat(errors.getFieldValue("email"), is("test@example.com")); assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), is("email must be same value with confirmEmail")); assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), is("Email required")); }
@Test // SPR-15123 public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMessageFormat() { messageSource.setAlwaysUseMessageFormat(true); TestBean testBean = new TestBean(); testBean.setEmail("test@example.com"); testBean.setConfirmEmail("TEST@EXAMPLE.IO"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("email"), is(1)); assertThat(errors.getFieldValue("email"), is("test@example.com")); assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), is("email must be same value with confirmEmail")); assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), is("Email required")); }
/** * Create a new StaticApplicationContext with the given parent. * @see #registerSingleton * @see #registerPrototype * @see #registerBeanDefinition * @see #refresh */ public StaticApplicationContext(@Nullable ApplicationContext parent) throws BeansException { super(parent); // Initialize and register a StaticMessageSource. this.staticMessageSource = new StaticMessageSource(); getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.staticMessageSource); }
/** * Associate the given message with the given code. * @param code lookup code * @param locale locale message should be found within * @param defaultMessage message associated with this lookup code * @see #getStaticMessageSource */ public void addMessage(String code, Locale locale, String defaultMessage) { getStaticMessageSource().addMessage(code, locale, defaultMessage); }
@Test // SPR-13406 public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() { TestBean testBean = new TestBean(); testBean.setPassword("password"); testBean.setConfirmPassword("PASSWORD"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldValue("password"), is("password")); assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Password must be same value with Password(Confirm)")); }
@Test // SPR-15123 public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMessageFormat() { messageSource.setAlwaysUseMessageFormat(true); TestBean testBean = new TestBean(); testBean.setEmail("test@example.com"); testBean.setConfirmEmail("TEST@EXAMPLE.IO"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("email"), is(1)); assertThat(errors.getFieldValue("email"), is("test@example.com")); assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); FieldError error1 = errors.getFieldError("email"); FieldError error2 = errors.getFieldError("confirmEmail"); assertNotNull(error1); assertNotNull(error2); assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail")); assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required")); assertTrue(error1.contains(ConstraintViolation.class)); assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email")); assertTrue(error2.contains(ConstraintViolation.class)); assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail")); }
@Bean public MessageSource messageSource() { StaticMessageSource messageSource = new StaticMessageSource(); messageSource.addMessage("exception.user.exists", Locale.ENGLISH, "User already exists!"); return messageSource; } }
@Bean(name = "messageSource") public MessageSource messageSource() { StaticMessageSource messageSource = new StaticMessageSource(); return messageSource; }
@Test public void nestedMessageSourceWithParamInParent() { StaticMessageSource source = new StaticMessageSource(); StaticMessageSource parent = new StaticMessageSource(); source.setParentMessageSource(parent); parent.addMessage("param", Locale.ENGLISH, "value"); source.addMessage("with.param", Locale.ENGLISH, "put {0} here"); MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable( new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")}); assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH)); }
/** * Associate the given message values with the given keys as codes. * @param messages the messages to register, with messages codes * as keys and message texts as values * @param locale the locale that the messages should be found within */ public void addMessages(Map<String, String> messages, Locale locale) { Assert.notNull(messages, "Messages Map must not be null"); messages.forEach((code, msg) -> addMessage(code, locale, msg)); }
@Test // SPR-13406 public void testNoStringArgumentValue() { TestBean testBean = new TestBean(); testBean.setPassword("pass"); testBean.setConfirmPassword("pass"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldValue("password"), is("pass")); assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Size of Password is must be between 8 and 128")); }