View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2026, 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.junit.jupiter;
22  
23  import static org.assertj.core.api.Assertions.assertThat;
24  import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
25  
26  import java.sql.Connection;
27  import java.sql.Statement;
28  
29  import org.dbunit.DatabaseEnvironment;
30  import org.dbunit.DefaultDatabaseTester;
31  import org.dbunit.DefaultPrepAndExpectedTestCase;
32  import org.dbunit.PrepAndExpectedTestCase;
33  import org.dbunit.annotation.DbUnitExpected;
34  import org.dbunit.annotation.DbUnitPrep;
35  import org.dbunit.annotation.DbUnitTearDown;
36  import org.dbunit.annotation.DbUnitTestCase;
37  import org.dbunit.annotation.DbUnitVerifyTable;
38  import org.dbunit.database.IDatabaseConnection;
39  import org.dbunit.operation.DbUnitOperation;
40  import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
41  import org.dbunit.util.fileloader.FullXmlDataFileLoader;
42  import org.junit.jupiter.api.Test;
43  import org.junit.jupiter.api.extension.ExtendWith;
44  import org.junit.platform.testkit.engine.EngineTestKit;
45  
46  /**
47   * Real-database integration test proving the extension leaves alone what an injected
48   * {@code @DbUnitTestCase} instance was built with when {@code @DbUnitConfig} says nothing about
49   * it. The instance here is built to keep its connection open, as a caller does when the
50   * connection comes from a pool or {@code CachingConnectionProvider} that outlives one test;
51   * nothing in {@code @DbUnitConfig} asks otherwise, so the connection must still be open
52   * afterward. {@link DatabaseEnvironment#getConnection()} silently reopens a closed connection,
53   * so this asserts on the very connection object handed to the tester.
54   */
55  class DbUnitExtensionInjectedTestCaseSettingsIT
56  {
57      private static final String TEST_TABLE = "TEST_TABLE";
58  
59      @Test
60      void testAfterTestExecution_injectedTestCaseBuiltToKeepItsConnection_isNotMadeToCloseIt()
61              throws Exception
62      {
63          final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
64          try
65          {
66              environment.deleteAllRows(TEST_TABLE);
67              final IDatabaseConnection connection = environment.getConnection();
68              KeepsConnectionSample.testCase = new DefaultPrepAndExpectedTestCase(
69                      new FlatXmlDataFileLoader(), new DefaultDatabaseTester(connection), false);
70  
71              EngineTestKit.engine("junit-jupiter")
72                      .selectors(selectClass(KeepsConnectionSample.class)).execute().testEvents()
73                      .assertStatistics(stats -> stats.started(1).succeeded(1));
74  
75              assertThat(connection.getConnection().isClosed())
76                      .as("The instance was built with closeConnectionAfterTest=false and"
77                              + " @DbUnitConfig does not set it, so the extension must not turn it"
78                              + " back on and close the connection.")
79                      .isFalse();
80          } finally
81          {
82              environment.deleteAllRows(TEST_TABLE);
83              environment.closeConnection();
84          }
85      }
86  
87      @Test
88      void testAfterTestExecution_injectedTestCaseBuiltToKeepItsConnectionAndAConnectionParameter_isNotMadeToCloseIt()
89              throws Exception
90      {
91          final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
92          try
93          {
94              final IDatabaseConnection connection = environment.getConnection();
95              KeepsConnectionWithParameterSample.testCase = new DefaultPrepAndExpectedTestCase(
96                      new FlatXmlDataFileLoader(), new DefaultDatabaseTester(connection), false);
97  
98              EngineTestKit.engine("junit-jupiter")
99                      .selectors(selectClass(KeepsConnectionWithParameterSample.class)).execute()
100                     .testEvents().assertStatistics(stats -> stats.started(1).succeeded(1));
101 
102             assertThat(connection.getConnection().isClosed())
103                     .as("The instance was built with closeConnectionAfterTest=false, so the"
104                             + " connection the extension injected as a parameter - the"
105                             + " instance's own - must stay open too.")
106                     .isFalse();
107         } finally
108         {
109             environment.closeConnection();
110         }
111     }
112 
113     @Test
114     void testAfterTestExecution_injectedTestCaseWithItsOwnLoaderAndNoVerifySpec_derivesTablesWithThatLoader()
115             throws Exception
116     {
117         final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
118         try
119         {
120             final IDatabaseConnection connection = environment.getConnection();
121             OwnLoaderSample.testCase = new DefaultPrepAndExpectedTestCase(
122                     new FullXmlDataFileLoader(), new DefaultDatabaseTester(connection), false);
123 
124             EngineTestKit.engine("junit-jupiter").selectors(selectClass(OwnLoaderSample.class))
125                     .execute().testEvents()
126                     .assertStatistics(stats -> stats.started(1).succeeded(1));
127         } finally
128         {
129             environment.closeConnection();
130         }
131     }
132 
133     @ExtendWith(DbUnitExtension.class)
134     static class KeepsConnectionSample
135     {
136         @DbUnitTestCase
137         static PrepAndExpectedTestCase testCase;
138 
139         @Test
140         @DbUnitPrep("annotation-it-prep.xml")
141         @DbUnitExpected(value = "annotation-it-expected.xml",
142                 verify = @DbUnitVerifyTable(value = TEST_TABLE, include = {"COLUMN0", "COLUMN1"}))
143         @DbUnitTearDown(operation = DbUnitOperation.DELETE_ALL)
144         void mutateTheSeededRow() throws Exception
145         {
146             final IDatabaseConnection connection = testCase.getDatabaseTester().getConnection();
147             try (Statement statement = connection.getConnection().createStatement())
148             {
149                 statement.execute("UPDATE " + TEST_TABLE
150                         + " SET COLUMN1 = 'after' WHERE COLUMN0 = 'row0'");
151             }
152         }
153     }
154 
155     @ExtendWith(DbUnitExtension.class)
156     static class KeepsConnectionWithParameterSample
157     {
158         @DbUnitTestCase
159         static PrepAndExpectedTestCase testCase;
160 
161         @Test
162         @DbUnitPrep("annotation-it-prep.xml")
163         @DbUnitExpected(value = "annotation-it-expected.xml",
164                 verify = @DbUnitVerifyTable(value = TEST_TABLE, include = {"COLUMN0", "COLUMN1"}))
165         @DbUnitTearDown(operation = DbUnitOperation.DELETE_ALL)
166         void mutateTheSeededRow(final Connection connection) throws Exception
167         {
168             try (Statement statement = connection.createStatement())
169             {
170                 statement.execute("UPDATE " + TEST_TABLE
171                         + " SET COLUMN1 = 'after' WHERE COLUMN0 = 'row0'");
172             }
173         }
174     }
175 
176     @ExtendWith(DbUnitExtension.class)
177     static class OwnLoaderSample
178     {
179         @DbUnitTestCase
180         static PrepAndExpectedTestCase testCase;
181 
182         @Test
183         @DbUnitPrep("annotation-it-full-prep.xml")
184         @DbUnitExpected("annotation-it-full-expected.xml")
185         @DbUnitTearDown(operation = DbUnitOperation.DELETE_ALL)
186         void mutateTheSeededRow() throws Exception
187         {
188             final IDatabaseConnection connection = testCase.getDatabaseTester().getConnection();
189             try (Statement statement = connection.getConnection().createStatement())
190             {
191                 statement.execute("UPDATE " + TEST_TABLE
192                         + " SET COLUMN1 = 'after' WHERE COLUMN0 = 'row0'");
193             }
194         }
195     }
196 }