Exemplo n.º 1
0
 /**
  * Assert that the given object is lenient equals by ignoring null fields value on other object.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  */
 public <A> void assertIsLenientEqualsToByIgnoringNullFields(
     AssertionInfo info, A actual, A other) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> fieldsNames = new LinkedList<String>();
   List<Object> values = new LinkedList<Object>();
   List<String> nullFields = new LinkedList<String>();
   for (Field field : actual.getClass().getDeclaredFields()) {
     try {
       Object otherFieldValue =
           propertySupport.propertyValue(field.getName(), field.getType(), other);
       if (otherFieldValue != null) {
         Object actualFieldValue =
             propertySupport.propertyValue(field.getName(), field.getType(), actual);
         if (!otherFieldValue.equals(actualFieldValue)) {
           fieldsNames.add(field.getName());
           values.add(otherFieldValue);
         }
       } else {
         nullFields.add(field.getName());
       }
     } catch (IntrospectionError e) {
       // Not readeable field, skip.
     }
   }
   if (fieldsNames.isEmpty()) return;
   throw failures.failure(
       info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, values, nullFields));
 }
Exemplo n.º 2
0
 /**
  * Asserts that the <em>actual</em> {@code File} represents an existing file.
  *
  * @param description the description of the <em>actual</em> {@code File}.
  * @param actual the <em>actual</em> {@code File}.
  * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
  * @throws AssertionError if the <em>actual</em> {@code File} does not represent an existing file.
  */
 public void assertIsFile(Description description, File actual) {
   assertNotNull(description, actual);
   if (!actual.isFile()) {
     String format = "expecting path:<%s> to represent an existing file";
     throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
   }
 }
Exemplo n.º 3
0
 /**
  * Asserts that the <em>actual</em> {@link File} represents a file or directory that does not
  * exist.
  *
  * @param description the description of the <em>actual</em> {@code File}.
  * @param actual the <em>actual</em> {@code File}.
  * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
  * @throws AssertionError if the <em>actual</em> {@link File} represents an existing file or
  *     directory.
  */
 public void assertDoesNotExist(Description description, File actual) {
   assertNotNull(description, actual);
   if (actual.exists()) {
     String format = "expecting resource in path:<%s> not to exist";
     throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
   }
 }
Exemplo n.º 4
0
 /**
  * Assert that the given object is lenient equals by ignoring fields.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @param fields the fields to ignore in comparison
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  */
 public <A> void assertIsLenientEqualsToByIgnoringFields(
     AssertionInfo info, A actual, A other, String... fields) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> fieldsNames = new LinkedList<String>();
   List<Object> expectedValues = new LinkedList<Object>();
   Set<String> ignoredFields = set(fields);
   for (Field field : actual.getClass().getDeclaredFields()) {
     try {
       if (!ignoredFields.contains(field.getName())) {
         String fieldName = field.getName();
         Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
         Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
         if (!org.fest.util.Objects.areEqual(actualFieldValue, otherFieldValue)) {
           fieldsNames.add(fieldName);
           expectedValues.add(otherFieldValue);
         }
       }
     } catch (IntrospectionError e) {
       // Not readeable field, skip.
     }
   }
   if (fieldsNames.isEmpty()) return;
   throw failures.failure(
       info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, expectedValues, list(fields)));
 }
Exemplo n.º 5
0
 /**
  * Verifies that the actual value is not exactly a instance of given type.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param type the type to check the actual value against.
  * @throws AssertionError if the actual is exactly a instance of given type.
  * @throws NullPointerException if the actual value is null.
  * @throws NullPointerException if the given object is null.
  */
 public void assertIsNotExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
   assertNotNull(info, actual);
   if (type == null) throw new NullPointerException("The given type should not be null");
   Class<?> current = actual.getClass();
   if (!type.equals(current)) return;
   throw failures.failure(info, shouldNotBeExactlyInstance(actual, type));
 }
Exemplo n.º 6
0
 /**
  * Asserts that two <code>{@link Comparable}</code>s are not equal by invoking <code>
  * {@link Comparable#compareTo(Object)}</code> .<br>
  * Note that it does not rely on the custom {@link #comparisonStrategy} if one has been set.
  *
  * @param <T> used to guarantee that two objects of the same type are being compared against each
  *     other.
  * @param info contains information about the assertion.
  * @param actual the actual value.
  * @param other the value to compare the actual value to.
  * @throws AssertionError if the actual value is {@code null}.
  * @throws AssertionError if the actual value is equal to the other one.
  */
 public <T extends Comparable<? super T>> void assertNotEqualByComparison(
     AssertionInfo info, T actual, T other) {
   assertNotNull(info, actual);
   // we don't delagate to comparisonStrategy, as this assertion makes it clear it relies on
   // Comparable
   if (actual.compareTo(other) != 0) return;
   throw failures.failure(info, shouldNotBeEqual(actual, other));
 }
Exemplo n.º 7
0
 /**
  * Verifies that the actual value does not have the same class as the given object.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to check type against.
  * @throws AssertionError if the actual has the same type has the given object.
  * @throws NullPointerException if the actual value is null.
  * @throws NullPointerException if the given object is null.
  */
 public void assertDoesNotHaveSameClassAs(AssertionInfo info, Object actual, Object other) {
   assertNotNull(info, actual);
   if (other == null) throw new NullPointerException("The given object should not be null");
   Class<?> actualClass = actual.getClass();
   Class<?> otherClass = other.getClass();
   if (!actualClass.equals(otherClass)) return;
   throw failures.failure(info, shouldNotHaveSameClass(actual, other));
 }
Exemplo n.º 8
0
 public void enterOrbitOps() {
   if (internalState == cevStatesEarthOrbit) {
     if (internalEarthOrbitState == earthOrbitStatesSafeHold) {
       if (failures.noEARTH_SENSORfailure()) {
         internalEarthOrbitState = earthOrbitStatesOrbitOps;
       }
       return;
     }
   }
   assert false;
 }
Exemplo n.º 9
0
 /**
  * Verifies that the given object is not an instance of any of the given types.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param types the types to check the given object against.
  * @throws NullPointerException if the given array is {@code null}.
  * @throws IllegalArgumentException if the given array is empty.
  * @throws NullPointerException if the given array has {@code null} elements.
  * @throws AssertionError if the given object is {@code null}.
  * @throws AssertionError if the given object is an instance of any of the given types.
  */
 public void assertIsNotInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types) {
   checkIsNotNullAndIsNotEmpty(types);
   assertNotNull(info, actual);
   boolean found = false;
   for (Class<?> type : types) {
     if (type == null) {
       String format = "The given array of types:<%s> should not have null elements";
       throw new NullPointerException(String.format(format, toStringOf(types)));
     }
     if (type.isInstance(actual)) {
       found = true;
       break;
     }
   }
   if (!found) return;
   throw failures.failure(info, shouldNotBeInstanceOfAny(actual, types));
 }
