Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionFailureBuilder.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14 import static org.junit.jupiter.api.AssertionUtils.getCanonicalName;
15
16 import java.util.function.Supplier;
17
18 import org.apiguardian.api.API;
19 import org.junit.platform.commons.util.StringUtils;
20 import org.opentest4j.AssertionFailedError;
21
22 /**
23 * Builder for {@link AssertionFailedError AssertionFailedErrors}.
24 * <p>
25 * Using this builder ensures consistency in how failure message are formatted
26 * within JUnit Jupiter and for custom user-defined assertions.
27 *
28 * @since 5.9
29 * @see AssertionFailedError
30 */
31 @API(status = STABLE, since = "5.9")
32 public class AssertionFailureBuilder {
33
34 private Object message;
35 private Throwable cause;
36 private boolean mismatch;
37 private Object expected;
38 private Object actual;
39 private String reason;
40 private boolean includeValuesInMessage = true;
41
42 /**
43 * Create a new {@code AssertionFailureBuilder}.
44 */
45 public static AssertionFailureBuilder assertionFailure() {
46 return new AssertionFailureBuilder();
47 }
48
49 private AssertionFailureBuilder() {
50 }
51
52 /**
53 * Set the user-defined message of the assertion.
54 * <p>
55 * The {@code message} may be passed as a {@link Supplier} or plain
56 * {@link String}. If any other type is passed, it is converted to
57 * {@code String} as per {@link StringUtils#nullSafeToString(Object)}.
58 *
59 * @param message the user-defined failure message; may be {@code null}
60 * @return this builder for method chaining
61 */
62 public AssertionFailureBuilder message(Object message) {
63 this.message = message;
64 return this;
65 }
66
67 /**
68 * Set the reason why the assertion failed.
69 *
70 * @param reason the failure reason; may be {@code null}
71 * @return this builder for method chaining
72 */
73 public AssertionFailureBuilder reason(String reason) {
74 this.reason = reason;
75 return this;
76 }
77
78 /**
79 * Set the cause of the assertion failure.
80 *
81 * @param cause the failure cause; may be {@code null}
82 * @return this builder for method chaining
83 */
84 public AssertionFailureBuilder cause(Throwable cause) {
85 this.cause = cause;
86 return this;
87 }
88
89 /**
90 * Set the expected value of the assertion.
91 *
92 * @param expected the expected value; may be {@code null}
93 * @return this builder for method chaining
94 */
95 public AssertionFailureBuilder expected(Object expected) {
96 this.mismatch = true;
97 this.expected = expected;
98 return this;
99 }
100
101 /**
102 * Set the actual value of the assertion.
103 *
104 * @param actual the actual value; may be {@code null}
105 * @return this builder for method chaining
106 */
107 public AssertionFailureBuilder actual(Object actual) {
108 this.mismatch = true;
109 this.actual = actual;
110 return this;
111 }
112
113 /**
114 * Set whether to include the actual and expected values in the generated
115 * failure message.
116 *
117 * @param includeValuesInMessage whether to include the actual and expected
118 * values
119 * @return this builder for method chaining
120 */
121 public AssertionFailureBuilder includeValuesInMessage(boolean includeValuesInMessage) {
122 this.includeValuesInMessage = includeValuesInMessage;
123 return this;
124 }
125
126 /**
127 * Build the {@link AssertionFailedError AssertionFailedError} and throw it.
128 *
129 * @throws AssertionFailedError always
130 */
131 public void buildAndThrow() throws AssertionFailedError {
132 throw build();
133 }
134
135 /**
136 * Build the {@link AssertionFailedError AssertionFailedError} without
137 * throwing it.
138 *
139 * @return the built assertion failure
140 */
141 public AssertionFailedError build() {
142 String reason = nullSafeGet(this.reason);
143 if (mismatch && includeValuesInMessage) {
144 reason = (reason == null ? "" : reason + ", ") + formatValues(expected, actual);
145 }
146 String message = nullSafeGet(this.message);
147 if (reason != null) {
148 message = buildPrefix(message) + reason;
149 }
150 return mismatch //
151 ? new AssertionFailedError(message, expected, actual, cause) //
152 : new AssertionFailedError(message, cause);
153 }
154
155 private static String nullSafeGet(Object messageOrSupplier) {
156 if (messageOrSupplier == null) {
157 return null;
158 }
159 if (messageOrSupplier instanceof Supplier) {
160 Object message = ((Supplier<?>) messageOrSupplier).get();
161 return StringUtils.nullSafeToString(message);
162 }
163 return StringUtils.nullSafeToString(messageOrSupplier);
164 }
165
166 private static String buildPrefix(String message) {
167 return (StringUtils.isNotBlank(message) ? message + " ==> " : "");
168 }
169
170 private static String formatValues(Object expected, Object actual) {
171 String expectedString = toString(expected);
172 String actualString = toString(actual);
173 if (expectedString.equals(actualString)) {
174 return String.format("expected: %s but was: %s", formatClassAndValue(expected, expectedString),
175 formatClassAndValue(actual, actualString));
176 }
177 return String.format("expected: <%s> but was: <%s>", expectedString, actualString);
178 }
179
180 private static String formatClassAndValue(Object value, String valueString) {
181 // If the value is null, return <null> instead of null<null>.
182 if (value == null) {
183 return "<null>";
184 }
185 String classAndHash = getClassName(value) + toHash(value);
186 // if it's a class, there's no need to repeat the class name contained in the valueString.
187 return (value instanceof Class ? "<" + classAndHash + ">" : classAndHash + "<" + valueString + ">");
188 }
189
190 private static String toString(Object obj) {
191 if (obj instanceof Class) {
192 return getCanonicalName((Class<?>) obj);
193 }
194 return StringUtils.nullSafeToString(obj);
195 }
196
197 private static String toHash(Object obj) {
198 return (obj == null ? "" : "@" + Integer.toHexString(System.identityHashCode(obj)));
199 }
200
201 private static String getClassName(Object obj) {
202 return (obj == null ? "null"
203 : obj instanceof Class ? getCanonicalName((Class<?>) obj) : obj.getClass().getName());
204 }
205 }