LocaleBasedTimeConverter implements Converter<Time> { public Time convert(String value, Class<? extends Time> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatHour = DateFormat.getTimeInstance(DateFormat.SHORT, locale); try { if (isUncompleteTime(value)) { value = value + ":00"; } return new Time(formatHour.parse(value).getTime()); } catch (ParseException pe) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_time"), value)); } } LocaleBasedTimeConverter(Localization localization); Time convert(String value, Class<? extends Time> type,
			ResourceBundle bundle);  }
@Test public void shouldBeAbleToConvert() throws ParseException { when(request.getAttribute(LOCALE_KEY + ".request")).thenReturn("pt_br"); Date date = new SimpleDateFormat("HH:mm:ss").parse("23:52:00"); assertThat(converter.convert("23:52", Time.class, bundle), is(equalTo(date))); assertThat(converter.convert("23:52:00", Time.class, bundle), is(equalTo(date))); }
@Test public void shouldUseTheDefaultLocale() throws ParseException { when(request.getAttribute(LOCALE_KEY + ".request")).thenReturn(null); when(request.getSession()).thenReturn(session); when(session.getAttribute(LOCALE_KEY + ".session")). thenReturn(null); when(context.getAttribute(LOCALE_KEY + ".application")). thenReturn(null); when(context.getInitParameter(LOCALE_KEY)). thenReturn(null); when(request.getLocale()).thenReturn(null); Date date = new SimpleDateFormat("HH:mm:ss").parse("23:52:00"); String formattedHour = DateFormat.getTimeInstance(DateFormat.SHORT).format(date); assertThat(converter.convert(formattedHour, Time.class, bundle), is(equalTo(date))); }
@Test public void shouldBeAbleToConvertEmpty() { assertThat(converter.convert("", Time.class, bundle), is(nullValue())); }
@Test public void shouldBeAbleToConvertNull() { assertThat(converter.convert(null, Time.class, bundle), is(nullValue())); }
@Test public void shouldThrowExceptionWhenUnableToParse() { when(request.getAttribute(LOCALE_KEY + ".request")).thenReturn("pt_br"); try { converter.convert("25:dd:88", Time.class, bundle); } catch (ConversionError e) { assertThat(e.getMessage(), is(equalTo("25:dd:88 is not a valid time."))); } }