KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > FlatXmlTableWriteTest


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 JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31
32 /**
33  * @author Manuel Laflamme
34  * @version $Revision: 1.12 $
35  * @since Mar 13, 2002
36  */

37 public class FlatXmlTableWriteTest extends FlatXmlTableTest
38 {
39     public FlatXmlTableWriteTest(String JavaDoc s)
40     {
41         super(s);
42     }
43
44     protected IDataSet createDataSet() throws Exception JavaDoc
45     {
46         File JavaDoc tempFile = File.createTempFile("flatXmlTableWriteTest", ".xml");
47         Writer JavaDoc out = new FileWriter JavaDoc(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 JavaDoc in = new FileReader JavaDoc(tempFile);
62             try
63             {
64                 return new FlatXmlDataSet(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 JavaDoc
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 JavaDoc tempFile = File.createTempFile("flatXmlTableWriteTest", "xml");
92         Writer JavaDoc out = new FileWriter JavaDoc(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 JavaDoc in = new FileReader JavaDoc(tempFile);
107             try
108             {
109                 FlatXmlDataSet xmlDataSet2 = new FlatXmlDataSet(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
137
Popular Tags