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 for assertion on object.
027 * <p>
028 * This interface is returned by the various methods
029 * {@link TestSuite#assertThat(Object) assertThat} exposed by {@link TestSuite}.
030 *
031 * @author borettim
032 * @param <T>
033 *            the object type.
034 */
035public interface AssertThatObject<T> {
036
037        /**
038         * Check that the object is matching the passed matcher.
039         * <p>
040         * <br>
041         * <i>By default, assertion can only be used from the main thread of the
042         * test ; When used from another thread, the assertion will be lost. In the
043         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
044         * annotation is used, the assertion may not be lost, in case the thread use
045         * an assertion method from the test object instance. </i>
046         * 
047         * @param matching
048         *            the matcher.
049         * @return true if the assertion is valid ; If the assertion is false,
050         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
051         *         fail the test, else, return false and the test will be failed
052         *         later.
053         * @see Test#fastFail() The documentation of the <code>fastFail</code>
054         *      attribute of the <code>@Test</code> annotation, regarding the action
055         *      done by this assertion.
056         */
057        boolean is(Matcher<? super T> matching);
058
059        /**
060         * Check that the object is the passed one (shortcut to Matcher.equalTo).
061         * <p>
062         * This method is a shortcut to <code>is(equalTo(compareTo))</code>. <br>
063         * <br>
064         * <i>By default, assertion can only be used from the main thread of the
065         * test ; When used from another thread, the assertion will be lost. In the
066         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
067         * annotation is used, the assertion may not be lost, in case the thread use
068         * an assertion method from the test object instance. </i>
069         * 
070         * @param compareTo
071         *            the value.
072         * @return true if the assertion is valid ; If the assertion is false,
073         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
074         *         fail the test, else, return false and the test will be failed
075         *         later.
076         * @see Test#fastFail() The documentation of the <code>fastFail</code>
077         *      attribute of the <code>@Test</code> annotation, regarding the action
078         *      done by this assertion.
079         */
080        default boolean is(T compareTo) {
081                return is(Matchers.equalTo(compareTo));
082        }
083
084        /**
085         * Check that the object is not the passed one.
086         * <p>
087         * This method is a shortcut to <code>isNot(equalTo(compareTo))</code>. <br>
088         * <br>
089         * <i>By default, assertion can only be used from the main thread of the
090         * test ; When used from another thread, the assertion will be lost. In the
091         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
092         * annotation is used, the assertion may not be lost, in case the thread use
093         * an assertion method from the test object instance. </i>
094         * 
095         * @param compareTo
096         *            the value
097         * @return true if the assertion is valid ; If the assertion is false,
098         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
099         *         fail the test, else, return false and the test will be failed
100         *         later.
101         * @see Test#fastFail() The documentation of the <code>fastFail</code>
102         *      attribute of the <code>@Test</code> annotation, regarding the action
103         *      done by this assertion.
104         */
105        default boolean isNot(T compareTo) {
106                return isNot(Matchers.equalTo(compareTo));
107        }
108
109        /**
110         * Check that the object is not matching the passed matcher.
111         * <p>
112         * This method is a shortcut to <code>is(not(matching))</code>. <br>
113         * <br>
114         * <i>By default, assertion can only be used from the main thread of the
115         * test ; When used from another thread, the assertion will be lost. In the
116         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
117         * annotation is used, the assertion may not be lost, in case the thread use
118         * an assertion method from the test object instance. </i>
119         * 
120         * @param matching
121         *            the matcher
122         * @return true if the assertion is valid ; If the assertion is false,
123         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
124         *         fail the test, else, return false and the test will be failed
125         *         later.
126         * @see Test#fastFail() The documentation of the <code>fastFail</code>
127         *      attribute of the <code>@Test</code> annotation, regarding the action
128         *      done by this assertion.
129         */
130        default boolean isNot(Matcher<T> matching) {
131                return is(Matchers.not(matching));
132        }
133
134        /**
135         * Check that the object is null.
136         * <p>
137         * This method is a shortcut to <code>is(nullValue())</code>. <br>
138         * <br>
139         * <i>By default, assertion can only be used from the main thread of the
140         * test ; When used from another thread, the assertion will be lost. In the
141         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
142         * annotation is used, the assertion may not be lost, in case the thread use
143         * an assertion method from the test object instance. </i>
144         * 
145         * @return true if the assertion is valid ; If the assertion is false,
146         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
147         *         fail the test, else, return false and the test will be failed
148         *         later.
149         * @see Test#fastFail() The documentation of the <code>fastFail</code>
150         *      attribute of the <code>@Test</code> annotation, regarding the action
151         *      done by this assertion.
152         */
153        default boolean isNull() {
154                return is(Matchers.nullValue());
155        }
156
157        /**
158         * Check that the object is not null.
159         * <p>
160         * This method is a shortcut to <code>is(notNullValue())</code>. <br>
161         * <br>
162         * <i>By default, assertion can only be used from the main thread of the
163         * test ; When used from another thread, the assertion will be lost. In the
164         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
165         * annotation is used, the assertion may not be lost, in case the thread use
166         * an assertion method from the test object instance. </i>
167         * 
168         * @return true if the assertion is valid ; If the assertion is false,
169         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
170         *         fail the test, else, return false and the test will be failed
171         *         later.
172         * @see Test#fastFail() The documentation of the <code>fastFail</code>
173         *      attribute of the <code>@Test</code> annotation, regarding the action
174         *      done by this assertion.
175         */
176        default boolean isNotNull() {
177                return is(Matchers.notNullValue());
178        }
179
180        /**
181         * Check that the object is of a specific class (or subclasses).
182         * <p>
183         * This method is a shortcut to <code>is(isA(clazz))</code>. <br>
184         * <br>
185         * <i>By default, assertion can only be used from the main thread of the
186         * test ; When used from another thread, the assertion will be lost. In the
187         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
188         * annotation is used, the assertion may not be lost, in case the thread use
189         * an assertion method from the test object instance. </i>
190         * 
191         * @param clazz
192         *            the class
193         * @return true if the assertion is valid ; If the assertion is false,
194         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
195         *         fail the test, else, return false and the test will be failed
196         *         later.
197         * @see Test#fastFail() The documentation of the <code>fastFail</code>
198         *      attribute of the <code>@Test</code> annotation, regarding the action
199         *      done by this assertion.
200         */
201        default boolean isA(Class<? super T> clazz) {
202                return is(Matchers.isA(clazz));
203        }
204
205}