Skip to content

Commit 57df721

Browse files
committed
Fix bug in test data collection paths
1 parent 5b85807 commit 57df721

3 files changed

Lines changed: 78 additions & 9 deletions

File tree

test-gadgets-core/src/main/java/uk/org/webcompere/testgadgets/testdatafactory/TestDataCollectionProxy.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
1111
import java.util.Optional;
12+
import java.util.stream.Stream;
1213

1314
/**
1415
* Act as an interface tagged by <code>@TestDataCollection</code>
@@ -17,18 +18,24 @@ public class TestDataCollectionProxy implements InvocationHandler {
1718
private Path parentPath;
1819
private TestDataLoader loader;
1920

20-
private TestDataCollectionProxy(TestData fieldAnnotation, Path parentPath, TestDataLoader loader) {
21-
if (parentPath == null && fieldAnnotation.value().length > 0) {
22-
this.parentPath = pathFrom(fieldAnnotation.value());
23-
} else if (parentPath != null && fieldAnnotation.value().length > 0) {
24-
this.parentPath = parentPath.resolve(pathFrom(fieldAnnotation.value()));
25-
} else if (parentPath != null) {
26-
this.parentPath = parentPath;
27-
}
21+
private TestDataCollectionProxy(
22+
TestData fieldAnnotation, TestDataCollection classAnnotation, Path parentPath, TestDataLoader loader) {
23+
this.parentPath = Stream.of(
24+
Optional.ofNullable(parentPath),
25+
toOptionalPath(fieldAnnotation.value()),
26+
toOptionalPath(classAnnotation.value()))
27+
.filter(Optional::isPresent)
28+
.map(Optional::get)
29+
.reduce(Path::resolve)
30+
.orElse(null);
2831

2932
this.loader = loader;
3033
}
3134

35+
private static Optional<Path> toOptionalPath(String[] path) {
36+
return Optional.of(path).filter(p -> p.length > 0).map(TestDataLoaderAnnotations::pathFrom);
37+
}
38+
3239
@Override
3340
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
3441
if (method.isAnnotationPresent(TestData.class)) {
@@ -71,6 +78,7 @@ public static <T> Optional<T> proxyFor(
7178
return Optional.of((T) Proxy.newProxyInstance(
7279
clazz.getClassLoader(),
7380
new Class<?>[] {clazz},
74-
new TestDataCollectionProxy(fieldAnnotation, parentPath, loader)));
81+
new TestDataCollectionProxy(
82+
fieldAnnotation, clazz.getAnnotation(TestDataCollection.class), parentPath, loader)));
7583
}
7684
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package uk.org.webcompere.testgadgets.testdatafactory;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static uk.org.webcompere.testgadgets.testdatafactory.TestDataLoaderAnnotations.bindAnnotatedFields;
6+
7+
import java.io.IOException;
8+
import org.junit.jupiter.api.Test;
9+
10+
class TestDataCollectionProxyTest {
11+
12+
interface NotACollection {}
13+
14+
class ClassThatInjectsNonCollection {
15+
@TestData
16+
NotACollection notACollection;
17+
}
18+
19+
@Test
20+
void cannotInjectTestDataAtAllForInterfaceWithNoAnnotation() {
21+
var loader = new TestDataLoader();
22+
var object = new ClassThatInjectsNonCollection();
23+
24+
assertThatThrownBy(() -> bindAnnotatedFields(loader, object)).isInstanceOf(IOException.class);
25+
}
26+
27+
@TestDataCollection("child")
28+
interface ChildCollection {
29+
@TestData("somefile.txt")
30+
String someText();
31+
32+
String someOtherMethod();
33+
}
34+
35+
class InjectNestedCollection {
36+
@TestData("loader")
37+
ChildCollection childCollection;
38+
}
39+
40+
@Test
41+
void canLoadChildViaPaths() throws Exception {
42+
var loader = new TestDataLoader();
43+
var object = new InjectNestedCollection();
44+
45+
bindAnnotatedFields(loader, object);
46+
47+
assertThat(object.childCollection.someText()).isEqualTo("Child1\nChild2");
48+
}
49+
50+
@Test
51+
void nonTestDataMethodsReturnNull() throws Exception {
52+
var loader = new TestDataLoader();
53+
var object = new InjectNestedCollection();
54+
55+
bindAnnotatedFields(loader, object);
56+
57+
assertThat(object.childCollection.someOtherMethod()).isNull();
58+
}
59+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Child1
2+
Child2

0 commit comments

Comments
 (0)