001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.util;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.ObjectUtils;
021
022/**
023 * Test assertions that are independent of any third-party assertion library.
024 *
025 * @author Lukas Krecan
026 * @author Arjen Poutsma
027 * @author Sam Brannen
028 * @since 3.2
029 */
030public abstract class AssertionErrors {
031
032        /**
033         * Fail a test with the given message.
034         * @param message a message that describes the reason for the failure
035         */
036        public static void fail(String message) {
037                throw new AssertionError(message);
038        }
039
040        /**
041         * Fail a test with the given message passing along expected and actual
042         * values to be appended to the message.
043         * <p>For example given:
044         * <pre class="code">
045         * String name = "Accept";
046         * String expected = "application/json";
047         * String actual = "text/plain";
048         * fail("Response header [" + name + "]", expected, actual);
049         * </pre>
050         * <p>The resulting message is:
051         * <pre class="code">
052         * Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt;
053         * </pre>
054         * @param message a message that describes the use case that failed
055         * @param expected the expected value
056         * @param actual the actual value
057         */
058        public static void fail(String message, @Nullable Object expected, @Nullable Object actual) {
059                throw new AssertionError(message + " expected:<" + expected + "> but was:<" + actual + ">");
060        }
061
062        /**
063         * Assert the given condition is {@code true} and raise an
064         * {@link AssertionError} otherwise.
065         * @param message a message that describes the reason for the failure
066         * @param condition the condition to test for
067         */
068        public static void assertTrue(String message, boolean condition) {
069                if (!condition) {
070                        fail(message);
071                }
072        }
073
074        /**
075         * Assert the given condition is {@code false} and raise an
076         * {@link AssertionError} otherwise.
077         * @param message a message that describes the reason for the failure
078         * @param condition the condition to test for
079         * @since 5.2.1
080         */
081        public static void assertFalse(String message, boolean condition) {
082                if (condition) {
083                        fail(message);
084                }
085        }
086
087        /**
088         * Assert that the given object is {@code null} and raise an
089         * {@link AssertionError} otherwise.
090         * @param message a message that describes the reason for the failure
091         * @param object the object to check
092         * @since 5.2.1
093         */
094        public static void assertNull(String message, @Nullable Object object) {
095                assertTrue(message, object == null);
096        }
097
098        /**
099         * Assert that the given object is not {@code null} and raise an
100         * {@link AssertionError} otherwise.
101         * @param message a message that describes the reason for the failure
102         * @param object the object to check
103         * @since 5.1.8
104         */
105        public static void assertNotNull(String message, @Nullable Object object) {
106                assertTrue(message, object != null);
107        }
108
109        /**
110         * Assert two objects are equal and raise an {@link AssertionError} otherwise.
111         * <p>For example:
112         * <pre class="code">
113         * assertEquals("Response header [" + name + "]", expected, actual);
114         * </pre>
115         * @param message a message that describes the value being checked
116         * @param expected the expected value
117         * @param actual the actual value
118         * @see #fail(String, Object, Object)
119         */
120        public static void assertEquals(String message, @Nullable Object expected, @Nullable Object actual) {
121                if (!ObjectUtils.nullSafeEquals(expected, actual)) {
122                        fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual));
123                }
124        }
125
126        /**
127         * Assert two objects are not equal and raise an {@link AssertionError} otherwise.
128         * <p>For example:
129         * <pre class="code">
130         * assertNotEquals("Response header [" + name + "]", expected, actual);
131         * </pre>
132         * @param message a message that describes the value being checked
133         * @param expected the expected value
134         * @param actual the actual value
135         */
136        public static void assertNotEquals(String message, @Nullable Object expected, @Nullable Object actual) {
137                if (ObjectUtils.nullSafeEquals(expected, actual)) {
138                        throw new AssertionError(message + " was not expected to be:" +
139                                        "<" + ObjectUtils.nullSafeToString(actual) + ">");
140                }
141        }
142
143}