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.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.DataSetException;
35  import org.dbunit.dataset.DataSetUtils;
36  import org.dbunit.dataset.IDataSet;
37  import org.dbunit.dataset.ITable;
38  import org.dbunit.dataset.ITableMetaData;
39  import org.dbunit.testutil.TestUtils;
40  import org.junit.Test;
41  
42  /**
43   * @author Manuel Laflamme
44   * @author Last changed by: $Author$
45   * @version $Revision$ $Date$
46   * @since Feb 17, 2002
47   */
48  public class XmlDataSetTest extends AbstractDataSetTest
49  {
50      public XmlDataSetTest(String s)
51      {
52          super(s);
53      }
54  
55      @Override
56      protected IDataSet createDataSet() throws Exception
57      {
58          Reader in = new FileReader(TestUtils.getFile("xml/dataSetTest.xml"));
59          return new XmlDataSet(in);
60      }
61  
62      @Override
63      protected IDataSet createDuplicateDataSet() throws Exception
64      {
65          InputStream in = new FileInputStream(
66                  TestUtils.getFile("xml/xmlDataSetDuplicateTest.xml"));
67          return new XmlDataSet(in);
68      }
69  
70      @Override
71      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception
72      {
73          InputStream in = new FileInputStream(TestUtils
74                  .getFile("xml/xmlDataSetDuplicateMultipleCaseTest.xml"));
75          return new XmlDataSet(in);
76      }
77  
78      public void testWrite() throws Exception
79      {
80          IDataSet expectedDataSet = createDataSet();
81          File tempFile = File.createTempFile("dataSetTest", ".xml");
82          try
83          {
84              OutputStream out = new FileOutputStream(tempFile);
85  
86              try
87              {
88                  // write dataset in temp file
89                  XmlDataSet.write(expectedDataSet, out);
90  
91                  // load new dataset from temp file
92                  IDataSet actualDataSet =
93                          new XmlDataSet(new FileReader(tempFile));
94  
95                  // verify table count
96                  assertEquals("table count",
97                          expectedDataSet.getTableNames().length,
98                          actualDataSet.getTableNames().length);
99  
100                 // verify each table
101                 ITable[] expected = DataSetUtils.getTables(expectedDataSet);
102                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
103                 assertEquals("table count", expected.length, actual.length);
104                 for (int i = 0; i < expected.length; i++)
105                 {
106                     String expectedName =
107                             expected[i].getTableMetaData().getTableName();
108                     String actualName =
109                             actual[i].getTableMetaData().getTableName();
110                     assertEquals("table name", expectedName, actualName);
111 
112                     assertTrue("not same instance", expected[i] != actual[i]);
113                     Assertion.assertEquals(expected[i], actual[i]);
114                 }
115             } finally
116             {
117                 out.close();
118             }
119         } finally
120         {
121             tempFile.delete();
122         }
123     }
124 
125     /**
126      * Overridden from parent because XmlDataSet has different behaviour than
127      * other datasets. It allows the occurrence of the same table multiple times
128      * in arbitrary locations.
129      *
130      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateDuplicateDataSet()
131      */
132     // @Override
133     @Override
134     public void testCreateDuplicateDataSet() throws Exception
135     {
136         IDataSet dataSet = createDuplicateDataSet();
137         ITable[] tables = dataSet.getTables();
138         assertEquals(2, tables.length);
139         assertEquals("DUPLICATE_TABLE",
140                 tables[0].getTableMetaData().getTableName());
141         assertEquals(3, tables[0].getRowCount());
142         assertEquals("EMPTY_TABLE",
143                 tables[1].getTableMetaData().getTableName());
144         assertEquals(0, tables[1].getRowCount());
145     }
146 
147     /**
148      * Overridden from parent because XmlDataSet has different behaviour than
149      * other datasets. It allows the occurrence of the same table multiple times
150      * in arbitrary locations.
151      *
152      * @see org.dbunit.dataset.AbstractDataSetTest#testCreateMultipleCaseDuplicateDataSet()
153      */
154     // @Override
155     @Override
156     public void testCreateMultipleCaseDuplicateDataSet() throws Exception
157     {
158         IDataSet dataSet = createMultipleCaseDuplicateDataSet();
159         ITable[] tables = dataSet.getTables();
160         assertEquals(2, tables.length);
161         assertEquals("DUPLICATE_TABLE",
162                 tables[0].getTableMetaData().getTableName());
163         assertEquals(3, tables[0].getRowCount());
164         assertEquals("EMPTY_TABLE",
165                 tables[1].getTableMetaData().getTableName());
166         assertEquals(0, tables[1].getRowCount());
167     }
168 
169     @Test
170     public void testCreate_$InTableName_Success() throws DataSetException
171     {
172         final String fileName = "/xml/dataSet$Test.xml";
173         final String tableName = "TEST_TA$BLE";
174 
175         InputStream inputStream = getClass().getResourceAsStream(fileName);
176         XmlDataSet dataSet = new XmlDataSet(inputStream);
177 
178         ITable table = dataSet.getTable(tableName);
179 
180         // getTable() throws NoSuchTableException if not found
181         // so these checks currently unnecessary but future proof
182         assertNotNull("DataSet table is null.", table);
183 
184         ITableMetaData tableMetaData = table.getTableMetaData();
185         String actualTableName = tableMetaData.getTableName();
186         assertEquals("Expected table name not found.", tableName,
187                 actualTableName);
188     }
189 }