ResultMessageUtils { public static String resolveMessage(ResultMessage message, MessageSource messageSource, Locale locale) throws NoSuchMessageException { Assert.notNull(messageSource, "messageSource must not be null!"); Assert.notNull(message, "message must not be null!"); Assert.notNull(locale, "locale must not be null!"); String msg; String code = message.getCode(); if (code != null) { try { msg = messageSource.getMessage(code, message.getArgs(), locale); } catch (NoSuchMessageException e) { String text = message.getText(); if (text != null) { if (logger.isDebugEnabled()) { logger.debug("messege is not found under code '" + code + "' for '" + locale + "'. use '" + text + "' instead", e); } msg = text; } else { throw e; } } } else { msg = message.getText(); } return msg; } private  ResultMessageUtils(); static String resolveMessage(ResultMessage message,
            MessageSource messageSource,
            Locale locale); static String resolveMessage(ResultMessage message,
            MessageSource messageSource);  }
@Test public void testResolveMessageLocaleNotPassed() { ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); when(message.getCode()).thenReturn("MSG001"); when(message.getArgs()).thenReturn(null); when(messageSource.getMessage("MSG001", null, Locale.getDefault())) .thenReturn("MESSAGE_TEXT"); String msg = ResultMessageUtils.resolveMessage(message, messageSource); assertThat(msg, is("MESSAGE_TEXT")); }
@Test public void testResolveMessageWithLocale() { ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); Locale locale = Locale.getDefault(); when(message.getCode()).thenReturn("MSG001"); when(message.getArgs()).thenReturn(null); when(messageSource.getMessage("MSG001", null, locale)).thenReturn( "MESSAGE_TEXT"); String msg = ResultMessageUtils.resolveMessage(message, messageSource, locale); assertThat(msg, is("MESSAGE_TEXT")); }
@Test public void testResolveMessageNullMessageCode() { ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); when(message.getCode()).thenReturn(null); when(message.getText()).thenReturn("MESSAGE_TEXT"); String msg = ResultMessageUtils.resolveMessage(message, messageSource); assertThat(msg, is("MESSAGE_TEXT")); }
@Test public void testResolveMessageWithNoSuchMessageException() { ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); Locale locale = Locale.getDefault(); when(message.getCode()).thenReturn("MSG001"); when(message.getArgs()).thenReturn(null); when(message.getText()).thenReturn("MESSAGE_TEXT"); when(messageSource.getMessage("MSG001", null, locale)).thenThrow( new NoSuchMessageException("MSG001")); String msg = ResultMessageUtils.resolveMessage(message, messageSource); assertThat(msg, is("MESSAGE_TEXT")); }
@Test(expected = NoSuchMessageException.class) public void testNoSuchMessageException() { ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); Locale locale = Locale.getDefault(); when(message.getCode()).thenReturn("MSG001"); when(message.getArgs()).thenReturn(null); when(message.getText()).thenReturn(null); when(messageSource.getMessage("MSG001", null, locale)).thenThrow( new NoSuchMessageException("MSG001")); ResultMessageUtils.resolveMessage(message, messageSource); }
@Test public void testResolveMessageIsDebugEnabledFalse() throws Exception { LogLevelChangeUtil.setLogLevel(LogLevelChangeUtil.LogLevel.INFO); ResultMessage message = mock(ResultMessage.class); MessageSource messageSource = mock(MessageSource.class); Locale locale = Locale.getDefault(); when(message.getCode()).thenReturn("MSG001"); when(message.getArgs()).thenReturn(null); when(message.getText()).thenReturn("MESSAGE_TEXT"); when(messageSource.getMessage("MSG001", null, locale)).thenThrow( new NoSuchMessageException("MSG001")); String msg = ResultMessageUtils.resolveMessage(message, messageSource, locale); assertThat(msg, is("MESSAGE_TEXT")); assertThat(logger.isDebugEnabled(), is(false)); LogLevelChangeUtil.resetLogLevel(); }