java - DBunit - Unable to typecast <1997/02/14> to TimeStamp -
i'm doing integration testing dbunit (2.49) + hibernate (4.1.3) following tutorial.
- production database : oracle 10
- test database : hsqldb 2.3.3
context
my data contains current format of date : yyyy/mm/dd
. however,according dbunit faq, dbunit supports format yyyy-mm-dd hh:mm:ss.fffffffff
, had create new format timestamp.
how tried fix it
i created
customtimestampdatatype
based on tutorial. changed part:string formats[] = {"yyyy-mm-dd hh:mm", "yyyy-mm-dd hh:mm a", "yyyy-mm-dd hh:mm:ss.fffffffff"};
into one:
string formats[] = {"yyyy/mm/dd"};
- i created
customedatatypefactory
following same tutorial. make extendoracle10datatypefactory
ratherdefaultdatattypefactory
. in
hibernatedbunittestcase
, overridesetdatabaseconfig()
following:@override protected void setupdatabaseconfig(databaseconfig config){ config.setproperty(databaseconfig.property_datatype_factory, new customdatatypefactory()); }
but got new errors
i ran unit test , got error.
org.dbunit.dataset.datatype.typecastexception: unable typecast value <1997/02/14> of type <java.lang.string> timestamp @ org.dbunit.dataset.datatype.timestampdatatype.typecast(timestampdatatype.java:120) @ org.dbunit.dataset.datatype.timestampdatatype.setsqlvalue(timestampdatatype.java:176) @ org.dbunit.database.statement.simplepreparedstatement.addvalue(simplepreparedstatement.java:73) @ org.dbunit.operation.refreshoperation$rowoperation.execute(refreshoperation.java:189) @ org.dbunit.operation.refreshoperation.execute(refreshoperation.java:113) @ org.dbunit.abstractdatabasetester.executeoperation(abstractdatabasetester.java:190) @ org.dbunit.abstractdatabasetester.onsetup(abstractdatabasetester.java:103) @ org.dbunit.databasetestcase.setup(databasetestcase.java:156) @ test.hibernatedbunittestcase.setup(hibernatedbunittestcase.java:85) @ test.playertest.setup(playertest.java:117) caused by: java.lang.illegalargumentexception: timestamp format must yyyy-mm-dd hh:mm:ss[.fffffffff] @ java.sql.timestamp.valueof(unknown source) @ org.dbunit.dataset.datatype.timestampdatatype.typecast(timestampdatatype.java:116) ... 20 more
that weird, it seemed customtimestamp not called, changed date in dataset using default format : 1997-02-14 00:00:00.0, , ran unit test again. got:
org.dbunit.dataset.datatype.typecastexception: unable typecast value <1997-02-14 00:00:00.0> of type <java.lang.string> timestamp @ test.customtimestampdatatype.typecast(customtimestampdatatype.java:69) @ test.customtimestampdatatype.setsqlvalue(customtimestampdatatype.java:84) @ org.dbunit.database.statement.simplepreparedstatement.addvalue(simplepreparedstatement.java:73) @ org.dbunit.operation.refreshoperation$rowoperation.execute(refreshoperation.java:189) @ org.dbunit.operation.refreshoperation.execute(refreshoperation.java:113) @ org.dbunit.abstractdatabasetester.executeoperation(abstractdatabasetester.java:190) @ org.dbunit.abstractdatabasetester.onsetup(abstractdatabasetester.java:103) @ org.dbunit.databasetestcase.setup(databasetestcase.java:156) @ test.hibernatedbunittestcase.setup(hibernatedbunittestcase.java:85) @ test.playertest.setup(playertest.java:117)
that means customtimestamp called. seems like, problem stemed databasetestcase.setup somehow called wrong timestampdatatype.
how fix issue?
my first option replace every
yyyy/mm/dd
yyyy-mm-dd
in dataset using regular expressions. worked fine, until had test method selected date based on request (so format yyyy-mm-dd) , compared current date. ( format yyyy / mm / dd). hsqldb can't compare 2 dates different format.my second option decompile dbunit.jar, rewrite timestampdatatype based on tutorial. i'm unfamiliar bytecode writing before entering uncharted waters, wanted know if had solution.
thank in advance
fixed it!
so ended using second option. detailed path need it.
- download
dbunit.2.2.source.jar
- unzip jar
- go eclipse,
file > new > java project
- uncheck
"use default location"
- in location : specify path new folder created jar
- click on
finish
modify timestampdatatype.java (if needed)
instead of
ts = java.sql.timestamp.valueof(stringvalue);
use code belowstring formats[] = {"dd/mm/yyyy hh:mm:ss.ss"}; //and more depending on need timestamp ts = null; (int = 0; < formats.length; i++) { simpledateformat sdf = new simpledateformat(formats[i]); try { java.util.date date = sdf.parse(stringvalue); ts = new timestamp(date.gettime()); return ts; } catch( parseexception e) { }
modify datedatatype.java (if needed)
instead of
return java.sql.date.valueof(stringvalue);
, use code belowstring formats[] = {"dd/mm/yyyy"}; //and more depending on need (int = 0; < formats.length; i++) { simpledateformat sdf = new simpledateformat(formats[i]); try { java.util.date date = sdf.parse(stringvalue); java.sql.date datesql = new java.sql.date(date.gettime()); return datesql; } catch( parseexception e) { } }
- right-click on project,
export
- select
jar file
,next
- fill export destination
finish
. - you have add new jar library make work.
Comments
Post a Comment