View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import org.dbunit.DatabaseUnitException;
4   import org.dbunit.dataset.ITable;
5   import org.dbunit.dataset.datatype.DataType;
6   import org.dbunit.dataset.datatype.TypeCastException;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  /**
11   * {@link ValueComparer} implementation that verifies actual value contains
12   * expected value by converting to {@link String}s and using
13   * {@link String#contains(CharSequence)}. Special case: if both are null, they
14   * match.
15   *
16   * @author Jeff Jensen
17   * @since 2.7.0
18   */
19  public class IsActualContainingExpectedStringValueComparer
20          extends ValueComparerTemplateBase
21  {
22      private final Logger log = LoggerFactory.getLogger(getClass());
23  
24      @Override
25      protected boolean isExpected(final ITable expectedTable,
26              final ITable actualTable, final int rowNum, final String columnName,
27              final DataType dataType, final Object expectedValue,
28              final Object actualValue) throws DatabaseUnitException
29      {
30          final boolean isExpected;
31  
32          // handle nulls: prevent NPE and isExpected=true when both null
33          if (expectedValue == null && actualValue == null)
34          {
35              // both are null, so match
36              isExpected = true;
37          } else if (expectedValue == null || actualValue == null)
38          {
39              // both aren't null, one is null, so no match
40              isExpected = false;
41          } else
42          {
43              // neither are null, so compare
44              isExpected = isContaining(expectedValue, actualValue);
45          }
46  
47          return isExpected;
48      }
49  
50      protected boolean isContaining(final Object expectedValue,
51              final Object actualValue) throws TypeCastException
52      {
53          final String expectedValueString = DataType.asString(expectedValue);
54          final String actualValueString = DataType.asString(actualValue);
55          log.debug("isContaining: expectedValueString={}, actualValueString={}",
56                  expectedValueString, actualValueString);
57  
58          return actualValueString.contains(expectedValueString);
59      }
60  
61      @Override
62      protected String getFailPhrase()
63      {
64          return "not containing";
65      }
66  }