KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractDataSetTest;
26 import org.dbunit.dataset.DataSetUtils;
27 import org.dbunit.dataset.IDataSet;
28 import org.dbunit.dataset.ITable;
29
30 import java.io.*;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * @author Manuel Laflamme
36  * @version $Revision: 1.11 $
37  * @since Feb 17, 2002
38  */

39 public class XmlDataSetTest extends AbstractDataSetTest
40 {
41     public XmlDataSetTest(String JavaDoc s)
42     {
43         super(s);
44     }
45
46     protected IDataSet createDataSet() throws Exception JavaDoc
47     {
48         Reader in = new FileReader(
49                 new File("src/xml/dataSetTest.xml"));
50         return new XmlDataSet(in);
51     }
52
53     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
54     {
55         InputStream in = new FileInputStream(
56                 new File("src/xml/xmlDataSetDuplicateTest.xml"));
57         return new XmlDataSet(in);
58     }
59
60     public void testWrite() throws Exception JavaDoc
61     {
62         List JavaDoc tableList = new ArrayList JavaDoc();
63
64         IDataSet expectedDataSet = (XmlDataSet)createDataSet();
65         File tempFile = File.createTempFile("dataSetTest", ".xml");
66         try
67         {
68             OutputStream out = new FileOutputStream(tempFile);
69
70             try
71             {
72                 // write dataset in temp file
73
XmlDataSet.write(expectedDataSet, out);
74
75                 // load new dataset from temp file
76
IDataSet actualDataSet = new XmlDataSet(new FileReader(tempFile));
77
78                 // verify table count
79
assertEquals("table count", expectedDataSet.getTableNames().length,
80                         actualDataSet.getTableNames().length);
81
82                 // verify each table
83
ITable[] expected = DataSetUtils.getTables(expectedDataSet);
84                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
85                 assertEquals("table count", expected.length, actual.length);
86                 for (int i = 0; i < expected.length; i++)
87                 {
88                     String JavaDoc expectedName = expected[i].getTableMetaData().getTableName();
89                     String JavaDoc actualName = actual[i].getTableMetaData().getTableName();
90                     assertEquals("table name", expectedName, actualName);
91
92                     assertTrue("not same instance", expected[i] != actual[i]);
93                     Assertion.assertEquals(expected[i], actual[i]);
94                 }
95             }
96             finally
97             {
98                 out.close();
99             }
100         }
101         finally
102         {
103             tempFile.delete();
104         }
105     }
106
107     public void testDuplicateWrite() throws Exception JavaDoc
108     {
109         List JavaDoc tableList = new ArrayList JavaDoc();
110
111         IDataSet expectedDataSet = (XmlDataSet)createDuplicateDataSet();
112         File tempFile = File.createTempFile("xmlDataSetDuplicateTest", ".xml");
113         try
114         {
115             OutputStream out = new FileOutputStream(tempFile);
116
117             try
118             {
119                 // write dataset in temp file
120
XmlDataSet.write(expectedDataSet, out);
121
122                 // load new dataset from temp file
123
IDataSet actualDataSet = new XmlDataSet(new FileReader(tempFile));
124
125                 // verify table count
126
assertEquals("table count", expectedDataSet.getTableNames().length,
127                         actualDataSet.getTableNames().length);
128
129                 // verify each table
130
ITable[] expected = DataSetUtils.getTables(expectedDataSet);
131                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
132                 assertEquals("table count", expected.length, actual.length);
133                 for (int i = 0; i < expected.length; i++)
134                 {
135                     String JavaDoc expectedName = expected[i].getTableMetaData().getTableName();
136                     String JavaDoc actualName = actual[i].getTableMetaData().getTableName();
137                     assertEquals("table name", expectedName, actualName);
138
139                     assertTrue("not same instance", expected[i] != actual[i]);
140                     Assertion.assertEquals(expected[i], actual[i]);
141                 }
142             }
143             finally
144             {
145                 out.close();
146             }
147         }
148         finally
149         {
150             tempFile.delete();
151         }
152     }
153
154 }
155
156
157
158
159
Popular Tags