IsActualContainingExpectedStringValueComparer.java

  1. package org.dbunit.assertion.comparer.value;

  2. import org.dbunit.DatabaseUnitException;
  3. import org.dbunit.dataset.ITable;
  4. import org.dbunit.dataset.datatype.DataType;
  5. import org.dbunit.dataset.datatype.TypeCastException;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;

  8. /**
  9.  * {@link ValueComparer} implementation that verifies actual value contains
  10.  * expected value by converting to {@link String}s and using
  11.  * {@link String#contains(CharSequence)}. Special case: if both are null, they
  12.  * match.
  13.  *
  14.  * @author Jeff Jensen
  15.  * @since 2.7.0
  16.  */
  17. public class IsActualContainingExpectedStringValueComparer
  18.         extends ValueComparerTemplateBase
  19. {
  20.     private final Logger log = LoggerFactory.getLogger(getClass());

  21.     @Override
  22.     protected boolean isExpected(final ITable expectedTable,
  23.             final ITable actualTable, final int rowNum, final String columnName,
  24.             final DataType dataType, final Object expectedValue,
  25.             final Object actualValue) throws DatabaseUnitException
  26.     {
  27.         final boolean isExpected;

  28.         // handle nulls: prevent NPE and isExpected=true when both null
  29.         if (expectedValue == null && actualValue == null)
  30.         {
  31.             // both are null, so match
  32.             isExpected = true;
  33.         } else if (expectedValue == null || actualValue == null)
  34.         {
  35.             // both aren't null, one is null, so no match
  36.             isExpected = false;
  37.         } else
  38.         {
  39.             // neither are null, so compare
  40.             isExpected = isContaining(expectedValue, actualValue);
  41.         }

  42.         return isExpected;
  43.     }

  44.     protected boolean isContaining(final Object expectedValue,
  45.             final Object actualValue) throws TypeCastException
  46.     {
  47.         final String expectedValueString = DataType.asString(expectedValue);
  48.         final String actualValueString = DataType.asString(actualValue);
  49.         log.debug("isContaining: expectedValueString={}, actualValueString={}",
  50.                 expectedValueString, actualValueString);

  51.         return actualValueString.contains(expectedValueString);
  52.     }

  53.     @Override
  54.     protected String getFailPhrase()
  55.     {
  56.         return "not containing";
  57.     }
  58. }