Skip to content

Commit 8a56fa7

Browse files
Nylleakutschera
authored andcommitted
Validate test-case arguments
Duplicate customisations for the same argument will throw an exception. Refs: #91
1 parent 1aa390c commit 8a56fa7

File tree

2 files changed

+824
-11
lines changed

2 files changed

+824
-11
lines changed

src/main/java/com/github/nylle/javafixture/annotations/testcases/ReflectedTestCase.java

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.nylle.javafixture.annotations.testcases;
22

3+
import com.github.nylle.javafixture.SpecimenException;
4+
35
import java.lang.reflect.InvocationTargetException;
46
import java.lang.reflect.Method;
57
import java.util.ArrayList;
@@ -9,21 +11,25 @@
911
import java.util.Map;
1012

1113
import static java.util.Arrays.stream;
14+
import static java.util.stream.Collectors.joining;
15+
import static java.util.stream.Collectors.toList;
1216

1317
public class ReflectedTestCase {
1418

15-
private static final Map<Class<?>, Class<?>> primitiveWrapperMap = Map.of(
16-
Boolean.class, Boolean.TYPE,
17-
Character.class, Character.TYPE,
18-
Byte.class, Byte.TYPE,
19-
Short.class, Short.TYPE,
20-
Integer.class, Integer.TYPE,
21-
Long.class, Long.TYPE,
22-
Float.class, Float.TYPE,
23-
Double.class, Double.TYPE
19+
private static final Map<Class<?>, ?> defaults = Map.of(
20+
Class.class, Object.class,
21+
String.class, "",
22+
Boolean.class, false,
23+
Character.class, Character.MIN_VALUE,
24+
Byte.class, (byte) 0,
25+
Short.class, (short) 0,
26+
Integer.class, 0,
27+
Long.class, 0L,
28+
Float.class, 0.0f,
29+
Double.class, 0.0d
2430
);
2531

26-
private Map<Class<?>, List<?>> matrix = new HashMap<>();
32+
private final Map<Class<?>, List<?>> matrix = new HashMap<>();
2733

2834
public ReflectedTestCase(TestCase testCase) {
2935
stream(TestCase.class.getDeclaredMethods())
@@ -33,7 +39,22 @@ public ReflectedTestCase(TestCase testCase) {
3339

3440
@SuppressWarnings("unchecked")
3541
public <T> T getTestCaseValueFor(Class<T> type, int i) {
36-
return (T) matrix.get(primitiveWrapperMap.getOrDefault(type, type)).get(i);
42+
validate(type, i);
43+
return (T) matrix.get(asPrimitive(type)).get(i);
44+
}
45+
46+
private <T> void validate(Class<T> type, int i) {
47+
var nonDefaultValues = defaults.keySet().stream()
48+
.map(key -> matrix.get(asPrimitive(key)).get(i))
49+
.filter(value -> !defaults.get(value.getClass()).equals(value))
50+
.collect(toList());
51+
52+
if(isInvalid(type, nonDefaultValues)) {
53+
throw new SpecimenException(String.format("Duplicate customisation found for argument at position %d, wanted: %s, found: %s",
54+
i + 1,
55+
type.getName(),
56+
nonDefaultValues.stream().map(x -> x.getClass().getName()).collect(joining(", "))));
57+
}
3758
}
3859

3960
@SuppressWarnings("unchecked")
@@ -54,4 +75,27 @@ private <T> List<T> addTo(List<T> list, T value) {
5475
return list;
5576
}
5677

78+
private static <T> boolean isInvalid(Class<T> type, List<?> nonDefaultValues) {
79+
if(nonDefaultValues.size() < 1) {
80+
return false;
81+
}
82+
if(nonDefaultValues.size() > 1) {
83+
return true;
84+
}
85+
return asPrimitive(type) != asPrimitive(nonDefaultValues.get(0).getClass());
86+
}
87+
88+
private static Class<?> asPrimitive(Class<?> type) {
89+
Map<Class<?>, Class<?>> primitiveWrapperMap = Map.of(
90+
Boolean.class, Boolean.TYPE,
91+
Character.class, Character.TYPE,
92+
Byte.class, Byte.TYPE,
93+
Short.class, Short.TYPE,
94+
Integer.class, Integer.TYPE,
95+
Long.class, Long.TYPE,
96+
Float.class, Float.TYPE,
97+
Double.class, Double.TYPE
98+
);
99+
return primitiveWrapperMap.getOrDefault(type, type);
100+
}
57101
}

0 commit comments

Comments
 (0)