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.math.BigDecimal;
25  import java.util.TimeZone;
26  
27  import org.dbunit.dataset.AbstractTableTest;
28  import org.dbunit.dataset.Column;
29  import org.dbunit.dataset.IDataSet;
30  import org.dbunit.dataset.ITable;
31  import org.dbunit.dataset.datatype.DataType;
32  import org.dbunit.testutil.TestUtils;
33  
34  /**
35   * @author Manuel Laflamme
36   * @author Last changed by: $Author$
37   * @version $Revision$ $Date$
38   * @since Feb 21, 2003
39   */
40  public class XlsTableTest extends AbstractTableTest
41  {
42  //    private static final long ONE_SECOND_IN_MILLIS = 1000;
43  //    private static final long ONE_MINUTE_IN_MILLIS = 60 * 1000;
44  //    private static final long ONE_HOUR_IN_MILLIS = 60 * ONE_MINUTE_IN_MILLIS;
45  //    private static final long ONE_DAY_IN_MILLIS = 24 * ONE_HOUR_IN_MILLIS;
46      
47      
48      public XlsTableTest(String s)
49      {
50          super(s);
51      }
52  
53      protected ITable createTable() throws Exception
54      {
55          return createDataSet().getTable("TEST_TABLE");
56      }
57  
58      protected IDataSet createDataSet() throws Exception
59      {
60          return new XlsDataSet(TestUtils.getFile("xml/tableTest.xls"));
61      }
62  
63      public void testGetMissingValue() throws Exception
64      {
65          int row = 0;
66          Object[] expected = {"row 0 col 0", null, "row 0 col 2"};
67  
68          ITable table = createDataSet().getTable("MISSING_VALUES");
69  
70          Column[] columns = table.getTableMetaData().getColumns();
71          assertEquals("column count", expected.length, columns.length);
72          assertEquals("row count", 1, table.getRowCount());
73          for (int i = 0; i < columns.length; i++)
74          {
75              assertEquals("value " + i, expected[i],
76                      table.getValue(row, columns[i].getColumnName()));
77          }
78      }
79      
80      public void testEmptyTableColumns() throws Exception
81      {
82      	Column[] expectedColumns = new Column[] {
83      			new Column("COLUMN0", DataType.UNKNOWN),
84      			new Column("COLUMN1", DataType.UNKNOWN),
85      			new Column("COLUMN2", DataType.UNKNOWN),
86      			new Column("COLUMN3", DataType.UNKNOWN)
87      	};
88          ITable table = createDataSet().getTable("EMPTY_TABLE");
89  
90          Column[] columns = table.getTableMetaData().getColumns();
91      	assertEquals("Column count", expectedColumns.length, columns.length);
92      	for (int i = 0; i < columns.length; i++) {
93  			assertEquals("Column " + i, expectedColumns[i], columns[i]);
94  		}
95      }
96      
97      public void testEmptySheet() throws Exception
98      {
99          ITable table = createDataSet().getTable("EMPTY_SHEET");
100 
101         Column[] columns = table.getTableMetaData().getColumns();
102     	assertEquals("Column count", 0, columns.length);
103     }
104 
105     
106     public void testDifferentDatatypes() throws Exception
107     {
108         int row = 0;
109         ITable table = createDataSet().getTable("TABLE_DIFFERENT_DATATYPES");
110         
111         // When cell type is numeric and cell value is datetime,
112         // Apache-POI returns datetime with system default timezone offset.
113         // And java.util.Date#getTime() returns time without timezone offset (= UTC).
114         // So actual time values in this case will be UTC time in the system default timezone.
115         // Expected time values also should be UTC time in the system default timezone.
116         long tzOffset = TimeZone.getDefault().getRawOffset();
117         Object[] expected = {
118 //                new Date(0-tzOffset), 
119 //                new Date(0-tzOffset + (10*ONE_HOUR_IN_MILLIS + 45*ONE_MINUTE_IN_MILLIS)),
120 //                new Date(0-tzOffset + (13*ONE_HOUR_IN_MILLIS + 30*ONE_MINUTE_IN_MILLIS + 55*ONE_SECOND_IN_MILLIS) ),
121 //                new Long(25569),// Dates stored as Long numbers
122 //                new Long(25569447916666668L),
123 //                new Long(563136574074074L),
124                 new Long(0-tzOffset),// Dates stored as Long numbers
125                 new Long(38700000-tzOffset),
126                 new Long(-2209026545000L-tzOffset),
127                 new BigDecimal("10000.00"), 
128                 new BigDecimal("-200"), 
129                 new BigDecimal("12345.123456789000"),
130                 new Long(1233398764000L-tzOffset),
131                 new Long(1233332866000L) // The last column is a dbunit-date-formatted column in the excel sheet
132                 };
133 
134         Column[] columns = table.getTableMetaData().getColumns();
135         assertEquals("column count", expected.length, columns.length);
136         for (int i = 0; i < columns.length; i++)
137         {
138             Object actual = table.getValue(row, columns[i].getColumnName());
139             String typesResult = " expected=" + (expected[i]!=null ? expected[i].getClass().getName() : "null") + " - actual=" 
140                                     + (actual!=null ? actual.getClass().getName() : "null");
141             assertEquals("value " + i + " (" + typesResult + ")", expected[i], actual);
142         }
143     }
144 
145     public void testNumberAsText() throws Exception
146     {
147         int row = 0;
148         ITable table = createDataSet().getTable("TABLE_NUMBER_AS_TEXT");
149         
150         String[] expected = {
151         		"0",
152         		"666",
153         		"66.6",
154         		"66.6",
155         		"-6.66"
156                 };
157 
158         Column[] columns = table.getTableMetaData().getColumns();
159         assertEquals("column count", expected.length, columns.length);
160         for (int i = 0; i < columns.length; i++)
161         {
162         	String columnName = columns[i].getColumnName();
163             Object actual = table.getValue(row, columnName).toString();
164             assertEquals(columns[i].getColumnName(),expected[i],actual);
165         }
166     }
167 }