Share

DbUnit

Tracker: Feature Requests

5 Support for UUID data type (Feature request) - ID: 2783531
Last Update: Settings changed ( gommma )

DBUnit currently doesn't support uuid / guid data type (supported for
example in PostgreSQL 8.4).


cyberroadie ( cyberroadie ) - 2009-04-29 10:15

5

Closed

Fixed

Nobody/Anonymous

None

None

Public


Comments ( 16 )

Date: 2009-05-03 20:57
Sender: gommma

Hi guys,

I committed the changes from Olivier's patch
dbunit-postgresql-patch-2009-05-03.diff to the current trunk (svn rev.
990). Thanks for this one.

@Olivier,
I am sure that we could implement a mechanism to detect the current RDMS
and the required DataTypeFactory dynamically. It would require some little
effort and if you like - as always - a patch is very welcome :-)

I will close this issue now.

regards,
matthias


Date: 2009-05-03 12:12
Sender: cyberroadie

Hi Mathias,

I've run my tests against a postgresql database (with maven) exporting and
importing of test data works both :-) Everything is included in latest
patch (dbunit-postgresql-patch-2009-05-03.diff)

Thanks Jarvis for the coding, help and tips :-)

Olivier




Date: 2009-05-03 11:47
Sender: cyberroadie

Hi,

looking at the code Jarvis posted:

con.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
dataTypeFactory);

Shouldn't this be handled in dbunit itself, in the DatabaseConfig class?
Depending on the jdbc driver type? Just my 2 cents worth.

Olivier






Date: 2009-05-03 11:37
Sender: cyberroadie

This latest patch also include the unit tests for postgresql. I removed the
postgresql depency in the pom, everything is done via reflection now.

Olivier


Date: 2009-05-03 11:29
Sender: cyberroadie

Hi Jarvis / Mathias,

Thanks for all the help. Still trying to figure out how dbunit works.
I've created a PostgreSQLConnection class, same as OracleConnection class.
This seems to be the class which load the correct DataTypeFactory. But I
can't really see where this class is created. If I look at the DbUnitTask
class for ant, I can see how it creates a database connection, like you do
in your posted code, but not how it creates one for a specific database.
It's like it's a factory pattern but not fully implemented?? I'm probably
overlooking something here. I've attached my latest changes to this
issue(dbunit-postgresql-patch-2009-05-03.diff )

Olivier
PS In my test project I use the maven dbunit plugin





Date: 2009-05-02 10:23
Sender: gommma

in pom.xml Postgresql version changed from jdbc4 to jdbc3 since dbunit is
still compiled on Java 1.4 (fixes the parabuild problem) - svn rev 989


Date: 2009-05-01 20:18
Sender: gommma

I committed the patch Jarvis sent on the dev-mailing list to the current
trunk rev. 988

Thanks to both of you, Jarvis and Olivier. The change will be available
with the 2.4.5 release (or as snapshot via the parabuild server at
http://parabuild.viewtier.com:8080/parabuild/index.htm?cid=72a4&view=detailed&buildid=30
).

regards,
matthias


Date: 2009-05-01 07:40
Sender: gommma

As a reference for Unit-Tests you could have a look into the
org.dbunit.ext.oracle.OracleDataTypeFactoryTest class.


Date: 2009-05-01 00:08
Sender: firsttiger

Hi Olivier,

DbUnit doesn't seem to automatically assign the correct DataTypeFactory. I
found I had to create a new DatabaseConnection, then set its
PROTOTYPE_DATATYPE_FACTORY property. I could then create a new
DefaultDatabaseTester using this new connection.

This is the test initialisation code from one of my projects:

IDatabaseConnection con = new
DatabaseDataSourceConnection(dataSource);

if (dataTypeFactory != null)

con.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
dataTypeFactory);

databaseTester = new DefaultDatabaseTester(con);
databaseTester.setSetUpOperation(setUpOperation);
databaseTester.setTearDownOperation(tearDownOperation);
databaseTester.setDataSet(
new FlatXmlDataSet(
new FileInputStream(
new File(dataSetFile))));

HTH

Jarvis


Date: 2009-04-30 17:48
Sender: cyberroadie

