View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import static org.junit.jupiter.api.Assertions.assertNotNull;
4   
5   import java.sql.Timestamp;
6   import java.time.LocalDateTime;
7   import java.time.format.DateTimeFormatter;
8   
9   import org.dbunit.DatabaseUnitException;
10  import org.dbunit.dataset.ITable;
11  import org.dbunit.dataset.datatype.DataType;
12  import org.dbunit.dataset.datatype.TypeCastException;
13  
14  /**
15   * Base class for {@link ValueComparer} implementations that verify Timestamps,
16   * ignoring the milliseconds.
17   *
18   * @author Jeff Jensen
19   * @since 3.1.0
20   */
21  public abstract class TimestampIgnoreMillisValueComparerBase
22          extends ValueComparerTemplateBase
23  {
24      private static final int ONE_SECOND_IN_MILLIS = 1000;
25  
26      @Override
27      protected boolean isExpected(final ITable expectedTable,
28              final ITable actualTable, final int rowNum, final String columnName,
29              final DataType dataType, final Object expectedValue,
30              final Object actualValue) throws DatabaseUnitException
31      {
32          final boolean isExpected;
33  
34          if (expectedValue == null || actualValue == null)
35          {
36              isExpected = isExpectedWithNull(expectedValue, actualValue);
37          } else
38          {
39              isExpected =
40                      isExpectedWithoutNull(dataType, expectedValue, actualValue);
41          }
42  
43          return isExpected;
44      }
45  
46      protected boolean isExpectedWithNull(final Object expectedValue,
47              final Object actualValue)
48      {
49          final boolean isExpected;
50  
51          if (expectedValue == null)
52          {
53              isExpected = actualValue == null;
54          } else
55          {
56              isExpected = expectedValue.equals(actualValue);
57          }
58  
59          return isExpected;
60      }
61  
62      protected boolean isExpectedWithoutNull(final DataType dataType,
63              final Object expectedValue, final Object actualValue)
64              throws TypeCastException
65      {
66          assertNotNull(expectedValue, "expectedValue is null.");
67          assertNotNull(actualValue, "actualValue is null.");
68  
69          long actualTime = convertValueToTimeInMillis(actualValue);
70          long expectedTime = convertValueToTimeInMillis(expectedValue);
71  
72          actualTime = truncateToSecond(actualTime);
73          expectedTime = truncateToSecond(expectedTime);
74  
75          return compareTimestamps(dataType, actualTime, expectedTime);
76      }
77  
78      private long truncateToSecond(final long timeInMilliseconds)
79      {
80          return ONE_SECOND_IN_MILLIS
81                  * (timeInMilliseconds / ONE_SECOND_IN_MILLIS);
82      }
83  
84      protected long convertValueToTimeInMillis(final Object timestampValue)
85      {
86          final Timestamp timestamp;
87          if (timestampValue instanceof Timestamp)
88          {
89              timestamp = (Timestamp) timestampValue;
90          } else if (timestampValue instanceof String)
91          {
92              final DateTimeFormatter dateFormat =
93                      DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
94              final LocalDateTime parsedDate =
95                      LocalDateTime.parse((String) timestampValue, dateFormat);
96              timestamp = Timestamp.valueOf(parsedDate);
97          } else
98          {
99              timestamp = null;
100         }
101 
102         final long time;
103         if (timestamp == null)
104         {
105             time = 0L;
106         } else
107         {
108             time = timestamp.getTime();
109         }
110         return time;
111     }
112 
113     abstract boolean compareTimestamps(final DataType dataType,
114             final long actualTime, final long expectedTime)
115             throws TypeCastException;
116 }