View Javadoc
1   package org.dbunit.dataset.excel;
2   
3   import static org.hamcrest.CoreMatchers.equalTo;
4   import static org.junit.Assert.assertNotEquals;
5   
6   import java.io.File;
7   import java.net.URI;
8   import java.net.URL;
9   import java.text.DateFormat;
10  import java.text.SimpleDateFormat;
11  import java.util.TimeZone;
12  
13  import org.dbunit.dataset.DataSetException;
14  import org.dbunit.dataset.ITable;
15  import org.junit.Ignore;
16  import org.junit.Rule;
17  import org.junit.Test;
18  import org.junit.rules.ErrorCollector;
19  
20  public class XlsTableTimezoneOffsetTest
21  {
22      private static final String EXCEL_SPREADSHEET =
23              "/excel/XlsTableTimezoneOffset.xlsx";
24      private static final String TABLE_NAME = "xxx_tz_offset_test";
25      private static final String FAIL_MSG =
26              "row '%d' column '%s' doesn't match.";
27  
28      private static final DateFormat dateFormat =
29              new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
30  
31      @Rule
32      public ErrorCollector collector = new ErrorCollector();
33  
34      private ITable table;
35  
36      /**
37       * Simple test that loads the specified excel file and makes sure that the
38       * date is correct if the JVM's time zone is different from GMT/UTC This
39       * test only makes sense when the default time zone is NOT UTC (i.e. has an
40       * offset different from 0), otherwise the test would succeed even if the
41       * bug is still present. Thus, the precondition check makes sure that the
42       * default time zone offset is not 0 and aborts the test if it is.
43       *
44       * To change the time zone offset to something other than 0, either change
45       * your machine's system time zone setting, or use JVM parameter
46       * '-Duser.timezone="Europe/Berlin"' (or any other valid timezone ID)
47       *
48       * @throws Exception
49       */
50      @Ignore("Remove the following two lines in XlsTable for this test to pass")
51      // long tzOffset = TimeZone.getDefault().getOffset(date.getTime());
52      // date = new Date(date.getTime() + tzOffset);
53      @Test
54      public void testTimestampTzOffsets() throws Exception
55      {
56          // uncomment to see available timezones
57          // System.err.println(Arrays.toString(TimeZone.getAvailableIDs()));
58  
59          assertNotEquals(
60                  "Precondition failed: default time zone must not have offset 0!"
61                          + " Use JVM parameter '-Duser.timezone=\"Europe/Berlin\"' or some other value, if you have to...",
62                  0, TimeZone.getDefault().getRawOffset());
63  
64          URL spreadsheetUrl = getClass().getResource(EXCEL_SPREADSHEET);
65          URI spreadsheetUri = spreadsheetUrl.toURI();
66          File spreadsheetFile = new File(spreadsheetUri);
67          XlsDataSet xlsDataSet = new XlsDataSet(spreadsheetFile);
68  
69          table = xlsDataSet.getTable(TABLE_NAME);
70  
71          // The values returned by ITable.getValue() should match the content of
72          // the actual file, regardless of the JVM's default timezone
73  
74          checkStringValue(0, "id", "1");
75          checkDateValue(0, "ts", "2015-03-14 00:00:00");
76          checkStringValue(1, "id", "2");
77          checkDateValue(1, "ts", "2015-03-18 02:00:00");
78          checkStringValue(2, "id", "3");
79          checkDateValue(2, "ts", "2015-12-19 23:00:00");
80      }
81  
82      private void checkStringValue(int row, String column, String expected)
83              throws DataSetException
84      {
85          String failMsg = String.format(FAIL_MSG, row, column);
86          String value = getValueAsString(row, column);
87          collector.checkThat(failMsg, value, equalTo(expected));
88      }
89  
90      private void checkDateValue(int row, String column, String expected)
91              throws DataSetException
92      {
93          String failMsg = String.format(FAIL_MSG, row, column);
94          String value = getValueAsDate(row, column);
95          collector.checkThat(failMsg, value, equalTo(expected));
96      }
97  
98      private String getValueAsString(int row, String column)
99              throws DataSetException
100     {
101         Object value = table.getValue(row, column);
102         return value.toString();
103     }
104 
105     private String getValueAsDate(int row, String column)
106             throws DataSetException
107     {
108         return dateFormat.format(table.getValue(row, column));
109     }
110 }