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.mockito.ArgumentMatchers.anyInt;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.sql.ResultSet;
30  import java.util.Arrays;
31  import java.util.LinkedList;
32  
33  import org.dbunit.DatabaseEnvironment;
34  import org.dbunit.dataset.Column;
35  import org.dbunit.dataset.ForwardOnlyTableTest;
36  import org.dbunit.dataset.ITable;
37  import org.dbunit.dataset.MockTableMetaData;
38  import org.dbunit.dataset.RowOutOfBoundsException;
39  import org.dbunit.operation.DatabaseOperation;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.extension.ExtendWith;
42  import org.mockito.Mock;
43  import org.mockito.junit.jupiter.MockitoExtension;
44  
45  /**
46   * @author Manuel Laflamme
47   * @since Apr 11, 2003
48   * @version $Revision$
49   */
50  @ExtendWith(MockitoExtension.class)
51  class ForwardOnlyResultSetTableIT extends ForwardOnlyTableTest
52  {
53  
54      @Mock
55      private ResultSet mockResultSet;
56  
57      @Override
58      protected ITable createTable() throws Exception
59      {
60          final DatabaseEnvironment env = DatabaseEnvironment.getInstance();
61          final IDatabaseConnection connection = env.getConnection();
62  
63          DatabaseOperation.CLEAN_INSERT.execute(connection,
64                  env.getInitDataSet());
65  
66          final String selectStatement =
67                  "select * from TEST_TABLE order by COLUMN0";
68          return new ForwardOnlyResultSetTable("TEST_TABLE", selectStatement,
69                  connection);
70      }
71  
72      @Override
73      protected String convertString(final String str) throws Exception
74      {
75          return DatabaseEnvironment.getInstance().convertString(str);
76      }
77  
78      @Override
79      @Test
80      public void testGetMissingValue_withMissingCells_returnsExpectedValues() throws Exception
81      {
82          // Do not test this!
83      }
84  
85      @Test
86      void testGetValueOnLastRowIsClosingResultSet_afterIteratingAllRows_closesResultSet() throws Exception
87      {
88          final String tableName = "TABLE";
89          final String[] columnNames = {"C0"};
90          // String[] columnNames = {"C0", "C1", "C2"};
91          final Object[][] expectedValues = new Object[][] {
92                  new Object[] {"1", "2", "3"}, new Object[] {"4", "5", "6"},
93                  new Object[] {"7", "8", "9"},};
94  
95          // Setup resultset
96          // Our resultSet wasNull call the array
97          final LinkedList<Object[]> results = new LinkedList<>();
98          Arrays.asList(expectedValues).forEach(results::add);
99          // We only have 1 column so just return value in position [0]
100         when(mockResultSet.getObject(anyInt()))
101                 .thenAnswer(invocation -> results.removeFirst()[0]);
102         when(mockResultSet.next()).thenReturn(true).thenReturn(true)
103                 .thenReturn(false);
104 
105         // Create table
106         final MockTableMetaData metaData =
107                 new MockTableMetaData(tableName, columnNames);
108         final ForwardOnlyResultSetTable table =
109                 new ForwardOnlyResultSetTable(metaData, mockResultSet);
110 
111         // Exercise getValue()
112         try
113         {
114             final Column[] columns = table.getTableMetaData().getColumns();
115 
116             for (int i = 0;; i++)
117             {
118                 for (int j = 0; j < columns.length; j++)
119                 {
120                     final String columnName = columns[j].getColumnName();
121                     final Object actualValue = table.getValue(i, columnName);
122                     final Object expectedValue = expectedValues[i][j];
123                     assertThat(actualValue)
124                             .as("row=" + i + ", col=" + columnName)
125                             .isEqualTo(expectedValue);
126 
127                 }
128             }
129         } catch (final RowOutOfBoundsException e)
130         {
131             // end of table
132         }
133 
134         // Verify that ResultSet have been closed
135         verify(mockResultSet, times(1)).close();
136     }
137 
138 }