Skip to content

Commit 0217d79

Browse files
authored
add rich actual/expected assertion details to test results (via #1273)
1 parent 7625d8c commit 0217d79

18 files changed

Lines changed: 854 additions & 43 deletions

File tree

allure-java-commons/src/main/java/io/qameta/allure/util/NamingUtils.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020

21-
import java.lang.reflect.Field;
2221
import java.util.Map;
2322
import java.util.Objects;
2423
import java.util.Optional;
@@ -94,42 +93,12 @@ private static String extractProperties(final Object object, final String[] part
9493
}
9594

9695
private static Object extractChild(final Object object, final String part) {
97-
final Class<?> type = object == null ? Object.class : object.getClass();
96+
Objects.requireNonNull(object, "object");
97+
final Class<?> type = object.getClass();
9898
try {
99-
return extractField(object, part, type);
99+
return ReflectionUtils.getFieldValue(object, part);
100100
} catch (ReflectiveOperationException e) {
101101
throw new IllegalStateException("Unable to extract " + part + " value from " + type.getName(), e);
102102
}
103103
}
104-
105-
@SuppressWarnings("PMD.EmptyCatchBlock")
106-
private static Object extractField(final Object object, final String part, final Class<?> type)
107-
throws ReflectiveOperationException {
108-
try {
109-
final Field field = type.getField(part);
110-
return fieldValue(object, field);
111-
} catch (NoSuchFieldException e) {
112-
Class<?> t = type;
113-
while (t != null) {
114-
try {
115-
final Field declaredField = t.getDeclaredField(part);
116-
return fieldValue(object, declaredField);
117-
} catch (NoSuchFieldException ignore) {
118-
// Ignore
119-
}
120-
t = t.getSuperclass();
121-
}
122-
throw e;
123-
}
124-
}
125-
126-
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
127-
private static Object fieldValue(final Object object, final Field field) throws IllegalAccessException {
128-
try {
129-
return field.get(object);
130-
} catch (IllegalAccessException e) {
131-
field.setAccessible(true);
132-
return field.get(object);
133-
}
134-
}
135104
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright 2016-2026 Qameta Software Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.util;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.Method;
23+
import java.util.Optional;
24+
25+
/**
26+
* Common reflection helpers.
27+
*/
28+
final class ReflectionUtils {
29+
30+
private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtils.class);
31+
private static final String GET_PREFIX = "get";
32+
private static final String IS_PREFIX = "is";
33+
private static final String METHOD_READ_ERROR = "Unable to read value via method {}";
34+
private static final String FIELD_READ_ERROR = "Unable to read value via field {}";
35+
36+
private ReflectionUtils() {
37+
throw new IllegalStateException("Do not instance");
38+
}
39+
40+
static Object getValue(final Object target, final String propertyName) {
41+
if (target == null || propertyName == null || propertyName.isEmpty()) {
42+
return null;
43+
}
44+
final Optional<Method> method = findGetter(target.getClass(), propertyName);
45+
if (method.isPresent()) {
46+
try {
47+
return invokeMethod(target, method.get());
48+
} catch (ReflectiveOperationException | SecurityException e) {
49+
LOGGER.trace(METHOD_READ_ERROR, method.get().getName(), e);
50+
return null;
51+
}
52+
}
53+
try {
54+
return getFieldValue(target, propertyName);
55+
} catch (ReflectiveOperationException | SecurityException e) {
56+
LOGGER.trace(FIELD_READ_ERROR, propertyName, e);
57+
return null;
58+
}
59+
}
60+
61+
static Boolean getBooleanValue(final Object target, final String propertyName) {
62+
if (target == null || propertyName == null || propertyName.isEmpty()) {
63+
return absentBooleanValue();
64+
}
65+
final Optional<Method> method = findPrimitiveBooleanGetter(target.getClass(), propertyName);
66+
if (method.isPresent()) {
67+
return invokeBooleanMethod(target, method.get());
68+
}
69+
return toBooleanValue(getValue(target, propertyName), propertyName);
70+
}
71+
72+
@SuppressWarnings("PMD.EmptyCatchBlock")
73+
static Object getFieldValue(final Object target, final String fieldName) throws ReflectiveOperationException {
74+
final Class<?> type = target.getClass();
75+
try {
76+
final Field field = type.getField(fieldName);
77+
return getFieldValue(target, field);
78+
} catch (NoSuchFieldException e) {
79+
Class<?> currentType = type;
80+
while (currentType != null) {
81+
try {
82+
final Field declaredField = currentType.getDeclaredField(fieldName);
83+
return getFieldValue(target, declaredField);
84+
} catch (NoSuchFieldException ignore) {
85+
// Ignore
86+
}
87+
currentType = currentType.getSuperclass();
88+
}
89+
throw e;
90+
}
91+
}
92+
93+
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
94+
private static Object getFieldValue(final Object target, final Field field) throws IllegalAccessException {
95+
try {
96+
return field.get(target);
97+
} catch (IllegalAccessException e) {
98+
field.setAccessible(true);
99+
return field.get(target);
100+
}
101+
}
102+
103+
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
104+
private static Object invokeMethod(final Object target, final Method method) throws ReflectiveOperationException {
105+
try {
106+
return method.invoke(target);
107+
} catch (IllegalAccessException e) {
108+
method.setAccessible(true);
109+
return method.invoke(target);
110+
}
111+
}
112+
113+
private static Optional<Method> findGetter(final Class<?> type, final String propertyName) {
114+
final String capitalizedName = capitalize(propertyName);
115+
final String[] methodNames = {GET_PREFIX + capitalizedName, propertyName};
116+
Class<?> currentType = type;
117+
while (currentType != null) {
118+
for (String methodName : methodNames) {
119+
try {
120+
return Optional.of(currentType.getDeclaredMethod(methodName));
121+
} catch (NoSuchMethodException ignored) {
122+
// Ignore
123+
} catch (SecurityException e) {
124+
LOGGER.trace(METHOD_READ_ERROR, methodName, e);
125+
return Optional.empty();
126+
}
127+
}
128+
currentType = currentType.getSuperclass();
129+
}
130+
return Optional.empty();
131+
}
132+
133+
private static Optional<Method> findPrimitiveBooleanGetter(final Class<?> type, final String propertyName) {
134+
final String methodName = IS_PREFIX + capitalize(propertyName);
135+
Class<?> currentType = type;
136+
while (currentType != null) {
137+
try {
138+
final Method method = currentType.getDeclaredMethod(methodName);
139+
return method.getReturnType() == boolean.class ? Optional.of(method) : Optional.empty();
140+
} catch (NoSuchMethodException ignored) {
141+
// Ignore
142+
} catch (SecurityException e) {
143+
LOGGER.trace(METHOD_READ_ERROR, methodName, e);
144+
return Optional.empty();
145+
}
146+
currentType = currentType.getSuperclass();
147+
}
148+
return Optional.empty();
149+
}
150+
151+
private static String capitalize(final String value) {
152+
return Character.toUpperCase(value.charAt(0)) + value.substring(1);
153+
}
154+
155+
private static Boolean invokeBooleanMethod(final Object target, final Method method) {
156+
try {
157+
return Boolean.class.cast(invokeMethod(target, method));
158+
} catch (ReflectiveOperationException | SecurityException e) {
159+
LOGGER.trace(METHOD_READ_ERROR, method.getName(), e);
160+
return absentBooleanValue();
161+
}
162+
}
163+
164+
private static Boolean toBooleanValue(final Object value, final String propertyName) {
165+
if (value instanceof Boolean) {
166+
return (Boolean) value;
167+
}
168+
if (value != null) {
169+
LOGGER.trace(
170+
"Unable to read boolean value {}: actual value type is {}",
171+
propertyName,
172+
value.getClass().getName()
173+
);
174+
}
175+
return absentBooleanValue();
176+
}
177+
178+
private static Boolean absentBooleanValue() {
179+
return Optional.<Boolean>empty().orElse(null);
180+
}
181+
}

