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 junit.framework.TestCase;
25
26 import org.dbunit.DatabaseEnvironment;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35 public abstract class AbstractTableTest extends TestCase
36 {
37 protected static final int ROW_COUNT = 6;
38 protected static final int COLUMN_COUNT = 4;
39
40 protected final Logger logger = LoggerFactory.getLogger(getClass());
41
42 public AbstractTableTest(String s)
43 {
44 super(s);
45 }
46
47
48
49
50
51
52 protected abstract ITable createTable() throws Exception;
53
54
55
56
57
58
59
60
61
62 protected String convertString(String str) throws Exception
63 {
64 return str;
65 }
66
67
68
69
70 public void testGetRowCount() throws Exception
71 {
72 assertEquals("row count", ROW_COUNT, createTable().getRowCount());
73 }
74
75 public void testTableMetaData() throws Exception
76 {
77 Column[] columns = createTable().getTableMetaData().getColumns();
78 assertEquals("column count", COLUMN_COUNT, columns.length);
79 for (int i = 0; i < columns.length; i++)
80 {
81 String expected = convertString("COLUMN" + i);
82 String actual = columns[i].getColumnName();
83 assertEquals("column name", expected, actual);
84 }
85 }
86
87 public void testGetValue() throws Exception
88 {
89 ITable table = createTable();
90 for (int i = 0; i < ROW_COUNT; i++)
91 {
92 for (int j = 0; j < COLUMN_COUNT; j++)
93 {
94 String columnName = "COLUMN" + j;
95 String expected = "row " + i + " col " + j;
96 Object value = table.getValue(i, columnName);
97 assertEquals("value", expected, value);
98 }
99 }
100 }
101
102 public void testGetValueCaseInsensitive() throws Exception
103 {
104 ITable table = createTable();
105 for (int i = 0; i < ROW_COUNT; i++)
106 {
107 for (int j = 0; j < COLUMN_COUNT; j++)
108 {
109 String columnName = "CoLUmN" + j;
110 String expected = "row " + i + " col " + j;
111 Object value = table.getValue(i, columnName);
112 assertEquals("value", expected, value);
113 }
114 }
115 }
116
117 public abstract void testGetMissingValue() throws Exception;
118
119 public void testGetValueRowBounds() throws Exception
120 {
121 int[] rows = new int[]{-2, -1, -ROW_COUNT, ROW_COUNT, ROW_COUNT + 1};
122 ITable table = createTable();
123 String columnName = table.getTableMetaData().getColumns()[0].getColumnName();
124
125 for (int i = 0; i < rows.length; i++)
126 {
127 try
128 {
129 table.getValue(rows[i], columnName);
130 fail("Should throw a RowOutOfBoundsException!");
131 }
132 catch (RowOutOfBoundsException e)
133 {
134 }
135 }
136 }
137
138 public void testGetValueAndNoSuchColumn() throws Exception
139 {
140 ITable table = createTable();
141 String columnName = "Unknown";
142
143 try
144 {
145 table.getValue(0, columnName);
146 fail("Should throw a NoSuchColumnException!");
147 }
148 catch (NoSuchColumnException e)
149 {
150 }
151 }
152
153
154
155
156
157
158
159 protected boolean runTest(String testName) {
160 return true;
161 }
162
163 protected void runTest() throws Throwable {
164 if ( runTest(getName()) ) {
165 super.runTest();
166 } else {
167 if ( logger.isDebugEnabled() ) {
168 logger.debug( "Skipping test " + getClass().getName() + "." + getName() );
169 }
170 }
171 }
172
173 }
174
175
176
177
178
179