View Javadoc
1   /*
2    * DatabaseConnectionTest.java   Mar 26, 2002
3    *
4    * The DbUnit Database Testing Framework
5    * Copyright (C)2002-2004, DbUnit.org
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  
21   *
22   */
23  
24  package org.dbunit.database;
25  
26  import java.sql.DatabaseMetaData;
27  import java.sql.PreparedStatement;
28  import java.util.Locale;
29  
30  import org.dbunit.DatabaseEnvironment;
31  import org.dbunit.DatabaseUnitException;
32  import org.dbunit.dataset.ITable;
33  
34  
35  /**
36   * @author Manuel Laflamme
37   * @version $Revision$
38   * @since Mar 26, 2002
39   */
40  public class DatabaseConnectionIT extends AbstractDatabaseConnectionIT
41  {
42      public DatabaseConnectionIT(String s)
43      {
44          super(s);
45      }
46  
47      protected String convertString(String str) throws Exception
48      {
49          return getEnvironment().convertString(str);
50      }
51  
52  
53      public void testCreateNullConnection() throws Exception
54      {
55          try
56          {
57              new DatabaseConnection(null);
58              fail("Should not be able to create a database connection without a JDBC connection");
59          }
60          catch(NullPointerException expected)
61          {
62              // all right
63          }
64      }
65  
66      public void testCreateConnectionWithNonExistingSchemaAndStrictValidation() throws Exception
67      {
68          DatabaseEnvironment environment = getEnvironment();
69          String schema = environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
70          IDatabaseConnection validConnection = super.getConnection();
71          // Try to create a database connection with an invalid schema
72          try
73          {
74          	boolean validate = true;
75              new DatabaseConnection(validConnection.getConnection(), schema, validate);
76              fail("Should not be able to create a database connection object with an unknown schema.");
77          }
78          catch(DatabaseUnitException expected)
79          {
80              String expectedMsg = "The given schema '" + convertString(schema) + "' does not exist.";
81              assertEquals(expectedMsg, expected.getMessage());
82          }
83      }
84      
85      public void testCreateConnectionWithNonExistingSchemaAndLenientValidation() throws Exception
86      {
87          DatabaseEnvironment environment = getEnvironment();
88          String schema = environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
89          IDatabaseConnection validConnection = super.getConnection();
90          // Try to create a database connection with an invalid schema
91      	boolean validate = false;
92          DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema, validate);
93          assertNotNull(dbConnection);
94      }
95  
96      
97      public void testCreateConnectionWithSchemaDbStoresUpperCaseIdentifiers() throws Exception
98      {
99          IDatabaseConnection validConnection = super.getConnection();
100         String schema = validConnection.getSchema();
101         assertNotNull("Precondition: schema of connection must not be null", schema);
102         
103         
104         DatabaseMetaData metaData = validConnection.getConnection().getMetaData();
105         if(metaData.storesUpperCaseIdentifiers())
106         {
107             boolean validate = true;
108             DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema.toLowerCase(Locale.ENGLISH), validate);
109             assertNotNull(dbConnection);
110             assertEquals(schema.toUpperCase(Locale.ENGLISH), dbConnection.getSchema());
111         }
112         else
113         {
114             // skip this test
115             assertTrue(true);
116         }
117     }
118 
119     
120     public void testCreateConnectionWithSchemaDbStoresLowerCaseIdentifiers() throws Exception
121     {
122         IDatabaseConnection validConnection = super.getConnection();
123         String schema = validConnection.getSchema();
124         assertNotNull("Precondition: schema of connection must not be null", schema);
125         
126         
127         DatabaseMetaData metaData = validConnection.getConnection().getMetaData();
128         if(metaData.storesLowerCaseIdentifiers())
129         {
130             boolean validate = true;
131             DatabaseConnection dbConnection = new DatabaseConnection(validConnection.getConnection(), schema.toUpperCase(Locale.ENGLISH), validate);
132             assertNotNull(dbConnection);
133             assertEquals(schema.toLowerCase(Locale.ENGLISH), dbConnection.getSchema());
134         }
135         else
136         {
137             // skip this test
138             assertTrue(true);
139         }
140     }
141 
142     public void testCreateQueryWithPreparedStatement() throws Exception
143     {
144         IDatabaseConnection connection = super.getConnection();
145         PreparedStatement pstmt = connection.getConnection().prepareStatement("select * from TEST_TABLE where COLUMN0=?");
146 
147         try{
148             pstmt.setString(1, "row 1 col 0");
149             ITable table = connection.createTable("MY_TABLE", pstmt);
150             assertEquals(1, table.getRowCount());
151             assertEquals(4, table.getTableMetaData().getColumns().length);
152             assertEquals("row 1 col 1", table.getValue(0, "COLUMN1"));
153             
154             // Now reuse the prepared statement
155             pstmt.setString(1, "row 2 col 0");
156             ITable table2 = connection.createTable("MY_TABLE", pstmt);
157             assertEquals(1, table2.getRowCount());
158             assertEquals(4, table2.getTableMetaData().getColumns().length);
159             assertEquals("row 2 col 1", table2.getValue(0, "COLUMN1"));
160         }
161         finally{
162             pstmt.close();
163         }
164     }
165 
166 }
167 
168