001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.assertion;
018
019import java.util.Collection;
020import java.util.Optional;
021import java.util.function.Supplier;
022
023import org.conqat.lib.commons.collections.CollectionUtils;
024import org.conqat.lib.commons.string.StringUtils;
025
026/**
027 * This class provides simple methods to implement assertions. Please refer to
028 * the {@linkplain org.conqat.lib.commons.assertion package documentation} for a
029 * discussion of assertions vs preconditions.
030 */
031public class CCSMAssert {
032
033        /**
034         * Checks if a condition is <code>true</code>.
035         *
036         * @param condition
037         *            condition to check
038         * @param message
039         *            exception message
040         * @throws AssertionError
041         *             if the condition is <code>false</code>
042         */
043        public static void isTrue(boolean condition, String message) throws AssertionError {
044                throwAssertionErrorIfTestFails(condition, message);
045        }
046
047        /**
048         * Checks if a condition is <code>true</code>.
049         *
050         * @param condition
051         *            condition to check
052         * @param messageSupplier
053         *            supplier for the exception message evaluated in case the assertion
054         *            fails
055         * @throws AssertionError
056         *             if the condition is <code>false</code>
057         */
058        public static void isTrue(boolean condition, Supplier<String> messageSupplier) throws AssertionError {
059                throwAssertionErrorIfTestFails(condition, messageSupplier);
060        }
061
062        /**
063         * Checks if a condition is <code>false</code>.
064         *
065         * @param condition
066         *            condition to check
067         * @param message
068         *            exception message
069         * @throws AssertionError
070         *             if the condition is <code>true</code>
071         */
072        public static void isFalse(boolean condition, String message) throws AssertionError {
073                throwAssertionErrorIfTestFails(!condition, message);
074        }
075
076        /**
077         * Checks if a condition is <code>false</code>.
078         *
079         * @param condition
080         *            condition to check
081         * @param messageSupplier
082         *            supplier for the exception message evaluated in case the assertion
083         *            fails
084         * @throws AssertionError
085         *             if the condition is <code>true</code>
086         */
087        public static void isFalse(boolean condition, Supplier<String> messageSupplier) throws AssertionError {
088                throwAssertionErrorIfTestFails(!condition, messageSupplier);
089        }
090
091        /** Checks that the object is a instance of the class. */
092        public static void isInstanceOf(Object object, Class<?> clazz) {
093                CCSMAssert.isNotNull(clazz, () -> "Object " + object + " can't be an instance of class null.");
094                CCSMAssert.isTrue(clazz.isInstance(object), () -> {
095                        // Object could be null. Hence the concatenation with the empty string.
096                        String message = object + "";
097                        if (object != null) {
098                                message += " of type " + object.getClass().getName();
099                        }
100                        return message + " must be an instance of " + clazz.getName();
101                });
102        }
103
104        /**
105         * This calls {@link #isInstanceOf(Object, Class)} and, if this doesn't fail
106         * returns the casted object.
107         */
108        @SuppressWarnings("unchecked")
109        public static <T> T checkedCast(Object object, Class<T> clazz) {
110                isInstanceOf(object, clazz);
111                return (T) object;
112        }
113
114        /**
115         * @throws AssertionError
116         *             with message and throwable
117         */
118        public static void fail(String message, Throwable t) throws AssertionError {
119                throw new AssertionError(message, t);
120        }
121
122        /**
123         * @throws AssertionError
124         *             with message
125         */
126        public static void fail(String message) throws AssertionError {
127                throw new AssertionError(message);
128        }
129
130        /**
131         * Checks whether a reference is <code>null</code>.
132         *
133         * @param reference
134         *            reference to check
135         * @throws AssertionError
136         *             if the reference is <code>null</code>
137         */
138        public static void isNotNull(Object reference) throws AssertionError {
139                isNotNull(reference, "Reference must not be null");
140        }
141
142        /**
143         * Checks whether a reference is <code>null</code>.
144         *
145         * @param reference
146         *            reference to check
147         * @param message
148         *            exception message
149         * @throws AssertionError
150         *             if the reference is <code>null</code>
151         */
152        public static void isNotNull(Object reference, String message) throws AssertionError {
153                throwAssertionErrorIfTestFails(reference != null, message);
154        }
155
156        /**
157         * Checks whether a reference is <code>null</code>.
158         *
159         * @param reference
160         *            reference to check
161         * @param messageSupplier
162         *            supplier for the exception message evaluated in case the assertion
163         *            fails
164         * @throws AssertionError
165         *             if the reference is <code>null</code>
166         */
167        public static void isNotNull(Object reference, Supplier<String> messageSupplier) throws AssertionError {
168                throwAssertionErrorIfTestFails(reference != null, messageSupplier);
169        }
170
171        /** @see #isNotNull(Object, Supplier) */
172        public static void isNotEmpty(String s, String message) throws AssertionError {
173                throwAssertionErrorIfTestFails(!StringUtils.isEmpty(s), message);
174        }
175
176        /**
177         * Checks whether a String is <code>null</code> or empty.
178         *
179         * @param s
180         *            String to check
181         * @param messageSupplier
182         *            supplier for the exception message evaluated in case the assertion
183         *            fails
184         * @throws AssertionError
185         *             if the String is empty
186         */
187        public static void isNotEmpty(String s, Supplier<String> messageSupplier) throws AssertionError {
188                throwAssertionErrorIfTestFails(!StringUtils.isEmpty(s), messageSupplier);
189        }
190
191        /** @see #isNotEmpty(Collection, Supplier) */
192        public static void isNotEmpty(Collection<?> collection, String message) throws AssertionError {
193                throwAssertionErrorIfTestFails(!CollectionUtils.isNullOrEmpty(collection), message);
194        }
195
196        /**
197         * Checks whether a collection is <code>null</code> or empty.
198         *
199         * @param collection
200         *            Collection to check
201         * @param messageSupplier
202         *            supplier for the exception message evaluated in case the assertion
203         *            fails
204         * @throws AssertionError
205         *             if the collection is empty
206         */
207        public static void isNotEmpty(Collection<?> collection, Supplier<String> messageSupplier) throws AssertionError {
208                throwAssertionErrorIfTestFails(!CollectionUtils.isNullOrEmpty(collection), messageSupplier);
209        }
210
211        /** @see #isPresent(Optional, Supplier) */
212        public static void isPresent(Optional<?> optional, String message) {
213                throwAssertionErrorIfTestFails(optional.isPresent(), message);
214        }
215
216        /**
217         * Checks whether an Optional is present.
218         *
219         * @param optional
220         *            Optional to check
221         * @param messageSupplier
222         *            supplier for the exception message evaluated in case the assertion
223         *            fails
224         * @throws AssertionError
225         *             if the Optional is not present
226         */
227        public static void isPresent(Optional<?> optional, Supplier<String> messageSupplier) {
228                throwAssertionErrorIfTestFails(optional.isPresent(), messageSupplier);
229        }
230
231        /**
232         * Throws an {@link AssertionError} if the test fails.
233         *
234         * @param test
235         *            test which should be true
236         * @param messageSupplier
237         *            supplier for a message evaluated only in case the assertion fails
238         * @throws AssertionError
239         *             if the test fails
240         */
241        private static void throwAssertionErrorIfTestFails(boolean test, Supplier<String> messageSupplier) {
242                if (!test) {
243                        throw new AssertionError(messageSupplier.get());
244                }
245        }
246
247        /**
248         * Throws an {@link AssertionError} if the test fails.
249         *
250         * @param test
251         *            test which should be true
252         * @param message
253         *            exception message
254         * @throws AssertionError
255         *             if the test fails
256         */
257        private static void throwAssertionErrorIfTestFails(boolean test, String message) {
258                if (!test) {
259                        throw new AssertionError(message);
260                }
261        }
262}