Class Lists


  • public class Lists
    extends Object
    Reusable assertions for Lists.
    Author:
    Alex Ruiz, Yvonne Wang, Joel Costigliola, Jacek Jackowiak
    • Method Detail

      • instance

        public static Lists instance()
        Returns the singleton instance of this class.
        Returns:
        the singleton instance of this class.
      • getComparator

        public Comparator<?> getComparator()
      • assertContains

        public void assertContains​(AssertionInfo info,
                                   List<?> actual,
                                   Object value,
                                   Index index)
        Verifies that the given List contains the given object at the given index.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        value - the object to look for.
        index - the index where the object should be stored in the given List.
        Throws:
        AssertionError - if the given List is null or empty.
        NullPointerException - if the given Index is null.
        IndexOutOfBoundsException - if the value of the given Index is equal to or greater than the size of the given List.
        AssertionError - if the given List does not contain the given object at the given index.
      • assertDoesNotContain

        public void assertDoesNotContain​(AssertionInfo info,
                                         List<?> actual,
                                         Object value,
                                         Index index)
        Verifies that the given List does not contain the given object at the given index.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        value - the object to look for.
        index - the index where the object should be stored in the given List.
        Throws:
        AssertionError - if the given List is null.
        NullPointerException - if the given Index is null.
        AssertionError - if the given List contains the given object at the given index.
      • assertIsSorted

        public void assertIsSorted​(AssertionInfo info,
                                   List<?> actual)
        Verifies that the actual list is sorted in ascending order according to the natural ordering of its elements.

        All list elements must implement the Comparable interface and must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list), examples :

        • a list composed of {"a1", "a2", "a3"} is ok because the element type (String) is Comparable
        • a list composed of Rectangle {r1, r2, r3} is NOT ok because Rectangle is not Comparable
        • a list composed of {True, "abc", False} is NOT ok because elements are not mutually comparable
        Empty lists are considered sorted.
        Unique element lists are considered sorted unless the element type is not Comparable.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        Throws:
        AssertionError - if the actual list is not sorted in ascending order according to the natural ordering of its elements.
        AssertionError - if the actual list is null.
        AssertionError - if the actual list element type does not implement Comparable.
        AssertionError - if the actual list elements are not mutually Comparable.
      • assertIsSortedAccordingToComparator

        public void assertIsSortedAccordingToComparator​(AssertionInfo info,
                                                        List<?> actual,
                                                        Comparator<?> comparator)
        Verifies that the actual list is sorted according to the given comparator.
        Empty lists are considered sorted whatever the comparator is.
        One element lists are considered sorted if the element is compatible with comparator.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        comparator - the Comparator used to compare list elements
        Throws:
        AssertionError - if the actual list is not sorted according to the given comparator.
        AssertionError - if the actual list is null.
        NullPointerException - if the given comparator is null.
        AssertionError - if the actual list elements are not mutually comparable according to given Comparator.
      • assertHas

        public <T> void assertHas​(AssertionInfo info,
                                  List<? extends T> actual,
                                  Condition<? super T> condition,
                                  Index index)
        Verifies that the given List satisfies the given Condition at the given index.
        Type Parameters:
        T - the type of the actual value and the type of values that given Condition takes.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        condition - the given Condition.
        index - the index where the object should be stored in the given List.
        Throws:
        AssertionError - if the given List is null or empty.
        NullPointerException - if the given Index is null.
        IndexOutOfBoundsException - if the value of the given Index is equal to or greater than the size of the given List.
        NullPointerException - if the given Condition is null.
        AssertionError - if the value in the given List at the given index does not satisfy the given Condition .
      • assertIs

        public <T> void assertIs​(AssertionInfo info,
                                 List<? extends T> actual,
                                 Condition<? super T> condition,
                                 Index index)
        Verifies that the given List satisfies the given Condition at the given index.
        Type Parameters:
        T - the type of the actual value and the type of values that given Condition takes.
        Parameters:
        info - contains information about the assertion.
        actual - the given List.
        condition - the given Condition.
        index - the index where the object should be stored in the given List.
        Throws:
        AssertionError - if the given List is null or empty.
        NullPointerException - if the given Index is null.
        IndexOutOfBoundsException - if the value of the given Index is equal to or greater than the size of the given List.
        NullPointerException - if the given Condition is null.
        AssertionError - if the value in the given List at the given index does not satisfy the given Condition .
      • conditionIsMetAtIndex

        private <T> boolean conditionIsMetAtIndex​(AssertionInfo info,
                                                  List<T> actual,
                                                  Condition<? super T> condition,
                                                  Index index)
      • assertNotNull

        private void assertNotNull​(AssertionInfo info,
                                   List<?> actual)
      • assertNotNull

        private void assertNotNull​(Condition<?> condition)
      • areEqual

        private boolean areEqual​(Object actual,
                                 Object other)