View Javadoc
1   package org.dbunit.ext.postgresql;
2   
3   import java.io.StringReader;
4   import java.sql.Statement;
5   
6   import junit.framework.TestCase;
7   
8   import org.dbunit.DatabaseEnvironment;
9   import org.dbunit.database.IDatabaseConnection;
10  import org.dbunit.dataset.Column;
11  import org.dbunit.dataset.ReplacementDataSet;
12  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
13  import org.dbunit.operation.DatabaseOperation;
14  import org.xml.sax.InputSource;
15  
16  /**
17   * Testcase for Postgresql to check SQL CREATE DOMAIN with
18   * FlatXmlDataSetBuilder to insert a dataset with SQL Domains (user-def-types).
19   * @author Philipp S. (Unwissender2009)
20   * @since Nov 23, 2009
21   */
22  public class SQLHelperDomainPostgreSQLIT extends TestCase{
23  
24  	private IDatabaseConnection _connection;
25  
26  	private static final String xmlData = "<?xml version=\"1.0\"?>" +
27  			"<dataset>" +
28  			"<T1 PK=\"1\" STATE=\"is_blabla\"/>" +
29  			"</dataset>";
30  
31  
32  	protected void setUp() throws Exception
33  	{
34  		super.setUp();
35  		//Load active postgreSQL profile and connection from Maven pom.xml.
36  	 	_connection = DatabaseEnvironment.getInstance().getConnection();
37  	}
38  
39  	protected void tearDown() throws Exception
40  	{
41  		super.tearDown();
42  		if(_connection!=null)
43  		{
44  		_connection.close();
45  		_connection = null;
46  		}
47  	}
48  
49      public void testOk()
50      {
51      }
52  
53  	 public void xtestDomainDataTypes() throws Exception {
54  
55  		 	assertNotNull( "didn't get a connection", _connection );
56  
57  		    Statement stat = _connection.getConnection().createStatement();
58  		    //DELETE SQL DOMAIN and Table with DOMAINS
59  		    stat.execute("DROP TABLE  IF EXISTS T1;");
60  		    stat.execute("DROP DOMAIN IF EXISTS MYSTATE;");
61  		    stat.execute("DROP DOMAIN IF EXISTS MYPK;");
62  
63  		    //Create SQL DOMAIN and Table with DOMAINS
64  		    stat.execute("CREATE DOMAIN MYSTATE AS VARCHAR(20) DEFAULT 'is_Valid';");
65  		    stat.execute("CREATE DOMAIN MYPK AS INTEGER DEFAULT 0;");
66  		    stat.execute("CREATE TABLE T1 (PK MYPK,STATE MYSTATE,PRIMARY KEY (PK));");
67  		    stat.close();
68  
69  		    try{
70  		    ReplacementDataSet dataSet = new ReplacementDataSet(
71  										 new FlatXmlDataSetBuilder().build(
72  												 new InputSource(
73  														 new StringReader(xmlData))
74  												 ));
75  			dataSet.addReplacementObject("[NULL]", null);
76  			dataSet.setStrictReplacement(true);
77  
78  			//THE TEST -> hopefully with no exception!!!
79  			DatabaseOperation.CLEAN_INSERT.execute(_connection, dataSet);
80  
81  			// Check Types.
82  		    for(int i=0;i<_connection.createDataSet().getTableMetaData("T1").getColumns().length;i++)
83  		    {
84  		    	Column c = _connection.createDataSet().getTableMetaData("T1").getColumns()[i];
85  
86  		    	if(c.getSqlTypeName().compareTo("mypk")==0)
87  			    {
88  			    	  assertEquals(java.sql.Types.INTEGER,c.getDataType().getSqlType());
89  			    }
90  			    else if(c.getSqlTypeName().compareTo("mystate")==0)
91  			    {
92  			    	  assertEquals(java.sql.Types.VARCHAR,c.getDataType().getSqlType());
93  			    }
94  			    else
95  			    {
96  			    	  assertTrue(false);
97  			    }
98  		    }
99  		    }catch(Exception e)
100 		    {
101 		    	assertEquals("DatabaseOperation.CLEAN_INSERT... no exception",""+e);
102 		    }
103 
104 	}
105 }