| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 44 |
|
| 45 |
public static AssertionFailureBuilder assertionFailure() { |
| 46 |
return new AssertionFailureBuilder(); |
| 47 |
} |
| 48 |
|
| 49 |
private AssertionFailureBuilder() { |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public AssertionFailureBuilder message(Object message) { |
| 63 |
this.message = message; |
| 64 |
return this; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public AssertionFailureBuilder reason(String reason) { |
| 74 |
this.reason = reason; |
| 75 |
return this; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public AssertionFailureBuilder cause(Throwable cause) { |
| 85 |
this.cause = cause; |
| 86 |
return this; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public AssertionFailureBuilder expected(Object expected) { |
| 96 |
this.mismatch = true; |
| 97 |
this.expected = expected; |
| 98 |
return this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public AssertionFailureBuilder actual(Object actual) { |
| 108 |
this.mismatch = true; |
| 109 |
this.actual = actual; |
| 110 |
return this; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public AssertionFailureBuilder includeValuesInMessage(boolean includeValuesInMessage) { |
| 122 |
this.includeValuesInMessage = includeValuesInMessage; |
| 123 |
return this; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void buildAndThrow() throws AssertionFailedError { |
| 132 |
throw build(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 182 |
if (value == null) { |
| 183 |
return "<null>"; |
| 184 |
} |
| 185 |
String classAndHash = getClassName(value) + toHash(value); |
| 186 |
|
| 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 |
} |