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.dataset;
23  
24  import static org.assertj.core.api.Assertions.assertThat;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import org.junit.jupiter.api.Test;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * @author Manuel Laflamme
33   * @version $Revision$
34   * @since Feb 17, 2002
35   */
36  public abstract class AbstractTableTest
37  {
38      protected static final int ROW_COUNT = 6;
39      protected static final int COLUMN_COUNT = 4;
40  
41      protected final Logger logger = LoggerFactory.getLogger(getClass());
42  
43      /**
44       * Creates a table having 6 row and 4 column where columns are named
45       * "COLUMN1, COLUMN2, COLUMN3, COLUMN4" and values are string follwing this
46       * template "row ? col ?"
47       */
48      protected abstract ITable createTable() throws Exception;
49  
50      /**
51       * Returns the string converted as an identifier according to the metadata
52       * rules of the database environment. Most databases convert all metadata
53       * identifiers to uppercase. PostgreSQL converts identifiers to lowercase.
54       * MySQL preserves case.
55       * 
56       * @param str
57       *            The identifier.
58       * @return The identifier converted according to database rules.
59       */
60      protected String convertString(final String str) throws Exception
61      {
62          return str;
63      }
64  
65      ////////////////////////////////////////////////////////////////////////////
66      // Test methods
67      @Test
68      void testGetRowCount_withSixRowTable_returnsRowCount() throws Exception
69      {
70          assertThat(createTable().getRowCount()).as("row count")
71                  .isEqualTo(ROW_COUNT);
72      }
73  
74      @Test
75      void testTableMetaData_withFourColumns_returnsColumnMetaData() throws Exception
76      {
77          final Column[] columns = createTable().getTableMetaData().getColumns();
78          assertThat(columns).as("column count").hasSize(COLUMN_COUNT);
79          for (int i = 0; i < columns.length; i++)
80          {
81              final String expected = convertString("COLUMN" + i);
82              final String actual = columns[i].getColumnName();
83              assertThat(actual).as("column name").isEqualTo(expected);
84          }
85      }
86  
87      @Test
88      protected void testGetValue_withValidRowAndColumn_returnsExpectedValue() throws Exception
89      {
90          final ITable table = createTable();
91          for (int i = 0; i < ROW_COUNT; i++)
92          {
93              for (int j = 0; j < COLUMN_COUNT; j++)
94              {
95                  final String columnName = "COLUMN" + j;
96                  final String expected = "row " + i + " col " + j;
97                  final Object value = table.getValue(i, columnName);
98                  assertThat(value).as("value").isEqualTo(expected);
99              }
100         }
101     }
102 
103     @Test
104     void testGetValueCaseInsensitive_withMixedCaseColumnName_returnsValue() throws Exception
105     {
106         final ITable table = createTable();
107         for (int i = 0; i < ROW_COUNT; i++)
108         {
109             for (int j = 0; j < COLUMN_COUNT; j++)
110             {
111                 final String columnName = "CoLUmN" + j;
112                 final String expected = "row " + i + " col " + j;
113                 final Object value = table.getValue(i, columnName);
114                 assertThat(value).as("value").isEqualTo(expected);
115             }
116         }
117     }
118 
119     public abstract void testGetMissingValue_withMissingCells_returnsExpectedValues() throws Exception;
120 
121     @Test
122     void testGetValueRowBounds_withOutOfBoundsRow_throwsRowOutOfBoundsException() throws Exception
123     {
124         final int[] rows =
125                 new int[] {-2, -1, -ROW_COUNT, ROW_COUNT, ROW_COUNT + 1};
126         final ITable table = createTable();
127         final String columnName =
128                 table.getTableMetaData().getColumns()[0].getColumnName();
129 
130         for (int i = 0; i < rows.length; i++)
131         {
132             final int row = i;
133             assertThrows(RowOutOfBoundsException.class,
134                     () -> table.getValue(rows[row], columnName),
135                     "Should throw a RowOutOfBoundsException!");
136         }
137     }
138 
139     @Test
140     void testGetValueAndNoSuchColumn_withUnknownColumn_throwsNoSuchColumnException() throws Exception
141     {
142         final ITable table = createTable();
143         final String columnName = "Unknown";
144 
145         assertThrows(NoSuchColumnException.class,
146                 () -> table.getValue(0, columnName),
147                 "Should throw a NoSuchColumnException!");
148 
149     }
150 }