Exemplo n.º 10
0
 /**
  * Assert that the given object is lenient equals to other object by comparing given fields value
  * only.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @param fields accepted fields
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  * @throws IntrospectionError if a field does not exist in actual.
  */
 public <A> void assertIsLenientEqualsToByAcceptingFields(
     AssertionInfo info, A actual, A other, String... fields) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> rejectedFieldsNames = new LinkedList<String>();
   List<Object> expectedValues = new LinkedList<Object>();
   for (String fieldName : fields) {
     Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
     Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
     if (!(actualFieldValue == otherFieldValue
         || (actualFieldValue != null && actualFieldValue.equals(otherFieldValue)))) {
       rejectedFieldsNames.add(fieldName);
       expectedValues.add(otherFieldValue);
     }
   }
   if (rejectedFieldsNames.isEmpty()) return;
   throw failures.failure(
       info,
       shouldBeLenientEqualByAccepting(actual, rejectedFieldsNames, expectedValues, list(fields)));
 }
Exemplo n.º 11
0
 /**
  * Asserts that the given {@code File}s represent existing files with equal content. It is assumed
  * that the content of the files is text.
  *
  * @param description the description of the <em>actual</em> {@code File}.
  * @param actual the <em>actual</em> {@code File}.
  * @param expected the <em>expected</em> {@code File}.
  * @param charset the charset to use when reading the contents of the given {@code File}s.
  * @throws NullPointerException if the <em>expected</em> {@code File} is {@code null}.
  * @throws IllegalArgumentException if the <em>expected</em> {@code File} does not represent an
  *     existing file.
  * @throws NullPointerException if the given charset is {@code null}.
  * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
  * @throws AssertionError if the <em>actual</em> {@code File} does not represent an existing file.
  * @throws IORuntimeException if an I/O error occurs.
  * @throws AssertionError if the given files do not have equal content.
  */
 public void assertEqualContent(
     Description description, File actual, File expected, Charset charset) {
   checkIsFile(expected);
   checkNotNull(charset);
   assertIsFile(description, actual);
   try {
     String lineSeparator = System.getProperty("line.separator");
     FileDiffs diffs = comparator.compareContents(actual, expected, charset);
     if (!diffs.isEmpty()) {
       String format =
           "files expected:<%s> and actual:<%s> do not have equal content:"
               + lineSeparator
               + "%s"
               + lineSeparator
               + "using charset:<%s>";
       throw failures.failure(
           description, new BasicErrorMessageFactory(format, expected, actual, diffs, charset));
     }
   } catch (IOException e) {
     String format = "Failed to compare contents of files:<%s> and:<%s> using charset:<%s>";
     throw new IORuntimeException(String.format(format, actual, expected, charset), e);
   }
 }
Exemplo n.º 12
0
 /**
  * Asserts that the given object is not present in the given collection.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param values the given collection.
  * @throws NullPointerException if the given iterable is {@code null}.
  * @throws IllegalArgumentException if the given collection is empty.
  * @throws AssertionError if the given object is present in the given collection.
  */
 public <A> void assertIsNotIn(AssertionInfo info, A actual, Iterable<? extends A> values) {
   checkIsNotNullAndNotEmpty(values);
   assertNotNull(info, actual);
   if (!isActualIn(actual, values)) return;
   throw failures.failure(info, shouldNotBeIn(actual, values, comparisonStrategy));
 }
Exemplo n.º 13
0
 /**
  * Asserts that the given object is not present in the given array.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param values the given array.
  * @throws NullPointerException if the given array is {@code null}.
  * @throws IllegalArgumentException if the given array is empty.
  * @throws AssertionError if the given object is present in the given array.
  */
 public void assertIsNotIn(AssertionInfo info, Object actual, Object[] values) {
   checkIsNotNullAndNotEmpty(values);
   assertNotNull(info, actual);
   if (!isItemInArray(actual, values)) return;
   throw failures.failure(info, shouldNotBeIn(actual, values, comparisonStrategy));
 }
Exemplo n.º 14
0
 /**
  * Asserts that two objects do not refer to the same object.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @throws AssertionError if the given objects refer to the same object.
  */
 public void assertNotSame(AssertionInfo info, Object actual, Object other) {
   if (actual != other) return;
   throw failures.failure(info, shouldNotBeSame(actual));
 }
Exemplo n.º 15
0
 /**
  * Asserts that two objects refer to the same object.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param expected the expected object.
  * @throws AssertionError if the given objects do not refer to the same object.
  */
 public void assertSame(AssertionInfo info, Object actual, Object expected) {
   if (actual == expected) return;
   throw failures.failure(info, shouldBeSame(actual, expected));
 }
Exemplo n.º 16
0
 /**
  * Asserts that the given object is not {@code null}.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @throws AssertionError if the given object is {@code null}.
  */
 public void assertNotNull(AssertionInfo info, Object actual) {
   if (actual != null) return;
   throw failures.failure(info, shouldNotBeNull());
 }
Exemplo n.º 17
0
 /**
  * Asserts that two T instances are equal.
  *
  * @param info contains information about the assertion.
  * @param actual the actual value.
  * @param expected the expected value.
  * @throws AssertionError if the actual value is {@code null}.
  * @throws AssertionError if the actual value is not equal to the expected one. This method will
  *     throw a {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the
  *     expected and actual values are not equal.
  */
 public <T> void assertEqual(AssertionInfo info, T actual, T expected) {
   assertNotNull(info, actual);
   if (areEqual(actual, expected)) return;
   throw failures.failure(info, shouldBeEqual(actual, expected, comparisonStrategy));
 }
Exemplo n.º 18
0
/**
 * Reusable assertions for {@link File}s.
 *
 * @author David DIDIER
 * @author Yvonne Wang
 * @author Alex Ruiz
 * @author Olivier Demeijer
 * @author Olivier Michallat
 */
public class Files {
  private static final Files INSTANCE = new Files();

  public static Files instance() {
    return INSTANCE;
  }

  @VisibleForTesting FileContentComparator comparator = new FileContentComparator();

  @VisibleForTesting Failures failures = Failures.instance();

  @VisibleForTesting
  Files() {}

  /**
   * Asserts that the <em>actual</em> {@link File} represents an existing file or directory.
   *
   * @param description the description of the <em>actual</em> {@code File}.
   * @param actual the <em>actual</em> {@code File}.
   * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@link File} represents a file or directory that
   *     does not exist.
   */
  public void assertExists(Description description, File actual) {
    assertNotNull(description, actual);
    if (!actual.exists()) {
      String format = "expecting resource in path:<%s> to exist";
      throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
    }
  }

