Skip to content

Commit

Permalink
prepare for isElementOfCustomAssert becoming protected - see #203
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed Nov 5, 2020
1 parent ab90c1d commit 18e329e
Showing 1 changed file with 7 additions and 43 deletions.
Expand Up @@ -14,19 +14,9 @@
package org.xmlunit.assertj3;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.error.AssertionErrorFactory;
import org.assertj.core.internal.Failures;

import java.util.List;

import static org.assertj.core.util.Lists.newArrayList;

/**
* AbstractAssert allow only to throw errors extending ErrorMessageFactory.
* CustomAbstractAssert allow to throw errors that based on AssertionErrorFactory.
*
* @see AbstractAssert#throwAssertionError(org.assertj.core.error.ErrorMessageFactory)
* @see org.xmlunit.assertj3.error.ComparisonFailureErrorFactory
* Ensures XMLUnit's stack trace lines get removed from AssertionError's stack traces if those of AssertJ are removed.
* @since XMLUnit 2.8.1
*/
abstract class CustomAbstractAssert<SELF extends CustomAbstractAssert<SELF, ACTUAL>, ACTUAL> extends AbstractAssert<SELF, ACTUAL> {
Expand All @@ -37,39 +27,13 @@ abstract class CustomAbstractAssert<SELF extends CustomAbstractAssert<SELF, ACTU
super(actual, selfType);
}

void throwAssertionError(AssertionErrorFactory assertionErrorFactory) {
AssertionError assertionError = assertionErrorFactory.newAssertionError(info.description(), info.representation());
Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError);
removeCustomAssertRelatedElementsFromStackTraceIfNeeded(assertionError);
throw assertionError;
}

private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) {

if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return;

List<StackTraceElement> filtered = newArrayList(assertionError.getStackTrace());
for (StackTraceElement element : assertionError.getStackTrace()) {
if (isElementOfCustomAssert(element)) {
filtered.remove(element);
}
}
StackTraceElement[] newStackTrace = filtered.toArray(new StackTraceElement[0]);
assertionError.setStackTrace(newStackTrace);
}

private boolean isElementOfCustomAssert(StackTraceElement stackTraceElement) {

Class<?> currentAssertClass = getClass();
while (currentAssertClass != AbstractAssert.class) {
if (stackTraceElement.getClassName().equals(currentAssertClass.getName())) {
return true;
}
if (stackTraceElement.getClassName().contains(ORG_XMLUNIT_ASSERTJ_ERROR)) {
return true;
}
currentAssertClass = currentAssertClass.getSuperclass();
// @Override
protected boolean isElementOfCustomAssert(final StackTraceElement stackTraceElement) {
if (stackTraceElement.getClassName().contains(ORG_XMLUNIT_ASSERTJ_ERROR)) {
return true;
}
// return super.isElementOfCustomAssert(stackTraceElement);
return false;
}

}

0 comments on commit 18e329e

Please sign in to comment.