1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
33
34
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
45
46
47
48 protected abstract ITable createTable() throws Exception;
49
50
51
52
53
54
55
56
57
58
59
60 protected String convertString(final String str) throws Exception
61 {
62 return str;
63 }
64
65
66
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 }