How to make DbUnit recognize the Postgresql POLYGON data type?

10

I have the following domain of type polygon in the database:

CREATE DOMAIN "global".polygon AS pg_catalog.polygon;

The following table that uses this domain:

CREATE TABLE user.area_geom
(
  id_area_geom serial NOT NULL,
  polygon global.polygon
);

And the following class that extends DBTestCase by setting the dataset before the tests are run:

public abstract class MyDBTestCase extends DBTestCase {

public MyDBTestCase () {
    // Configura url, user, password e driver do banco de dados.
}

@Override
protected IDataSet getDataSet() throws Exception {
    // Meu arquivo .xml com os dados de teste.
    return new FlatXmlDataSetBuilder()
            .build(new FileInputStream(MyTestDB.FLAT_XML_DATASET));
}

@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {

    /* Habilita case sensitive para os nomes de tabelas do banco */
    config.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES,
            true);

    /* Habilita a utilização de fully qualified names para tabelas */
    config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

    /* Configura o tipo de dado para o PostgreSQL */
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
            new PostgresqlDataTypeFactory());
}
}

This setup works perfectly for all commonly used data types, but when I mapped the area_geom table that contains a polygon domain, the console started generating the following output:

9081 [main] WARN org.dbunit.util.SQLHelper - area_geom.polygon data type (1111, 'polygon') not recognized and will be ignored. See FAQ for more information.

In the FAQ proposed by the error message, there is nothing to tell me how to implement a data type " custom "over an existing data type, or make DbUnit recognize my polygon , I've spent a few hours of Google and found nothing.

Does anyone have any light for this problem?

    
asked by anonymous 13.12.2013 / 17:30

1 answer

4

The interesting item in the FAQ is this: How to replace the default data type factory? , which explains how set up a DBunit custom data type factory - basically a class that implements IDataTypeFactory .

You will need to add a DataType for Polygon (you can draw inspiration from IntervalType ), something like this:

public class PolygonType extends AbstractDataType {
      public PolygonType() {
          super("polygon", Types.OTHER, String.class, false);
      }
      /* ... implemente aqui getSqlValue(..) e setSqlValue(..) para o tipo POLYGON */
}

Then create a class by extending the PostgresqlDataTypeFactory (code here ) that implements special logic for type POLYGON and delegates to the other types, something like:

public class PolygonPostgresDataFactory extends PostgresqlDataTypeFactory {
    public DataType createDataType(int sqlType, String sqlTypeName)
                                         throws DataTypeException {
        logger.debug("createDataType(sqlType={}, sqlTypeName={})",
                       String.valueOf(sqlType), sqlTypeName);
        if (sqlType == Types.OTHER && "polygon".equals(sqlTypeName))
             return new PolygonType();
        }      
        return super.createDataType(sqlType, sqlTypeName);
    }
}

Finally, you change there in your MyDBTestCase::setUpDatabaseConfig method to use the custom DataTypeFactory:

/* Configura o tipo de dado para o PostgreSQL */
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                           new PostgresqlDataTypeFactory());
    
14.12.2013 / 14:09