[Junit|Test] cleanup: use field and method name constants for accessing them by comparison failure

Issues IDEA-359095, IDEA-353644

GitOrigin-RevId: e0f5c5098c3e2b902944cd520f481ba865f28273
This commit is contained in:
Sergei Vorobyov
2024-10-11 12:48:22 +02:00
committed by intellij-monorepo-bot
parent 386665b49e
commit 84e0f088dc

View File

@@ -16,13 +16,26 @@ public class ComparisonFailureData {
public static final String JUNIT_3_COMPARISON_FAILURE = "junit.framework.ComparisonFailure";
public static final String JUNIT_4_COMPARISON_FAILURE = "org.junit.ComparisonFailure";
private static final String JUNIT_COMPARISON_FAILURE_EXPECTED = "fExpected";
private static final String JUNIT_COMPARISON_FAILURE_ACTUAL = "fActual";
private static final String ASSERTION_CLASS_NAME = "java.lang.AssertionError";
private static final String ASSERTION_FAILED_CLASS_NAME = "junit.framework.AssertionFailedError";
private static final String OPENTEST4J_ASSERTION = "org.opentest4j.AssertionFailedError";
private static final String OPENTEST4J_ASSERTION_EXPECTED_DEFINED = "isExpectedDefined";
private static final String OPENTEST4J_ASSERTION_ACTUAL_DEFINED = "isActualDefined";
private static final String OPENTEST4J_ASSERTION_EXPECTED = "getExpected";
private static final String OPENTEST4J_ASSERTION_ACTUAL = "getActual";
private static final String OPENTEST4J_VALUE_WRAPPER = "org.opentest4j.ValueWrapper";
private static final String OPENTEST4J_VALUE_WRAPPER_VALUE = "getValue";
private static final String OPENTEST4J_VALUE_WRAPPER_STRING_REPRESENTATION = "getStringRepresentation";
private static final String OPENTEST4J_FILE_INFO = "org.opentest4j.FileInfo";
private static final String OPENTEST4J_FILE_INFO_PATH = "getPath";
private static final String OPENTEST4J_FILE_INFO_CONTENT_AS_STRING = "getContentsAsString";
private static final Charset OPENTEST4J_FILE_CONTENT_CHARSET = StandardCharsets.UTF_8;
private final String myExpected;
@@ -43,11 +56,11 @@ public class ComparisonFailureData {
private static void init(String exceptionClassName) throws NoSuchFieldException, ClassNotFoundException {
Class<?> exceptionClass = Class.forName(exceptionClassName, false, ComparisonFailureData.class.getClassLoader());
final Field expectedField = exceptionClass.getDeclaredField("fExpected");
final Field expectedField = exceptionClass.getDeclaredField(JUNIT_COMPARISON_FAILURE_EXPECTED);
expectedField.setAccessible(true);
EXPECTED.put(exceptionClass, expectedField);
final Field actualField = exceptionClass.getDeclaredField("fActual");
final Field actualField = exceptionClass.getDeclaredField(JUNIT_COMPARISON_FAILURE_ACTUAL);
actualField.setAccessible(true);
ACTUAL.put(exceptionClass, actualField);
}
@@ -234,13 +247,13 @@ public class ComparisonFailureData {
private static ComparisonFailureData createOpentest4jAssertion(Throwable assertion) {
try {
if (isInstance(assertion.getClass(), OPENTEST4J_ASSERTION)) {
Method isExpectedDefinedMethod = assertion.getClass().getMethod("isExpectedDefined");
Method isActualDefinedMethod = assertion.getClass().getMethod("isActualDefined");
Method isExpectedDefinedMethod = assertion.getClass().getMethod(OPENTEST4J_ASSERTION_EXPECTED_DEFINED);
Method isActualDefinedMethod = assertion.getClass().getMethod(OPENTEST4J_ASSERTION_ACTUAL_DEFINED);
boolean isExpectedDefined = ((Boolean)isExpectedDefinedMethod.invoke(assertion)).booleanValue();
boolean isActualDefined = ((Boolean)isActualDefinedMethod.invoke(assertion)).booleanValue();
if (isExpectedDefined && isActualDefined) {
Method expectedMethod = assertion.getClass().getMethod("getExpected");
Method actualMethod = assertion.getClass().getMethod("getActual");
Method expectedMethod = assertion.getClass().getMethod(OPENTEST4J_ASSERTION_EXPECTED);
Method actualMethod = assertion.getClass().getMethod(OPENTEST4J_ASSERTION_ACTUAL);
Object expectedValueWrapper = expectedMethod.invoke(assertion);
Object actualValueWrapper = actualMethod.invoke(assertion);
AssertionValue expected = getOpentest4jAssertionValue(expectedValueWrapper);
@@ -258,17 +271,17 @@ public class ComparisonFailureData {
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (isInstance(valueWrapper.getClass(), OPENTEST4J_VALUE_WRAPPER)) {
Method valueMethod = valueWrapper.getClass().getMethod("getValue");
Method valueMethod = valueWrapper.getClass().getMethod(OPENTEST4J_VALUE_WRAPPER_VALUE);
Object value = valueMethod.invoke(valueWrapper);
if (value != null && isInstance(value.getClass(), OPENTEST4J_FILE_INFO)) {
Method contentAsStringMethod = value.getClass().getMethod("getContentsAsString", Charset.class);
Method contentAsStringMethod = value.getClass().getMethod(OPENTEST4J_FILE_INFO_CONTENT_AS_STRING, Charset.class);
String valueString = (String)contentAsStringMethod.invoke(value, OPENTEST4J_FILE_CONTENT_CHARSET);
Method pathMethod = value.getClass().getMethod("getPath");
Method pathMethod = value.getClass().getMethod(OPENTEST4J_FILE_INFO_PATH);
String valuePath = (String)pathMethod.invoke(value);
return new AssertionValue(valueString, valuePath);
}
else {
Method stringRepresentationMethod = valueWrapper.getClass().getMethod("getStringRepresentation");
Method stringRepresentationMethod = valueWrapper.getClass().getMethod(OPENTEST4J_VALUE_WRAPPER_STRING_REPRESENTATION);
String valueString = (String)stringRepresentationMethod.invoke(valueWrapper);
return new AssertionValue(valueString, null);
}
@@ -288,11 +301,11 @@ public class ComparisonFailureData {
}
public static String getActual(Throwable assertion) throws IllegalAccessException, NoSuchFieldException {
return get(assertion, ACTUAL, "fActual");
return get(assertion, ACTUAL, JUNIT_COMPARISON_FAILURE_ACTUAL);
}
public static String getExpected(Throwable assertion) throws IllegalAccessException, NoSuchFieldException {
return get(assertion, EXPECTED, "fExpected");
return get(assertion, EXPECTED, JUNIT_COMPARISON_FAILURE_EXPECTED);
}
private static String get(final Throwable assertion, final Map<Class<?>, Field> staticMap, final String fieldName) throws IllegalAccessException, NoSuchFieldException {