Skip to content

Commit f93986f

Browse files
committed
Issue ota4j-team#21: Alternative: Introduce AssertionError again
As the decision about ota4j-team#4 is not yet clear and some points (support of old frameworks/tests) this is an alternative introducing the AssertionError again to the hierarchy... with the drawback of duplicated code.
1 parent 4cfa5ef commit f93986f

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/main/java/org/opentest4j/AssertionFailedException.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.opentest4j;
1818

1919
import org.opentest4j.debug.DebugInformation;
20+
import org.opentest4j.debug.DebuggableObject;
2021

2122
/**
2223
* <p>
@@ -35,39 +36,54 @@
3536
* @author Mark Michaelis
3637
* @since 1.0
3738
*/
38-
public class AssertionFailedException extends DebuggableException {
39+
public class AssertionFailedException extends AssertionError implements DebuggableObject {
3940

4041
private static final long serialVersionUID = 1L;
42+
private final DebugInformation debugInformation;
4143

4244
public AssertionFailedException() {
45+
this((DebugInformation) null);
4346
}
4447

4548
public AssertionFailedException(String message) {
46-
super(message);
49+
this(message, (DebugInformation) null);
4750
}
4851

4952
public AssertionFailedException(String message, Throwable cause) {
50-
super(message, cause);
53+
this(message, null, cause);
5154
}
5255

5356
public AssertionFailedException(Throwable cause) {
54-
super(cause);
57+
this((DebugInformation) null, cause);
5558
}
5659

5760
public AssertionFailedException(DebugInformation debugInformation) {
58-
super(debugInformation);
61+
this.debugInformation = debugInformation;
5962
}
6063

6164
public AssertionFailedException(String message, DebugInformation debugInformation) {
62-
super(message, debugInformation);
65+
super(message);
66+
this.debugInformation = debugInformation;
6367
}
6468

6569
public AssertionFailedException(String message, DebugInformation debugInformation, Throwable cause) {
66-
super(message, debugInformation, cause);
70+
super(message);
71+
this.initCause(cause);
72+
this.debugInformation = debugInformation;
6773
}
6874

6975
public AssertionFailedException(DebugInformation debugInformation, Throwable cause) {
70-
super(debugInformation, cause);
76+
super(cause);
77+
this.debugInformation = debugInformation;
78+
}
79+
80+
@Override
81+
public DebugInformation getDebugInformation() {
82+
return this.debugInformation;
7183
}
7284

85+
@Override
86+
public boolean hasDebugInformation() {
87+
return this.debugInformation != null;
88+
}
7389
}

src/test/java/org/opentest4j/ExceptionTestHelper.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void assertBehavesLikeStandardException(Class<? extends Throwable>
5252
assertMessageCauseConstructorBehavesAsExpected(exceptionClass);
5353
}
5454

55-
public static void assertBehavesLikeDebuggableException(Class<? extends DebuggableException> exceptionClass)
55+
public static void assertBehavesLikeDebuggableException(Class<? extends Throwable> exceptionClass)
5656
throws Exception {
5757
assertBehavesLikeStandardException(exceptionClass);
5858

@@ -126,22 +126,24 @@ private static void assertMessageCauseConstructorBehavesAsExpected(Class<? exten
126126
//------------------------------------------------------------------------------------------------------------------
127127

128128
private static void assertOnlyDebugInformationConstructorBehavesAsExpected(
129-
Class<? extends DebuggableException> exceptionClass) throws Exception {
130-
DebuggableException exceptionInstance = exceptionClass.getConstructor(DebugInformation.class).newInstance(
129+
Class<? extends Throwable> exceptionClass) throws Exception {
130+
Throwable exceptionInstance = exceptionClass.getConstructor(DebugInformation.class).newInstance(
131131
DEBUG_INFORMATION_REFERENCE);
132132

133133
assertExceptionSimilarTo("only debuginformation", EXCEPTION_REFERENCE_NO_ARG, exceptionInstance);
134134
assertCanInitCause(exceptionInstance);
135-
assertTrue("hasDebugInformation", exceptionInstance.hasDebugInformation());
136-
assertSame("debugInformation", DEBUG_INFORMATION_REFERENCE, exceptionInstance.getDebugInformation());
135+
assertTrue("instance of DebuggableObject", exceptionInstance instanceof DebuggableObject);
136+
DebuggableObject debuggableObject = (DebuggableObject) exceptionInstance;
137+
assertTrue("hasDebugInformation", debuggableObject.hasDebugInformation());
138+
assertSame("debugInformation", DEBUG_INFORMATION_REFERENCE, debuggableObject.getDebugInformation());
137139

138-
DebuggableException deserializedInstance = exceptionClass.cast(
140+
Throwable deserializedInstance = exceptionClass.cast(
139141
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
140142
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
141-
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
143+
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
142144
}
143145

144-
private static <T extends DebuggableException> void assertDebugInformationCauseConstructorBehavesAsExpected(
146+
private static <T extends Throwable> void assertDebugInformationCauseConstructorBehavesAsExpected(
145147
Class<T> exceptionClass) throws Exception {
146148
T exceptionInstance = exceptionClass.getConstructor(DebugInformation.class, Throwable.class).newInstance(
147149
DEBUG_INFORMATION_REFERENCE, CAUSE_EXCEPTION);
@@ -152,10 +154,10 @@ private static <T extends DebuggableException> void assertDebugInformationCauseC
152154
T deserializedInstance = exceptionClass.cast(
153155
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
154156
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
155-
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
157+
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
156158
}
157159

158-
private static <T extends DebuggableException> void assertMessageDebugInformationConstructorBehavesAsExpected(
160+
private static <T extends Throwable> void assertMessageDebugInformationConstructorBehavesAsExpected(
159161
Class<T> exceptionClass) throws Exception {
160162
T exceptionInstance = exceptionClass.getConstructor(String.class, DebugInformation.class).newInstance(
161163
MESSAGE_REFERENCE, DEBUG_INFORMATION_REFERENCE);
@@ -166,10 +168,10 @@ private static <T extends DebuggableException> void assertMessageDebugInformatio
166168
T deserializedInstance = exceptionClass.cast(
167169
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
168170
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
169-
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
171+
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
170172
}
171173

172-
private static <T extends DebuggableException> void assertMessageDebugInformationCauseConstructorBehavesAsExpected(
174+
private static <T extends Throwable> void assertMessageDebugInformationCauseConstructorBehavesAsExpected(
173175
Class<T> exceptionClass) throws Exception {
174176
T exceptionInstance = exceptionClass.getConstructor(String.class, DebugInformation.class,
175177
Throwable.class).newInstance(MESSAGE_REFERENCE, DEBUG_INFORMATION_REFERENCE, CAUSE_EXCEPTION);
@@ -181,7 +183,7 @@ private static <T extends DebuggableException> void assertMessageDebugInformatio
181183
T deserializedInstance = exceptionClass.cast(
182184
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
183185
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
184-
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
186+
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
185187
}
186188

187189
//</editor-fold>

0 commit comments

Comments
 (0)