/** * <p>Validate using the specified <code>Locale</code>.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return <code>true</code> if the value is valid. */ @Override public boolean isValid(String value, String pattern, Locale locale) { Object parsedValue = parse(value, pattern, locale); return (parsedValue == null ? false : true); }
/** * <p>Parse the value using the specified pattern.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return The parsed value if valid or <code>null</code> if invalid. */ protected Object parse(String value, String pattern, Locale locale) { value = (value == null ? null : value.trim()); if (value == null || value.length() == 0) { return null; } Format formatter = getFormat(pattern, locale); return parse(value, formatter); }
/** * Test Min/Max values allowed */ public void testValidateMinMax() { DecimalFormat fmt = new DecimalFormat("#"); if (max != null) { assertEquals("Test Max", max, validator.parse(fmt.format(max), "#", null)); assertNull("Test Max + 1", validator.parse(fmt.format(maxPlusOne), "#", null)); assertEquals("Test Min", min, validator.parse(fmt.format(min), "#", null)); assertNull("Test min - 1", validator.parse(fmt.format(minMinusOne), "#", null)); } }
/** * Test different Locale */ public void testValidateLocale() { assertEquals("US Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, Locale.US)); assertNull("US Locale, DE Format", strictValidator.parse(testStringDE, null, Locale.US)); // Default German Locale assertEquals("DE Locale, DE Format", testNumber, strictValidator.parse(testStringDE, null, Locale.GERMAN)); assertNull("DE Locale, US Format", strictValidator.parse(testStringUS, null, Locale.GERMAN)); // Default Locale has been set to Locale.US in setup() assertEquals("Default Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, null)); assertNull("Default Locale, DE Format", strictValidator.parse(testStringDE, null, null)); }
/** * Test Invalid, strict=false */ public void testInvalidNotStrict() { for (int i = 0; i < invalid.length; i++) { String text = "idx=["+i+"] value=[" + invalid[i] + "]"; assertNull("(A) " + text, validator.parse(invalid[i], null, Locale.US)); assertFalse("(B) " + text, validator.isValid(invalid[i], null, Locale.US)); assertNull("(C) " + text, validator.parse(invalid[i], testPattern, null)); assertFalse("(D) " + text, validator.isValid(invalid[i], testPattern, null)); } }
/** * Test Valid, strict=false */ public void testValidNotStrict() { for (int i = 0; i < valid.length; i++) { String text = "idx=["+i+"] value=[" + validCompare[i] + "]"; assertEquals("(A) " + text, validCompare[i], validator.parse(valid[i], null, Locale.US)); assertTrue("(B) " + text, validator.isValid(valid[i], null, Locale.US)); assertEquals("(C) " + text, validCompare[i], validator.parse(valid[i], testPattern, null)); assertTrue("(D) " + text, validator.isValid(valid[i], testPattern, null)); } }
/** * Test Invalid, strict=true */ public void testInvalidStrict() { for (int i = 0; i < invalidStrict.length; i++) { String text = "idx=["+i+"] value=[" + invalidStrict[i] + "]"; assertNull("(A) " + text, strictValidator.parse(invalidStrict[i], null, Locale.US)); assertFalse("(B) " + text, strictValidator.isValid(invalidStrict[i], null, Locale.US)); assertNull("(C) " + text, strictValidator.parse(invalidStrict[i], testPattern, null)); assertFalse("(D) " + text, strictValidator.isValid(invalidStrict[i], testPattern, null)); } }
/** * Test Valid, strict=true */ public void testValidStrict() { for (int i = 0; i < validStrict.length; i++) { String text = "idx=["+i+"] value=[" + validStrictCompare[i] + "]"; assertEquals("(A) " + text, validStrictCompare[i], strictValidator.parse(validStrict[i], null, Locale.US)); assertTrue("(B) " + text, strictValidator.isValid(validStrict[i], null, Locale.US)); assertEquals("(C) " + text, validStrictCompare[i], strictValidator.parse(validStrict[i], testPattern, null)); assertTrue("(D) " + text, strictValidator.isValid(validStrict[i], testPattern, null)); } }
/** * <p>Validate using the specified <code>Locale</code>.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return <code>true</code> if the value is valid. */ public boolean isValid(String value, String pattern, Locale locale) { Object parsedValue = parse(value, pattern, locale); return (parsedValue == null ? false : true); }
/** * <p>Validate using the specified <code>Locale</code>.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return <code>true</code> if the value is valid. */ @Override public boolean isValid(String value, String pattern, Locale locale) { Object parsedValue = parse(value, pattern, locale); return (parsedValue == null ? false : true); }
/** * <p>Parse the value using the specified pattern.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return The parsed value if valid or <code>null</code> if invalid. */ protected Object parse(String value, String pattern, Locale locale) { value = (value == null ? null : value.trim()); if (value == null || value.length() == 0) { return null; } Format formatter = getFormat(pattern, locale); return parse(value, formatter); }
/** * <p>Parse the value using the specified pattern.</p> * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the <code>Locale</code> if <code>null</code>. * @param locale The locale to use for the date format, system default if null. * @return The parsed value if valid or <code>null</code> if invalid. */ protected Object parse(String value, String pattern, Locale locale) { value = (value == null ? null : value.trim()); if (value == null || value.length() == 0) { return null; } Format formatter = getFormat(pattern, locale); return parse(value, formatter); }