View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, 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.database;
23  
24  import java.sql.Connection;
25  import java.sql.DriverManager;
26  import java.sql.SQLException;
27  
28  import org.dbunit.AbstractDatabaseIT;
29  import org.dbunit.DatabaseProfile;
30  import org.dbunit.IDatabaseTester;
31  
32  /**
33   * @author Manuel Laflamme
34   * @version $Revision$
35   * @since Mar 26, 2002
36   */
37  public abstract class AbstractDatabaseConnectionIT extends AbstractDatabaseIT
38  {
39  	private String schema;
40  	private DatabaseProfile profile;
41  	
42  	public AbstractDatabaseConnectionIT(String s)
43      {
44          super(s);
45      }
46  
47      
48  	protected void setUp() throws Exception {
49  		super.setUp();
50      	this.profile = super.getEnvironment().getProfile();
51  		this.schema = this.profile.getSchema();
52  	}
53  
54  
55  	public final void testGetRowCount() throws Exception
56      {
57          assertEquals("EMPTY_TABLE", 0, _connection.getRowCount("EMPTY_TABLE", null));
58          assertEquals("EMPTY_TABLE", 0, _connection.getRowCount("EMPTY_TABLE"));
59  
60          assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE", null));
61          assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE"));
62  
63          assertEquals("PK_TABLE", 1, _connection.getRowCount("PK_TABLE", "where PK0 = 0"));
64      }
65  
66      public final void testGetRowCount_NonexistingSchema() throws Exception
67      {
68      	DatabaseProfile profile = super.getEnvironment().getProfile();
69      	String nonexistingSchema = profile.getSchema() + "_444_XYZ_TEST";
70      	this.schema = nonexistingSchema;
71  
72      	IDatabaseTester dbTester = this.newDatabaseTester(nonexistingSchema);
73      	try {
74  			IDatabaseConnection dbConnection = dbTester.getConnection();
75  			
76  			assertEquals(convertString(nonexistingSchema), dbConnection.getSchema());
77  			try {
78  				dbConnection.getRowCount("TEST_TABLE");
79  				fail("Should not be able to retrieve row count for non-existing schema " + nonexistingSchema);
80  			}
81  			catch(SQLException expected)
82  			{
83  				// All right
84  			}
85      	}
86      	finally {
87      		// Reset the testers schema for subsequent tests (environment.dbTester is a singleton)
88      		dbTester.setSchema(profile.getSchema());    		
89      	}
90      }
91  
92      public final void testGetRowCount_NoSchemaSpecified() throws Exception
93      {
94      	DatabaseProfile profile = super.getEnvironment().getProfile();
95      	this.schema = null;
96      	IDatabaseTester dbTester = this.newDatabaseTester(this.schema);
97      	try {
98  			IDatabaseConnection dbConnection = dbTester.getConnection();
99  			
100 			assertEquals(null, dbConnection.getSchema());
101 	        assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE", null));
102     	}
103     	finally {
104     		// Reset the testers schema for subsequent tests (environment.dbTester is a singleton)
105     		dbTester.setSchema(profile.getSchema());    		
106     	}
107     }
108 
109     
110     private IDatabaseTester newDatabaseTester(String schema) throws Exception {
111     	IDatabaseTester tester = super.newDatabaseTester();
112     	tester.setSchema(schema);
113     	return tester;
114 	}
115 
116 
117 	protected IDatabaseConnection getConnection() throws Exception {
118         String name = profile.getDriverClass();
119         Class.forName(name);
120         Connection connection = DriverManager.getConnection(
121                 profile.getConnectionUrl(), profile.getUser(),
122                 profile.getPassword());
123         _connection = new DatabaseConnection(connection,
124                 profile.getSchema());
125 		
126         IDatabaseConnection dbunitConnection = new DatabaseConnection(connection,
127                 this.schema);
128         return dbunitConnection;
129 	}
130     
131     
132 
133 }
134 
135