  /**
   * Asserts that the <em>actual</em> {@link File} represents a file or directory that does not
   * exist.
   *
   * @param description the description of the <em>actual</em> {@code File}.
   * @param actual the <em>actual</em> {@code File}.
   * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@link File} represents an existing file or
   *     directory.
   */
  public void assertDoesNotExist(Description description, File actual) {
    assertNotNull(description, actual);
    if (actual.exists()) {
      String format = "expecting resource in path:<%s> not to exist";
      throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
    }
  }

  /**
   * Asserts that the <em>actual</em> {@code File} represents an existing file.
   *
   * @param description the description of the <em>actual</em> {@code File}.
   * @param actual the <em>actual</em> {@code File}.
   * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@code File} does not represent an existing file.
   */
  public void assertIsFile(Description description, File actual) {
    assertNotNull(description, actual);
    if (!actual.isFile()) {
      String format = "expecting path:<%s> to represent an existing file";
      throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
    }
  }

  /**
   * Asserts that the <em>actual</em> {@code File} represents an existing directory.
   *
   * @param description the description of the <em>actual</em> {@code File}.
   * @param actual the <em>actual</em> {@code File}.
   * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@code File} does not represent an existing
   *     directory.
   */
  public void assertIsDirectory(Description description, File actual) {
    assertNotNull(description, actual);
    if (!actual.isDirectory()) {
      String format = "expecting path:<%s> to represent an existing directory";
      throw failures.failure(description, new BasicErrorMessageFactory(format, actual));
    }
  }

  /**
   * Asserts that the given {@code File}s represent existing files with equal content. It is assumed
   * that the content of the files is text.
   *
   * @param description the description of the <em>actual</em> {@code File}.
   * @param actual the <em>actual</em> {@code File}.
   * @param expected the <em>expected</em> {@code File}.
   * @param charset the charset to use when reading the contents of the given {@code File}s.
   * @throws NullPointerException if the <em>expected</em> {@code File} is {@code null}.
   * @throws IllegalArgumentException if the <em>expected</em> {@code File} does not represent an
   *     existing file.
   * @throws NullPointerException if the given charset is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@code File} is {@code null}.
   * @throws AssertionError if the <em>actual</em> {@code File} does not represent an existing file.
   * @throws IORuntimeException if an I/O error occurs.
   * @throws AssertionError if the given files do not have equal content.
   */
  public void assertEqualContent(
      Description description, File actual, File expected, Charset charset) {
    checkIsFile(expected);
    checkNotNull(charset);
    assertIsFile(description, actual);
    try {
      String lineSeparator = System.getProperty("line.separator");
      FileDiffs diffs = comparator.compareContents(actual, expected, charset);
      if (!diffs.isEmpty()) {
        String format =
            "files expected:<%s> and actual:<%s> do not have equal content:"
                + lineSeparator
                + "%s"
                + lineSeparator
                + "using charset:<%s>";
        throw failures.failure(
            description, new BasicErrorMessageFactory(format, expected, actual, diffs, charset));
      }
    } catch (IOException e) {
      String format = "Failed to compare contents of files:<%s> and:<%s> using charset:<%s>";
      throw new IORuntimeException(String.format(format, actual, expected, charset), e);
    }
  }

  private void checkIsFile(File expected) {
    checkNotNull(expected);
    if (!expected.isFile()) {
      throw new IllegalArgumentException();
    }
  }

  private static void assertNotNull(Description description, File actual) {
    Objects.instance().assertNotNull(description, actual);
  }
}
Exemplo n.º 19
0
 /**
  * Asserts that two objects are equal.
  *
  * @param info contains information about the assertion.
  * @param actual the "actual" object.
  * @param expected the "expected" object.
  * @throws AssertionError if {@code actual} is not equal to {@code expected}. This method will
  *     throw a {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the
  *     given objects are not equal.
  */
 public void assertEqual(AssertionInfo info, Object actual, Object expected) {
   if (areEqual(actual, expected)) return;
   throw failures.failure(info, shouldBeEqual(actual, expected, comparisonStrategy));
 }
Exemplo n.º 20
0
 public void completion() {
   if (internalState == cevStatesAscent) {
     if (internalAscentState == ascentStatesHoldLaunch) {
       internalState = cevStatesEndState;
       return;
     }
     if (internalAscentState == ascentStatesPadAbort) {
       internalState = cevStatesEndState;
       return;
     }
     if (internalAscentState == ascentStatesAbortPassiveLAS) {
       spacecraft.doLowPassiveAbort();
       internalState = cevStatesEntry;
       internalEntryState = entryStatesChuteSequence;
       return;
     }
     if (internalAscentState == ascentStatesAbortLowActiveLAS) {
       assert failures.noLAS_CNTRLfailure() : errors.log("active LAS with failed control motor");
       spacecraft.doLowActiveAbort();
       internalState = cevStatesEntry;
       internalEntryState = entryStatesChuteSequence;
       return;
     }
     if (internalAscentState == ascentStatesAbortHighActiveLAS) {
       assert failures.noLAS_CNTRLfailure() : errors.log("active LAS with failed control motor");
       internalState = cevStatesEntry;
       internalEntryState = entryStatesChuteSequence;
       return;
     }
   }
   if (internalState == cevStatesEarthOrbit) {
     if (internalEarthOrbitState == earthOrbitStatesInsertion) {
       if (failures.noEARTH_SENSORfailure()) {
         internalEarthOrbitState = earthOrbitStatesOrbitOps;
       } else {
         internalEarthOrbitState = earthOrbitStatesSafeHold;
         // <ERR> never reached
       }
       return;
     }
   }
   if (internalState == cevStatesLunarOps) {
     if (internalLunarOpsState == lunarOpsStatesInsertion) {
       internalLunarOpsState = lunarOpsStatesLunarOrbit;
       return;
     }
     if (internalLunarOpsState == lunarOpsStatesLunarLanding) {
       internalLunarOpsState = lunarOpsStatesLunarOrbit;
     }
   }
   if (internalState == cevStatesEntry) {
     if (internalEntryState == entryStatesEntryInterface) {
       internalEntryState = entryStatesNominalEntry;
       return;
     }
     if (internalEntryState == entryStatesNominalEntry) {
       internalEntryState = entryStatesChuteSequence;
       return;
     }
     if (internalEntryState == entryStatesChuteSequence) {
       assert spacecraft.readyForChuteSequence() : errors.last();
       internalEntryState = entryStatesLanding;
       return;
     }
     if (internalEntryState == entryStatesLanding) {
       internalState = cevStatesEndState;
       return;
     }
     if (internalEntryState == entryStatesAbortEntryBallistic) {
       internalEntryState = entryStatesChuteSequence;
       return;
     }
     if (internalEntryState == entryStatesAbortEntryFixedBank) {
       internalEntryState = entryStatesChuteSequence;
       return;
     }
     if (internalState == cevStatesLunarOps) {
       if (internalLunarOpsState == lunarOpsStatesLunarLanding) {
         if (internalLunarLandingLSAMState == lunarLandingLSAMStatesLunarDescent) {
           internalLunarLandingLSAMState = lunarLandingLSAMStatesSurfaceOps;
           return;
         }
       }
     }
   }
   assert false;
 }
