KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > excel > XlsDataSetTest


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 package org.dbunit.dataset.excel;
22
23 import org.dbunit.Assertion;
24 import org.dbunit.dataset.AbstractDataSetTest;
25 import org.dbunit.dataset.DataSetUtils;
26 import org.dbunit.dataset.IDataSet;
27 import org.dbunit.dataset.ITable;
28
29 import java.io.*;
30
31 /**
32  * @author Manuel Laflamme
33  * @since Feb 22, 2003
34  * @version $Revision: 1.4 $
35  */

36 public class XlsDataSetTest extends AbstractDataSetTest
37 {
38     public XlsDataSetTest(String JavaDoc s)
39     {
40         super(s);
41     }
42
43     protected IDataSet createDataSet() throws Exception JavaDoc
44     {
45         return new XlsDataSet(new File("src/xml/dataSetTest.xls"));
46     }
47
48     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
49     {
50         return new XlsDataSet(
51                 new File("src/xml/dataSetDuplicateTest.xls"));
52     }
53
54     public void testWrite() throws Exception JavaDoc
55     {
56         IDataSet expectedDataSet = createDataSet();
57         File tempFile = File.createTempFile("xlsDataSetTest", ".xls");
58         try
59         {
60             OutputStream out = new FileOutputStream(tempFile);
61
62             // write dataset in temp file
63
try
64             {
65                 XlsDataSet.write(expectedDataSet, out);
66             }
67             finally
68             {
69                 out.close();
70             }
71
72             // load new dataset from temp file
73
InputStream in = new FileInputStream(tempFile);
74             try
75             {
76                 IDataSet actualDataSet = new XlsDataSet(in);
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                 in.close();
99             }
100         }
101         finally
102         {
103             tempFile.delete();
104         }
105     }
106
107     public void testDuplicateWrite() throws Exception JavaDoc
108     {
109         IDataSet expectedDataSet = createDuplicateDataSet();
110         File tempFile = File.createTempFile("xlsDataSetDuplicateTest", ".xls");
111         try
112         {
113             OutputStream out = new FileOutputStream(tempFile);
114
115             // write dataset in temp file
116
try
117             {
118                 XlsDataSet.write(expectedDataSet, out);
119             }
120             finally
121             {
122                 out.close();
123             }
124
125             // load new dataset from temp file
126
InputStream in = new FileInputStream(tempFile);
127             try
128             {
129                 IDataSet actualDataSet = new XlsDataSet(in);
130
131                 // verify table count
132
assertEquals("table count", expectedDataSet.getTableNames().length,
133                         actualDataSet.getTableNames().length);
134
135                 // verify each table
136
ITable[] expected = DataSetUtils.getTables(expectedDataSet);
137                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
138                 assertEquals("table count", expected.length, actual.length);
139                 for (int i = 0; i < expected.length; i++)
140                 {
141                     String JavaDoc expectedName = expected[i].getTableMetaData().getTableName();
142                     String JavaDoc actualName = actual[i].getTableMetaData().getTableName();
143                     assertEquals("table name", expectedName, actualName);
144
145                     assertTrue("not same instance", expected[i] != actual[i]);
146                     Assertion.assertEquals(expected[i], actual[i]);
147                 }
148             }
149             finally
150             {
151                 in.close();
152             }
153         }
154         finally
155         {
156             tempFile.delete();
157         }
158     }
159
160 }
161
Popular Tags