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