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