Ninja-Squad / DbSetup

An API for populating a database in unit tests
http://dbsetup.ninja-squad.com/
212 stars 34 forks source link

Support for Joda Time #32

Open dobermai opened 10 years ago

dobermai commented 10 years ago

Working with plain Java Dates and Calendars is a pain and so Joda Time became one of the de-facto standard library for non-trivial Date handling. It would be awesome Joda objects like DateTime for timestamps and other Date data.

What would be the best approach to do this with DBSetup? My understanding is, that a Binder implementation won't help me too much since DateTime Objects aren't supported natively by any database. Is there some kind of Converter I can hook into DBSetup?

Also, Joda Support would probably be a good extension since I don't think its a good idea to bring the Joda Dependency to the core.

jnizet commented 9 years ago

Sorry for the late answer: I was on holidays.

Joda-Time support is indeed a feature I have in mind for some time, but I never implemented because nobody asked me for it yet, and the projects where I use DbSetup myself use Java 8, where joda time is not really useful anymore thanks to the introduction of the new time api. A Binder would certainly be useful though. The default binder configuration returns a binder implementation based on the parameter metadata. So, you could define and use a custom BinderConfiguration which, for example, if the type of the parameter is TIMESTAMP, returns a Binder which transforms a DateTime or LocalDateTime instance into a java.sql.Timestamp, and passes this Timestamp instance as parameter to the statement. See http://dbsetup.ninja-squad.com/apidoc/1.3.1/src-html/com/ninja_squad/dbsetup/bind/Binders.html#line.268 for the implementation of the binder used for TIMESTAMP, as an example.

Regarding native support for Joda-Time, I indeed don't want to add a dependency on Joda-Time to DbSetup. But I don't really like the idea of an extension either. My idea would be to rely on reflection: if the name of one of the interfaces implemented by the class of the object passed to the binder is "org.joda.time.ReadableInstant", for example, then I could invoke toInstant() and get the time in millis reflectively, and transform it to a Timestamp. I could of course use the same kind of trick for LocalXxx objects.

I'll try to implement this when I have some time. Hopefully soon.