- Revision
- 656
- Author
- mauro
- Date
- 2008-04-29 05:31:42 -0500 (Tue, 29 Apr 2008)
Log Message
Made day and time property name matches configurable via regex.
Modified Paths
- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java
- trunk/waffle-core/src/test/resources/FakeResourceBundle.properties
Diff
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java (655 => 656)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java 2008-04-28 14:14:11 UTC (rev 655) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java 2008-04-29 10:31:42 UTC (rev 656) @@ -29,10 +29,14 @@ * (message defaults to [EMAIL PROTECTED] #DEFAULT_DATE_MISSING_MESSAGE})</li> * <li>"date.format" ([EMAIL PROTECTED] #DATE_FORMAT_KEY}): date format used in parsing (defaults to * [EMAIL PROTECTED] #DEFAULT_DATE_FORMAT})</li> - * <li>"date.format.day" ([EMAIL PROTECTED] #DAY_FORMAT_KEY}): date format used in parsing properties that end in "Day" - * (defaults to [EMAIL PROTECTED] #DEFAULT_DAY_FORMAT})</li> - * <li>"date.format.time" ([EMAIL PROTECTED] #TIME_FORMAT_KEY}): date format used in parsing properties that end in "Time" - * (defaults to [EMAIL PROTECTED] #DEFAULT_TIME_FORMAT})</li> + * <li>"date.format.day" ([EMAIL PROTECTED] #DAY_FORMAT_KEY}): date format used in parsing values whose property name matches + * the day name (defaults to [EMAIL PROTECTED] #DEFAULT_DAY_FORMAT})</li> + * <li>"date.format.day.name" ([EMAIL PROTECTED] #DAY_NAME_KEY}): regex to match the day name (defaults to + * [EMAIL PROTECTED] #DEFAULT_DAY_NAME})</li> + * <li>"date.format.time" ([EMAIL PROTECTED] #TIME_FORMAT_KEY}): date format used in parsing values whose property name matches + * the time name (defaults to [EMAIL PROTECTED] #DEFAULT_TIME_FORMAT})</li> + * <li>"date.format.time.name" ([EMAIL PROTECTED] #TIME_NAME_KEY}): regex to match the time name (defaults to + * [EMAIL PROTECTED] #DEFAULT_TIME_NAME})</li> * </ul> * The patterns are also optionally injectable via <code>Properties</code> in the constructor and take precedence over * the ones configured in the messages resources. @@ -41,18 +45,20 @@ * @author Mauro Talevi */ public class DateValueConverter extends AbstractValueConverter { - static final String BIND_ERROR_DATE_KEY = "bind.error.date"; - static final String BIND_ERROR_DATE_MISSING_KEY = "bind.error.date.missing"; - static final String DATE_FORMAT_KEY = "date.format"; - static final String DAY_FORMAT_KEY = "date.format.day"; - static final String TIME_FORMAT_KEY = "date.format.time"; - static final String DAY_SUFFIX = "Day"; - static final String TIME_SUFFIX = "Time"; - static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy"; - static final String DEFAULT_DAY_FORMAT = "dd/MM/yyyy"; - static final String DEFAULT_TIME_FORMAT = "dd/MM/yyyy HH:mm:ss"; - static final String DEFAULT_DATE_MESSAGE = "Invalid date {1} (using format {2}) for field {0}"; - static final String DEFAULT_DATE_MISSING_MESSAGE = "Missing date value for field {0}"; + public static final String BIND_ERROR_DATE_KEY = "bind.error.date"; + public static final String BIND_ERROR_DATE_MISSING_KEY = "bind.error.date.missing"; + public static final String DATE_FORMAT_KEY = "date.format"; + public static final String DAY_FORMAT_KEY = "date.format.day"; + public static final String DAY_NAME_KEY = "date.format.day.name"; + public static final String TIME_FORMAT_KEY = "date.format.time"; + public static final String TIME_NAME_KEY = "date.format.time.name"; + public static final String DEFAULT_DAY_FORMAT = "dd/MM/yyyy"; + public static final String DEFAULT_DAY_NAME = ".*Day"; + public static final String DEFAULT_TIME_FORMAT = "dd/MM/yyyy HH:mm:ss"; + public static final String DEFAULT_TIME_NAME = ".*Time"; + public static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy"; + public static final String DEFAULT_DATE_MESSAGE = "Invalid date {1} (using format {2}) for field {0}"; + public static final String DEFAULT_DATE_MISSING_MESSAGE = "Missing date value for field {0}"; private Properties patterns; @@ -111,24 +117,24 @@ return new SimpleDateFormat(pattern); } - private String patternFor(String key, String defaultPattern) { - if ( patterns.containsKey(key)) { - return patterns.getProperty(key); - } - return messageFor(key, defaultPattern); - } - private DateType dateType(String propertyName) { - if (endsWith(propertyName, DAY_SUFFIX)) { + if (matches(propertyName, patternFor(DAY_NAME_KEY , DEFAULT_DAY_NAME))) { return DateType.DAY; - } else if (endsWith(propertyName, TIME_SUFFIX)) { + } else if (matches(propertyName, patternFor(TIME_NAME_KEY , DEFAULT_TIME_NAME))) { return DateType.TIME; } return DateType.DATE; } - private boolean endsWith(String propertyName, String suffix) { - return propertyName != null && propertyName.endsWith(suffix); + private boolean matches(String propertyName, String regex) { + return propertyName != null && propertyName.matches(regex); } + private String patternFor(String key, String defaultPattern) { + if ( patterns.containsKey(key)) { + return patterns.getProperty(key); + } + return messageFor(key, defaultPattern); + } + }
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java (655 => 656)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java 2008-04-28 14:14:11 UTC (rev 655) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/DateValueConverterTest.java 2008-04-29 10:31:42 UTC (rev 656) @@ -5,11 +5,16 @@ import static org.codehaus.waffle.bind.converters.DateValueConverter.BIND_ERROR_DATE_MISSING_KEY; import static org.codehaus.waffle.bind.converters.DateValueConverter.DATE_FORMAT_KEY; import static org.codehaus.waffle.bind.converters.DateValueConverter.DAY_FORMAT_KEY; +import static org.codehaus.waffle.bind.converters.DateValueConverter.DAY_NAME_KEY; import static org.codehaus.waffle.bind.converters.DateValueConverter.DEFAULT_DATE_FORMAT; import static org.codehaus.waffle.bind.converters.DateValueConverter.DEFAULT_DATE_MESSAGE; import static org.codehaus.waffle.bind.converters.DateValueConverter.DEFAULT_DATE_MISSING_MESSAGE; +import static org.codehaus.waffle.bind.converters.DateValueConverter.DEFAULT_DAY_FORMAT; +import static org.codehaus.waffle.bind.converters.DateValueConverter.DEFAULT_TIME_FORMAT; import static org.codehaus.waffle.bind.converters.DateValueConverter.TIME_FORMAT_KEY; +import static org.codehaus.waffle.bind.converters.DateValueConverter.TIME_NAME_KEY; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -46,40 +51,47 @@ DateValueConverter converter = new DateValueConverter(new DefaultMessageResources()); assertTrue(converter.accept(Date.class)); assertTrue(converter.accept(java.sql.Date.class)); + assertFalse(converter.accept(Object.class)); } @Test - public void canConvertWithDateFormatConfiguredViaMessageResource() { + public void canConvertWithDefaultDateFormats() { + DateValueConverter converter = new DateValueConverter(new DefaultMessageResources()); + assertDateFormattable("04/03/2008", DEFAULT_DATE_FORMAT, converter.convertValue("property-name", "04/03/2008", + Date.class)); + assertDateFormattable("04/03/2008", DEFAULT_DAY_FORMAT, converter.convertValue("propertyDay", "04/03/2008", + Date.class)); + assertDateFormattable("04/03/2008 11:11:11", DEFAULT_TIME_FORMAT, converter.convertValue("propertyTime", + "04/03/2008 11:11:11", Date.class)); + } + + @Test + public void canConvertWithDateFormatsConfiguredViaMessageResource() { DefaultMessageResources resources = new DefaultMessageResources(configuration); DateValueConverter converter = new DateValueConverter(resources); assertDateFormattable("04-03-2008", resources.getMessage(DATE_FORMAT_KEY), converter.convertValue( "property-name", "04-03-2008", Date.class)); - assertDateFormattable("04", resources.getMessage(DAY_FORMAT_KEY), converter.convertValue("someDay", "04", + assertDateFormattable("04", resources.getMessage(DAY_FORMAT_KEY), converter.convertValue("day-property", "04", Date.class)); - assertDateFormattable("11:11:11", resources.getMessage(TIME_FORMAT_KEY), converter.convertValue("someTime", - "11:11:11", Date.class)); + assertDateFormattable("11:11:11", resources.getMessage(TIME_FORMAT_KEY), converter.convertValue( + "time-property", "11:11:11", Date.class)); } @Test - public void canConvertWithDateFormatConfiguredViaProperties() { + public void canConvertWithDateFormatsConfiguredViaProperties() { Properties patterns = new Properties(); - patterns.setProperty(DateValueConverter.DATE_FORMAT_KEY, "dd-MM-yyyy"); - patterns.setProperty(DateValueConverter.DAY_FORMAT_KEY, "dd"); - patterns.setProperty(DateValueConverter.TIME_FORMAT_KEY, "HH:mm:ss"); + patterns.setProperty(DATE_FORMAT_KEY, "dd-MM-yyyy"); + patterns.setProperty(DAY_FORMAT_KEY, "dd"); + patterns.setProperty(TIME_FORMAT_KEY, "HH:mm:ss"); + patterns.setProperty(DAY_NAME_KEY, "day.*"); + patterns.setProperty(TIME_NAME_KEY, "time.*"); DateValueConverter converter = new DateValueConverter(new DefaultMessageResources(), patterns); assertDateFormattable("04-03-2008", "dd-MM-yyyy", converter.convertValue("property-name", "04-03-2008", Date.class)); - assertDateFormattable("04", "dd", converter.convertValue("someDay", "04", Date.class)); - assertDateFormattable("11:11:11", "HH:mm:ss", converter.convertValue("someTime", "11:11:11", Date.class)); + assertDateFormattable("04", "dd", converter.convertValue("day-property", "04", Date.class)); + assertDateFormattable("11:11:11", "HH:mm:ss", converter.convertValue("time-property", "11:11:11", Date.class)); } - @Test - public void canConvertWithDefaultDateFormat() { - DateValueConverter converter = new DateValueConverter(new DefaultMessageResources()); - assertDateFormattable("04/03/2008", DEFAULT_DATE_FORMAT, converter.convertValue("property-name", "04/03/2008", - Date.class)); - } - private void assertDateFormattable(String value, String pattern, Date date) { assertEquals(value, new SimpleDateFormat(pattern).format(date)); }
Modified: trunk/waffle-core/src/test/resources/FakeResourceBundle.properties (655 => 656)
--- trunk/waffle-core/src/test/resources/FakeResourceBundle.properties 2008-04-28 14:14:11 UTC (rev 655) +++ trunk/waffle-core/src/test/resources/FakeResourceBundle.properties 2008-04-29 10:31:42 UTC (rev 656) @@ -2,7 +2,9 @@ foo.bar=hello {0} date.format=dd-MM-yyyy date.format.day=dd +date.format.day.name=day.* date.format.time=HH:mm:ss +date.format.time.name=time.* bind.error.date=Date {1} has invalid format {2} for field {0} bind.error.date.missing=Date field {0} is missing bind.error.list=No list values for field {0}
To unsubscribe from this list please visit:
