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 Mar 13, 2002
36   */
37  public class FlatXmlTableWriteTest extends FlatXmlTableTest
38  {
39      public FlatXmlTableWriteTest(String s)
40      {
41          super(s);
42      }
43  
44      protected IDataSet createDataSet() throws Exception
45      {
46          File tempFile = File.createTempFile("flatXmlTableWriteTest", ".xml");
47          Writer out = new FileWriter(tempFile);
48          try
49          {
50              // write DefaultTable in temp file
51              try
52              {
53                  FlatXmlDataSet.write(super.createDataSet(true), 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 FlatXmlDataSetBuilder().build(in);
65              }
66              finally
67              {
68                  in.close();
69              }
70          }
71          finally
72          {
73              tempFile.delete();
74          }
75      }
76  
77      public void testWriteMultipleTable() throws Exception
78      {
79          int tableCount = 5;
80          ITable sourceTable = super.createTable();
81  
82          ITable[] tables = new ITable[tableCount];
83          for (int i = 0; i < tables.length; i++)
84          {
85              ITableMetaData metaData = new DefaultTableMetaData("table" + i,
86                      sourceTable.getTableMetaData().getColumns());
87              tables[i] = new CompositeTable(metaData, sourceTable);
88          }
89  
90          IDataSet dataSet = new DefaultDataSet(tables);
91          File tempFile = File.createTempFile("flatXmlTableWriteTest", "xml");
92          Writer out = new FileWriter(tempFile);
93          try
94          {
95              // write DefaultTable in temp file
96              try
97              {
98                  FlatXmlDataSet.write(dataSet, out);
99              }
100             finally
101             {
102                 out.close();
103             }
104 
105             // load new dataset from temp file
106             FileReader in = new FileReader(tempFile);
107             try
108             {
109                 FlatXmlDataSet xmlDataSet2 = new FlatXmlDataSetBuilder().build(in);
110 
111                 // verify each table
112                 for (int i = 0; i < tables.length; i++)
113                 {
114                     ITable table = tables[i];
115                     Assertion.assertEquals(table, xmlDataSet2.getTable(xmlDataSet2.getTableNames()[i]));
116                 }
117             }
118             finally
119             {
120                 in.close();
121             }
122         }
123         finally
124         {
125             tempFile.delete();
126         }
127     }
128 
129 }
130 
131 
132 
133 
134 
135 
136