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.AbstractDataSetTest;
31  import org.dbunit.dataset.Column;
32  import org.dbunit.dataset.Columns;
33  import org.dbunit.dataset.DataSetUtils;
34  import org.dbunit.dataset.IDataSet;
35  import org.dbunit.dataset.ITable;
36  import org.dbunit.testutil.TestUtils;
37  
38  /**
39   * @author Manuel Laflamme
40   * @since Feb 22, 2003
41   * @version $Revision$
42   */
43  public class XlsDataSetTest extends AbstractDataSetTest
44  {
45      public XlsDataSetTest(String s)
46      {
47          super(s);
48      }
49  
50      protected IDataSet createDataSet() throws Exception
51      {
52          return new XlsDataSet(TestUtils.getFile("xml/dataSetTest.xls"));
53      }
54  
55      protected IDataSet createDuplicateDataSet() throws Exception
56      {
57          return new XlsDataSet(
58                  TestUtils.getFile("xml/dataSetDuplicateTest.xls"));
59      }
60  
61      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 
62      {
63          throw new UnsupportedOperationException("Excel does not support the same sheet name with different cases in one file");
64      }
65  
66      public void testCreateMultipleCaseDuplicateDataSet() throws Exception 
67      {
68          // Not supported
69      }
70  
71      public void testWrite() throws Exception
72      {
73          IDataSet expectedDataSet = createDataSet();
74          File tempFile = File.createTempFile("xlsDataSetTest", ".xls");
75          try
76          {
77              OutputStream out = new FileOutputStream(tempFile);
78  
79              // write dataset in temp file
80              try
81              {
82                  XlsDataSet.write(expectedDataSet, out);
83              }
84              finally
85              {
86                  out.close();
87              }
88  
89              // load new dataset from temp file
90              InputStream in = new FileInputStream(tempFile);
91              try
92              {
93                  IDataSet actualDataSet = new XlsDataSet(in);
94  
95                  // verify table count
96                  assertEquals("table count", expectedDataSet.getTableNames().length,
97                          actualDataSet.getTableNames().length);
98  
99                  // verify each table
100                 ITable[] expected = DataSetUtils.getTables(expectedDataSet);
101                 ITable[] actual = DataSetUtils.getTables(actualDataSet);
102                 assertEquals("table count", expected.length, actual.length);
103                 for (int i = 0; i < expected.length; i++)
104                 {
105                     String expectedName = expected[i].getTableMetaData().getTableName();
106                     String actualName = actual[i].getTableMetaData().getTableName();
107                     assertEquals("table name", expectedName, actualName);
108 
109                     assertTrue("not same instance", expected[i] != actual[i]);
110                     Assertion.assertEquals(expected[i], actual[i]);
111                 }
112             }
113             finally
114             {
115                 in.close();
116             }
117         }
118         finally
119         {
120             tempFile.delete();
121         }
122     }
123 
124     public void testColumnNameWithSpace() throws Exception
125     {
126     		IDataSet dataSet = new XlsDataSet(TestUtils.getFileInputStream("xml/contactor.xls"));
127     		ITable customerTable = dataSet.getTable("customer");
128             Column column = Columns.getColumn("name",  customerTable.getTableMetaData().getColumns());
129             assertNotNull(column);
130     }
131 
132 }