Author: vgritsenko Date: Sat Sep 2 07:17:15 2006 New Revision: 439601 URL: http://svn.apache.org/viewvc?rev=439601&view=rev Log: BEANUTILS-239: Better implementation of SqlDateConverter. Modified SqlDateConverter, SqlTimeConverter and SqlTimestampConverter to accept java.util.Date and Calendar object instances. Added tests.
Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java (with props) jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java (with props) jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java (with props) jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java (with props) Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java?rev=439601&r1=439600&r2=439601&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java Sat Sep 2 07:17:15 2006 @@ -20,6 +20,8 @@ import java.sql.Date; +import java.util.Calendar; + import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; @@ -107,6 +109,14 @@ if (value instanceof Date) { return (value); + } + + if (value instanceof java.util.Date) { + return new Date(((java.util.Date)value).getTime()); + } + + if (value instanceof Calendar) { + return new Date(((Calendar)value).getTime().getTime()); } try { Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java?rev=439601&r1=439600&r2=439601&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java Sat Sep 2 07:17:15 2006 @@ -20,6 +20,9 @@ import java.sql.Time; +import java.util.Calendar; +import java.util.Date; + import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; @@ -107,6 +110,14 @@ if (value instanceof Time) { return (value); + } + + if (value instanceof Date) { + return new Time(((Date)value).getTime()); + } + + if (value instanceof Calendar) { + return new Time(((Calendar)value).getTime().getTime()); } try { Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java?rev=439601&r1=439600&r2=439601&view=diff ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java Sat Sep 2 07:17:15 2006 @@ -20,6 +20,9 @@ import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; + import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; @@ -107,6 +110,14 @@ if (value instanceof Timestamp) { return (value); + } + + if (value instanceof Date) { + return new Timestamp(((Date)value).getTime()); + } + + if (value instanceof Calendar) { + return new Timestamp(((Calendar)value).getTime().getTime()); } try { Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java?rev=439601&view=auto ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java Sat Sep 2 07:17:15 2006 @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.beanutils.converters; + +import java.util.Date; + +import junit.framework.TestCase; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.beanutils.ConversionException; + +/** + * Abstract base for <Date>Converter classes. + * + * @version $Revision$ $Date$ + */ + +public abstract class DateConverterTestBase extends TestCase { + + // ------------------------------------------------------------------------ + + public DateConverterTestBase(String name) { + super(name); + } + + // ------------------------------------------------------------------------ + + protected abstract Converter makeConverter(); + protected abstract Class getExpectedType(); + + // ------------------------------------------------------------------------ + + /** + * Assumes ConversionException in response to covert(getExpectedType(), null). + */ + public void testConvertNull() throws Exception { + try { + makeConverter().convert(getExpectedType(), null); + fail("Expected ConversionException"); + } catch(ConversionException e) { + // expected + } + } + + /** + * Assumes convert() returns some non-null + * instance of getExpectedType(). + */ + public void testConvertDate() throws Exception { + String[] message= { + "from Date", + "from Calendar", + "from SQL Date", + "from SQL Time", + "from SQL Timestamp" + }; + + long now = System.currentTimeMillis(); + + Object[] date = { + new Date(now), + new java.util.GregorianCalendar(), + new java.sql.Date(now), + new java.sql.Time(now), + new java.sql.Timestamp(now) + }; + + for (int i = 0; i < date.length; i++) { + Object val = makeConverter().convert(getExpectedType(), date[i]); + assertNotNull("Convert " + message[i] + " should not be null", val); + assertTrue("Convert " + message[i] + " should return a " + getExpectedType().getName(), + getExpectedType().isInstance(val)); + assertEquals("Convert " + message[i] + " should return a " + date[0], + now, ((Date) val).getTime()); + } + } +} Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DateConverterTestBase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java?rev=439601&view=auto ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java Sat Sep 2 07:17:15 2006 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.beanutils.converters; + +import java.sql.Date; + +import junit.framework.TestSuite; +import org.apache.commons.beanutils.Converter; + +/** + * Test Case for the [EMAIL PROTECTED] SqlDateConverter} class. + * + * @version $Revision$ $Date$ + */ + +public class SqlDateConverterTestCase extends DateConverterTestBase { + + public SqlDateConverterTestCase(String name) { + super(name); + } + + // ------------------------------------------------------------------------ + + public static TestSuite suite() { + return new TestSuite(SqlDateConverterTestCase.class); + } + + // ------------------------------------------------------------------------ + + protected Converter makeConverter() { + return new SqlDateConverter(); + } + + protected Class getExpectedType() { + return Date.class; + } + +} Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java?rev=439601&view=auto ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java Sat Sep 2 07:17:15 2006 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.beanutils.converters; + +import java.sql.Time; + +import junit.framework.TestSuite; +import org.apache.commons.beanutils.Converter; + +/** + * Test Case for the [EMAIL PROTECTED] SqlTimeConverter} class. + * + * @version $Revision$ $Date$ + */ + +public class SqlTimeConverterTestCase extends DateConverterTestBase { + + public SqlTimeConverterTestCase(String name) { + super(name); + } + + // ------------------------------------------------------------------------ + + public static TestSuite suite() { + return new TestSuite(SqlTimeConverterTestCase.class); + } + + // ------------------------------------------------------------------------ + + protected Converter makeConverter() { + return new SqlTimeConverter(); + } + + protected Class getExpectedType() { + return Time.class; + } + +} \ No newline at end of file Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java?rev=439601&view=auto ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java Sat Sep 2 07:17:15 2006 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.beanutils.converters; + +import java.sql.Timestamp; + +import junit.framework.TestSuite; +import org.apache.commons.beanutils.Converter; + +/** + * Test Case for the [EMAIL PROTECTED] SqlTimestampConverter} class. + * + * @version $Revision$ $Date$ + */ + +public class SqlTimestampConverterTestCase extends DateConverterTestBase { + + public SqlTimestampConverterTestCase(String name) { + super(name); + } + + // ------------------------------------------------------------------------ + + public static TestSuite suite() { + return new TestSuite(SqlTimestampConverterTestCase.class); + } + + // ------------------------------------------------------------------------ + + protected Converter makeConverter() { + return new SqlTimestampConverter(); + } + + protected Class getExpectedType() { + return Timestamp.class; + } + +} \ No newline at end of file Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]