Exemplo n.º 21
0
 /**
  * Asserts that the actual value is greater than or equal to the other one.
  *
  * @param <T> used to guarantee that two objects of the same type are being compared against each
  *     other.
  * @param info contains information about the assertion.
  * @param actual the actual value.
  * @param other the value to compare the actual value to.
  * @throws AssertionError if the actual value is {@code null}.
  * @throws AssertionError if the actual value is less than the other one.
  */
 public <T extends Comparable<? super T>> void assertGreaterThanOrEqualTo(
     AssertionInfo info, T actual, T other) {
   assertNotNull(info, actual);
   if (!isLessThan(actual, other)) return;
   throw failures.failure(info, shouldBeGreaterOrEqual(actual, other, comparisonStrategy));
 }
Exemplo n.º 22
0
 /**
  * Verifies that the given object is not an instance of the given type.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param type the type to check the given object against.
  * @throws NullPointerException if the given type is {@code null}.
  * @throws AssertionError if the given object is {@code null}.
  * @throws AssertionError if the given object is an instance of the given type.
  */
 public void assertIsNotInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
   if (type == null) throw new NullPointerException("The given type should not be null");
   assertNotNull(info, actual);
   if (!type.isInstance(actual)) return;
   throw failures.failure(info, shouldNotBeInstance(actual, type));
 }
Exemplo n.º 23
0
 /**
  * Asserts that two T instances are not equal.
  *
  * @param info contains information about the assertion.
  * @param actual the actual value.
  * @param other the value to compare the actual value to.
  * @throws AssertionError if the actual value is {@code null}.
  * @throws AssertionError if the actual value is equal to the other one.
  */
 public <T> void assertNotEqual(AssertionInfo info, T actual, T other) {
   assertNotNull(info, actual);
   if (!areEqual(actual, other)) return;
   throw failures.failure(info, shouldNotBeEqual(actual, other, comparisonStrategy));
 }
Exemplo n.º 24
0
 /**
  * Asserts that two objects are not equal.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @throws AssertionError if {@code actual} is equal to {@code other}.
  */
 public void assertNotEqual(AssertionInfo info, Object actual, Object other) {
   if (!areEqual(actual, other)) return;
   throw failures.failure(info, shouldNotBeEqual(actual, other, comparisonStrategy));
 }
Exemplo n.º 25
0
 /**
  * Asserts that the given object is {@code null}.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @throws AssertionError if the given object is not {@code null}.
  */
 public void assertNull(AssertionInfo info, Object actual) {
   if (actual == null) return;
   throw failures.failure(info, shouldBeEqual(actual, null, comparisonStrategy));
 }
Exemplo n.º 26
0
/**
 * Reusable assertions for {@code Object}s.
 *
 * @author Yvonne Wang
 * @author Alex Ruiz
 * @author Nicolas François
 * @author Mikhail Mazursky
 */
public class Objects {

  private static final Objects INSTANCE = new Objects();

  /**
   * Returns the singleton instance of this class based on {@link StandardComparisonStrategy}.
   *
   * @return the singleton instance of this class based on {@link StandardComparisonStrategy}.
   */
  public static Objects instance() {
    return INSTANCE;
  }

  @VisibleForTesting Failures failures = Failures.instance();

  @VisibleForTesting PropertySupport propertySupport = PropertySupport.instance();

  private final ComparisonStrategy comparisonStrategy;

  @VisibleForTesting
  Objects() {
    this(StandardComparisonStrategy.instance());
  }

  public Objects(ComparisonStrategy comparisonStrategy) {
    this.comparisonStrategy = comparisonStrategy;
  }

