diff --git a/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java b/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java index d089c1da7a1bb00335099d86d5714ad5f4a26534..0a99cd3077b520f7deb40c648f694fac221b5c95 100644 --- a/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java +++ b/common/source/java/ch/systemsx/cisd/common/converter/ConverterPool.java @@ -90,7 +90,7 @@ public final class ConverterPool // The setter registerConverter() will ensure that no converter can be entered that is of the wrong type. @SuppressWarnings("unchecked") - private <T> Converter<T> getConverter(Class<T> type) + private final <T> Converter<T> getConverter(Class<T> type) { return converters.get(type); } diff --git a/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java b/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java index 11b7f51f28917cee63c9babfc9dcc685727acd61..7617d7891bad605d78a94a3a67875cbc5bacfa81 100644 --- a/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java +++ b/common/source/java/ch/systemsx/cisd/common/converter/IdentityStringConverter.java @@ -17,13 +17,29 @@ package ch.systemsx.cisd.common.converter; /** - * The identity converter for <code>String</code>s. - * + * The identity converter for <code>String</code>s. + * * @author Bernd Rinn */ -public class IdentityStringConverter implements Converter<String> +public final class IdentityStringConverter implements Converter<String> { + private final String defaultValue; + + public IdentityStringConverter() + { + this(null); + } + + public IdentityStringConverter(final String defaultValue) + { + this.defaultValue = defaultValue; + } + + // + // Converter + // + public String convert(String value) { return value; @@ -36,11 +52,6 @@ public class IdentityStringConverter implements Converter<String> */ public String getDefaultValue() { - return null; - } - - public void setFormat(String format) - { + return defaultValue; } - } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/converter/DateConverterTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/converter/DateConverterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8ce6c7543c94beb9f4d0001eabac6997fe113cc7 --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/converter/DateConverterTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.converter; + +import java.util.Calendar; +import java.util.Date; + +import org.apache.commons.lang.time.DateUtils; +import org.testng.annotations.Test; + +/** + * Test cases for the {@link DateConverter}. + * + * @author Christian Ribeaud + */ +public final class DateConverterTest +{ + + @Test(expectedExceptions = + { AssertionError.class }) + public final void testConstructor() + { + new DateConverter(null); + } + + @Test(expectedExceptions = + { IllegalArgumentException.class }) + public final void testConstructorWithBadFormat() + { + new DateConverter("badFormat"); + } + + @Test + public final void testConvert() + { + DateConverter converter = new DateConverter("dd.MM.yyyy"); + Date date = converter.convert("08.11.1971"); + Calendar calendar = Calendar.getInstance(); + calendar.set(1971, Calendar.NOVEMBER, 8); + // Calendar saves the current time as well. + assert date.equals(calendar.getTime()) == false; + Calendar birthday = Calendar.getInstance(); + birthday.setTime(date); + assert DateUtils.isSameDay(calendar, birthday); + } + + @Test + public final void testGetDefault() { + // Explicit call to <code>getDefaultValue()</code> + Calendar today = Calendar.getInstance(); + DateConverter converter = new DateConverter("dd.MM.yyyy"); + Calendar result = Calendar.getInstance(); + result.setTime(converter.getDefaultValue()); + assert DateUtils.isSameDay(today, result); + // Implicit call to <code>getDefaultValue()</code> + result.setTime(converter.convert("notParsableDate")); + assert DateUtils.isSameDay(today, result); + } +} \ No newline at end of file