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 org.dbunit.Assertion;
25  import org.dbunit.dataset.*;
26  
27  import java.io.File;
28  import java.io.FileReader;
29  import java.io.FileWriter;
30  import java.io.Writer;
31  
32  /**
33   * @author Manuel Laflamme
34   * @version $Revision$
35   * @since Feb 18, 2002
36   */
37  public class XmlTableWriteTest extends XmlTableTest
38  {
39      public XmlTableWriteTest(String s)
40      {
41          super(s);
42      }
43  
44      protected IDataSet createDataSet() throws Exception
45      {
46          File tempFile = File.createTempFile("xmlDataSetWriteTest", ".xml");
47          Writer out = new FileWriter(tempFile);
48          try
49          {
50              // write DefaultTable in temp file
51              try
52              {
53                  XmlDataSet.write(super.createDataSet(), out);
54              }
55              finally
56              {
57                  out.close();
58              }
59  
60              // load new dataset from temp file
61              FileReader in = new FileReader(tempFile);
62              try
63              {
64                  return new XmlDataSet(in);
65              }
66              finally
67              {
68                  in.close();
69              }
70          }
71          finally
72          {
73              tempFile.delete();
74          }
75  
76      }
77  
78      public void testWriteMultipleTable() throws Exception
79      {
80          int tableCount = 5;
81          ITable sourceTable = super.createTable();
82  
83          ITable[] tables = new ITable[tableCount];
84          for (int i = 0; i < tables.length; i++)
85          {
86              ITableMetaData metaData = new DefaultTableMetaData("table" + i,
87                      sourceTable.getTableMetaData().getColumns());
88              tables[i] = new CompositeTable(metaData, sourceTable);
89          }
90  
91          IDataSet dataSet = new DefaultDataSet(tables);
92          File tempFile = File.createTempFile("xmlDataSetWriteTest", "xml");
93          Writer out = new FileWriter(tempFile);
94          try
95          {
96              // write DefaultTable in temp file
97              try
98              {
99                  XmlDataSet.write(dataSet, out);
100             }
101             finally
102             {
103                 out.close();
104             }
105 
106             // load new dataset from temp file
107             FileReader in = new FileReader(tempFile);
108             try
109             {
110                 XmlDataSet xmlDataSet2 = new XmlDataSet(in);
111 
112                 // verify each table
113                 for (int i = 0; i < tables.length; i++)
114                 {
115                     ITable table = tables[i];
116                     Assertion.assertEquals(table, xmlDataSet2.getTable(xmlDataSet2.getTableNames()[i]));
117                 }
118             }
119             finally
120             {
121                 in.close();
122             }
123         }
124         finally
125         {
126             tempFile.delete();
127         }
128 
129     }
130 
131 }
132 
133 
134 
135