From 5ae20708bddc8f24eb7cda47e3c523ee41c46290 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Mon, 11 Jun 2007 13:38:12 +0000 Subject: [PATCH] add: - Unit test for DateConverter. change: - Possibility to set a default value in IdentityStingConverter constructor. minor: - private method made final. SVN: 448 --- .../cisd/common/converter/ConverterPool.java | 2 +- .../converter/IdentityStringConverter.java | 29 +++++--- .../common/converter/DateConverterTest.java | 73 +++++++++++++++++++ 3 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/converter/DateConverterTest.java 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 d089c1da7a1..0a99cd3077b 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 11b7f51f289..7617d7891ba 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 00000000000..8ce6c7543c9 --- /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 -- GitLab