View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import java.sql.Timestamp;
4   
5   import org.dbunit.DatabaseUnitException;
6   import org.dbunit.dataset.ITable;
7   import org.dbunit.dataset.datatype.DataType;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  /**
12   * {@link ValueComparer} implementation for {@link Timestamp}s that verifies
13   * actual value is within a low and high milliseconds tolerance of expected
14   * value.
15   *
16   * @author jjensen
17   * @since 2.6.0
18   */
19  public class IsActualWithinToleranceOfExpectedTimestampValueComparer
20          extends ValueComparerTemplateBase
21  {
22      private final Logger log = LoggerFactory.getLogger(getClass());
23  
24      public static final long ONE_SECOND_IN_MILLIS = 1000;
25      public static final long TWO_SECONDS_IN_MILLIS = ONE_SECOND_IN_MILLIS * 2;
26      public static final long THREE_SECONDS_IN_MILLIS = ONE_SECOND_IN_MILLIS * 3;
27      public static final long FOUR_SECONDS_IN_MILLIS = ONE_SECOND_IN_MILLIS * 4;
28      public static final long FIVE_SECONDS_IN_MILLIS = ONE_SECOND_IN_MILLIS * 5;
29  
30      public static final long ONE_MINUTE_IN_MILLIS = ONE_SECOND_IN_MILLIS * 60;
31      public static final long TWO_MINUTES_IN_MILLIS = ONE_MINUTE_IN_MILLIS * 2;
32      public static final long THREE_MINUTES_IN_MILLIS = ONE_MINUTE_IN_MILLIS * 3;
33      public static final long FOUR_MINUTES_IN_MILLIS = ONE_MINUTE_IN_MILLIS * 4;
34      public static final long FIVE_MINUTES_IN_MILLIS = ONE_MINUTE_IN_MILLIS * 5;
35      public static final long TEN_MINUTES_IN_MILLIS = ONE_MINUTE_IN_MILLIS * 10;
36  
37      private long lowToleranceValueInMillis;
38      private long highToleranceValueInMillis;
39  
40      public IsActualWithinToleranceOfExpectedTimestampValueComparer(
41              final long lowToleranceValueInMillis,
42              final long highToleranceValueInMillis)
43      {
44          this.lowToleranceValueInMillis = lowToleranceValueInMillis;
45          this.highToleranceValueInMillis = highToleranceValueInMillis;
46      }
47  
48      @Override
49      protected boolean isExpected(final ITable expectedTable,
50              final ITable actualTable, final int rowNum, final String columnName,
51              final DataType dataType, final Object expectedValue,
52              final Object actualValue) throws DatabaseUnitException
53      {
54          final long actualTime = convertValueToTimeInMillis(actualValue);
55          final long expectedTime = convertValueToTimeInMillis(expectedValue);
56  
57          final long diffTime = calcTimeDifference(actualTime, expectedTime);
58          return isTolerant(diffTime);
59      }
60  
61      protected boolean isTolerant(final long diffTime)
62      {
63          final boolean isLowTolerant = diffTime >= lowToleranceValueInMillis;
64          final boolean isHighTolerant = diffTime <= highToleranceValueInMillis;
65  
66          log.debug(
67                  "isTolerant: diffTime={}, lowToleranceValueInMillis={},"
68                          + " highToleranceValueInMillis={}",
69                  diffTime, lowToleranceValueInMillis,
70                  highToleranceValueInMillis);
71  
72          return isLowTolerant && isHighTolerant;
73      }
74  
75      protected long convertValueToTimeInMillis(final Object timestampValue)
76      {
77          final Timestamp timestamp = (Timestamp) timestampValue;
78          return timestamp.getTime();
79      }
80  
81      protected long calcTimeDifference(final long actualTimeInMillis,
82              final long expectedTimeInMillis)
83      {
84          final long diffTime = actualTimeInMillis - expectedTimeInMillis;
85          final long diffTimeAbs = Math.abs(diffTime);
86          log.debug(
87                  "calcTimeDifference: "
88                          + "actualTimeInMillis={}, expectedTimeInMillis={},"
89                          + " diffInMillisTime={}, diffTimeInMillisAbs={}",
90                  actualTimeInMillis, expectedTimeInMillis, diffTime,
91                  diffTimeAbs);
92  
93          return diffTimeAbs;
94      }
95  
96      @Override
97      protected String getFailPhrase()
98      {
99          return "not within tolerance of " + lowToleranceValueInMillis + " - "
100                 + highToleranceValueInMillis + " milliseconds of";
101     }
102 }