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 org.dbunit.DatabaseEnvironment;
27  import org.dbunit.DefaultDatabaseTester;
28  import org.dbunit.IDatabaseTester;
29  import org.dbunit.IOperationListener;
30  import org.dbunit.annotation.DbUnitConfig;
31  import org.dbunit.annotation.DbUnitExpected;
32  import org.dbunit.annotation.DbUnitPrep;
33  import org.dbunit.annotation.DbUnitProperty;
34  import org.dbunit.annotation.DbUnitTester;
35  import org.dbunit.database.DatabaseConfig;
36  import org.dbunit.database.IDatabaseConnection;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.extension.ExtendWith;
39  import org.junit.platform.testkit.engine.EngineTestKit;
40  
41  /**
42   * Real-database integration test proving an {@code @DbUnitProperty} value actually reaches
43   * {@link IDatabaseConnection#getConfig()} on both the setup/teardown path (the {@code
44   * IOperationListener} wiring described in the annotations plan's execution model) and the
45   * prep/expected path ({@code DefaultPrepAndExpectedTestCase}'s own connection resolution,
46   * which never goes through that listener) - neither is something
47   * {@code AnnotatedTestExecutorTest}'s mocked connection can prove.
48   */
49  class DbUnitConfigPropertiesIT
50  {
51      @Test
52      void testBeforeTestExecution_dbUnitPropertyDeclared_reachesConnectionConfig()
53              throws Exception
54      {
55          final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
56          try
57          {
58              final IDatabaseConnection connection = environment.getConnection();
59              connection.getConfig().setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, false);
60              PropertySample.databaseTester = new DefaultDatabaseTester(connection);
61              PropertySample.databaseTester
62                      .setOperationListener(IOperationListener.NO_OP_OPERATION_LISTENER);
63  
64              EngineTestKit.engine("junit-jupiter").selectors(selectClass(PropertySample.class))
65                      .execute().testEvents()
66                      .assertStatistics(stats -> stats.started(1).succeeded(1));
67  
68              assertThat(
69                      connection.getConfig().getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS))
70                              .as("The @DbUnitProperty value must have been applied to the real"
71                                      + " connection's DatabaseConfig.")
72                              .isTrue();
73          } finally
74          {
75              // The connection is shared/cached across every IT class; discard it rather than
76              // leaving the FEATURE_BATCHED_STATEMENTS mutation on it for whichever test runs
77              // next.
78              environment.closeConnection();
79          }
80      }
81  
82      @ExtendWith(DbUnitExtension.class)
83      // closeConnectionAfterTest = false: databaseTester wraps the environment's shared
84      // connection (see the finally block above), not one owned by this one test.
85      @DbUnitConfig(closeConnectionAfterTest = false,
86              properties = @DbUnitProperty(name = "batchedStatements", value = "true"))
87      @DbUnitPrep("empty.xml")
88      static class PropertySample
89      {
90          @DbUnitTester
91          static IDatabaseTester databaseTester;
92  
93          @Test
94          void testPropertyIsAppliedBeforeTheTestRuns()
95          {
96          }
97      }
98  
99      @Test
100     void testBeforeTestExecution_dbUnitPropertyDeclaredOnExpectedPath_reachesConnectionConfig()
101             throws Exception
102     {
103         final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
104         try
105         {
106             final IDatabaseConnection connection = environment.getConnection();
107             connection.getConfig().setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, false);
108             ExpectedPathPropertySample.databaseTester = new DefaultDatabaseTester(connection);
109             ExpectedPathPropertySample.databaseTester
110                     .setOperationListener(IOperationListener.NO_OP_OPERATION_LISTENER);
111 
112             EngineTestKit.engine("junit-jupiter")
113                     .selectors(selectClass(ExpectedPathPropertySample.class)).execute()
114                     .testEvents().assertStatistics(stats -> stats.started(1).succeeded(1));
115 
116             assertThat(
117                     connection.getConfig().getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS))
118                             .as("The @DbUnitProperty value must reach the connection"
119                                     + " DefaultPrepAndExpectedTestCase actually uses for"
120                                     + " setupData()/verifyData()/cleanupData() - the"
121                                     + " setup/teardown path's IOperationListener wiring is never"
122                                     + " triggered by those methods.")
123                             .isTrue();
124         } finally
125         {
126             // The connection is shared/cached across every IT class; discard it rather than
127             // leaving the FEATURE_BATCHED_STATEMENTS mutation on it for whichever test runs
128             // next.
129             environment.closeConnection();
130         }
131     }
132 
133     @ExtendWith(DbUnitExtension.class)
134     // closeConnectionAfterTest = false: databaseTester wraps the environment's shared
135     // connection (see the finally block above), not one owned by this one test.
136     @DbUnitConfig(closeConnectionAfterTest = false,
137             properties = @DbUnitProperty(name = "batchedStatements", value = "true"))
138     @DbUnitExpected("empty.xml")
139     static class ExpectedPathPropertySample
140     {
141         @DbUnitTester
142         static IDatabaseTester databaseTester;
143 
144         @Test
145         void testPropertyIsAppliedBeforeVerification()
146         {
147         }
148     }
149 }