View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, 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.dataset;
23  
24  import java.io.FileReader;
25  
26  import org.dbunit.dataset.xml.XmlDataSet;
27  import org.dbunit.testutil.TestUtils;
28  
29  /**
30   * @author Manuel Laflamme
31   * @version $Revision$
32   * @since Feb 22, 2002
33   */
34  public class FilteredDataSetTest extends AbstractDataSetTest
35  {
36      public FilteredDataSetTest(String s)
37      {
38          super(s);
39      }
40  
41      protected IDataSet createDataSet() throws Exception
42      {
43          IDataSet dataSet1 = new XmlDataSet(
44                  TestUtils.getFileReader("xml/dataSetTest.xml"));
45          IDataSet dataSet2 = new XmlDataSet(
46                  TestUtils.getFileReader("xml/filteredDataSetTest.xml"));
47  
48          IDataSet dataSet = new CompositeDataSet(dataSet1, dataSet2);
49          assertEquals("count before filter", getExpectedNames().length + 1,
50                  dataSet.getTableNames().length);
51          return new FilteredDataSet(getExpectedNames(), dataSet);
52      }
53  
54      protected IDataSet createDuplicateDataSet() throws Exception
55      {
56          IDataSet dataSet1 = new XmlDataSet(
57                  TestUtils.getFileReader("xml/xmlDataSetDuplicateTest.xml"));
58          IDataSet dataSet2 = new XmlDataSet(
59                  TestUtils.getFileReader("xml/filteredDataSetTest.xml"));
60  
61          assertEquals(2, dataSet1.getTableNames().length);
62          assertEquals(1, dataSet2.getTableNames().length);
63          
64          IDataSet dataSet = new CompositeDataSet(dataSet1, dataSet2, false);
65          assertEquals("count before filter", 3, dataSet.getTableNames().length);
66          return new FilteredDataSet(getExpectedDuplicateNames(), dataSet);
67      }
68  
69      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception
70      {
71          String[] names = getExpectedDuplicateNames();
72          names[0] = names[0].toLowerCase();
73  
74          return new FilteredDataSet(names, createDuplicateDataSet());
75      }
76  
77      public void testGetFilteredTableNames() throws Exception
78      {
79          String[] originalNames = getExpectedNames();
80          String expectedName = originalNames[0];
81          IDataSet dataSet = createDataSet();
82          assertTrue("original count", dataSet.getTableNames().length > 1);
83  
84          IDataSet filteredDataSet = new FilteredDataSet(new String[]{expectedName}, dataSet);
85          assertEquals("filtered count", 1, filteredDataSet.getTableNames().length);
86          assertEquals("filtered names", expectedName, filteredDataSet.getTableNames()[0]);
87      }
88  
89      public void testGetFilteredTable() throws Exception
90      {
91          String[] originalNames = getExpectedNames();
92          IDataSet filteredDataSet = new FilteredDataSet(
93                  new String[]{originalNames[0]}, createDataSet());
94  
95  
96          for (int i = 0; i < originalNames.length; i++)
97          {
98              String name = originalNames[i];
99              if (i == 0)
100             {
101                 assertEquals("table " + i, name,
102                         filteredDataSet.getTable(name).getTableMetaData().getTableName());
103             }
104             else
105             {
106                 try
107                 {
108                     filteredDataSet.getTable(name);
109                     fail("Should throw a NoSuchTableException");
110                 }
111                 catch (NoSuchTableException e)
112                 {
113                 }
114             }
115         }
116     }
117 
118     public void testGetFilteredTableMetaData() throws Exception
119     {
120         String[] originalNames = getExpectedNames();
121         IDataSet filteredDataSet = new FilteredDataSet(
122                 new String[]{originalNames[0]}, createDataSet());
123 
124 
125         for (int i = 0; i < originalNames.length; i++)
126         {
127             String name = originalNames[i];
128             if (i == 0)
129             {
130                 assertEquals("table " + i, name,
131                         filteredDataSet.getTableMetaData(name).getTableName());
132             }
133             else
134             {
135                 try
136                 {
137                     filteredDataSet.getTableMetaData(name);
138                     fail("Should throw a NoSuchTableException");
139                 }
140                 catch (NoSuchTableException e)
141                 {
142                 }
143             }
144         }
145     }
146 
147 }
148 
149 
150 
151 
152