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.database;
23
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.dbunit.AbstractHSQLTestCase;
28 import org.dbunit.dataset.Column;
29 import org.dbunit.dataset.DataSetException;
30 import org.dbunit.dataset.IDataSet;
31 import org.dbunit.dataset.ITable;
32 import org.dbunit.dataset.ITableMetaData;
33 import org.dbunit.dataset.RowOutOfBoundsException;
34 import org.dbunit.util.CollectionsHelper;
35
36
37
38
39
40
41 public class PrimaryKeyFilteredTableWrapperTest extends AbstractHSQLTestCase {
42
43 private ITable fTable;
44 private IDataSet fDataSet;
45
46 public PrimaryKeyFilteredTableWrapperTest( String name ) {
47 super( name, "hypersonic_dataset.sql" );
48 }
49
50 protected void setUp() throws Exception {
51 super.setUp();
52 this.fDataSet = super.getConnection().createDataSet();
53 this.fTable = this.fDataSet.getTable(E);
54 }
55
56 public void testConstructorNullTable() throws DataSetException {
57 try {
58 PrimaryKeyFilteredTableWrapper table = new PrimaryKeyFilteredTableWrapper( null, new HashSet() );
59 fail( "constructor accepted null argument and returned " + table );
60 } catch( IllegalArgumentException e ) {
61 assertNotNull( e.getMessage() );
62 }
63 }
64
65 public void testConstructorNullSet() throws DataSetException {
66 try {
67 PrimaryKeyFilteredTableWrapper table = new PrimaryKeyFilteredTableWrapper( this.fTable, null );
68 fail( "constructor accepted null argument and returned " + table );
69 } catch( IllegalArgumentException e ) {
70 assertNotNull( e.getMessage() );
71 }
72 }
73
74 public void testDenyEverything() throws DataSetException {
75 PrimaryKeyFilteredTableWrapper table = new PrimaryKeyFilteredTableWrapper( this.fTable, new HashSet() );
76 assertMetaInformationEquals( this.fTable, table );
77 assertEquals( "table not empty", 0, table.getRowCount() );
78 assertSecondTableIsEmpty( this.fTable, table );
79 }
80
81 public void testAllowEverything() throws DataSetException {
82 Set allowedPKs = getPKs( this.fTable );
83 allowEveryThingTest( allowedPKs );
84 }
85
86 public void testAllowEverythingWithClonedSet() throws DataSetException {
87 Set allowedPKs = getPKs( this.fTable );
88 Set newSet = new HashSet( allowedPKs );
89 allowEveryThingTest( newSet );
90 }
91
92 public void testFilterLast() throws DataSetException {
93 doFilter( new String[] { E1, E2, E3 } );
94 }
95
96 public void testFilterFirst() throws DataSetException {
97 doFilter( new String[] { E2, E3, E4 } );
98 }
99
100 public void testFilterMiddle() throws DataSetException {
101 doFilter( new String[] { E1, E4 } );
102 }
103
104 private void doFilter( String[] ids) throws DataSetException {
105 Set allowedIds = CollectionsHelper.objectsToSet(ids);
106 ITable table = new PrimaryKeyFilteredTableWrapper( this.fTable, allowedIds );
107 assertEquals( "size of table does not match", ids.length, table.getRowCount() );
108 String pkColumn = table.getTableMetaData().getPrimaryKeys()[0].getColumnName();
109 int size = table.getRowCount();
110 for ( int i=0; i<size; i++ ) {
111 Object pk = table.getValue( i, pkColumn );
112 assertEquals( "id didn't match at index " + i, ids[i], pk );
113 }
114 }
115
116 private void allowEveryThingTest( Set set ) throws DataSetException {
117 PrimaryKeyFilteredTableWrapper table = new PrimaryKeyFilteredTableWrapper( this.fTable, new HashSet(set) );
118 assertTableSize( this.fTable, set.size() );
119 assertMetaInformationEquals( this.fTable, table );
120 assertTrue( "table is empty", table.getRowCount() > 0 );
121 assertContentIsSame( this.fTable, table );
122 }
123
124 private void assertTableSize(ITable table, int i) {
125 int size = table.getRowCount();
126 assertEquals( "getRowCount() didn't match", i, size );
127 }
128
129 private Set getPKs(ITable table) throws DataSetException {
130 String pkColumn = table.getTableMetaData().getPrimaryKeys()[0].getColumnName();
131 HashSet set = new HashSet();
132 int size = table.getRowCount();
133 for ( int i=0; i<size; i++ ) {
134 Object pk = table.getValue( i, pkColumn );
135 set.add( pk );
136 }
137 return set;
138 }
139
140
141 private void assertSecondTableIsEmpty(ITable t1, ITable t2) throws DataSetException {
142 int size = t1.getRowCount();
143 Column[] cols = t1.getTableMetaData().getColumns();
144 for ( int i=0; i<size; i++ ) {
145 for ( int j=0; j<cols.length; j++ ) {
146 String col = cols[j].getColumnName();
147 try {
148 Object o = t2.getValue( j, col );
149 fail( "there is an element at (" + i + ", " + col + ")" + o);
150 } catch ( RowOutOfBoundsException e ) {
151 assertNotNull( e.getMessage() );
152 }
153 }
154 }
155 }
156
157 private void assertContentIsSame(ITable t1, ITable t2) throws DataSetException {
158 int size = t1.getRowCount();
159 Column[] cols = t1.getTableMetaData().getColumns();
160 for ( int i=0; i<size; i++ ) {
161 for ( int j=0; j<cols.length; j++ ) {
162 String col = cols[j].getColumnName();
163 Object o1 = t1.getValue( j, col );
164 Object o2 = t2.getValue( j, col );
165 assertEquals( "element at (" + i + ", " + col + ") is not the same: ", o1, o2);
166 }
167 }
168 }
169
170
171 private void assertMetaInformationEquals(ITable t1, ITable t2) {
172 ITableMetaData metaData1 = t1.getTableMetaData();
173 ITableMetaData metaData2 = t2.getTableMetaData();
174 assertEquals( "metadata are not equal", metaData1, metaData2 );
175 }
176
177
178 }