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.xml;
23  
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.FileWriter;
27  import java.io.StringReader;
28  import java.io.Writer;
29  
30  import org.dbunit.Assertion;
31  import org.dbunit.dataset.AbstractDataSetTest;
32  import org.dbunit.dataset.Column;
33  import org.dbunit.dataset.DataSetUtils;
34  import org.dbunit.dataset.IDataSet;
35  import org.dbunit.dataset.ITable;
36  import org.dbunit.dataset.ITableMetaData;
37  import org.dbunit.testutil.TestUtils;
38  
39  /**
40   * @author Manuel Laflamme
41   * @version $Revision$
42   * @since Mar 13, 2002
43   */
44  public class FlatXmlDataSetTest extends AbstractDataSetTest
45  {
46      public static final File DATASET_FILE =
47              TestUtils.getFile("xml/flatXmlDataSetTest.xml");
48      public static final File DUPLICATE_DATASET_FILE =
49              TestUtils.getFile("xml/flatXmlDataSetDuplicateTest.xml");
50      public static final File DUPLICATE_DATASET_MULTIPLE_CASE_FILE = 
51              TestUtils.getFile("xml/flatXmlDataSetDuplicateMultipleCaseTest.xml");
52  
53      private static final File FLAT_XML_TABLE = 
54      		    TestUtils.getFile("xml/flatXmlTableTest.xml");
55  
56      private static final File FLAT_XML_DTD_DIFFERENT_CASE_FILE = 
57              TestUtils.getFile("xml/flatXmlDataSetDtdDifferentCaseTest.xml");
58      
59      public FlatXmlDataSetTest(String s)
60      {
61          super(s);
62      }
63  
64      protected IDataSet createDataSet() throws Exception
65      {
66          return new FlatXmlDataSetBuilder().build(DATASET_FILE);
67      }
68  
69      protected IDataSet createDuplicateDataSet() throws Exception
70      {
71          return new FlatXmlDataSetBuilder().build(DUPLICATE_DATASET_FILE);
72      }
73  
74      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 
75      {
76          return new FlatXmlDataSetBuilder().build(DUPLICATE_DATASET_MULTIPLE_CASE_FILE);
77      }
78  
79      public void testMissingColumnAndEnableDtdMetadata() throws Exception
80      {
81          FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
82          builder.setDtdMetadata(true);
83          IDataSet dataSet = builder.build(FLAT_XML_TABLE);
84  
85          ITable table = dataSet.getTable("MISSING_VALUES");
86  
87          Column[] columns = table.getTableMetaData().getColumns();
88          assertEquals("column count", 3, columns.length);
89      }
90  
91      public void testMissingColumnAndDisableDtdMetadata() throws Exception
92      {
93          FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
94          builder.setDtdMetadata(false);
95          IDataSet dataSet = builder.build(FLAT_XML_TABLE);
96          
97          ITable table = dataSet.getTable("MISSING_VALUES");
98  
99          Column[] columns = table.getTableMetaData().getColumns();
100         assertEquals("column count", 2, columns.length);
101     }
102 
103     public void testMissingColumnAndDisableDtdMetadataEnableSensing() throws Exception
104     {
105         FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
106         builder.setDtdMetadata(false);
107         builder.setColumnSensing(true);
108         IDataSet dataSet = builder.build(FLAT_XML_TABLE);
109 
110         ITable table = dataSet.getTable("MISSING_VALUES_SENSING");
111 
112         Column[] columns = table.getTableMetaData().getColumns();
113         assertEquals("column count", 3, columns.length);
114         assertEquals("COLUMN0", columns[0].getColumnName());
115         assertEquals("COLUMN3", columns[1].getColumnName());
116         assertEquals("COLUMN1", columns[2].getColumnName());
117         assertEquals(3, table.getRowCount());
118         assertEquals("row 0 col 0", table.getValue(0, "COLUMN0"));
119         assertEquals("row 0 col 3", table.getValue(0, "COLUMN3"));
120         assertEquals("row 1 col 0", table.getValue(1, "COLUMN0"));
121         assertEquals("row 1 col 1", table.getValue(1, "COLUMN1"));
122         assertEquals("row 2 col 3", table.getValue(2, "COLUMN3"));
123     }
124 
125     
126     public void testWrite() throws Exception
127     {
128         IDataSet expectedDataSet = createDataSet();
129         File tempFile = File.createTempFile("flatXmlDataSetTest", ".xml");
130         try
131         {
132             Writer out = new FileWriter(tempFile);
133 
134             // write dataset in temp file
135             try
136             {
137                 FlatXmlDataSet.write(expectedDataSet, out);
138             }
139             finally
140             {
141                 out.close();
142             }
143 
144             // load new dataset from temp file
145             FileReader in = new FileReader(tempFile);
146             try
147             {
148                 IDataSet actualDataSet = new FlatXmlDataSetBuilder().build(in);
149 
150                 // verify table count
151                 assertEquals("table count", expectedDataSet.getTableNames().length,
152                         actualDataSet.getTableNames().length);
153 
154                 // verify each table
155                 ITable[] expected = DataSetUtils.getTables(expectedDataSet);
156                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
157                 assertEquals("table count", expected.length, actual.length);
158                 for (int i = 0; i < expected.length; i++)
159                 {
160                     String expectedName = expected[i].getTableMetaData().getTableName();
161                     String actualName = actual[i].getTableMetaData().getTableName();
162                     assertEquals("table name", expectedName, actualName);
163 
164                     assertTrue("not same instance", expected[i] != actual[i]);
165                     Assertion.assertEquals(expected[i], actual[i]);
166                 }
167             }
168             finally
169             {
170                 in.close();
171             }
172         }
173         finally
174         {
175             tempFile.delete();
176         }
177     }
178 
179     
180     public void testReadFlatXmlWithDifferentCaseInDtd()throws Exception
181     {
182         // The creation of such a dataset should work
183         IDataSet ds = new FlatXmlDataSetBuilder().build(FLAT_XML_DTD_DIFFERENT_CASE_FILE);
184         assertEquals(1, ds.getTableNames().length);
185         assertEquals("emp", ds.getTableNames()[0]);
186     }
187 
188     
189     public void testCreateMultipleCaseDuplicateDataSet_CaseSensitive() throws Exception
190     {
191         FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
192         builder.setDtdMetadata(false);
193         builder.setColumnSensing(false);
194         // Create a FlatXmlDataSet having caseSensitivity=true
195         builder.setCaseSensitiveTableNames(true);
196         IDataSet dataSet = builder.build(DUPLICATE_DATASET_MULTIPLE_CASE_FILE);
197 
198         ITable[] tables = dataSet.getTables();
199         assertEquals(3, tables.length);
200         assertEquals("DUPLICATE_TABLE", tables[0].getTableMetaData().getTableName());
201         assertEquals("EMPTY_TABLE", tables[1].getTableMetaData().getTableName());
202         assertEquals("duplicate_TABLE", tables[2].getTableMetaData().getTableName());
203     }
204     
205     /**
206      * Overridden from parent because FlatXml has different behaviour than other datasets.
207      * It allows the occurrence of the same table multiple times in arbitrary locations.
208      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateDuplicateDataSet()
209      */
210     //@Override
211     public void testCreateDuplicateDataSet() throws Exception
212     {
213             IDataSet dataSet = createDuplicateDataSet();
214             ITable[] tables = dataSet.getTables();
215             assertEquals(2, tables.length);
216             assertEquals("DUPLICATE_TABLE", tables[0].getTableMetaData().getTableName());
217             assertEquals(3, tables[0].getRowCount());
218             assertEquals("EMPTY_TABLE", tables[1].getTableMetaData().getTableName());
219             assertEquals(0, tables[1].getRowCount());
220     }
221 
222     /**
223      * Overridden from parent because FlatXml has different behaviour than other datasets.
224      * It allows the occurrence of the same table multiple times in arbitrary locations.
225      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateMultipleCaseDuplicateDataSet()
226      */
227     //@Override
228     public void testCreateMultipleCaseDuplicateDataSet() throws Exception
229     {
230         IDataSet dataSet = createMultipleCaseDuplicateDataSet();
231         ITable[] tables = dataSet.getTables();
232         assertEquals(2, tables.length);
233         assertEquals("DUPLICATE_TABLE", tables[0].getTableMetaData().getTableName());
234         assertEquals(3, tables[0].getRowCount());
235         assertEquals("EMPTY_TABLE", tables[1].getTableMetaData().getTableName());
236         assertEquals(0, tables[1].getRowCount());
237     }
238 
239     public void testCreateDuplicateDataSetWithVaryingColumnsAndColumnSensing() throws Exception
240     {
241         String xmlString = 
242             "<dataset>" +
243                 "<MISSING_VALUES_SENSING COLUMN0='row 0 col 0' COLUMN3='row 0 col 3'/>"+
244                 "<MISSING_VALUES         COLUMN0='row 1 col 0' COLUMN2='row 1 col 2'/>"+
245                 "<MISSING_VALUES_SENSING COLUMN0='row 1 col 0' COLUMN1='row 1 col 1'/>"+
246             "</dataset>";
247         
248         FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
249         builder.setDtdMetadata(false);
250         builder.setColumnSensing(true);
251         IDataSet dataSet = builder.build(new StringReader(xmlString));
252         ITable[] tables = dataSet.getTables();
253         assertEquals(2, tables.length);
254         
255         ITableMetaData meta1 = tables[0].getTableMetaData();
256         assertEquals("MISSING_VALUES_SENSING", meta1.getTableName());
257         assertEquals(3, meta1.getColumns().length);
258         assertEquals("COLUMN0", meta1.getColumns()[0].getColumnName());
259         assertEquals("COLUMN3", meta1.getColumns()[1].getColumnName());
260         assertEquals("COLUMN1", meta1.getColumns()[2].getColumnName());
261         assertEquals(2, tables[0].getRowCount());
262         assertEquals("row 0 col 0", tables[0].getValue(0, "COLUMN0"));
263         assertEquals("row 0 col 3", tables[0].getValue(0, "COLUMN3"));
264         assertEquals(null,          tables[0].getValue(0, "COLUMN1"));
265         assertEquals("row 1 col 0", tables[0].getValue(1, "COLUMN0"));
266         assertEquals(null,          tables[0].getValue(1, "COLUMN3"));
267         assertEquals("row 1 col 1", tables[0].getValue(1, "COLUMN1"));
268         
269         assertEquals("MISSING_VALUES", tables[1].getTableMetaData().getTableName());
270         assertEquals(1, tables[1].getRowCount());
271     }
272 
273     
274 }
275 
276 
277 
278 
279 
280 
281 
282