allure-java-commons/src/main/java/io/qameta/allure/util/ResultsUtils.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public final class ResultsUtils {
105105
private static final String ALLURE_DESCRIPTIONS_FOLDER = "META-INF/allureDescriptions/";
106106
private static final String MD_5 = "MD5";
107107
private static final String DOT = ".";
108+
private static final String ACTUAL = "actual";
109+
private static final String EXPECTED = "expected";
110+
private static final String DEFINED_PROPERTY_SUFFIX = "Defined";
108111

109112
private static String cachedHost;
110113

@@ -335,13 +338,17 @@ public static Optional<Status> getStatus(final Throwable throwable) {
335338

336339
public static Optional<StatusDetails> getStatusDetails(final Throwable e) {
337340
return Optional.ofNullable(e)
338-
.map(throwable -> new StatusDetails()
339-
.setMessage(Optional
340-
.ofNullable(throwable.getMessage())
341-
.orElse(throwable.getClass().getName())
342-
)
343-
.setTrace(getStackTraceAsString(throwable))
344-
);
341+
.map(throwable -> {
342+
final StatusDetails details = new StatusDetails()
343+
.setMessage(Optional
344+
.ofNullable(throwable.getMessage())
345+
.orElse(throwable.getClass().getName())
346+
)
347+
.setTrace(getStackTraceAsString(throwable));
348+
getRichErrorProperty(throwable, ACTUAL).ifPresent(details::setActual);
349+
getRichErrorProperty(throwable, EXPECTED).ifPresent(details::setExpected);
350+
return details;
351+
});
345352
}
346353

347354
public static Optional<String> getJavadocDescription(final ClassLoader classLoader,
@@ -440,6 +447,20 @@ private static String getStackTraceAsString(final Throwable throwable) {
440447
return stringWriter.toString();
441448
}
442449

450+
private static Optional<String> getRichErrorProperty(final Throwable throwable, final String propertyName) {
451+
final Boolean propertyDefined = ReflectionUtils.getBooleanValue(
452+
throwable,
453+
propertyName + DEFINED_PROPERTY_SUFFIX
454+
);
455+
// Some assertion libraries expose isActualDefined/isExpectedDefined to distinguish absent values
456+
// from values that are present but null.
457+
if (Boolean.FALSE.equals(propertyDefined)) {
458+
return Optional.empty();
459+
}
460+
final Object value = ReflectionUtils.getValue(throwable, propertyName);
461+
return Optional.ofNullable(value).map(ObjectUtils::toString);
462+
}
463+
443464
public static void processDescription(final ClassLoader classLoader,
444465
final Method method,
445466
final Consumer<String> setDescription,

allure-java-commons/src/test/java/io/qameta/allure/FileSystemResultsWriterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.qameta.allure;
1717

18+
import io.qameta.allure.model.StatusDetails;
1819
import io.qameta.allure.model.TestResult;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.io.TempDir;
@@ -73,6 +74,24 @@ void shouldWriteTitlePath(@TempDir final Path folder) throws IOException {
7374
.contains("\"child\"");
7475
}
7576

77+
@Test
78+
void shouldWriteStatusDetailsActualAndExpected(@TempDir final Path folder) throws IOException {
79+
FileSystemResultsWriter writer = new FileSystemResultsWriter(folder);
80+
final String uuid = UUID.randomUUID().toString();
81+
final TestResult testResult = new TestResult()
82+
.setUuid(uuid)
83+
.setStatusDetails(new StatusDetails()
84+
.setActual("actual value")
85+
.setExpected("expected value"));
86+
87+
writer.write(testResult);
88+
89+
final String payload = Files.readString(folder.resolve(generateTestResultName(uuid)));
90+
assertThat(payload)
91+
.contains("\"actual\":\"actual value\"")
92+
.contains("\"expected\":\"expected value\"");
93+
}
94+
7695
@Test
7796
void shouldPreserveOldResultsWhenCleanIsDisabled(@TempDir final Path folder) throws IOException {
7897
Path existingFile = folder.resolve("existing-result.json");

0 commit comments

Comments
 (0)