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  
22  package org.dbunit.database;
23  
24  import static org.assertj.core.api.Assertions.assertThat;
25  import static org.junit.jupiter.api.Assumptions.assumeTrue;
26  
27  import java.sql.Statement;
28  
29  import org.dbunit.AbstractDatabaseIT;
30  import org.dbunit.DatabaseEnvironment;
31  import org.dbunit.TestFeature;
32  import org.dbunit.dataset.AbstractTableTest;
33  import org.dbunit.dataset.ITable;
34  import org.dbunit.dataset.ITableMetaData;
35  import org.dbunit.operation.DatabaseOperation;
36  import org.junit.jupiter.api.AfterEach;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Integration test for {@link ScrollableResultSetTable}.
42   *
43   * <p>Tests inherited from {@link AbstractTableTest} are skipped when the active
44   * database profile does not support {@link TestFeature#SCROLLABLE_RESULTSET}.
45   *
46   * <p>The H2-based tests ({@code *_withH2Connection_*}) always run and verify
47   * that both constructors produce a scrollable table with correct row count and
48   * random-access {@code getValue()}.  They would fail before the
49   * {@code TYPE_SCROLL_INSENSITIVE} fix.
50   *
51   * @author Manuel Laflamme
52   * @version $Revision$
53   * @since Feb 19, 2002
54   */
55  public class ScrollableResultSetTableIT extends AbstractTableTest
56  {
57      private static final String H2_TABLE = "T";
58      private static final String H2_SELECT = "SELECT * FROM T ORDER BY ID";
59  
60      private IDatabaseConnection h2Connection;
61  
62      @BeforeEach
63      void setUpH2() throws Exception
64      {
65          h2Connection = InMemoryDatabaseConnection.create("PUBLIC");
66          final Statement stmt = h2Connection.getConnection().createStatement();
67          stmt.execute("CREATE TABLE T (ID INTEGER PRIMARY KEY, NAME VARCHAR(50))");
68          stmt.execute("INSERT INTO T VALUES (1, 'Alice')");
69          stmt.execute("INSERT INTO T VALUES (2, 'Bob')");
70          stmt.close();
71      }
72  
73      @AfterEach
74      void tearDownH2() throws Exception
75      {
76          if (h2Connection != null)
77          {
78              h2Connection.close();
79          }
80      }
81  
82      /**
83       * {@inheritDoc}
84       *
85       * <p>Skips when the active database profile does not support
86       * {@link TestFeature#SCROLLABLE_RESULTSET}.
87       */
88      @Override
89      protected ITable createTable() throws Exception
90      {
91          assumeTrue(AbstractDatabaseIT.environmentHasFeature(TestFeature.SCROLLABLE_RESULTSET),
92                  "Skipping: SCROLLABLE_RESULTSET not supported by current database profile.");
93  
94          final DatabaseEnvironment env = DatabaseEnvironment.getInstance();
95          final IDatabaseConnection connection = env.getConnection();
96  
97          DatabaseOperation.CLEAN_INSERT.execute(connection, env.getInitDataSet());
98  
99          final String selectStatement = "select * from TEST_TABLE order by COLUMN0";
100         return new ScrollableResultSetTable("TEST_TABLE", selectStatement, connection);
101     }
102 
103     @Override
104     @Test
105     public void testGetMissingValue_withMissingCells_returnsExpectedValues() throws Exception
106     {
107         // ScrollableResultSetTable does not have missing-cell semantics; skip this inherited test.
108     }
109 
110     // -------------------------------------------------------------------------
111     // H2-based tests — always run, verify the TYPE_SCROLL_INSENSITIVE fix
112     // -------------------------------------------------------------------------
113 
114     @Test
115     void testConstructorWithMetaDataAndConnection_withH2Connection_returnsCorrectRowCount()
116             throws Exception
117     {
118         final ITableMetaData metaData =
119                 h2Connection.createDataSet().getTableMetaData(H2_TABLE);
120 
121         final ScrollableResultSetTable table =
122                 new ScrollableResultSetTable(metaData, h2Connection);
123 
124         assertThat(table.getRowCount()).as("row count.").isEqualTo(2);
125         table.close();
126     }
127 
128     @Test
129     void testConstructorWithMetaDataAndConnection_withH2Connection_allowsRandomAccessToEarlierRow()
130             throws Exception
131     {
132         final ITableMetaData metaData =
133                 h2Connection.createDataSet().getTableMetaData(H2_TABLE);
134 
135         final ScrollableResultSetTable table =
136                 new ScrollableResultSetTable(metaData, h2Connection);
137 
138         // Access row 1 first, then row 0 — requires a scrollable result set.
139         assertThat(table.getValue(1, "ID")).as("row 1 ID.").isEqualTo(2);
140         assertThat(table.getValue(0, "ID")).as("row 0 ID after row 1.").isEqualTo(1);
141         table.close();
142     }
143 
144     @Test
145     void testConstructorWithSelectStatement_withH2Connection_returnsCorrectRowCount()
146             throws Exception
147     {
148         final ScrollableResultSetTable table =
149                 new ScrollableResultSetTable(H2_TABLE, H2_SELECT, h2Connection);
150 
151         assertThat(table.getRowCount()).as("row count.").isEqualTo(2);
152         table.close();
153     }
154 
155     @Test
156     void testConstructorWithSelectStatement_withH2Connection_allowsRandomAccessToEarlierRow()
157             throws Exception
158     {
159         final ScrollableResultSetTable table =
160                 new ScrollableResultSetTable(H2_TABLE, H2_SELECT, h2Connection);
161 
162         // Access row 1 first, then row 0 — requires a scrollable result set.
163         assertThat(table.getValue(1, "ID")).as("row 1 ID.").isEqualTo(2);
164         assertThat(table.getValue(0, "ID")).as("row 0 ID after row 1.").isEqualTo(1);
165         table.close();
166     }
167 }