@Before public void setUp() { info = new WritableAssertionInfo(); errorFactory = mock(AssertionErrorFactory.class); failures = Failures.instance(); }
/** * Reusable assertions for arrays of {@code byte}s. * * @author Alex Ruiz * @author Mikhail Mazursky * @author Nicolas François */ public class ByteArrays { private static final ByteArrays INSTANCE = new ByteArrays(); /** * Returns the singleton instance of this class. * * @return the singleton instance of this class. */ public static ByteArrays instance() { return INSTANCE; } private Arrays arrays = Arrays.instance(); @VisibleForTesting Failures failures = Failures.instance(); @VisibleForTesting ByteArrays() { this(StandardComparisonStrategy.instance()); } public ByteArrays(ComparisonStrategy comparisonStrategy) { this.arrays = new Arrays(comparisonStrategy); } @VisibleForTesting public Comparator<?> getComparator() { return arrays.getComparator(); } /** * 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, byte[] 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, byte[] 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, byte[] 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, byte[] actual, int expectedSize) { arrays.assertHasSize(info, failures, actual, expectedSize); } /** * Assert that the actual array has the same size as the other {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given array. * @param other the group to compare * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the other group is {@code null}. * @throws AssertionError if the actual group does not have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, byte[] actual, Iterable<?> other) { arrays.assertHasSameSizeAs(info, actual, other); } /** * 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, byte[] actual, byte[] 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, byte[] actual, byte 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, byte[] actual, byte 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, byte[] actual, byte[] values) { arrays.assertContainsOnly(info, failures, actual, values); } public void assertContainsExactly(AssertionInfo info, byte[] actual, byte[] values) { arrays.assertContainsExactly(info, failures, actual, values); } /** * Asserts that the given array contains only once the given values. * * @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 more than once values. */ public void assertContainsOnlyOnce(AssertionInfo info, byte[] actual, byte[] values) { arrays.assertContainsOnlyOnce(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, byte[] actual, byte[] sequence) { arrays.assertContainsSequence(info, failures, actual, sequence); } /** * Verifies that the given array contains the given sequence of values (possibly with other values * between them). * * @param info contains information about the assertion. * @param actual the given array. * @param subsequence the subsequence 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 assertContainsSubsequence(AssertionInfo info, byte[] actual, byte[] subsequence) { arrays.assertContainsSubsequence(info, failures, actual, subsequence); } /** * 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, byte[] actual, byte[] 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, byte[] 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, byte[], byte[])}</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, byte[] actual, byte[] 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, byte[], byte[])} * </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, byte[] actual, byte[] 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, byte[] 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, byte[] actual, Comparator<? super Byte> comparator) { Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator); } }
/** * Implements the assertion methods on the type of a column. * * <p>The different type of values are enumerated in {@link org.assertj.db.type.ValueType}. * * @author Régis Pouiller * @see org.assertj.db.api.assertions.AssertOnColumnType */ public class AssertionsOnColumnType { /** To notice failures in the assertion. */ private static final Failures failures = Failures.instance(); /** Private constructor. */ private AssertionsOnColumnType() { // Empty } /** * /** Verifies that the type of the values of the column is equal to the type in parameter. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param expected The expected type to compare to. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is different to the type in parameter. */ public static <A extends AbstractAssert> A isOfType( A assertion, WritableAssertionInfo info, List<Object> valuesList, ValueType expected, boolean lenient) { if (lenient) { return isOfAnyTypeIn(assertion, info, valuesList, expected, ValueType.NOT_IDENTIFIED); } int index = 0; for (Object value : valuesList) { ValueType type = ValueType.getType(value); if (type != expected) { throw failures.failure( info, ShouldBeValueType.shouldBeValueType(index, value, expected, type)); } index++; } return assertion; } /** * Verifies that the type of the column is equal to one of the types in parameters. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param expected The expected types to compare to. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is different to all the types in parameters. */ public static <A extends AbstractAssert> A isOfAnyTypeIn( A assertion, WritableAssertionInfo info, List<Object> valuesList, ValueType... expected) { int index = 0; loop: for (Object value : valuesList) { ValueType type = ValueType.getType(value); for (ValueType valueType : expected) { if (type == valueType) { index++; continue loop; } } throw failures.failure(info, shouldBeValueTypeOfAny(index, value, type, expected)); } return assertion; } /** * Verifies that the type of the values of the column is number. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not number. */ public static <A extends AbstractAssert> A isNumber( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.NUMBER, lenient); } /** * Verifies that the type of the values of the column is boolean. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not boolean. */ public static <A extends AbstractAssert> A isBoolean( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.BOOLEAN, lenient); } /** * Verifies that the type of the values of the column is date. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not date. */ public static <A extends AbstractAssert> A isDate( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.DATE, lenient); } /** * Verifies that the type of the values of the column is time. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not time. */ public static <A extends AbstractAssert> A isTime( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.TIME, lenient); } /** * Verifies that the type of the values of the column is date/time. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not date/time. */ public static <A extends AbstractAssert> A isDateTime( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.DATE_TIME, lenient); } /** * Verifies that the type of the values of the column is array of bytes. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not array of bytes. */ public static <A extends AbstractAssert> A isBytes( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.BYTES, lenient); } /** * Verifies that the type of the values of the column is text. * * @param <A> The type of the assertion which call this method. * @param assertion The assertion which call this method. * @param info Writable information about an assertion. * @param valuesList The list of values. * @param lenient {@code true} if the test is lenient : if the type of a value is not identified * (for example when the value is {@code null}), it consider that it is ok. * @return {@code this} assertion object. * @throws AssertionError If the type of the column is not text. */ public static <A extends AbstractAssert> A isText( A assertion, WritableAssertionInfo info, List<Object> valuesList, boolean lenient) { return isOfType(assertion, info, valuesList, ValueType.TEXT, lenient); } }
/** * Reusable assertions for <code>{@link Map}</code>s. * * @author Alex Ruiz * @author Nicolas François */ public class Maps { private static Maps INSTANCE = new Maps(); /** * Returns the singleton instance of this class. * * @return the singleton instance of this class. */ public static Maps instance() { return INSTANCE; } @VisibleForTesting Failures failures = Failures.instance(); @VisibleForTesting Maps() {} /** * Asserts that the given {@code Map} is {@code null} or empty. * * @param info contains information about the assertion. * @param actual the given map. * @throws AssertionError if the given {@code Map} is not {@code null} *and* contains one or more * entries. */ public void assertNullOrEmpty(AssertionInfo info, Map<?, ?> actual) { if (actual == null || actual.isEmpty()) { return; } throw failures.failure(info, shouldBeNullOrEmpty(actual)); } /** * Asserts that the given {@code Map} is empty. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Map} is not empty. */ public void assertEmpty(AssertionInfo info, Map<?, ?> actual) { assertNotNull(info, actual); if (actual.isEmpty()) { return; } throw failures.failure(info, shouldBeEmpty(actual)); } /** * Asserts that the given {@code Map} is not empty. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Map} is empty. */ public void assertNotEmpty(AssertionInfo info, Map<?, ?> actual) { assertNotNull(info, actual); if (!actual.isEmpty()) { return; } throw failures.failure(info, shouldNotBeEmpty()); } /** * Asserts that the number of entries in the given {@code Map} is equal to the expected one. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param expectedSize the expected size of {@code actual}. * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the number of entries in the given {@code Map} is different than the * expected one. */ public void assertHasSize(AssertionInfo info, Map<?, ?> actual, int expectedSize) { assertNotNull(info, actual); int sizeOfActual = actual.size(); if (sizeOfActual == expectedSize) { return; } throw failures.failure(info, shouldHaveSize(actual, sizeOfActual, expectedSize)); } /** * Asserts that the number of entries in the given {@code Map} has the same size as the other * {@code Iterable}. * * @param info contains information about the assertion. * @param map the given {@code Map}. * @param other the group to compare * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the number of entries in the given {@code Map} does not have the same * size. */ public void assertHasSameSizeAs(AssertionInfo info, Map<?, ?> map, Iterable<?> other) { assertNotNull(info, map); if (other == null) { throw new NullPointerException("The iterable to look for should not be null"); } int sizeOfActual = map.size(); int sizeOfOther = sizeOf(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(map, sizeOfActual, sizeOfOther)); } /** * Asserts that the number of entries in the given {@code Map} has the same size as the other * array. * * @param info contains information about the assertion. * @param map the given {@code Map}. * @param other the group to compare * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given array is {@code null}. * @throws AssertionError if the number of entries in the given {@code Map} does not have the same * size. */ public void assertHasSameSizeAs(AssertionInfo info, Map<?, ?> map, Object[] other) { assertNotNull(info, map); if (other == null) { throw arrayOfValuesToLookForIsNull(); } int sizeOfActual = map.size(); int sizeOfOther = Array.getLength(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(map, sizeOfActual, sizeOfOther)); } /** * Asserts that the given {@code Map} contains the given entries, in any order. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param entries the entries that are expected to be in the given {@code Map}. * @throws NullPointerException if the array of entries is {@code null}. * @throws IllegalArgumentException if the array of entries is empty. * @throws NullPointerException if any of the entries in the given array is {@code null}. * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Map} does not contain the given entries. */ public void assertContains(AssertionInfo info, Map<?, ?> actual, MapEntry[] entries) { isNotEmptyOrNull(entries); assertNotNull(info, actual); Set<MapEntry> notFound = new LinkedHashSet<MapEntry>(); for (MapEntry entry : entries) { if (!containsEntry(actual, entry)) { notFound.add(entry); } } if (notFound.isEmpty()) { return; } throw failures.failure(info, shouldContain(actual, entries, notFound)); } /** * Asserts that the given {@code Map} does not contain the given entries. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param entries the entries that are expected to be in the given {@code Map}. * @throws NullPointerException if the array of entries is {@code null}. * @throws IllegalArgumentException if the array of entries is empty. * @throws NullPointerException if any of the entries in the given array is {@code null}. * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Map} contains any of the given entries. */ public void assertDoesNotContain(AssertionInfo info, Map<?, ?> actual, MapEntry[] entries) { isNotEmptyOrNull(entries); assertNotNull(info, actual); Set<MapEntry> found = new LinkedHashSet<MapEntry>(); for (MapEntry entry : entries) { if (containsEntry(actual, entry)) { found.add(entry); } } if (found.isEmpty()) { return; } throw failures.failure(info, shouldNotContain(actual, entries, found)); } /** * Verifies that the actual map contain the given key. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param key the given key * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if the actual map not contains the given key. */ public <K, V> void assertContainsKey(AssertionInfo info, Map<K, V> actual, K key) { assertNotNull(info, actual); if (actual.containsKey(key)) { return; } throw failures.failure(info, shouldContainKey(actual, key)); } /** * Verifies that the actual map not contains the given key. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param key the given key * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if the actual map contains the given key. */ public <K, V> void assertDoesNotContainKey(AssertionInfo info, Map<K, V> actual, K key) { assertNotNull(info, actual); if (!actual.containsKey(key)) { return; } throw failures.failure(info, shouldNotContainKey(actual, key)); } /** * Verifies that the actual map contain the given value. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param value the given value * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if the actual map not contains the given value. */ public <K, V> void assertContainsValue(AssertionInfo info, Map<K, V> actual, V value) { assertNotNull(info, actual); if (actual.containsValue(value)) { return; } throw failures.failure(info, shouldContainValue(actual, value)); } /** * Verifies that the actual map not contains the given value. * * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param value the given value * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if the actual map contains the given value. */ public <K, V> void assertDoesNotContainValue(AssertionInfo info, Map<K, V> actual, V value) { assertNotNull(info, actual); if (!actual.containsValue(value)) { return; } throw failures.failure(info, shouldNotContainValue(actual, value)); } private void isNotEmptyOrNull(MapEntry[] entries) { if (entries == null) { throw new NullPointerException("The array of entries to look for should not be null"); } if (entries.length == 0) { throw new IllegalArgumentException("The array of entries to look for should not be empty"); } } private boolean containsEntry(Map<?, ?> actual, MapEntry entry) { if (entry == null) { throw new NullPointerException("Entries to look for should not be null"); } if (!actual.containsKey(entry.key)) { return false; } return areEqual(actual.get(entry.key), entry.value); } private void assertNotNull(AssertionInfo info, Map<?, ?> actual) { Objects.instance().assertNotNull(info, actual); } }