  @VisibleForTesting
  public Comparator<?> getComparator() {
    if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) {
      return ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator();
    }
    return null;
  }

  /**
   * Verifies that the given object is an instance of the given type.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param type the type to check the given object against.
   * @throws NullPointerException if the given type is {@code null}.
   * @throws AssertionError if the given object is {@code null}.
   * @throws AssertionError if the given object is not an instance of the given type.
   */
  public void assertIsInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
    if (type == null) throw new NullPointerException("The given type should not be null");
    assertNotNull(info, actual);
    if (type.isInstance(actual)) return;
    throw failures.failure(info, shouldBeInstance(actual, type));
  }

  /**
   * Verifies that the given object is an instance of any of the given types.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param types the types to check the given object against.
   * @throws NullPointerException if the given array is {@code null}.
   * @throws IllegalArgumentException if the given array is empty.
   * @throws NullPointerException if the given array has {@code null} elements.
   * @throws AssertionError if the given object is {@code null}.
   * @throws AssertionError if the given object is not an instance of any of the given types.
   */
  public void assertIsInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types) {
    checkIsNotNullAndIsNotEmpty(types);
    assertNotNull(info, actual);
    boolean found = false;
    for (Class<?> type : types) {
      if (type == null) {
        String format = "The given array of types:<%s> should not have null elements";
        throw new NullPointerException(String.format(format, toStringOf(types)));
      }
      if (type.isInstance(actual)) {
        found = true;
        break;
      }
    }
    if (found) return;
    throw failures.failure(info, shouldBeInstanceOfAny(actual, types));
  }

  /**
   * Verifies that the given object is not an instance of the given type.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param type the type to check the given object against.
   * @throws NullPointerException if the given type is {@code null}.
   * @throws AssertionError if the given object is {@code null}.
   * @throws AssertionError if the given object is an instance of the given type.
   */
  public void assertIsNotInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
    if (type == null) throw new NullPointerException("The given type should not be null");
    assertNotNull(info, actual);
    if (!type.isInstance(actual)) return;
    throw failures.failure(info, shouldNotBeInstance(actual, type));
  }

  /**
   * Verifies that the given object is not an instance of any of the given types.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param types the types to check the given object against.
   * @throws NullPointerException if the given array is {@code null}.
   * @throws IllegalArgumentException if the given array is empty.
   * @throws NullPointerException if the given array has {@code null} elements.
   * @throws AssertionError if the given object is {@code null}.
   * @throws AssertionError if the given object is an instance of any of the given types.
   */
  public void assertIsNotInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types) {
    checkIsNotNullAndIsNotEmpty(types);
    assertNotNull(info, actual);
    boolean found = false;
    for (Class<?> type : types) {
      if (type == null) {
        String format = "The given array of types:<%s> should not have null elements";
        throw new NullPointerException(String.format(format, toStringOf(types)));
      }
      if (type.isInstance(actual)) {
        found = true;
        break;
      }
    }
    if (!found) return;
    throw failures.failure(info, shouldNotBeInstanceOfAny(actual, types));
  }

  /**
   * Verifies that the actual value has the same class as the given object.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @throws AssertionError if the actual has not the same type has the given object.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given object is null.
   */
  public void assertHasSameClassAs(AssertionInfo info, Object actual, Object other) {
    assertNotNull(info, actual);
    if (other == null) throw new NullPointerException("The given object should not be null");
    Class<?> actualClass = actual.getClass();
    Class<?> otherClass = other.getClass();
    if (actualClass.equals(otherClass)) return;
    throw failures.failure(info, shouldHaveSameClass(actual, other));
  }

  /**
   * Verifies that the actual value does not have the same class as the given object.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to check type against.
   * @throws AssertionError if the actual has the same type has the given object.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given object is null.
   */
  public void assertDoesNotHaveSameClassAs(AssertionInfo info, Object actual, Object other) {
    assertNotNull(info, actual);
    if (other == null) throw new NullPointerException("The given object should not be null");
    Class<?> actualClass = actual.getClass();
    Class<?> otherClass = other.getClass();
    if (!actualClass.equals(otherClass)) return;
    throw failures.failure(info, shouldNotHaveSameClass(actual, other));
  }

  /**
   * Verifies that the actual value is exactly a instance of given type.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param type the type to check the actual value against.
   * @throws AssertionError if the actual is not exactly a instance of given type.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given object is null.
   */
  public void assertIsExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
    assertNotNull(info, actual);
    if (type == null) throw new NullPointerException("The given type should not be null");
    Class<?> current = actual.getClass();
    if (type.equals(current)) return;
    throw failures.failure(info, shouldBeExactlyInstance(actual, type));
  }

  /**
   * Verifies that the actual value is not exactly a instance of given type.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param type the type to check the actual value against.
   * @throws AssertionError if the actual is exactly a instance of given type.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given object is null.
   */
  public void assertIsNotExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type) {
    assertNotNull(info, actual);
    if (type == null) throw new NullPointerException("The given type should not be null");
    Class<?> current = actual.getClass();
    if (!type.equals(current)) return;
    throw failures.failure(info, shouldNotBeExactlyInstance(actual, type));
  }

  /**
   * Verifies that the actual value type is in given types.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param types the types to check the actual value against.
   * @throws AssertionError if the actual value type is in given type.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given types is null.
   */
  public void assertIsOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types) {
    assertNotNull(info, actual);
    if (types == null) throw new NullPointerException("The given types should not be null");
    if (isItemInArray(actual.getClass(), types)) return;
    throw failures.failure(info, shouldBeOfClassIn(actual, types));
  }

  /**
   * Verifies that the actual value type is not in given types.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param types the types to check the actual value against.
   * @throws AssertionError if the actual value type is in given type.
   * @throws NullPointerException if the actual value is null.
   * @throws NullPointerException if the given types is null.
   */
  public void assertIsNotOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types) {
    assertNotNull(info, actual);
    if (types == null) throw new NullPointerException("The given types should not be null");
    if (!isItemInArray(actual.getClass(), types)) return;
    throw failures.failure(info, shouldNotBeOfClassIn(actual, types));
  }

  private void checkIsNotNullAndIsNotEmpty(Class<?>[] types) {
    if (types == null)
      throw new NullPointerException("The given array of types should not be null");
    if (types.length == 0)
      throw new IllegalArgumentException("The given array of types should not be empty");
  }

  /**
   * Asserts that two objects are equal.
   *
   * @param info contains information about the assertion.
   * @param actual the "actual" object.
   * @param expected the "expected" object.
   * @throws AssertionError if {@code actual} is not equal to {@code expected}. This method will
   *     throw a {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the
   *     given objects are not equal.
   */
  public void assertEqual(AssertionInfo info, Object actual, Object expected) {
    if (areEqual(actual, expected)) return;
    throw failures.failure(info, shouldBeEqual(actual, expected, comparisonStrategy));
  }

  /**
   * Asserts that two objects are not equal.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to compare {@code actual} to.
   * @throws AssertionError if {@code actual} is equal to {@code other}.
   */
  public void assertNotEqual(AssertionInfo info, Object actual, Object other) {
    if (!areEqual(actual, other)) return;
    throw failures.failure(info, shouldNotBeEqual(actual, other, comparisonStrategy));
  }

  /**
   * Compares actual and other with standard strategy (null safe equals check).
   *
   * @param actual the object to compare to other
   * @param other the object to compare to actual
   * @return true if actual and other are equal (null safe equals check), false otherwise.
   */
  private boolean areEqual(Object actual, Object other) {
    return comparisonStrategy.areEqual(other, actual);
  }

  /**
   * Asserts that the given object is {@code null}.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @throws AssertionError if the given object is not {@code null}.
   */
  public void assertNull(AssertionInfo info, Object actual) {
    if (actual == null) return;
    throw failures.failure(info, shouldBeEqual(actual, null, comparisonStrategy));
  }

  /**
   * Asserts that the given object is not {@code null}.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @throws AssertionError if the given object is {@code null}.
   */
  public void assertNotNull(AssertionInfo info, Object actual) {
    if (actual != null) return;
    throw failures.failure(info, shouldNotBeNull());
  }

  /**
   * Asserts that two objects refer to the same object.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param expected the expected object.
   * @throws AssertionError if the given objects do not refer to the same object.
   */
  public void assertSame(AssertionInfo info, Object actual, Object expected) {
    if (actual == expected) return;
    throw failures.failure(info, shouldBeSame(actual, expected));
  }

  /**
   * Asserts that two objects do not refer to the same object.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to compare {@code actual} to.
   * @throws AssertionError if the given objects refer to the same object.
   */
  public void assertNotSame(AssertionInfo info, Object actual, Object other) {
    if (actual != other) return;
    throw failures.failure(info, shouldNotBeSame(actual));
  }

  /**
   * Asserts that the given object is present in the given array.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param values the given array.
   * @throws NullPointerException if the given array is {@code null}.
   * @throws IllegalArgumentException if the given array is empty.
   * @throws AssertionError if the given object is not present in the given array.
   */
  public void assertIsIn(AssertionInfo info, Object actual, Object[] values) {
    checkIsNotNullAndNotEmpty(values);
    assertNotNull(info, actual);
    if (isItemInArray(actual, values)) return;
    throw failures.failure(info, shouldBeIn(actual, values, comparisonStrategy));
  }

  /**
   * Asserts that the given object is not present in the given array.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param values the given array.
   * @throws NullPointerException if the given array is {@code null}.
   * @throws IllegalArgumentException if the given array is empty.
   * @throws AssertionError if the given object is present in the given array.
   */
  public void assertIsNotIn(AssertionInfo info, Object actual, Object[] values) {
    checkIsNotNullAndNotEmpty(values);
    assertNotNull(info, actual);
    if (!isItemInArray(actual, values)) return;
    throw failures.failure(info, shouldNotBeIn(actual, values, comparisonStrategy));
  }

  private void checkIsNotNullAndNotEmpty(Object[] values) {
    if (values == null) throw new NullPointerException("The given array should not be null");
    if (values.length == 0)
      throw new IllegalArgumentException("The given array should not be empty");
  }

  /**
   * Returns <code>true</code> if given item is in given array, <code>false</code> otherwise.
   *
   * @param item the object to look for in arrayOfValues
   * @param arrayOfValues the array of values
   * @return <code>true</code> if given item is in given array, <code>false</code> otherwise.
   */
  private boolean isItemInArray(Object item, Object[] arrayOfValues) {
    for (Object value : arrayOfValues) if (areEqual(value, item)) return true;
    return false;
  }

  /**
   * Asserts that the given object is present in the given collection.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param values the given iterable.
   * @throws NullPointerException if the given collection is {@code null}.
   * @throws IllegalArgumentException if the given collection is empty.
   * @throws AssertionError if the given object is not present in the given collection.
   */
  public <A> void assertIsIn(AssertionInfo info, A actual, Iterable<? extends A> values) {
    checkIsNotNullAndNotEmpty(values);
    assertNotNull(info, actual);
    if (isActualIn(actual, values)) return;
    throw failures.failure(info, shouldBeIn(actual, values, comparisonStrategy));
  }

  /**
   * Asserts that the given object is not present in the given collection.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param values the given collection.
   * @throws NullPointerException if the given iterable is {@code null}.
   * @throws IllegalArgumentException if the given collection is empty.
   * @throws AssertionError if the given object is present in the given collection.
   */
  public <A> void assertIsNotIn(AssertionInfo info, A actual, Iterable<? extends A> values) {
    checkIsNotNullAndNotEmpty(values);
    assertNotNull(info, actual);
    if (!isActualIn(actual, values)) return;
    throw failures.failure(info, shouldNotBeIn(actual, values, comparisonStrategy));
  }

  private void checkIsNotNullAndNotEmpty(Iterable<?> values) {
    if (values == null) throw new NullPointerException("The given iterable should not be null");
    if (!values.iterator().hasNext())
      throw new IllegalArgumentException("The given iterable should not be empty");
  }

  private <A> boolean isActualIn(A actual, Iterable<? extends A> values) {
    for (A value : values) if (areEqual(value, actual)) return true;
    return false;
  }

  /**
   * Assert that the given object is lenient equals by ignoring null fields value on other object.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to compare {@code actual} to.
   * @throws NullPointerException if the actual type is {@code null}.
   * @throws NullPointerException if the other type is {@code null}.
   * @throws AssertionError if the actual and the given object are not lenient equals.
   * @throws AssertionError if the other object is not an instance of the actual type.
   */
  public <A> void assertIsLenientEqualsToByIgnoringNullFields(
      AssertionInfo info, A actual, A other) {
    assertIsInstanceOf(info, other, actual.getClass());
    List<String> fieldsNames = new LinkedList<String>();
    List<Object> values = new LinkedList<Object>();
    List<String> nullFields = new LinkedList<String>();
    for (Field field : actual.getClass().getDeclaredFields()) {
      try {
        Object otherFieldValue =
            propertySupport.propertyValue(field.getName(), field.getType(), other);
        if (otherFieldValue != null) {
          Object actualFieldValue =
              propertySupport.propertyValue(field.getName(), field.getType(), actual);
          if (!otherFieldValue.equals(actualFieldValue)) {
            fieldsNames.add(field.getName());
            values.add(otherFieldValue);
          }
        } else {
          nullFields.add(field.getName());
        }
      } catch (IntrospectionError e) {
        // Not readeable field, skip.
      }
    }
    if (fieldsNames.isEmpty()) return;
    throw failures.failure(
        info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, values, nullFields));
  }

  /**
   * Assert that the given object is lenient equals to other object by comparing given fields value
   * only.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to compare {@code actual} to.
   * @param fields accepted fields
   * @throws NullPointerException if the actual type is {@code null}.
   * @throws NullPointerException if the other type is {@code null}.
   * @throws AssertionError if the actual and the given object are not lenient equals.
   * @throws AssertionError if the other object is not an instance of the actual type.
   * @throws IntrospectionError if a field does not exist in actual.
   */
  public <A> void assertIsLenientEqualsToByAcceptingFields(
      AssertionInfo info, A actual, A other, String... fields) {
    assertIsInstanceOf(info, other, actual.getClass());
    List<String> rejectedFieldsNames = new LinkedList<String>();
    List<Object> expectedValues = new LinkedList<Object>();
    for (String fieldName : fields) {
      Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
      Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
      if (!(actualFieldValue == otherFieldValue
          || (actualFieldValue != null && actualFieldValue.equals(otherFieldValue)))) {
        rejectedFieldsNames.add(fieldName);
        expectedValues.add(otherFieldValue);
      }
    }
    if (rejectedFieldsNames.isEmpty()) return;
    throw failures.failure(
        info,
        shouldBeLenientEqualByAccepting(actual, rejectedFieldsNames, expectedValues, list(fields)));
  }

  /**
   * Assert that the given object is lenient equals by ignoring fields.
   *
   * @param info contains information about the assertion.
   * @param actual the given object.
   * @param other the object to compare {@code actual} to.
   * @param fields the fields to ignore in comparison
   * @throws NullPointerException if the actual type is {@code null}.
   * @throws NullPointerException if the other type is {@code null}.
   * @throws AssertionError if the actual and the given object are not lenient equals.
   * @throws AssertionError if the other object is not an instance of the actual type.
   */
  public <A> void assertIsLenientEqualsToByIgnoringFields(
      AssertionInfo info, A actual, A other, String... fields) {
    assertIsInstanceOf(info, other, actual.getClass());
    List<String> fieldsNames = new LinkedList<String>();
    List<Object> expectedValues = new LinkedList<Object>();
    Set<String> ignoredFields = set(fields);
    for (Field field : actual.getClass().getDeclaredFields()) {
      try {
        if (!ignoredFields.contains(field.getName())) {
          String fieldName = field.getName();
          Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
          Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
          if (!org.fest.util.Objects.areEqual(actualFieldValue, otherFieldValue)) {
            fieldsNames.add(fieldName);
            expectedValues.add(otherFieldValue);
          }
        }
      } catch (IntrospectionError e) {
        // Not readeable field, skip.
      }
    }
    if (fieldsNames.isEmpty()) return;
    throw failures.failure(
        info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, expectedValues, list(fields)));
  }
}
Exemplo n.º 27
0
/**
 * Reusable assertions for <code>{@link Comparable}</code>s.
 *
 * @author Alex Ruiz
 * @author Joel Costigliola
 */
