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 static org.assertj.core.api.Assertions.assertThat;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertSame;
26  
27  import java.sql.SQLException;
28  
29  import org.dbunit.database.DatabaseConfig;
30  import org.dbunit.database.IDatabaseConnection;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.ITable;
33  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
34  import org.dbunit.dataset.xml.FlatXmlDataSetTest;
35  import org.dbunit.operation.DatabaseOperation;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * @author gommma
40   * @author Last changed by: $Author: gommma $
41   * @version $Revision: 789 $ $Date: 2008-08-15 16:45:18 +0200 (Fr, 15. Aug 2008)
42   * @since 2.4.3
43   */
44  class DBTestCaseIT
45  {
46      /**
47       * Tests whether the user can simply change the {@link DatabaseConfig} by
48       * overriding the method
49       * {@link DatabaseTestCase#setUpDatabaseConfig(DatabaseConfig)}.
50       * 
51       * @throws Exception
52       */
53      @Test
54      void testConfigureConnection_withCustomBatchSizeAndBatchedStatements_setsConfigPropertiesOnConnection() throws Exception
55      {
56          final DatabaseEnvironment dbEnv = DatabaseEnvironment.getInstance();
57          final IDatabaseConnection conn = dbEnv.getConnection();
58          final DefaultDatabaseTester tester = new DefaultDatabaseTester(conn);
59          final DatabaseOperation operation = new DatabaseOperation()
60          {
61              @Override
62              public void execute(final IDatabaseConnection connection,
63                      final IDataSet dataSet)
64                      throws DatabaseUnitException, SQLException
65              {
66                  assertThat(connection.getConfig()
67                          .getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE))
68                                  .isEqualTo(Integer.valueOf(97));
69                  assertThat(connection.getConfig()
70                          .getProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS))
71                                  .isEqualTo(true);
72              }
73          };
74  
75          final DBTestCase testSubject = new DBTestCase()
76          {
77              /**
78               * method under test
79               */
80              @Override
81              protected void setUpDatabaseConfig(final DatabaseConfig config)
82              {
83                  config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE,
84                          Integer.valueOf(97));
85                  config.setProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS,
86                          true);
87              }
88  
89              @Override
90              protected IDatabaseTester newDatabaseTester() throws Exception
91              {
92                  return tester;
93              }
94  
95              @Override
96              protected DatabaseOperation getSetUpOperation() throws Exception
97              {
98                  return operation;
99              }
100 
101             @Override
102             protected DatabaseOperation getTearDownOperation() throws Exception
103             {
104                 return operation;
105             }
106 
107             @Override
108             protected IDataSet getDataSet() throws Exception
109             {
110                 return null;
111             }
112         };
113 
114         // Simulate JUnit which first of all calls the "setUp" method
115         testSubject.setUp();
116 
117         final IDatabaseConnection actualConn = testSubject.getConnection();
118         assertThat(actualConn.getConfig()
119                 .getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE))
120                         .isEqualTo(Integer.valueOf(97));
121         assertSame(conn, actualConn);
122 
123         final IDatabaseConnection actualConn2 =
124                 testSubject.getDatabaseTester().getConnection();
125         assertThat(actualConn2.getConfig()
126                 .getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE))
127                         .isEqualTo(Integer.valueOf(97));
128         assertSame(tester, testSubject.getDatabaseTester());
129         assertSame(conn, testSubject.getDatabaseTester().getConnection());
130     }
131 
132     /**
133      * Tests the simple setup/teardown invocations while keeping the
134      * DatabaseConnection open.
135      * 
136      * @throws Exception
137      */
138     @Test
139     void testExecuteSetUpTearDown_withCleanInsertAndDeleteAllOperations_keepsConnectionOpenAndManagesData() throws Exception
140     {
141         // TODO implement this
142         final DatabaseEnvironment dbEnv = DatabaseEnvironment.getInstance();
143         // Retrieve one single connection which is
144         final IDatabaseConnection conn = dbEnv.getConnection();
145         try
146         {
147             final DefaultDatabaseTester tester =
148                     new DefaultDatabaseTester(conn);
149             final IDataSet dataset = new FlatXmlDataSetBuilder()
150                     .build(FlatXmlDataSetTest.DATASET_FILE);
151 
152             // Connection should not be closed during setUp/tearDown because of
153             // userDefined IOperationListener
154             final DBTestCase testSubject = new DBTestCase()
155             {
156                 @Override
157                 protected IDatabaseTester newDatabaseTester() throws Exception
158                 {
159                     return tester;
160                 }
161 
162                 @Override
163                 protected DatabaseOperation getSetUpOperation() throws Exception
164                 {
165                     return DatabaseOperation.CLEAN_INSERT;
166                 }
167 
168                 @Override
169                 protected DatabaseOperation getTearDownOperation()
170                         throws Exception
171                 {
172                     return DatabaseOperation.DELETE_ALL;
173                 }
174 
175                 @Override
176                 protected IDataSet getDataSet() throws Exception
177                 {
178                     return dataset;
179                 }
180 
181                 @Override
182                 protected IOperationListener getOperationListener()
183                 {
184                     return new DefaultOperationListener()
185                     {
186                         @Override
187                         public void operationSetUpFinished(
188                                 final IDatabaseConnection connection)
189                         {
190                             // Do not invoke the "super" method to avoid that
191                             // the connection is closed
192                             // Just do nothing
193                         }
194 
195                         @Override
196                         public void operationTearDownFinished(
197                                 final IDatabaseConnection connection)
198                         {
199                             // Do not invoke the "super" method to avoid that
200                             // the connection is closed
201                             // Just do nothing
202                         }
203                     };
204                 }
205             };
206 
207             // Simulate JUnit which first of all calls the "setUp" method
208             testSubject.setUp();
209             // The connection should still be open so we should be able to
210             // select from the DB
211             final ITable testTableAfterSetup = conn.createTable("TEST_TABLE");
212             assertThat(testTableAfterSetup.getRowCount()).isEqualTo(6);
213             assertFalse(conn.getConnection().isClosed());
214 
215             // Simulate JUnit and invoke "tearDown"
216             testSubject.tearDown();
217             // The connection should still be open so we should be able to
218             // select from the DB
219             final ITable testTableAfterTearDown =
220                     conn.createTable("TEST_TABLE");
221             assertThat(testTableAfterTearDown.getRowCount()).isEqualTo(0);
222             assertFalse(conn.getConnection().isClosed());
223         } finally
224         {
225             // Ensure that the connection is closed again so that
226             // it can be established later by subsequent test cases
227             dbEnv.closeConnection();
228         }
229     }
230 }