Hi Mathias,

I put my changes so far in the diff attached to this issue.
I'm currently looking through the existing tests to see if there is a way
to test the datatypefactory creation. Have you got any tips for this?

Olivier


Date: 2009-04-30 17:28
Sender: gommma

Hi Olivier,

I suppose you did not properly configure your Postgres DataTypeFactory. If
you are stuck don't hesitate to post some code.


Date: 2009-04-30 11:48
Sender: cyberroadie

just attached what i've got so far: compiles and test runs, but when i run
it in one of my projects, it doesn't work and i get the following warning:

600 [main] WARN org.dbunit.dataset.AbstractTableMetaData - Potential
problem found: The configured data type factory 'class
org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems
with the current database 'PostgreSQL' (e.g. some datatypes may not be
supported properly). In rare cases you might see this message because the
list of supported database products is incomplete (list=[derby]). If so
please request a java-class update via the forums.

Is this because the incorrect data type factory is created? (default one
instead of the postgresql)
Not sure how to progress

Olivier



Date: 2009-04-29 11:46
Sender: cyberroadie

Thanks!

I copied it into a file and tried to patch it, but it failed:

patch -p0 -i dbunit-patch
patching file src/java/org/dbunit/dataset/AbstractTableMetaData.java
patch: **** malformed patch at line 6: import
org.dbunit.ext.mysql.MySqlDataTypeFactory;

Am I doing something wrong?






Date: 2009-04-29 11:27
Sender: firsttiger

Patch as submitted to mailing list:

Index: src/java/org/dbunit/dataset/AbstractTableMetaData.java
===================================================================
--- src/java/org/dbunit/dataset/AbstractTableMetaData.java (revision 985)
+++ src/java/org/dbunit/dataset/AbstractTableMetaData.java (working copy)
@@ -44,6 +44,7 @@
import org.dbunit.ext.mysql.MySqlDataTypeFactory;
import org.dbunit.ext.oracle.Oracle10DataTypeFactory;
import org.dbunit.ext.oracle.OracleDataTypeFactory;
+import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -208,7 +209,7 @@
addValidCombinationInternal(Db2DataTypeFactory.class, "db2");
// addValidCombinationInternal(DefaultDataTypeFactory.class, "hsql");
addValidCombinationInternal(DefaultDataTypeFactory.class, "derby");
- addValidCombinationInternal(DefaultDataTypeFactory.class,
"PostgreSQL");
+ addValidCombinationInternal(PostgresqlDataTypeFactory.class,
"PostgreSQL");
addValidCombinationInternal(H2DataTypeFactory.class, "h2");
addValidCombinationInternal(HsqldbDataTypeFactory.class, "hsql");
addValidCombinationInternal(MsSqlDataTypeFactory.class, "mssql");
Index: src/java/org/dbunit/ext/postgresql/PostgresqlDataTypeFactory.java
===================================================================
---
src/java/org/dbunit/ext/postgresql/PostgresqlDataTypeFactory.java (revision
0)
+++
src/java/org/dbunit/ext/postgresql/PostgresqlDataTypeFactory.java (revision
0)
@@ -0,0 +1,60 @@
+/*
+ *
+ * The DbUnit Database Testing Framework
+ * Copyright (C)2002-2004, DbUnit.org
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
+ *
+ */
+package org.dbunit.ext.postgresql;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.dbunit.dataset.datatype.DataType;
+import org.dbunit.dataset.datatype.DataTypeException;
+import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
+import java.sql.Types;
+
+/**
+ * Specialized factory that recognizes Postgresql data types.
+ * <p>
+ * Derived from work by manuel.laflamme</p>
+ *
+ * @author Jarvis Cochrane (jarvis@cochrane.com.au)
+ * @author manuel.laflamme
+ * @since Apr 27, 2009
+ */
+public class PostgresqlDataTypeFactory
+ extends DefaultDataTypeFactory {
+
+ /**
+ * Logger for this class
+ */
+ private static final Logger logger = LoggerFactory.getLogger(
+ PostgresqlDataTypeFactory.class);
+
+ public DataType createDataType(int sqlType, String sqlTypeName)
throws DataTypeException {
+ logger.debug("createDataType(sqlType={}, sqlTypeName={})",
+ String.valueOf(sqlType), sqlTypeName);
+
+ if (sqlType == Types.OTHER)
+ // Treat Postgresql UUID types as VARCHARS
+ if ("uuid".equals(sqlTypeName))
+ return new UuidType();
+
+ return super.createDataType(sqlType, sqlTypeName);
+ }
+
+}
Index: src/java/org/dbunit/ext/postgresql/UuidType.java
===================================================================
--- src/java/org/dbunit/ext/postgresql/UuidType.java (revision 0)
+++ src/java/org/dbunit/ext/postgresql/UuidType.java (revision 0)
@@ -0,0 +1,70 @@
+/*
+ *
+ * The DbUnit Database Testing Framework
+ * Copyright (C)2002-2004, DbUnit.org
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
+ *
+ */
+package org.dbunit.ext.postgresql;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import org.dbunit.dataset.datatype.AbstractDataType;
+import org.dbunit.dataset.datatype.TypeCastException;
+
+/**
+ * Adapter to handle conversion between Postgresql
+ * native UUID type and Strings.
+ *
+ * @author Jarvis Cochrane (jarvis@cochrane.com.au)
+ * @since Apr 27, 2009
+ */
+public class UuidType
+ extends AbstractDataType {
+
+ public class UUID
+ extends org.postgresql.util.PGobject {
+
+ public static final long serialVersionUID = 668353936136517917L;
+
+ public UUID(String s) throws java.sql.SQLException {
+ super();
+ this.setType("uuid");
+ this.setValue(s);
+ }
+
+ }
+
+ public UuidType() {
+ super("uuid", Types.OTHER, String.class, false);
+ }
+
+ public Object getSqlValue(int column, ResultSet resultSet) throws
SQLException, TypeCastException {
+ return resultSet.getString(column);
+ }
+
+ public void setSqlValue(Object uuid, int column,
+ PreparedStatement statement) throws
SQLException, TypeCastException {
+ statement.setObject(column, new UUID(uuid.toString()));
+ }
+
+ public Object typeCast(Object arg0) throws TypeCastException {
+ return arg0.toString();
+ }
+
+}



