Skip to content

Commit

Permalink
Issue ota4j-team#21: Alternative: Introduce AssertionError again
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mmichaelis committed Jul 24, 2016
1 parent 4cfa5ef commit f93986f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
32 changes: 24 additions & 8 deletions src/main/java/org/opentest4j/AssertionFailedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.opentest4j;

import org.opentest4j.debug.DebugInformation;
import org.opentest4j.debug.DebuggableObject;

/**
* <p>
Expand All @@ -35,39 +36,54 @@
* @author Mark Michaelis
* @since 1.0
*/
public class AssertionFailedException extends DebuggableException {
public class AssertionFailedException extends AssertionError implements DebuggableObject {

private static final long serialVersionUID = 1L;
private final DebugInformation debugInformation;

public AssertionFailedException() {
this((DebugInformation) null);
}

public AssertionFailedException(String message) {
super(message);
this(message, (DebugInformation) null);
}

public AssertionFailedException(String message, Throwable cause) {
super(message, cause);
this(message, null, cause);
}

public AssertionFailedException(Throwable cause) {
super(cause);
this((DebugInformation) null, cause);
}

public AssertionFailedException(DebugInformation debugInformation) {
super(debugInformation);
this.debugInformation = debugInformation;
}

public AssertionFailedException(String message, DebugInformation debugInformation) {
super(message, debugInformation);
super(message);
this.debugInformation = debugInformation;
}

public AssertionFailedException(String message, DebugInformation debugInformation, Throwable cause) {
super(message, debugInformation, cause);
super(message);
this.initCause(cause);
this.debugInformation = debugInformation;
}

public AssertionFailedException(DebugInformation debugInformation, Throwable cause) {
super(debugInformation, cause);
super(cause);
this.debugInformation = debugInformation;
}

@Override
public DebugInformation getDebugInformation() {
return this.debugInformation;
}

@Override
public boolean hasDebugInformation() {
return this.debugInformation != null;
}
}
28 changes: 15 additions & 13 deletions src/test/java/org/opentest4j/ExceptionTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void assertBehavesLikeStandardException(Class<? extends Throwable>
assertMessageCauseConstructorBehavesAsExpected(exceptionClass);
}

public static void assertBehavesLikeDebuggableException(Class<? extends DebuggableException> exceptionClass)
public static void assertBehavesLikeDebuggableException(Class<? extends Throwable> exceptionClass)
throws Exception {
assertBehavesLikeStandardException(exceptionClass);

Expand Down Expand Up @@ -126,22 +126,24 @@ private static void assertMessageCauseConstructorBehavesAsExpected(Class<? exten
//------------------------------------------------------------------------------------------------------------------

private static void assertOnlyDebugInformationConstructorBehavesAsExpected(
Class<? extends DebuggableException> exceptionClass) throws Exception {
DebuggableException exceptionInstance = exceptionClass.getConstructor(DebugInformation.class).newInstance(
Class<? extends Throwable> exceptionClass) throws Exception {
Throwable exceptionInstance = exceptionClass.getConstructor(DebugInformation.class).newInstance(
DEBUG_INFORMATION_REFERENCE);

assertExceptionSimilarTo("only debuginformation", EXCEPTION_REFERENCE_NO_ARG, exceptionInstance);
assertCanInitCause(exceptionInstance);
assertTrue("hasDebugInformation", exceptionInstance.hasDebugInformation());
assertSame("debugInformation", DEBUG_INFORMATION_REFERENCE, exceptionInstance.getDebugInformation());
assertTrue("instance of DebuggableObject", exceptionInstance instanceof DebuggableObject);
DebuggableObject debuggableObject = (DebuggableObject) exceptionInstance;
assertTrue("hasDebugInformation", debuggableObject.hasDebugInformation());
assertSame("debugInformation", DEBUG_INFORMATION_REFERENCE, debuggableObject.getDebugInformation());

DebuggableException deserializedInstance = exceptionClass.cast(
Throwable deserializedInstance = exceptionClass.cast(
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
}

private static <T extends DebuggableException> void assertDebugInformationCauseConstructorBehavesAsExpected(
private static <T extends Throwable> void assertDebugInformationCauseConstructorBehavesAsExpected(
Class<T> exceptionClass) throws Exception {
T exceptionInstance = exceptionClass.getConstructor(DebugInformation.class, Throwable.class).newInstance(
DEBUG_INFORMATION_REFERENCE, CAUSE_EXCEPTION);
Expand All @@ -152,10 +154,10 @@ private static <T extends DebuggableException> void assertDebugInformationCauseC
T deserializedInstance = exceptionClass.cast(
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
}

private static <T extends DebuggableException> void assertMessageDebugInformationConstructorBehavesAsExpected(
private static <T extends Throwable> void assertMessageDebugInformationConstructorBehavesAsExpected(
Class<T> exceptionClass) throws Exception {
T exceptionInstance = exceptionClass.getConstructor(String.class, DebugInformation.class).newInstance(
MESSAGE_REFERENCE, DEBUG_INFORMATION_REFERENCE);
Expand All @@ -166,10 +168,10 @@ private static <T extends DebuggableException> void assertMessageDebugInformatio
T deserializedInstance = exceptionClass.cast(
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
}

private static <T extends DebuggableException> void assertMessageDebugInformationCauseConstructorBehavesAsExpected(
private static <T extends Throwable> void assertMessageDebugInformationCauseConstructorBehavesAsExpected(
Class<T> exceptionClass) throws Exception {
T exceptionInstance = exceptionClass.getConstructor(String.class, DebugInformation.class,
Throwable.class).newInstance(MESSAGE_REFERENCE, DEBUG_INFORMATION_REFERENCE, CAUSE_EXCEPTION);
Expand All @@ -181,7 +183,7 @@ private static <T extends DebuggableException> void assertMessageDebugInformatio
T deserializedInstance = exceptionClass.cast(
TestSerializationHelper.serializeAndDeserialize(exceptionInstance));
assertExceptionEqualTo("serialization", exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation(exceptionInstance, deserializedInstance);
assertHaveEqualDebugInformation((DebuggableObject) exceptionInstance, (DebuggableObject) deserializedInstance);
}

//</editor-fold>
Expand Down

0 comments on commit f93986f

Please sign in to comment.