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.database;
22  
23  import static org.assertj.core.api.Assertions.assertThat;
24  import static org.assertj.core.api.Assertions.assertThatThrownBy;
25  
26  import java.sql.Connection;
27  import java.sql.DriverManager;
28  import java.sql.SQLException;
29  
30  import org.dbunit.AbstractDatabaseIT;
31  import org.dbunit.DatabaseProfile;
32  import org.dbunit.IDatabaseTester;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * @author Manuel Laflamme
38   * @version $Revision$
39   * @since Mar 26, 2002
40   */
41  abstract class AbstractDatabaseConnectionIT extends AbstractDatabaseIT
42  {
43      private String schema;
44      private DatabaseProfile profile;
45  
46      @Override
47      @BeforeEach
48      protected void setUp() throws Exception
49      {
50          super.setUp();
51          this.profile = super.getEnvironment().getProfile();
52          this.schema = this.profile.getSchema();
53      }
54  
55      @Test
56      final void testGetRowCount_withPopulatedTables_returnsCorrectCounts() throws Exception
57      {
58          assertThat(_connection.getRowCount("EMPTY_TABLE", null))
59                  .as("EMPTY_TABLE").isZero();
60          assertThat(_connection.getRowCount("EMPTY_TABLE")).as("EMPTY_TABLE")
61                  .isZero();
62  
63          assertThat(_connection.getRowCount("TEST_TABLE", null)).as("TEST_TABLE")
64                  .isEqualTo(6);
65          assertThat(_connection.getRowCount("TEST_TABLE")).as("TEST_TABLE")
66                  .isEqualTo(6);
67  
68          assertThat(_connection.getRowCount("PK_TABLE", "where PK0 = 0"))
69                  .as("PK_TABLE").isEqualTo(1);
70      }
71  
72      @Test
73      final void testGetRowCount_NonexistingSchema() throws Exception
74      {
75          final DatabaseProfile profile = super.getEnvironment().getProfile();
76          final String nonexistingSchema = profile.getSchema() + "_444_XYZ_TEST";
77          this.schema = nonexistingSchema;
78  
79          final IDatabaseTester dbTester =
80                  this.newDatabaseTester(nonexistingSchema);
81          try
82          {
83              final IDatabaseConnection dbConnection = dbTester.getConnection();
84  
85              assertThat(dbConnection.getSchema())
86                      .isEqualTo(convertString(nonexistingSchema));
87              assertThatThrownBy(() -> dbConnection.getRowCount("TEST_TABLE")).as(
88                      "Should not be able to retrieve row count for non-existing schema "
89                              + nonexistingSchema)
90                      .isInstanceOf(SQLException.class);
91  
92          } finally
93          {
94              // Reset the testers schema for subsequent tests
95              // (environment.dbTester is a
96              // singleton)
97              dbTester.setSchema(profile.getSchema());
98          }
99      }
100 
101     @Test
102     final void testGetRowCount_NoSchemaSpecified() throws Exception
103     {
104         final DatabaseProfile profile = super.getEnvironment().getProfile();
105         this.schema = null;
106         final IDatabaseTester dbTester = this.newDatabaseTester(this.schema);
107         try
108         {
109             final IDatabaseConnection dbConnection = dbTester.getConnection();
110 
111             assertThat(dbConnection.getSchema()).isNull();
112             assertThat(_connection.getRowCount("TEST_TABLE", null))
113                     .as("TEST_TABLE").isEqualTo(6);
114         } finally
115         {
116             // Reset the testers schema for subsequent tests
117             // (environment.dbTester is a
118             // singleton)
119             dbTester.setSchema(profile.getSchema());
120         }
121     }
122 
123     private IDatabaseTester newDatabaseTester(final String schema)
124             throws Exception
125     {
126         final IDatabaseTester tester = super.newDatabaseTester();
127         tester.setSchema(schema);
128         return tester;
129     }
130 
131     @Override
132     protected IDatabaseConnection getConnection() throws Exception
133     {
134         final String name = profile.getDriverClass();
135         Class.forName(name);
136         final Connection connection =
137                 DriverManager.getConnection(profile.getConnectionUrl(),
138                         profile.getUser(), profile.getPassword());
139         _connection = new DatabaseConnection(connection, profile.getSchema());
140 
141         final IDatabaseConnection dbunitConnection =
142                 new DatabaseConnection(connection, this.schema);
143         return dbunitConnection;
144     }
145 
146 }