/** * <p>Compare Minutes (hours and minutes).</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * parameter's minutes are less than the seconds and +1 if the first * parameter's minutes are greater than. */ public int compareMinutes(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.MINUTE); }
/** * <p>Validate/convert a time using the specified <code>Locale</code> * default <code>TimeZone</code>. * * @param value The value validation is being performed on. * @param locale The locale to use for the time format, system default if null. * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. */ public Calendar validate(String value, Locale locale) { return (Calendar)parse(value, (String)null, locale, (TimeZone)null); }
@Override protected void setUp() throws Exception { super.setUp(); validator = new TimeValidator(); }
Calendar hourLess = createTime(GMT, testTime - hour, 100); // -1 hour assertEquals("mili LT", -1, validator.compareTime(value, milliGreater)); // > milli assertEquals("mili EQ", 0, validator.compareTime(value, value)); // same time assertEquals("mili GT", 1, validator.compareTime(value, milliLess)); // < milli assertEquals("secs LT", -1, validator.compareSeconds(value, secGreater)); // +1 sec assertEquals("secs =1", 0, validator.compareSeconds(value, milliGreater)); // > milli assertEquals("secs =2", 0, validator.compareSeconds(value, value)); // same time assertEquals("secs =3", 0, validator.compareSeconds(value, milliLess)); // < milli assertEquals("secs GT", 1, validator.compareSeconds(value, secLess)); // -1 sec assertEquals("mins LT", -1, validator.compareMinutes(value, minGreater)); // +1 min assertEquals("mins =1", 0, validator.compareMinutes(value, secGreater)); // +1 sec assertEquals("mins =2", 0, validator.compareMinutes(value, value)); // same time assertEquals("mins =3", 0, validator.compareMinutes(value, secLess)); // -1 sec assertEquals("mins GT", 1, validator.compareMinutes(value, minLess)); // -1 min assertEquals("hour LT", -1, validator.compareHours(value, hourGreater)); // +1 hour assertEquals("hour =1", 0, validator.compareHours(value, minGreater)); // +1 min assertEquals("hour =2", 0, validator.compareHours(value, value)); // same time assertEquals("hour =3", 0, validator.compareHours(value, minLess)); // -1 min assertEquals("hour GT", 1, validator.compareHours(value, hourLess)); // -1 hour
/** * Test Invalid Dates with "pattern" validation */ public void testPatternInvalid() { for (int i = 0; i < patternInvalid.length; i++) { String text = i + " value=[" +patternInvalid[i]+"] passed "; Object date = validator.validate(patternInvalid[i], "HH-mm-ss"); assertNull("validate() " + text + date, date); assertFalse("isValid() " + text, validator.isValid(patternInvalid[i], "HH-mm-ss")); } }
/** * Test Invalid Dates with "locale" validation */ public void testFormat() { // Set the default Locale Locale origDefault = Locale.getDefault(); Locale.setDefault(Locale.UK); Object test = TimeValidator.getInstance().validate("16:49:23", "HH:mm:ss"); assertNotNull("Test Date ", test); assertEquals("Format pattern", "16-49-23", validator.format(test, "HH-mm-ss")); assertEquals("Format locale", "4:49 PM", validator.format(test, Locale.US)); assertEquals("Format default", "16:49", validator.format(test)); // Restore the original default Locale.setDefault(origDefault); }
@Override public ValidationResult validate(MuleEvent event) { org.apache.commons.validator.routines.TimeValidator validator = org.apache.commons.validator.routines.TimeValidator.getInstance(); Locale locale = new Locale(this.locale); if (pattern != null) { if (!validator.isValid(time, pattern, locale)) { errorMessage = getMessages().invalidTime(time, this.locale, pattern); return fail(); } } else { if (!validator.isValid(time, locale)) { errorMessage = getMessages().invalidTime(time, this.locale, pattern); return fail(); } } return ok(); }
/** * Test Invalid Dates with "locale" validation */ public void testLocaleInvalid() { for (int i = 0; i < localeInvalid.length; i++) { String text = i + " value=[" +localeInvalid[i]+"] passed "; Object date = validator.validate(localeInvalid[i], Locale.US); assertNull("validate() " + text + date, date); assertFalse("isValid() " + text, validator.isValid(localeInvalid[i], Locale.UK)); } }
/** * <p>Validate/convert a time using the default <code>Locale</code> * and <code>TimeZone</code>. * * @param value The value validation is being performed on. * @return The parsed <code>Calendar</code> if valid or <code>null</code> * if invalid. */ public Calendar validate(String value) { return (Calendar)parse(value, (String)null, (Locale)null, (TimeZone)null); }
/** * <p>Compare Times (hour, minute, second and millisecond - not date).</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * time is less than the seconds and +1 if the first * time is greater than. */ public int compareTime(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.MILLISECOND); }
/** * Test Valid Dates with "locale" validation */ public void testLocaleValid() { for (int i = 0; i < localeValid.length; i++) { String text = i + " value=[" +localeValid[i]+"] failed "; Calendar calendar = validator.validate(localeValid[i], Locale.UK); assertNotNull("validate() " + text, calendar); Date date = calendar.getTime(); assertTrue("isValid() " + text, validator.isValid(localeValid[i], Locale.UK)); assertEquals("compare " + text, localeExpect[i], date); } }
/** * <p>Validate/convert a time using the specified <code>TimeZone</code> * and default <code>Locale</code>. * * @param value The value validation is being performed on. * @param timeZone The Time Zone used to parse the time, system default if null. * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. */ public Calendar validate(String value, TimeZone timeZone) { return (Calendar)parse(value, (String)null, (Locale)null, timeZone); }
/** * <p>Compare Hours.</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * parameter's hour is less than the seconds and +1 if the first * parameter's hour is greater than. */ public int compareHours(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.HOUR_OF_DAY); }
/** * Test Valid Dates with "pattern" validation */ public void testPatternValid() { for (int i = 0; i < patternValid.length; i++) { String text = i + " value=[" +patternValid[i]+"] failed "; Calendar calendar = validator.validate(patternValid[i], "HH-mm-ss"); assertNotNull("validateObj() " + text, calendar); Date date = calendar.getTime(); assertTrue("isValid() " + text, validator.isValid(patternValid[i], "HH-mm-ss")); assertEquals("compare " + text, patternExpect[i], date); } }
/** * <p>Validate/convert a time using the specified <i>pattern</i> and * default <code>TimeZone</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against. * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. */ public Calendar validate(String value, String pattern) { return (Calendar)parse(value, pattern, (Locale)null, (TimeZone)null); }
/** * <p>Compare Seconds (hours, minutes and seconds).</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * parameter's seconds are less than the seconds and +1 if the first * parameter's seconds are greater than. */ public int compareSeconds(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.SECOND); }
/** * <p>Validate/convert a time using the specified <i>pattern</i> * and <code>TimeZone</code>. * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against. * @param timeZone The Time Zone used to parse the time, system default if null. * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. */ public Calendar validate(String value, String pattern, TimeZone timeZone) { return (Calendar)parse(value, pattern, (Locale)null, timeZone); }
/** * <p>Compare Hours.</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * parameter's hour is less than the seconds and +1 if the first * parameter's hour is greater than. */ public int compareHours(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.HOUR_OF_DAY); }
/** * <p>Validate/convert a time using the specified specified <code>Locale</code> * and <code>TimeZone</code>. * * @param value The value validation is being performed on. * @param locale The locale to use for the time format, system default if null. * @param timeZone The Time Zone used to parse the time, system default if null. * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. */ public Calendar validate(String value, Locale locale, TimeZone timeZone) { return (Calendar)parse(value, (String)null, locale, timeZone); }
/** * <p>Compare Seconds (hours, minutes and seconds).</p> * * @param value The <code>Calendar</code> value to check. * @param compare The <code>Calendar</code> to compare the value to. * @return Zero if the hours are equal, -1 if first * parameter's seconds are less than the seconds and +1 if the first * parameter's seconds are greater than. */ public int compareSeconds(Calendar value, Calendar compare) { return compareTime(value, compare, Calendar.SECOND); }