001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit;
021
022import org.hamcrest.Matcher;
023import org.hamcrest.Matchers;
024
025/**
026 * DSL on assertion on iterable.
027 * <p>
028 * This interface is returned by the various methods
029 * {@link TestSuite#assertThatIterable(Iterable) assertThatIterable} exposed by
030 * {@link TestSuite}.
031 *
032 * @author borettim
033 * @param <T>
034 *            the Element type of the Iterator
035 * @see AssertThatObject assertion on object are also available on iterable.
036 */
037public interface AssertThatIterable<T> extends AssertThatObject<Iterable<T>> {
038        /**
039         * Check the size of the iterable.
040         * <p>
041         * 
042         * For instance :
043         * 
044         * <pre>
045         * assertThatIterable(myCollection).hasSize(3);
046         * </pre>
047         * 
048         * <br>
049         * <i>By default, assertion can only be used from the main thread of the
050         * test ; When used from another thread, the assertion will be lost. In the
051         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
052         * annotation is used, the assertion may not be lost, in case the thread use
053         * an assertion method from the test object instance. </i>
054         * 
055         * @param size
056         *            the expected size.
057         * @return true if the assertion is valid ; If the assertion is false,
058         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
059         *         fail the test, else, return false and the test will be failed
060         *         later.
061         */
062        @SuppressWarnings({ "unchecked", "rawtypes" })
063        default boolean hasSize(int size) {
064                return is((Matcher) Matchers.hasSize(size));
065        }
066
067        /**
068         * Check the size is not the one passed.
069         * <p>
070         * For instance :
071         * 
072         * <pre>
073         * assertThatIterable(myCollection).hasNotSize(4);
074         * </pre>
075         * 
076         * <br>
077         * <i>By default, assertion can only be used from the main thread of the
078         * test ; When used from another thread, the assertion will be lost. In the
079         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
080         * annotation is used, the assertion may not be lost, in case the thread use
081         * an assertion method from the test object instance. </i>
082         * 
083         * @param size
084         *            the not expected size.
085         * @return true if the assertion is valid ; If the assertion is false,
086         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
087         *         fail the test, else, return false and the test will be failed
088         *         later.
089         * @see Test#fastFail() The documentation of the <code>fastFail</code>
090         *      attribute of the <code>@Test</code> annotation, regarding the action
091         *      done by this assertion.
092         */
093        @SuppressWarnings({ "unchecked", "rawtypes" })
094        default boolean hasNotSize(int size) {
095                return isNot((Matcher) Matchers.hasSize(size));
096        }
097
098        /**
099         * Check that the iterable contains the items.
100         * <p>
101         * For instance :
102         * 
103         * <pre>
104         * assertThatIterable(myList).contains(1, 3, 5);
105         * </pre>
106         * 
107         * <br>
108         * <i>By default, assertion can only be used from the main thread of the
109         * test ; When used from another thread, the assertion will be lost. In the
110         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
111         * annotation is used, the assertion may not be lost, in case the thread use
112         * an assertion method from the test object instance. </i>
113         * 
114         * @param items
115         *            the expected items.
116         * @return true if the assertion is valid ; If the assertion is false,
117         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
118         *         fail the test, else, return false and the test will be failed
119         *         later.
120         * @see Test#fastFail() The documentation of the <code>fastFail</code>
121         *      attribute of the <code>@Test</code> annotation, regarding the action
122         *      done by this assertion.
123         */
124        default boolean contains(@SuppressWarnings("unchecked") T... items) {
125                return is(Matchers.contains(items));
126        }
127
128        /**
129         * Check that the iterable contains the items.
130         * <p>
131         * For instance
132         * 
133         * <pre>
134         * assertThatIterable(myList).containsMatching(equalTo(1), equalTo(3), equalTo(5));
135         * </pre>
136         * 
137         * <br>
138         * <i>By default, assertion can only be used from the main thread of the
139         * test ; When used from another thread, the assertion will be lost. In the
140         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
141         * annotation is used, the assertion may not be lost, in case the thread use
142         * an assertion method from the test object instance. </i>
143         * 
144         * @param items
145         *            the matcher for each item.
146         * @return true if the assertion is valid ; If the assertion is false,
147         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
148         *         fail the test, else, return false and the test will be failed
149         *         later.
150         * @see Test#fastFail() The documentation of the <code>fastFail</code>
151         *      attribute of the <code>@Test</code> annotation, regarding the action
152         *      done by this assertion.
153         */
154        default boolean containsMatching(
155                        @SuppressWarnings("unchecked") Matcher<? super T>... items) {
156                return is(Matchers.contains(items));
157        }
158
159        /**
160         * Check that the iterable contains the items in any order.
161         * <p>
162         * For instance :
163         * 
164         * <pre>
165         * assertThatIterable(myCollection).containsInAnyOrder(5, 3, 1);
166         * </pre>
167         * 
168         * <br>
169         * <i>By default, assertion can only be used from the main thread of the
170         * test ; When used from another thread, the assertion will be lost. In the
171         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
172         * annotation is used, the assertion may not be lost, in case the thread use
173         * an assertion method from the test object instance. </i>
174         * 
175         * @param items
176         *            the expected items.
177         * @return true if the assertion is valid ; If the assertion is false,
178         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
179         *         fail the test, else, return false and the test will be failed
180         *         later.
181         * @see Test#fastFail() The documentation of the <code>fastFail</code>
182         *      attribute of the <code>@Test</code> annotation, regarding the action
183         *      done by this assertion.
184         */
185        default boolean containsInAnyOrder(
186                        @SuppressWarnings("unchecked") T... items) {
187                return is(Matchers.containsInAnyOrder(items));
188        }
189
190        /**
191         * Check that the iterable contains the items in any order.
192         * <p>
193         * For instance :
194         * 
195         * <pre>
196         * assertThatIterable(myCollection).containsInAnyOrderMatching(equalTo(5),
197         *              equalTo(3), equalTo(1));
198         * </pre>
199         * 
200         * <br>
201         * <i>By default, assertion can only be used from the main thread of the
202         * test ; When used from another thread, the assertion will be lost. In the
203         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
204         * annotation is used, the assertion may not be lost, in case the thread use
205         * an assertion method from the test object instance. </i>
206         * 
207         * @param items
208         *            the matcher for each item.
209         * @return true if the assertion is valid ; If the assertion is false,
210         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
211         *         fail the test, else, return false and the test will be failed
212         *         later.
213         * @see Test#fastFail() The documentation of the <code>fastFail</code>
214         *      attribute of the <code>@Test</code> annotation, regarding the action
215         *      done by this assertion.
216         */
217        default boolean containsInAnyOrderMatching(
218                        @SuppressWarnings("unchecked") Matcher<? super T>... items) {
219                return is(Matchers.containsInAnyOrder(items));
220        }
221}