public class Comparables {

  private static final Comparables INSTANCE = new Comparables();

  /**
   * Returns the singleton instance of this class based on {@link StandardComparisonStrategy}.
   *
   * @return the singleton instance of this class based on {@link StandardComparisonStrategy}.
   */
  public static Comparables instance() {
    return INSTANCE;
  }

  @VisibleForTesting Failures failures = Failures.instance();
  ComparisonStrategy comparisonStrategy;

  @VisibleForTesting
  Comparables() {
    this(StandardComparisonStrategy.instance());
  }

  public Comparables(ComparisonStrategy comparisonStrategy) {
    this.comparisonStrategy = comparisonStrategy;
  }

  @VisibleForTesting
  public Comparator<?> getComparator() {
    if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) {
      return ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator();
    }
    return null;
  }

  @VisibleForTesting
  void setFailures(Failures failures) {
    this.failures = failures;
  }

  /**
   * Asserts that two T instances are equal.
   *
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param expected the expected value.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is not equal to the expected one. This method will
   *     throw a {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the
   *     expected and actual values are not equal.
   */
  public <T> void assertEqual(AssertionInfo info, T actual, T expected) {
    assertNotNull(info, actual);
    if (areEqual(actual, expected)) return;
    throw failures.failure(info, shouldBeEqual(actual, expected, comparisonStrategy));
  }

  protected <T> boolean areEqual(T actual, T expected) {
    return comparisonStrategy.areEqual(actual, expected);
  }

  /**
   * Asserts that two T instances are not equal.
   *
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is equal to the other one.
   */
  public <T> void assertNotEqual(AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    if (!areEqual(actual, other)) return;
    throw failures.failure(info, shouldNotBeEqual(actual, other, comparisonStrategy));
  }

  /**
   * Asserts that two <code>{@link Comparable}</code>s are equal by invoking <code>
   * {@link Comparable#compareTo(Object)}</code>.<br>
   * Note that it does not rely on the custom {@link #comparisonStrategy} if one has been set.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param expected the expected value.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is not equal to the expected one. This method will
   *     throw a {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the
   *     expected and actual values are not equal.
   */
  public <T extends Comparable<? super T>> void assertEqualByComparison(
      AssertionInfo info, T actual, T expected) {
    assertNotNull(info, actual);
    // we don't delegate to comparisonStrategy, as this assertion makes it clear it relies on
    // Comparable
    if (actual.compareTo(expected) == 0) return;
    throw failures.failure(info, shouldBeEqual(actual, expected));
  }

  /**
   * Asserts that two <code>{@link Comparable}</code>s are not equal by invoking <code>
   * {@link Comparable#compareTo(Object)}</code> .<br>
   * Note that it does not rely on the custom {@link #comparisonStrategy} if one has been set.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is equal to the other one.
   */
  public <T extends Comparable<? super T>> void assertNotEqualByComparison(
      AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    // we don't delagate to comparisonStrategy, as this assertion makes it clear it relies on
    // Comparable
    if (actual.compareTo(other) != 0) return;
    throw failures.failure(info, shouldNotBeEqual(actual, other));
  }

  /**
   * Asserts that the actual value is less than the other one.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is not less than the other one: this assertion will
   *     fail if the actual value is equal to or greater than the other value.
   */
  public <T extends Comparable<? super T>> void assertLessThan(
      AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    if (isLessThan(actual, other)) return;
    throw failures.failure(info, shouldBeLess(actual, other, comparisonStrategy));
  }

  /**
   * Asserts that the actual value is less than or equal to the other one.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is greater than the other one.
   */
  public <T extends Comparable<? super T>> void assertLessThanOrEqualTo(
      AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    if (!isGreaterThan(actual, other)) return;
    throw failures.failure(info, shouldBeLessOrEqual(actual, other, comparisonStrategy));
  }

  /**
   * Asserts that the actual value is greater than the other one.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is not greater than the other one: this assertion
   *     will fail if the actual value is equal to or less than the other value.
   */
  public <T extends Comparable<? super T>> void assertGreaterThan(
      AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    if (isGreaterThan(actual, other)) return;
    throw failures.failure(info, shouldBeGreater(actual, other, comparisonStrategy));
  }

  /** delegates to {@link #comparisonStrategy#isGreaterThan(Object, Object)} */
  private boolean isGreaterThan(Object actual, Object other) {
    return comparisonStrategy.isGreaterThan(actual, other);
  }

  /**
   * Asserts that the actual value is greater than or equal to the other one.
   *
   * @param <T> used to guarantee that two objects of the same type are being compared against each
   *     other.
   * @param info contains information about the assertion.
   * @param actual the actual value.
   * @param other the value to compare the actual value to.
   * @throws AssertionError if the actual value is {@code null}.
   * @throws AssertionError if the actual value is less than the other one.
   */
  public <T extends Comparable<? super T>> void assertGreaterThanOrEqualTo(
      AssertionInfo info, T actual, T other) {
    assertNotNull(info, actual);
    if (!isLessThan(actual, other)) return;
    throw failures.failure(info, shouldBeGreaterOrEqual(actual, other, comparisonStrategy));
  }

  private boolean isLessThan(Object actual, Object other) {
    return comparisonStrategy.isLessThan(actual, other);
  }

  protected static <T> void assertNotNull(AssertionInfo info, T actual) {
    Objects.instance().assertNotNull(info, actual);
  }
}
Exemplo n.º 28
0
 /**
  * Verifies that the actual value type is not in given types.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param types the types to check the actual value against.
  * @throws AssertionError if the actual value type is in given type.
  * @throws NullPointerException if the actual value is null.
  * @throws NullPointerException if the given types is null.
  */
 public void assertIsNotOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types) {
   assertNotNull(info, actual);
   if (types == null) throw new NullPointerException("The given types should not be null");
   if (!isItemInArray(actual.getClass(), types)) return;
   throw failures.failure(info, shouldNotBeOfClassIn(actual, types));
 }
