View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.util;
23  
24  import java.sql.Connection;
25  import java.sql.DatabaseMetaData;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  
29  import org.dbunit.AbstractHSQLTestCase;
30  import org.dbunit.dataset.Column;
31  import org.dbunit.dataset.ColumnMetaData;
32  import org.dbunit.dataset.datatype.DataType;
33  import org.dbunit.dataset.datatype.DataTypeException;
34  import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
35  import org.dbunit.dataset.datatype.IDataTypeFactory;
36  
37  import com.mockobjects.sql.MockDatabaseMetaData;
38  import com.mockobjects.sql.MockResultSetMetaData;
39  import com.mockobjects.sql.MockSingleRowResultSet;
40  
41  /**
42   * @author Felipe Leme (dbunit@felipeal.net)
43   * @version $Revision$
44   * @since Nov 5, 2005
45   */
46  public class SQLHelperTest extends AbstractHSQLTestCase {
47    
48    public SQLHelperTest( String name ) {
49      super( name, "hypersonic_dataset.sql" );
50    }  
51    
52    public void testGetPrimaryKeyColumn() throws SQLException {
53      String[] tables = { "A", "B", "C", "D", "E", "F", "G", "H" };
54      Connection conn = getConnection().getConnection();
55      assertNotNull( "didn't get a connection", conn );
56      for (int i = 0; i < tables.length; i++) {
57        String table = tables[i];
58        String expectedPK = "PK" + table;
59        String actualPK = SQLHelper.getPrimaryKeyColumn( conn, table );
60        assertNotNull( actualPK );
61        assertEquals( "primary key column for table " + table + " does not match", expectedPK, actualPK );
62      }
63    }
64    
65    public void testGetDatabaseInfoWithException() throws Exception{
66        final String productName="Some product";
67        final String exceptionText="Dummy exception to simulate unimplemented operation exception as occurs " +
68        "in sybase 'getDatabaseMajorVersion()' (com.sybase.jdbc3.utils.UnimplementedOperationException)";
69        
70        DatabaseMetaData metaData = new MockDatabaseMetaData(){
71            public String getDatabaseProductName() throws SQLException {
72                return productName;
73            }
74            public String getDatabaseProductVersion() throws SQLException{
75                return null;
76            }
77            public int getDriverMajorVersion() {
78                return -1;
79            }
80            public int getDriverMinorVersion() {
81                return -1;
82            }
83            public String getDriverName() throws SQLException {
84                return null;
85            }
86            public String getDriverVersion() throws SQLException {
87                return null;
88            }
89            public int getDatabaseMajorVersion() throws SQLException {
90                throw new SQLException(exceptionText);
91            }
92            public int getDatabaseMinorVersion() throws SQLException {
93                return -1;
94            }
95        };
96        String info = SQLHelper.getDatabaseInfo(metaData);
97        assertNotNull(info);
98        assertTrue(info.indexOf(productName)>-1);
99        assertTrue(info.indexOf(SQLHelper.ExceptionWrapper.NOT_AVAILABLE_TEXT)>-1);
100   }
101   
102   /**
103    * Regression test for changes to SQLHelper.createColumn.
104    * Also covers org.dbunit.dataset.ColumnMetaData
105    */
106   public void testCreateColumn() {
107 	  try {
108 		  _testCreateColumn(new Object[]{"CAT", "SCHEMA", "table_1", "col01", 12, 
109 			  "VARCHAR", 30, null, 2, 2, 
110 			  0, "Test data", null, null, null, 
111 			  30, 4, "YES", null, null,
112 			  null, null, "NO", "NO"});
113 		  _testCreateColumn(new Object[]{"CAT", "SCHEMA", "other_table", "some_id", 3, 
114 				  "DECIMAL", 10, null, 2, 2, 
115 				  0, "Primary key", "0.00", null, null, 
116 				  4, 1, "NO", null, null,
117 				  null, null, "YES", "YES"});
118 		  _testCreateColumn(new Object[]{"CAT", "SCHEMA", "same_table", "some_ud_ref", -3, 
119 				  "VARBINARY", 8, null, 0, 0, 
120 				  1, "User defined ref", null, null, null, 
121 				  null, 7, "YES", "Scope cat", "Scope schema",
122 				  "Scope table", (short)12, "YES", "YES"});
123 	  } catch (Exception e) {
124 		  e.printStackTrace();
125 		  fail(e.getMessage());
126 	  }
127   }
128   
129   private void _testCreateColumn(Object[] metadataValues) throws Exception {
130 	  MockSingleRowResultSet resultSet = new MockSingleRowResultSet();
131 	  resultSet.addExpectedIndexedValues(metadataValues);
132 	  MockResultSetMetaData mockMeta = new MockResultSetMetaData();
133 	  mockMeta.setupGetColumnCount(metadataValues.length);
134 	  resultSet.setupMetaData(mockMeta);
135 	  IDataTypeFactory dtFactory = new DefaultDataTypeFactory();
136 	  _testCreateColumn(resultSet, dtFactory);
137   }
138   
139   private void _testCreateColumn(ResultSet resultSet, IDataTypeFactory dtFactory) throws Exception {
140 	  Column oldCol = oldCreateColumn(resultSet, dtFactory, true);
141 	  ColumnMetaData colmd = new ColumnMetaData(resultSet);
142 	  // Check that the new method creates a column equal to the on returned by the legacy method:
143 	  Column newCol = SQLHelper.createColumn(colmd, dtFactory, true);
144 	  assertEquals(oldCol, newCol);
145 	  // Check that the backwards-compatibility method also produces a congruent column: 
146 	  Column newOldCol = SQLHelper.createColumn(resultSet, dtFactory, true);
147 	  assertEquals(oldCol, newOldCol);
148   }
149   
150   /*
151    * This is a direct lift of the previous SQLHelper.createColumn, for comparison 
152    * with the new implementation in regression testing.   
153    */
154   private static final Column oldCreateColumn(ResultSet resultSet,
155           IDataTypeFactory dataTypeFactory, boolean datatypeWarning)
156                   throws SQLException, DataTypeException
157                   {
158       String tableName = resultSet.getString(3);
159       String columnName = resultSet.getString(4);
160       int sqlType = resultSet.getInt(5);
161       //If Types.DISTINCT like SQL DOMAIN, then get Source Date Type of SQL-DOMAIN
162       if(sqlType == java.sql.Types.DISTINCT)
163       {
164           sqlType = resultSet.getInt("SOURCE_DATA_TYPE");
165       }
166 
167       String sqlTypeName = resultSet.getString(6);
168       //        int columnSize = resultSet.getInt(7);
169       int nullable = resultSet.getInt(11);
170       String remarks = resultSet.getString(12);
171       String columnDefaultValue = resultSet.getString(13);
172       // This is only available since Java 5 - so we can try it and if it does not work default it
173       String isAutoIncrement = Column.AutoIncrement.NO.getKey();
174       try {
175           isAutoIncrement = resultSet.getString(23);
176       }
177       catch (Exception e)
178       {
179           // Ignore this one here
180           final String msg =
181                   "Could not retrieve the 'isAutoIncrement' property"
182                           + " because not yet running on Java 1.5 -"
183                           + " defaulting to NO. Table={}, Column={}";
184           System.err.println(msg);
185           e.printStackTrace();
186       }
187 
188       // Convert SQL type to DataType
189       DataType dataType =
190               dataTypeFactory.createDataType(sqlType, sqlTypeName, tableName, columnName);
191       if (dataType != DataType.UNKNOWN)
192       {
193           Column column = new Column(columnName, dataType,
194                   sqlTypeName, Column.nullableValue(nullable), columnDefaultValue, remarks,
195                   Column.AutoIncrement.autoIncrementValue(isAutoIncrement));
196           return column;
197       }
198       else
199       {
200           if (datatypeWarning)
201               System.err.println(
202                       tableName + "." + columnName +
203                       " data type (" + sqlType + ", '" + sqlTypeName +
204                       "') not recognized and will be ignored. See FAQ for more information.");
205 
206           // datatype unknown - column not created
207           return null;
208       }
209                   }
210 
211   
212 }