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.SQLException;
27  
28  import org.dbunit.AbstractHSQLTestCase;
29  
30  import com.mockobjects.sql.MockDatabaseMetaData;
31  
32  /**
33   * @author Felipe Leme (dbunit@felipeal.net)
34   * @version $Revision$
35   * @since Nov 5, 2005
36   */
37  public class SQLHelperTest extends AbstractHSQLTestCase {
38    
39    public SQLHelperTest( String name ) {
40      super( name, "hypersonic_dataset.sql" );
41    }  
42    
43    public void testGetPrimaryKeyColumn() throws SQLException {
44      String[] tables = { "A", "B", "C", "D", "E", "F", "G", "H" };
45      Connection conn = getConnection().getConnection();
46      assertNotNull( "didn't get a connection", conn );
47      for (int i = 0; i < tables.length; i++) {
48        String table = tables[i];
49        String expectedPK = "PK" + table;
50        String actualPK = SQLHelper.getPrimaryKeyColumn( conn, table );
51        assertNotNull( actualPK );
52        assertEquals( "primary key column for table " + table + " does not match", expectedPK, actualPK );
53      }
54    }
55    
56    public void testGetDatabaseInfoWithException() throws Exception{
57        final String productName="Some product";
58        final String exceptionText="Dummy exception to simulate unimplemented operation exception as occurs " +
59        "in sybase 'getDatabaseMajorVersion()' (com.sybase.jdbc3.utils.UnimplementedOperationException)";
60        
61        DatabaseMetaData metaData = new MockDatabaseMetaData(){
62            public String getDatabaseProductName() throws SQLException {
63                return productName;
64            }
65            public String getDatabaseProductVersion() throws SQLException{
66                return null;
67            }
68            public int getDriverMajorVersion() {
69                return -1;
70            }
71            public int getDriverMinorVersion() {
72                return -1;
73            }
74            public String getDriverName() throws SQLException {
75                return null;
76            }
77            public String getDriverVersion() throws SQLException {
78                return null;
79            }
80            public int getDatabaseMajorVersion() throws SQLException {
81                throw new SQLException(exceptionText);
82            }
83            public int getDatabaseMinorVersion() throws SQLException {
84                return -1;
85            }
86        };
87        String info = SQLHelper.getDatabaseInfo(metaData);
88        assertNotNull(info);
89        assertTrue(info.indexOf(productName)>-1);
90        assertTrue(info.indexOf(SQLHelper.ExceptionWrapper.NOT_AVAILABLE_TEXT)>-1);
91    }
92  }