/**
 * Reusable assertions for arrays of {@code char}s.
 *
 * @author Alex Ruiz
 * @author Joel Costigliola
 */
public class CharArrays {

  private static final CharArrays INSTANCE = new CharArrays();

  /**
   * Returns the singleton instance of this class.
   *
   * @return the singleton instance of this class.
   */
  public static CharArrays instance() {
    return INSTANCE;
  }

  private Arrays arrays = Arrays.instance();

  @VisibleForTesting Failures failures = Failures.instance();

  @VisibleForTesting
  CharArrays() {
    this(StandardComparisonStrategy.instance());
  }

  @VisibleForTesting
  public Comparator<?> getComparator() {
    return arrays.getComparator();
  }

  public CharArrays(ComparisonStrategy comparisonStrategy) {
    this.arrays = new Arrays(comparisonStrategy);
  }

  /**
   * Asserts that the given array is {@code null} or empty.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @throws AssertionError if the given array is not {@code null} *and* contains one or more
   *     elements.
   */
  public void assertNullOrEmpty(AssertionInfo info, char[] actual) {
    arrays.assertNullOrEmpty(info, failures, actual);
  }

  /**
   * Asserts that the given array is empty.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array is not empty.
   */
  public void assertEmpty(AssertionInfo info, char[] actual) {
    arrays.assertEmpty(info, failures, actual);
  }

