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 java.io.File;
25 import java.io.IOException;
26
27 import org.dbunit.dataset.datatype.DataType;
28 import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
29 import org.dbunit.testutil.TestUtils;
30
31
32
33
34
35 public class SortedTableTest extends AbstractTableTest
36 {
37 private File sortedTableTestFile = TestUtils.getFile("xml/sortedTableTest.xml");
38
39 public SortedTableTest(String s)
40 {
41 super(s);
42 }
43
44 protected ITable createTable() throws Exception
45 {
46 return createDataSet().getTable("TEST_TABLE");
47 }
48
49 protected IDataSet createDataSet() throws Exception
50 {
51 return new SortedDataSet(createUnsortedDataSet());
52 }
53
54 private IDataSet createUnsortedDataSet() throws DataSetException, IOException
55 {
56 return new FlatXmlDataSetBuilder().build(sortedTableTestFile);
57 }
58
59 private ITable createNumericTable() throws Exception
60 {
61
62 Column[] columns = new Column[]{
63 new Column("COLUMN0", DataType.NUMERIC),
64 new Column("COLUMN1", DataType.VARCHAR)
65 };
66 DefaultTable table = new DefaultTable("TEST_TABLE", columns);
67 Object[] row1 = new Object[]{new Integer(9), "row 9"};
68 Object[] row2 = new Object[]{new Integer(10), "row 10"};
69 Object[] row3 = new Object[]{new Integer(11), "row 11"};
70 table.addRow(row1);
71 table.addRow(row2);
72 table.addRow(row3);
73 return table;
74 }
75
76
77 public void testSetUseComparableTooLate() throws Exception
78 {
79 ITable table = createTable();
80 SortedTable sortedTable = new SortedTable(table);
81
82 sortedTable.getValue(0, "COLUMN0");
83
84 try
85 {
86 sortedTable.setUseComparable(true);
87 fail("Should not be able to set 'useComparable' after table has already been in use");
88 }
89 catch(IllegalStateException expected)
90 {
91 String msgStart = "Do not use this method after the table has been used";
92 assertTrue("Msg should start with: " + msgStart, expected.getMessage().startsWith(msgStart));
93 }
94 }
95
96
97 public void testSortByComparable() throws Exception
98 {
99
100 String columnName = "COLUMN0";
101
102 ITable table = createNumericTable();
103 SortedTable sortedTable = new SortedTable(table, new String[]{columnName});
104 sortedTable.setUseComparable(true);
105
106 Column[] columns = sortedTable.getTableMetaData().getColumns();
107 assertEquals("column count", 2, columns.length);
108 assertEquals("row count", 3, sortedTable.getRowCount());
109
110 Object[] expected = {new Integer(9), new Integer(10), new Integer(11)};
111 for (int i = 0; i < sortedTable.getRowCount(); i++)
112 {
113 assertEquals("value row " + i, expected[i],
114 sortedTable.getValue(i, columnName));
115 }
116 }
117
118
119
120
121
122 public void testSortByString() throws Exception
123 {
124
125 String columnName = "COLUMN0";
126
127 ITable table = createNumericTable();
128 SortedTable sortedTable = new SortedTable(table, new String[]{columnName});
129
130 Column[] columns = sortedTable.getTableMetaData().getColumns();
131 assertEquals("column count", 2, columns.length);
132 assertEquals("row count", 3, sortedTable.getRowCount());
133
134 Object[] expected = {new Integer(10), new Integer(11), new Integer(9)};
135 for (int i = 0; i < sortedTable.getRowCount(); i++)
136 {
137 assertEquals("value row " + i, expected[i],
138 sortedTable.getValue(i, columnName));
139 }
140 }
141
142
143 public void testGetMissingValue() throws Exception
144 {
145 String columnName = "COLUMN2";
146 Object[] expected = {null, null, null, "0", "1"};
147
148 ITable table = createDataSet().getTable("MISSING_VALUES");
149
150 Column[] columns = table.getTableMetaData().getColumns();
151 assertEquals("column count", 3, columns.length);
152 assertEquals("row count", 5, table.getRowCount());
153 for (int i = 0; i < table.getRowCount(); i++)
154 {
155 assertEquals("value row " + i, expected[i],
156 table.getValue(i, columnName));
157 }
158 }
159
160 public void testCustomColumnsWithUnknownColumnName() throws Exception
161 {
162 String[] sortColumnNames = new String[] {"COLUMN2", "COLUMNXY_UNDEFINED"};
163
164 ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
165 try {
166 new SortedTable(unsortedTable, sortColumnNames);
167 fail("Should not be able to create a SortedTable with unexisting columns");
168 }catch(NoSuchColumnException expected) {
169 assertTrue(expected.getMessage().startsWith("MISSING_VALUES.COLUMNXY_UNDEFINED"));
170 }
171 }
172
173 public void testCustomColumnsWithUnknownColumn() throws Exception
174 {
175 Column[] sortColumns = new Column[] {
176 new Column("COLUMN2", DataType.UNKNOWN, Column.NULLABLE),
177 new Column("COLUMNXY_UNDEFINED", DataType.UNKNOWN, Column.NULLABLE)
178 };
179
180 ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
181 try {
182 new SortedTable(unsortedTable, sortColumns);
183 fail("Should not be able to create a SortedTable with unexisting columns");
184 }catch(NoSuchColumnException expected) {
185 assertTrue(expected.getMessage().startsWith("MISSING_VALUES.COLUMNXY_UNDEFINED"));
186 }
187 }
188
189 public void testCustomColumnsWithDifferentColumnTypesButSameName() throws Exception
190 {
191 Column sortColumn = new Column("COLUMN2", DataType.CHAR, Column.NO_NULLS);
192 Column[] sortColumns = new Column[] { sortColumn };
193
194 ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
195 SortedTable sortedTable = new SortedTable(unsortedTable, sortColumns);
196
197 Column actualSortColumn = sortedTable.getSortColumns()[0];
198
199 assertNotSame(sortColumn, actualSortColumn);
200 assertEquals(DataType.UNKNOWN, actualSortColumn.getDataType());
201 assertEquals("COLUMN2", actualSortColumn.getColumnName());
202 assertEquals(Column.NULLABLE, actualSortColumn.getNullable());
203 }
204
205 }
206
207
208
209
210
211
212
213
214