KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
25
26 import java.io.*;
27
28 /**
29  * @author Manuel Laflamme
30  * @since Feb 22, 2003
31  * @version $Revision: 1.4 $
32  */

33 public class XlsTableWriteTest extends XlsTableTest
34 {
35     public XlsTableWriteTest(String JavaDoc s)
36     {
37         super(s);
38     }
39
40     protected IDataSet createDataSet() throws Exception JavaDoc
41     {
42         File tempFile = File.createTempFile("tableWriteTest", ".xls");
43 // System.out.println(tempFile.getAbsoluteFile());
44
OutputStream out = new FileOutputStream(tempFile);
45         try
46         {
47             // write source dataset in temp file
48
try
49             {
50                 XlsDataSet.write(super.createDataSet(), out);
51             }
52             finally
53             {
54                 out.close();
55             }
56
57             // load new dataset from temp file
58
InputStream in = new FileInputStream(tempFile);
59             try
60             {
61                 return new XlsDataSet(in);
62             }
63             finally
64             {
65                 in.close();
66             }
67         }
68         finally
69         {
70             tempFile.delete();
71         }
72     }
73
74     public void testGetValue() throws Exception JavaDoc
75     {
76         super.testGetValue();
77     }
78
79
80     public void testWriteMultipleTable() throws Exception JavaDoc
81     {
82         int tableCount = 5;
83         ITable sourceTable = super.createTable();
84
85         ITable[] tables = new ITable[tableCount];
86         for (int i = 0; i < tables.length; i++)
87         {
88             ITableMetaData metaData = new DefaultTableMetaData("table" + i,
89                     sourceTable.getTableMetaData().getColumns());
90             tables[i] = new CompositeTable(metaData, sourceTable);
91         }
92
93         IDataSet dataSet = new DefaultDataSet(tables);
94         File tempFile = File.createTempFile("tableWriteTest", ".xls");
95         OutputStream out = new FileOutputStream(tempFile);
96         try
97         {
98             // write DefaultTable in temp file
99
try
100             {
101                 XlsDataSet.write(dataSet, out);
102             }
103             finally
104             {
105                 out.close();
106             }
107
108             // load new dataset from temp file
109
FileInputStream in = new FileInputStream(tempFile);
110             try
111             {
112                 XlsDataSet dataSet2 = new XlsDataSet(in);
113
114                 // verify each table
115
for (int i = 0; i < tables.length; i++)
116                 {
117                     ITable table = tables[i];
118                     Assertion.assertEquals(table,
119                             dataSet2.getTable(dataSet2.getTableNames()[i]));
120                 }
121             }
122             finally
123             {
124                 in.close();
125             }
126
127         }
128         finally
129         {
130             tempFile.delete();
131         }
132     }
133
134 }
135
Popular Tags