View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2008, 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  package org.dbunit;
22  
23  import java.sql.SQLException;
24  
25  import org.dbunit.database.DatabaseConfig;
26  import org.dbunit.database.IDatabaseConnection;
27  import org.dbunit.dataset.IDataSet;
28  import org.dbunit.dataset.ITable;
29  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
30  import org.dbunit.dataset.xml.FlatXmlDataSetTest;
31  import org.dbunit.operation.DatabaseOperation;
32  
33  import junit.framework.TestCase;
34  
35  /**
36   * @author gommma
37   * @author Last changed by: $Author: gommma $
38   * @version $Revision: 789 $ $Date: 2008-08-15 16:45:18 +0200 (Fr, 15. Aug 2008) $
39   * @since 2.4.3
40   */
41  public class DBTestCaseIT extends TestCase
42  {
43  
44  	/**
45  	 * Tests whether the user can simply change the {@link DatabaseConfig} by
46  	 * overriding the method {@link DatabaseTestCase#setUpDatabaseConfig(DatabaseConfig)}.
47  	 * @throws Exception
48  	 */
49  	public void testConfigureConnection() throws Exception
50  	{
51  	    DatabaseEnvironment dbEnv = DatabaseEnvironment.getInstance();
52  	    final IDatabaseConnection conn = dbEnv.getConnection();
53  	    final DefaultDatabaseTester tester = new DefaultDatabaseTester(conn);
54  	    final DatabaseOperation operation = new DatabaseOperation(){
55  	        public void execute(IDatabaseConnection connection, IDataSet dataSet)
56              throws DatabaseUnitException, SQLException {
57  	            assertEquals(new Integer(97), connection.getConfig().getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE));
58  	            assertEquals(true, connection.getConfig().getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS));
59  	        }
60  	    };
61  	    
62  	    DBTestCase testSubject = new DBTestCase() {
63  	        
64              /**
65               * method under test
66               */
67              protected void setUpDatabaseConfig(DatabaseConfig config) {
68                  config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, new Integer(97));
69                  config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true);
70              }
71  
72              protected IDatabaseTester newDatabaseTester() throws Exception {
73                  return tester;
74              }
75  
76              protected DatabaseOperation getSetUpOperation() throws Exception {
77                  return operation;
78              }
79  
80              protected DatabaseOperation getTearDownOperation() throws Exception {
81                  return operation;
82              }
83  
84              protected IDataSet getDataSet() throws Exception {
85                  return null;
86              }
87          };
88          
89          // Simulate JUnit which first of all calls the "setUp" method
90          testSubject.setUp();
91          
92          IDatabaseConnection actualConn = testSubject.getConnection();
93          assertEquals(new Integer(97), actualConn.getConfig().getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE));
94          assertSame(conn, actualConn);
95          
96          IDatabaseConnection actualConn2 = testSubject.getDatabaseTester().getConnection();
97          assertEquals(new Integer(97), actualConn2.getConfig().getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE));
98          assertSame(tester, testSubject.getDatabaseTester());
99          assertSame(conn, testSubject.getDatabaseTester().getConnection());
100 	}
101 	
102 	
103 	/**
104      * Tests the simple setup/teardown invocations while keeping the DatabaseConnection open.
105      * @throws Exception
106      */
107     public void testExecuteSetUpTearDown() throws Exception
108     {
109         //TODO implement this
110         DatabaseEnvironment dbEnv = DatabaseEnvironment.getInstance();
111         // Retrieve one single connection which is 
112         final IDatabaseConnection conn = dbEnv.getConnection();
113         try{
114             final DefaultDatabaseTester tester = new DefaultDatabaseTester(conn);
115             final IDataSet dataset = new FlatXmlDataSetBuilder().build(FlatXmlDataSetTest.DATASET_FILE);
116             
117             // Connection should not be closed during setUp/tearDown because of userDefined IOperationListener
118             DBTestCase testSubject = new DBTestCase() {
119                 
120                 protected IDatabaseTester newDatabaseTester() throws Exception {
121                     return tester;
122                 }
123     
124                 protected DatabaseOperation getSetUpOperation() throws Exception {
125                     return DatabaseOperation.CLEAN_INSERT;
126                 }
127     
128                 protected DatabaseOperation getTearDownOperation() throws Exception {
129                     return DatabaseOperation.DELETE_ALL;
130                 }
131     
132                 protected IDataSet getDataSet() throws Exception {
133                     return dataset;
134                 }
135 
136                 protected IOperationListener getOperationListener() {
137                     return new DefaultOperationListener(){
138                         public void operationSetUpFinished(
139                                 IDatabaseConnection connection) 
140                         {
141                             // Do not invoke the "super" method to avoid that the connection is closed
142                             // Just do nothing
143                         }
144 
145                         public void operationTearDownFinished(
146                                 IDatabaseConnection connection) 
147                         {
148                             // Do not invoke the "super" method to avoid that the connection is closed
149                             // Just do nothing
150                         }
151                         
152                     };
153                 }
154                 
155                 
156             };
157             
158             // Simulate JUnit which first of all calls the "setUp" method
159             testSubject.setUp();
160             // The connection should still be open so we should be able to select from the DB
161             ITable testTableAfterSetup = conn.createTable("TEST_TABLE");
162             assertEquals(6, testTableAfterSetup.getRowCount());
163             assertFalse(conn.getConnection().isClosed());
164             
165             // Simulate JUnit and invoke "tearDown"
166             testSubject.tearDown();
167             // The connection should still be open so we should be able to select from the DB
168             ITable testTableAfterTearDown = conn.createTable("TEST_TABLE");
169             assertEquals(0, testTableAfterTearDown.getRowCount());
170             assertFalse(conn.getConnection().isClosed());
171         }
172         finally{
173             // Ensure that the connection is closed again so that 
174             // it can be established later by subsequent test cases
175             dbEnv.closeConnection();
176         }
177     }
178 
179 }