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  package org.dbunit;
22  
23  import static org.assertj.core.api.Assertions.assertThat;
24  import static org.assertj.core.api.Assertions.catchThrowable;
25  
26  import java.sql.SQLException;
27  
28  import org.dbunit.database.DatabaseConfig;
29  import org.dbunit.database.IDatabaseConnection;
30  import org.dbunit.dataset.IDataSet;
31  import org.dbunit.operation.DatabaseOperation;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * @author gommma
36   * @author Last changed by: $Author$
37   * @version $Revision$ $Date$
38   * @since 2.3.0
39   */
40  class DatabaseTestCaseIT
41  {
42  
43      @Test
44      void testTearDownExceptionDoesNotObscureTestException() throws Exception
45      {
46          final IDatabaseConnection conn =
47                  DatabaseEnvironment.getInstance().getConnection();
48  
49          final SQLException tearDownFailure =
50                  new SQLException("Simulated tear-down operation failure.");
51          final DatabaseOperation failingTearDownOperation =
52                  new DatabaseOperation()
53                  {
54                      @Override
55                      public void execute(final IDatabaseConnection connection,
56                              final IDataSet dataSet) throws SQLException
57                      {
58                          throw tearDownFailure;
59                      }
60                  };
61  
62          final DatabaseTestCase testSubject =
63                  makeTestSubject(conn, failingTearDownOperation);
64          testSubject.setUp();
65  
66          final RuntimeException testFailure =
67                  new RuntimeException("Simulated test body failure.");
68  
69          final Throwable propagated =
70                  catchThrowable(() -> testSubject.tearDown(testFailure));
71  
72          assertThat(propagated)
73                  .as("The original test failure must propagate, not the tear-down failure.")
74                  .isSameAs(testFailure);
75          assertThat(propagated.getSuppressed())
76                  .as("The tear-down failure must be attached as suppressed, not lost.")
77                  .containsExactly(tearDownFailure);
78      }
79  
80      @Test
81      void testTearDownErrorDoesNotObscureTestException() throws Exception
82      {
83          final IDatabaseConnection conn =
84                  DatabaseEnvironment.getInstance().getConnection();
85  
86          final AssertionError tearDownFailure = new AssertionError(
87                  "Simulated tear-down operation failure (an Error, not an Exception).");
88          final DatabaseOperation failingTearDownOperation =
89                  new DatabaseOperation()
90                  {
91                      @Override
92                      public void execute(final IDatabaseConnection connection,
93                              final IDataSet dataSet)
94                      {
95                          throw tearDownFailure;
96                      }
97                  };
98  
99          final DatabaseTestCase testSubject =
100                 makeTestSubject(conn, failingTearDownOperation);
101         testSubject.setUp();
102 
103         final RuntimeException testFailure =
104                 new RuntimeException("Simulated test body failure.");
105 
106         final Throwable propagated =
107                 catchThrowable(() -> testSubject.tearDown(testFailure));
108 
109         assertThat(propagated)
110                 .as("The original test failure must propagate, not the tear-down Error.")
111                 .isSameAs(testFailure);
112         assertThat(propagated.getSuppressed())
113                 .as("The tear-down Error must be attached as suppressed, not lost.")
114                 .containsExactly(tearDownFailure);
115     }
116 
117     private DatabaseTestCase makeTestSubject(final IDatabaseConnection conn,
118             final DatabaseOperation tearDownOperation)
119     {
120         return new DatabaseTestCase()
121         {
122             @Override
123             protected IDatabaseConnection getConnection() throws Exception
124             {
125                 return conn;
126             }
127 
128             @Override
129             protected IDataSet getDataSet() throws Exception
130             {
131                 return null;
132             }
133 
134             @Override
135             protected DatabaseOperation getSetUpOperation() throws Exception
136             {
137                 return DatabaseOperation.NONE;
138             }
139 
140             @Override
141             protected DatabaseOperation getTearDownOperation() throws Exception
142             {
143                 return tearDownOperation;
144             }
145         };
146     }
147 
148     /**
149      * Tests whether the user can simply change the {@link DatabaseConfig} by
150      * overriding the method
151      * {@link DatabaseTestCase#setUpDatabaseConfig(DatabaseConfig)}.
152      * 
153      * @throws Exception
154      */
155     @Test
156     void testConfigureConnection_withCustomBatchSize_setsConfigPropertyOnConnection() throws Exception
157     {
158         final DatabaseEnvironment dbEnv = DatabaseEnvironment.getInstance();
159         final IDatabaseConnection conn = dbEnv.getConnection();
160 
161         final DatabaseTestCase testSubject = new DatabaseTestCase()
162         {
163 
164             /**
165              * method under test
166              */
167             @Override
168             protected void setUpDatabaseConfig(final DatabaseConfig config)
169             {
170                 config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE,
171                         Integer.valueOf(97));
172             }
173 
174             @Override
175             protected IDatabaseConnection getConnection() throws Exception
176             {
177                 return conn;
178             }
179 
180             @Override
181             protected IDataSet getDataSet() throws Exception
182             {
183                 return null;
184             }
185 
186             @Override
187             protected DatabaseOperation getSetUpOperation() throws Exception
188             {
189                 return DatabaseOperation.NONE;
190             }
191 
192             @Override
193             protected DatabaseOperation getTearDownOperation() throws Exception
194             {
195                 return DatabaseOperation.NONE;
196             }
197         };
198 
199         // Simulate JUnit which first of all calls the "setUp" method
200         testSubject.setUp();
201 
202         final IDatabaseConnection actualConn = testSubject.getConnection();
203         assertThat(actualConn.getConfig()
204                 .getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE))
205                         .isEqualTo(Integer.valueOf(97));
206 
207         final IDatabaseConnection actualConn2 =
208                 testSubject.getDatabaseTester().getConnection();
209         assertThat(actualConn2.getConfig()
210                 .getProperty(DatabaseConfig.PROPERTY_BATCH_SIZE))
211                         .isEqualTo(Integer.valueOf(97));
212     }
213 }