View Javadoc
1   package org.dbunit.dataset;
2   
3   import java.io.FileReader;
4   
5   import junit.framework.TestCase;
6   
7   import org.dbunit.dataset.filter.IRowFilter;
8   import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
9   import org.dbunit.testutil.TestUtils;
10  
11  /**
12   * @author gommma
13   * @since 2.3.0
14   */
15  public class RowFilterTableTest extends TestCase
16  {
17  	
18      private IDataSet getDataSet() throws Exception
19      {
20          return new FlatXmlDataSetBuilder().build(TestUtils.getFileReader(
21                  "xml/rowFilterTableTest.xml"));
22      }
23  
24  
25      public void testRowFilter_HappyPath() throws Exception
26      {
27      	ITable testTable = getDataSet().getTable("TEST_TABLE");
28      	IRowFilter rowFilter = new IRowFilter() {
29  			public boolean accept(IRowValueProvider rowValueProvider) {
30  				try {
31  					String value = (String)rowValueProvider.getColumnValue("COLUMN0");
32  					// filter out first row
33  					if(value.equals("row 0 col 0")) {
34  						return false;
35  					}
36  					return true;
37  				} catch (DataSetException e) {
38  					throw new RuntimeException("Should not happen in this unit test",e);
39  				}
40  			}
41  		
42  		};
43      	ITable rowFilterTable = new RowFilterTable(testTable, rowFilter);
44      	// The first row should be filtered
45      	assertEquals(3, rowFilterTable.getRowCount());
46      	assertEquals("row 1 col 0", rowFilterTable.getValue(0, "COLUMN0"));
47      	assertEquals("row 2 col 0", rowFilterTable.getValue(1, "COLUMN0"));
48      	assertEquals("row 3 col 0", rowFilterTable.getValue(2, "COLUMN0"));
49      }
50  }