Date: 2009-04-29 11:25
Sender: firsttiger

I've done some work on this already, and submitted a patch to the
dbunit-developers list. I'll attach my patch to this request also.


Date: 2009-04-29 10:44
Sender: cyberroadie

I was looking if I could write this support myself, but am a bit in doubt
what the best way forward would be.

UUID is not a native java.sql.* type, so instead of writing a
org.dbunit.dataset.datatype.uuid data type maybe it's better to write a
org.dbunit.ext..postgresql.PostgreSQLDataTypeFactory?

Anyone has any thoughts on this?



Attached Files ( 2 )

Filename Description Download
dbunit-relfection-tests-connector.diff with postgresql connector Download
dbunit-postgresql-patch-2009-05-03.diff latest with postgresql connection class Download

Changes ( 16 )

Field Old Value Date By
close_date - 2009-05-03 20:57 gommma
allow_comments 1 2009-05-03 20:57 gommma
resolution_id Accepted 2009-05-03 20:57 gommma
status_id Open 2009-05-03 20:57 gommma
File Added 325523: dbunit-postgresql-patch-2009-05-03.diff 2009-05-03 11:31 cyberroadie
resolution_id Fixed 2009-05-02 10:15 gommma
status_id Closed 2009-05-02 10:15 gommma
allow_comments 0 2009-05-02 10:15 gommma
close_date 2009-05-01 20:18 2009-05-02 10:15 gommma
resolution_id None 2009-05-01 20:18 gommma
allow_comments 1 2009-05-01 20:18 gommma
close_date - 2009-05-01 20:18 gommma
status_id Open 2009-05-01 20:18 gommma
File Deleted 325106: 2009-04-30 12:48 cyberroadie
File Added 325116: dbunit-relfection-tests-connector.diff 2009-04-30 12:47 cyberroadie
File Added 325106: dbunit-relfection-tests.diff 2009-04-30 11:45 cyberroadie