Skip to content

Commit f8d0f22

Browse files
committed
fix test and improve code
1 parent f89c8ad commit f8d0f22

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

src/main/java/de/tum/in/test/api/internal/sanitization/ThrowableSets.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ final class ThrowableSets {
3333
private static final String OPENTEST4J_CHECK_CLASS = "org.opentest4j.AssertionFailedError"; //$NON-NLS-1$
3434

3535
static {
36-
HashSet<Class<? extends Throwable>> join = new HashSet<>(Java.SAFE_TYPES);
37-
join.addAll(Own.SAFE_TYPES);
36+
HashSet<Class<? extends Throwable>> safeTypesJoin = new HashSet<>(Java.SAFE_TYPES);
37+
safeTypesJoin.addAll(Own.SAFE_TYPES);
3838
if (classCanBeFound(JUNIT4_CHECK_CLASS))
39-
join.addAll(JUnit4.SAFE_TYPES);
39+
safeTypesJoin.addAll(JUnit4.SAFE_TYPES);
4040
if (classCanBeFound(JUNIT5_CHECK_CLASS))
41-
join.addAll(JUnit5.SAFE_TYPES);
41+
safeTypesJoin.addAll(JUnit5.SAFE_TYPES);
4242
if (classCanBeFound(JQWIK_CHECK_CLASS))
43-
join.addAll(Jqwik.SAFE_TYPES);
43+
safeTypesJoin.addAll(Jqwik.SAFE_TYPES);
4444
if (classCanBeFound(ASSERTJ_CHECK_CLASS))
45-
join.addAll(AssertJ.SAFE_TYPES);
45+
safeTypesJoin.addAll(AssertJ.SAFE_TYPES);
4646
if (classCanBeFound(OPENTEST4J_CHECK_CLASS))
47-
join.addAll(OpenTest4J.SAFE_TYPES);
48-
SAFE_TYPES = Collections.unmodifiableSet(join);
47+
safeTypesJoin.addAll(OpenTest4J.SAFE_TYPES);
48+
SAFE_TYPES = Collections.unmodifiableSet(safeTypesJoin);
4949
}
5050

5151
private ThrowableSets() {

src/main/java/de/tum/in/test/api/internal/sanitization/ThrowableUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ static <T extends Throwable> Constructor<T> findPreferredConstructor(Class<T> ty
7777
var classPropertyTypeCount = getRelevantPropertiesWithMethods(type, IGNORE_PROPERTIES).stream()
7878
.collect(Collectors.groupingBy(property -> property.getValue().getReturnType(), Collectors.counting()));
7979
return Stream.of(allConstructors)
80-
.filter(constructor -> isSatisfiableByProperties(constructor, classPropertyTypeCount))
81-
.sorted(getConstructorPreferenceOrder()).findFirst().orElse(allConstructors[0]);
80+
.filter(constructor -> isSatisfiableByProperties(constructor, classPropertyTypeCount)).min(getConstructorPreferenceOrder()).orElse(allConstructors[0]);
8281
}
8382

8483
static boolean isSatisfiableByProperties(Constructor<?> constructor,

src/test/java/de/tum/in/test/api/internal/sanitization/ThrowableUtilsTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class ThrowableUtilsTest {
3636
private static final Set<String> FALSE_POSITIVES = Set.of( //
3737
"junit.framework.AssertionFailedError", // deviation for null message is not an issue
3838
"java.util.IllformedLocaleException", // we don't need the index attribute itself
39-
"java.awt.HeadlessException" // CI systems don't like this, but the constructor works
39+
"java.awt.HeadlessException", // CI systems don't like this, but the constructor works
40+
"org.assertj.core.util.introspection.IntrospectionError" // this is a wrapper for a Throwable, so it is not an issue
4041
);
4142

4243
private static final Set<Class<?>> SAFE_PROPERTY_TYPES = Set.of(Throwable.class, Throwable[].class);
@@ -56,7 +57,7 @@ void testConstructorInstantiation() {
5657
return !validate(type, preferredConstructor);
5758
}).sorted(Comparator.comparing(Object::toString)).collect(Collectors.toList());
5859
assertThat(duplicationFailures)
59-
.as("the default Throwable duplication works for all SAFE_TYPE classes that are not specially handeled")
60+
.as("the default Throwable duplication works for all SAFE_TYPE classes that are not specially handled")
6061
.isEmpty();
6162
}
6263

@@ -89,18 +90,16 @@ void checkProperties() {
8990
.map(Method::getReturnType).distinct().filter(type -> {
9091
Class<?> containedType;
9192
if (type.isArray())
92-
containedType = Stream.<Class<?>>iterate(type, Class::getComponentType)
93-
.takeWhile(Objects::nonNull).reduce(null, (a, b) -> b);
93+
containedType = Stream.<Class<?>>iterate(type, Objects::nonNull, Class::getComponentType).reduce(null, (a, b) -> b);
9494
else
9595
containedType = type;
96-
if (containedType.isPrimitive())
96+
assertThat(containedType).isNotNull();
97+
if (containedType.isPrimitive())
9798
return false;
9899
if (Modifier.isFinal(containedType.getModifiers()))
99100
return false;
100-
if (SAFE_PROPERTY_TYPES.stream().anyMatch(safeType -> safeType.isAssignableFrom(containedType)))
101-
return false;
102-
return true;
103-
}).collect(Collectors.toSet());
101+
return SAFE_PROPERTY_TYPES.stream().noneMatch(safeType -> safeType.isAssignableFrom(containedType));
102+
}).collect(Collectors.toSet());
104103
assertThat(potentiallyUnsafeProperties).as("property types are all safe or sanitizable").isEmpty();
105104
}
106105

0 commit comments

Comments
 (0)