001 /* 002 * Created on Jul 25, 2010 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2010-2011 the original author or authors. 014 */ 015 package org.fest.assertions.core; 016 017 import java.util.Comparator; 018 019 /** 020 * Assertions applicable to groups of values that can be enumerated (e.g. arrays, collections or strings.) 021 * @param <S> the "self" type of this assertion class. Please read "<a href="http://bit.ly/anMa4g" 022 * target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>" 023 * for more details. 024 * @param <T> the type of elements of the "actual" value. 025 * 026 * @author Yvonne Wang 027 * @author Alex Ruiz 028 * @author Mikhail Mazursky 029 */ 030 public interface EnumerableAssert<S extends EnumerableAssert<S, T>, T> { 031 032 /** 033 * Verifies that the actual group of values is {@code null} or empty. 034 * @throws AssertionError if the actual group of values is not {@code null} or not empty. 035 */ 036 void isNullOrEmpty(); 037 038 /** 039 * Verifies that the actual group of values is empty. 040 * @throws AssertionError if the actual group of values is not empty. 041 */ 042 void isEmpty(); 043 044 /** 045 * Verifies that the actual group of values is not empty. 046 * @return {@code this} assertion object. 047 * @throws AssertionError if the actual group of values is empty. 048 */ 049 S isNotEmpty(); 050 051 /** 052 * Verifies that the number of values in the actual group is equal to the given one. 053 * @param expected the expected number of values in the actual group. 054 * @return {@code this} assertion object. 055 * @throws AssertionError if the number of values of the actual group is not equal to the given one. 056 */ 057 S hasSize(int expected); 058 059 /** 060 * Use given custom comparator instead of relying on actual type A <code>equals</code> method to compare group 061 * elements for incoming assertion checks. 062 * <p> 063 * Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default 064 * comparison strategy. 065 * <p> 066 * Examples : 067 * 068 * <pre> 069 * // compares invoices by payee 070 * assertThat(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList). 071 * 072 * // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator 073 * assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice) 074 * 075 * // as assertThat(invoiceList) creates a new assertion, it falls back to standard comparison strategy 076 * // based on Invoice's equal method to compare invoiceList elements to lowestInvoice. 077 * assertThat(invoiceList).contains(lowestInvoice). 078 * 079 * // standard comparison : the fellowshipOfTheRing includes Gandalf but not Sauron (believe me) ... 080 * assertThat(fellowshipOfTheRing).contains(gandalf) 081 * .doesNotContain(sauron); 082 * 083 * // ... but if we compare only races, Sauron is in fellowshipOfTheRing because he's a Maia like Gandalf. 084 * assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator) 085 * .contains(sauron); 086 * </pre> 087 * 088 * @param customComparator the comparator to use for incoming assertion checks. 089 * @throws NullPointerException if the given comparator is {@code null}. 090 * @return {@code this} assertion object. 091 */ 092 S usingElementComparator(Comparator<? super T> customComparator); 093 094 /** 095 * Revert to standard comparison for incoming assertion group element checks. 096 * <p> 097 * This method should be used to disable a custom comparison strategy set by calling 098 * {@link #usingElementComparator(Comparator)}. 099 * @return {@code this} assertion object. 100 */ 101 S usingDefaultElementComparator(); 102 }