KickJava   Java API By Example, From Geeks To Geeks.

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


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.17 $
35  * @since Mar 13, 2002
36  */

37 public class FlatXmlDataSetTest extends AbstractDataSetTest
38 {
39     protected static final File JavaDoc DATASET_FILE =
40             new File JavaDoc("src/xml/flatXmlDataSetTest.xml");
41     protected static final File JavaDoc DUPLICATE_DATASET_FILE =
42             new File JavaDoc("src/xml/flatXmlDataSetDuplicateTest.xml");
43
44     public FlatXmlDataSetTest(String JavaDoc s)
45     {
46         super(s);
47     }
48
49     protected IDataSet createDataSet() throws Exception JavaDoc
50     {
51         return new FlatXmlDataSet(DATASET_FILE);
52     }
53
54     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
55     {
56         return new FlatXmlDataSet(
57                 DUPLICATE_DATASET_FILE);
58     }
59
60     public void testMissingColumnAndEnableDtdMetadata() throws Exception JavaDoc
61     {
62         IDataSet dataSet = new FlatXmlDataSet(
63                 new File JavaDoc("src/xml/flatXmlTableTest.xml"), true);
64
65         ITable table = dataSet.getTable("MISSING_VALUES");
66
67         Column[] columns = table.getTableMetaData().getColumns();
68         assertEquals("column count", 3, columns.length);
69     }
70
71     public void testMissingColumnAndDisableDtdMetadata() throws Exception JavaDoc
72     {
73         IDataSet dataSet = new FlatXmlDataSet(
74                 new File JavaDoc("src/xml/flatXmlTableTest.xml"), false);
75
76         ITable table = dataSet.getTable("MISSING_VALUES");
77
78         Column[] columns = table.getTableMetaData().getColumns();
79         assertEquals("column count", 2, columns.length);
80     }
81
82     public void testWrite() throws Exception JavaDoc
83     {
84         IDataSet expectedDataSet = createDataSet();
85         File JavaDoc tempFile = File.createTempFile("flatXmlDataSetTest", ".xml");
86         try
87         {
88             Writer JavaDoc out = new FileWriter JavaDoc(tempFile);
89
90             // write dataset in temp file
91
try
92             {
93                 FlatXmlDataSet.write(expectedDataSet, out);
94             }
95             finally
96             {
97                 out.close();
98             }
99
100             // load new dataset from temp file
101
FileReader JavaDoc in = new FileReader JavaDoc(tempFile);
102             try
103             {
104                 IDataSet actualDataSet = new FlatXmlDataSet(in);
105
106                 // verify table count
107
assertEquals("table count", expectedDataSet.getTableNames().length,
108                         actualDataSet.getTableNames().length);
109
110                 // verify each table
111
ITable[] expected = DataSetUtils.getTables(expectedDataSet);
112                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
113                 assertEquals("table count", expected.length, actual.length);
114                 for (int i = 0; i < expected.length; i++)
115                 {
116                     String JavaDoc expectedName = expected[i].getTableMetaData().getTableName();
117                     String JavaDoc actualName = actual[i].getTableMetaData().getTableName();
118                     assertEquals("table name", expectedName, actualName);
119
120                     assertTrue("not same instance", expected[i] != actual[i]);
121                     Assertion.assertEquals(expected[i], actual[i]);
122                 }
123             }
124             finally
125             {
126                 in.close();
127             }
128         }
129         finally
130         {
131             tempFile.delete();
132         }
133     }
134
135     public void testDuplicateWrite() throws Exception JavaDoc
136     {
137         IDataSet expectedDataSet = createDuplicateDataSet();
138         File JavaDoc tempFile = File.createTempFile("flatXmlDataSetDuplicateTest", ".xml");
139         try
140         {
141             Writer JavaDoc out = new FileWriter JavaDoc(tempFile);
142
143             // write dataset in temp file
144
try
145             {
146                 FlatXmlDataSet.write(expectedDataSet, out);
147             }
148             finally
149             {
150                 out.close();
151             }
152
153             // load new dataset from temp file
154
FileReader JavaDoc in = new FileReader JavaDoc(tempFile);
155             try
156             {
157                 IDataSet actualDataSet = new FlatXmlDataSet(in);
158
159                 // verify table count
160
assertEquals("table count", expectedDataSet.getTableNames().length,
161                         actualDataSet.getTableNames().length);
162
163                 // verify each table
164
ITable[] expected = DataSetUtils.getTables(expectedDataSet);
165                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
166                 assertEquals("table count", expected.length, actual.length);
167                 for (int i = 0; i < expected.length; i++)
168                 {
169                     String JavaDoc expectedName = expected[i].getTableMetaData().getTableName();
170                     String JavaDoc actualName = actual[i].getTableMetaData().getTableName();
171                     assertEquals("table name", expectedName, actualName);
172
173                     assertTrue("not same instance", expected[i] != actual[i]);
174                     Assertion.assertEquals(expected[i], actual[i]);
175                 }
176             }
177             finally
178             {
179                 in.close();
180             }
181         }
182         finally
183         {
184             tempFile.delete();
185         }
186     }
187
188 }
189
190
191
192
193
194
195
196
Popular Tags