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.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.FileReader;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.io.Reader;
31  
32  import org.dbunit.Assertion;
33  import org.dbunit.dataset.AbstractDataSetTest;
34  import org.dbunit.dataset.DataSetUtils;
35  import org.dbunit.dataset.IDataSet;
36  import org.dbunit.dataset.ITable;
37  import org.dbunit.testutil.TestUtils;
38  
39  /**
40   * @author Manuel Laflamme
41   * @author Last changed by: $Author: jbhurst $
42   * @version $Revision: 1162 $ $Date: 2010-02-12 00:29:37 +0100 (ven, 12 feb 2010) $
43   * @since Feb 17, 2002
44   */
45  public class XmlDataSetTest extends AbstractDataSetTest
46  {
47      public XmlDataSetTest(String s)
48      {
49          super(s);
50      }
51  
52      protected IDataSet createDataSet() throws Exception
53      {
54          Reader in = new FileReader(
55                  TestUtils.getFile("xml/dataSetTest.xml"));
56          return new XmlDataSet(in);
57      }
58  
59      protected IDataSet createDuplicateDataSet() throws Exception
60      {
61          InputStream in = new FileInputStream(
62                  TestUtils.getFile("xml/xmlDataSetDuplicateTest.xml"));
63          return new XmlDataSet(in);
64      }
65  
66      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 
67      {
68          InputStream in = new FileInputStream(
69                  TestUtils.getFile("xml/xmlDataSetDuplicateMultipleCaseTest.xml"));
70          return new XmlDataSet(in);
71      }
72  
73      public void testWrite() throws Exception
74      {
75          IDataSet expectedDataSet = (XmlDataSet)createDataSet();
76          File tempFile = File.createTempFile("dataSetTest", ".xml");
77          try
78          {
79              OutputStream out = new FileOutputStream(tempFile);
80  
81              try
82              {
83                  // write dataset in temp file
84                  XmlDataSet.write(expectedDataSet, out);
85  
86                  // load new dataset from temp file
87                  IDataSet actualDataSet = new XmlDataSet(new FileReader(tempFile));
88  
89                  // verify table count
90                  assertEquals("table count", expectedDataSet.getTableNames().length,
91                          actualDataSet.getTableNames().length);
92  
93                  // verify each table
94                  ITable[] expected = DataSetUtils.getTables(expectedDataSet);
95                  ITable[] actual = DataSetUtils.getTables(actualDataSet);
96                  assertEquals("table count", expected.length, actual.length);
97                  for (int i = 0; i < expected.length; i++)
98                  {
99                      String expectedName = expected[i].getTableMetaData().getTableName();
100                     String actualName = actual[i].getTableMetaData().getTableName();
101                     assertEquals("table name", expectedName, actualName);
102 
103                     assertTrue("not same instance", expected[i] != actual[i]);
104                     Assertion.assertEquals(expected[i], actual[i]);
105                 }
106             }
107             finally
108             {
109                 out.close();
110             }
111         }
112         finally
113         {
114             tempFile.delete();
115         }
116     }
117 
118     
119     /**
120      * Overridden from parent because XmlDataSet has different behaviour than other datasets.
121      * It allows the occurrence of the same table multiple times in arbitrary locations.
122      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateDuplicateDataSet()
123      */
124     //@Override
125     public void testCreateDuplicateDataSet() throws Exception
126     {
127             IDataSet dataSet = createDuplicateDataSet();
128             ITable[] tables = dataSet.getTables();
129             assertEquals(2, tables.length);
130             assertEquals("DUPLICATE_TABLE", tables[0].getTableMetaData().getTableName());
131             assertEquals(3, tables[0].getRowCount());
132             assertEquals("EMPTY_TABLE", tables[1].getTableMetaData().getTableName());
133             assertEquals(0, tables[1].getRowCount());
134     }
135 
136     /**
137      * Overridden from parent because XmlDataSet has different behaviour than other datasets.
138      * It allows the occurrence of the same table multiple times in arbitrary locations.
139      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateMultipleCaseDuplicateDataSet()
140      */
141     //@Override
142     public void testCreateMultipleCaseDuplicateDataSet() throws Exception
143     {
144         IDataSet dataSet = createMultipleCaseDuplicateDataSet();
145         ITable[] tables = dataSet.getTables();
146         assertEquals(2, tables.length);
147         assertEquals("DUPLICATE_TABLE", tables[0].getTableMetaData().getTableName());
148         assertEquals(3, tables[0].getRowCount());
149         assertEquals("EMPTY_TABLE", tables[1].getTableMetaData().getTableName());
150         assertEquals(0, tables[1].getRowCount());
151     }
152 
153 }
154 
155 
156 
157