Class RegularExpressionValueComparer

All Implemented Interfaces:
ValueComparer

public class RegularExpressionValueComparer extends ValueComparerTemplateBase
ValueComparer implementation that verifies the actual value matches the regular expression supplied as the expected value.

The expected value, converted to a String, is the regular expression; the actual value, also converted to a String, is the input tested against it. This mirrors how IsActualContainingExpectedStringValueComparer treats the expected value as the substring to look for.

The comparison succeeds only when the pattern matches the entire actual value, using Matcher.matches(), consistent with String.matches(String). To match only part of the actual value, make the pattern permissive at both ends, for example .*[0-9]{4}.*. A . does not match a line terminator unless the DOTALL flag is set, so prefix such a pattern with (?s) when the actual value may contain a newline, for example (?s).*[0-9]{4}.*.

Useful for columns whose exact content a test does not control but whose format it does, such as database-generated identifiers, UUID columns, or timestamps rendered into a text column.

Special case: if both values are null, they match; if exactly one is null, they do not.

This comparer adds no dependency beyond java.util.regex, so it is also available as ValueComparers.regularExpressionValueComparer.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • RegularExpressionValueComparer

      public RegularExpressionValueComparer()
  • Method Details

    • isExpected

      protected boolean isExpected(ITable expectedTable, ITable actualTable, int rowNum, String columnName, DataType dataType, Object expectedValue, Object actualValue) throws DatabaseUnitException
      Description copied from class: ValueComparerTemplateBase
      Determines whether the actual value compares as expected against the expected value.
      Specified by:
      isExpected in class ValueComparerTemplateBase
      Parameters:
      expectedTable - Table containing all expected results.
      actualTable - Table containing all actual results.
      rowNum - The current row number comparing.
      columnName - The name of the current column comparing.
      dataType - The DataType for the current column comparing.
      expectedValue - The current expected value for the column.
      actualValue - The current actual value for the column.
      Returns:
      true if comparing actual to expected is as expected.
      Throws:
      DatabaseUnitException - if the comparison cannot be performed.
    • isMatching

      protected boolean isMatching(int rowNum, String columnName, Object expectedValue, Object actualValue) throws DatabaseUnitException
      Returns whether the regular expression held in the expected value matches the whole actual value, both converted to strings.
      Parameters:
      rowNum - The current row number comparing, used only to identify an invalid pattern.
      columnName - The name of the current column comparing, used only to identify an invalid pattern.
      expectedValue - The expected value, holding the regular expression.
      actualValue - The actual value tested against the regular expression.
      Returns:
      true if the regular expression matches the entire actual value string.
      Throws:
      DatabaseUnitException - If either value cannot be converted to a string, or the expected value is not a valid regular expression.
    • getFailPhrase

      protected String getFailPhrase()
      Description copied from class: ValueComparerTemplateBase
      Returns the text snippet for substitution in ValueComparerBase.BASE_FAIL_MSG.
      Specified by:
      getFailPhrase in class ValueComparerTemplateBase
      Returns:
      The text snippet for substitution in ValueComparerBase.BASE_FAIL_MSG.