View Javadoc
1   package org.dbunit.ext.postgresql;
2   
3   import junit.framework.TestCase;
4   import org.dbunit.DatabaseEnvironment;
5   import org.dbunit.database.DatabaseConfig;
6   import org.dbunit.database.IDatabaseConnection;
7   import org.dbunit.dataset.*;
8   import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
9   import org.dbunit.operation.DatabaseOperation;
10  import org.xml.sax.InputSource;
11  
12  import java.io.StringReader;
13  import java.sql.Statement;
14  import java.sql.Types;
15  
16  import static org.junit.Assert.assertArrayEquals;
17  
18  public class PostgresSQLOidIT extends TestCase
19  {
20      private IDatabaseConnection _connection;
21  
22   // @formatter:off
23      private static final String xmlData ="<?xml version=\"1.0\"?>" +
24              "<dataset>" +
25              "<T2 DATA=\"[NULL]\" />" +
26              "<T2 DATA=\"\\[text UTF-8](Anything)\" />" +
27              "</dataset>";
28   // @formatter:on
29  
30      protected void setUp() throws Exception
31      {
32          super.setUp();
33          // Load active postgreSQL profile and connection from Maven pom.xml.
34          _connection = DatabaseEnvironment.getInstance().getConnection();
35      }
36  
37      protected void tearDown() throws Exception
38      {
39          super.tearDown();
40          if (_connection != null)
41          {
42              _connection.close();
43              _connection = null;
44          }
45      }
46  
47      public void testOk()
48      {
49      }
50  
51      public void xtestOidDataType() throws Exception
52      {
53          final String testTable = "t2";
54          assertNotNull("didn't get a connection", _connection);
55          DatabaseConfig config = _connection.getConfig();
56          config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
57                  new PostgresqlDataTypeFactory());
58          Statement stat = _connection.getConnection().createStatement();
59          // DELETE SQL OID tables
60          stat.execute("DROP TABLE IF EXISTS " + testTable + ";");
61  
62          // Create SQL OID tables
63          stat.execute("CREATE TABLE " + testTable + "(DATA OID);");
64          stat.close();
65  
66          try
67          {
68              ReplacementDataSet dataSet =
69                      new ReplacementDataSet(new FlatXmlDataSetBuilder()
70                              .build(new InputSource(new StringReader(xmlData))));
71              dataSet.addReplacementObject("[NULL]", null);
72              dataSet.setStrictReplacement(true);
73  
74              IDataSet ids;
75              ids = _connection.createDataSet();
76              ITableMetaData itmd = ids.getTableMetaData(testTable);
77              Column[] cols = itmd.getColumns();
78              ids = _connection.createDataSet();
79              for (Column col : cols)
80              {
81                  assertEquals(Types.BIGINT, col.getDataType().getSqlType());
82                  assertEquals("oid", col.getSqlTypeName());
83              }
84  
85              DatabaseOperation.CLEAN_INSERT.execute(_connection, dataSet);
86              ids = _connection.createDataSet();
87              ITable it = ids.getTable(testTable);
88              assertNull(it.getValue(0, "DATA"));
89              assertArrayEquals("\\[text UTF-8](Anything)".getBytes(),
90                      (byte[]) it.getValue(1, "DATA"));
91          } catch (Exception e)
92          {
93              assertEquals("DatabaseOperation.CLEAN_INSERT... no exception",
94                      "" + e);
95          }
96      }
97  }