  /**
   * Asserts that the given array is not empty.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array is empty.
   */
  public void assertNotEmpty(AssertionInfo info, char[] actual) {
    arrays.assertNotEmpty(info, failures, actual);
  }

  /**
   * Asserts that the number of elements in the given array is equal to the expected one.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param expectedSize the expected size of {@code actual}.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the number of elements in the given array is different than the
   *     expected one.
   */
  public void assertHasSize(AssertionInfo info, char[] actual, int expectedSize) {
    arrays.assertHasSize(info, failures, actual, expectedSize);
  }

  /**
   * Asserts that the given array contains the given values, in any order.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param values the values that are expected to be in the given array.
   * @throws NullPointerException if the array of values is {@code null}.
   * @throws IllegalArgumentException if the array of values is empty.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array does not contain the given values.
   */
  public void assertContains(AssertionInfo info, char[] actual, char[] values) {
    arrays.assertContains(info, failures, actual, values);
  }

  /**
   * Verifies that the given array contains the given value at the given index.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param value the value to look for.
   * @param index the index where the value should be stored in the given array.
   * @throws AssertionError if the given array is {@code null} or empty.
   * @throws NullPointerException if the given {@code Index} is {@code null}.
   * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or
   *     greater than the size of the given array.
   * @throws AssertionError if the given array does not contain the given value at the given index.
   */
  public void assertContains(AssertionInfo info, char[] actual, char value, Index index) {
    arrays.assertContains(info, failures, actual, value, index);
  }

  /**
   * Verifies that the given array does not contain the given value at the given index.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param value the value to look for.
   * @param index the index where the value should be stored in the given array.
   * @throws AssertionError if the given array is {@code null}.
   * @throws NullPointerException if the given {@code Index} is {@code null}.
   * @throws AssertionError if the given array contains the given value at the given index.
   */
  public void assertDoesNotContain(AssertionInfo info, char[] actual, char value, Index index) {
    arrays.assertDoesNotContain(info, failures, actual, value, index);
  }

  /**
   * Asserts that the given array contains only the given values and nothing else, in any order.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param values the values that are expected to be in the given array.
   * @throws NullPointerException if the array of values is {@code null}.
   * @throws IllegalArgumentException if the array of values is empty.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array does not contain the given values or if the given
   *     array contains values that are not in the given array.
   */
  public void assertContainsOnly(AssertionInfo info, char[] actual, char[] values) {
    arrays.assertContainsOnly(info, failures, actual, values);
  }

  /**
   * Verifies that the given array contains the given sequence of values, without any other values
   * between them.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param sequence the sequence of values to look for.
   * @throws AssertionError if the given array is {@code null}.
   * @throws NullPointerException if the given sequence is {@code null}.
   * @throws IllegalArgumentException if the given sequence is empty.
   * @throws AssertionError if the given array does not contain the given sequence of values.
   */
  public void assertContainsSequence(AssertionInfo info, char[] actual, char[] sequence) {
    arrays.assertContainsSequence(info, failures, actual, sequence);
  }

  /**
   * Asserts that the given array does not contain the given values.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param values the values that are expected not to be in the given array.
   * @throws NullPointerException if the array of values is {@code null}.
   * @throws IllegalArgumentException if the array of values is empty.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array contains any of given values.
   */
  public void assertDoesNotContain(AssertionInfo info, char[] actual, char[] values) {
    arrays.assertDoesNotContain(info, failures, actual, values);
  }

  /**
   * Asserts that the given array does not have duplicate values.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @throws NullPointerException if the array of values is {@code null}.
   * @throws IllegalArgumentException if the array of values is empty.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array contains duplicate values.
   */
  public void assertDoesNotHaveDuplicates(AssertionInfo info, char[] actual) {
    arrays.assertDoesNotHaveDuplicates(info, failures, actual);
  }

  /**
   * Verifies that the given array starts with the given sequence of values, without any other
   * values between them. Similar to <code>
   * {@link #assertContainsSequence(AssertionInfo, char[], char[])}</code>, but it also verifies
   * that the first element in the sequence is also the first element of the given array.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param sequence the sequence of values to look for.
   * @throws NullPointerException if the given argument is {@code null}.
   * @throws IllegalArgumentException if the given argument is an empty array.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array does not start with the given sequence of values.
   */
  public void assertStartsWith(AssertionInfo info, char[] actual, char[] sequence) {
    arrays.assertStartsWith(info, failures, actual, sequence);
  }

  /**
   * Verifies that the given array ends with the given sequence of values, without any other values
   * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, char[], char[])}
   * </code>, but it also verifies that the last element in the sequence is also the last element of
   * the given array.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param sequence the sequence of values to look for.
   * @throws NullPointerException if the given argument is {@code null}.
   * @throws IllegalArgumentException if the given argument is an empty array.
   * @throws AssertionError if the given array is {@code null}.
   * @throws AssertionError if the given array does not end with the given sequence of values.
   */
  public void assertEndsWith(AssertionInfo info, char[] actual, char[] sequence) {
    arrays.assertEndsWith(info, failures, actual, sequence);
  }

  /**
   * Concrete implementation of {@link ArraySortedAssert#isSorted()}.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   */
  public void assertIsSorted(AssertionInfo info, char[] actual) {
    arrays.assertIsSorted(info, failures, actual);
  }

  /**
   * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}.
   *
   * @param info contains information about the assertion.
   * @param actual the given array.
   * @param comparator the {@link Comparator} used to compare array elements
   */
  public void assertIsSortedAccordingToComparator(
      AssertionInfo info, char[] actual, Comparator<? extends Character> comparator) {
    Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator);
  }
}