Skip to content

Commit b3ea06e

Browse files
committed
Add loading via as property
1 parent 595056f commit b3ea06e

5 files changed

Lines changed: 62 additions & 8 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ void someTest() {
141141

142142
> Note: for asserting JSON responses, maybe consider [Model Assert](https://github.com/webcompere/model-assert)
143143
144+
We may prefer to load the file using a different loader than is suggested by its file extension. For example,
145+
we may have a JSON file and wish to load it into a String as text, rather than serialize `"some value"` into `String`
146+
from the file using JSON deserialization.
147+
148+
For this, we can use the `as` property of the `@TestData` annotation:
149+
150+
```java
151+
// will get `src/test/resources/somejson.json` loaded as a string
152+
// rather than deserialized as though it's a JSON string literal
153+
@TestData(value = "somejson.json", as = ".txt")
154+
private String jsonContents;
155+
```
156+
144157
### Customising the Loader
145158

146159
We can customise the loader by setting its root directory, default immutability, default file extension (for when

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919
* Is this test data to be treated as immutable and thus cached and served from the cache.
2020
*/
2121
Immutable immutable() default Immutable.DEFAULT;
22+
23+
/**
24+
* If non blank, use the loader for this alternative file extension (including the .) to load
25+
* the file into the target object
26+
*/
27+
String as() default "";
2228
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,28 @@ public TestDataLoader addLoader(String extension, ObjectLoader objectLoader) {
7272
* @param <T> the type of object to load
7373
* @throws IOException on any error
7474
*/
75-
@SuppressWarnings("unchecked")
7675
public <T> T load(Path pathToFile, Type type, boolean useCache) throws IOException {
76+
return load(pathToFile, type, useCache, null);
77+
}
78+
79+
/**
80+
* Load a file into an object
81+
* @param pathToFile the path to the file relative to the paths in the engine
82+
* @param type the type to hydrate
83+
* @param useCache whether to use the cache at all
84+
* @param overrideExtension can be null or blank, but if present, it's the file extension that defines which
85+
* loader to use, in place of the native file extension
86+
* @return the object required
87+
* @param <T> the type of object to load
88+
* @throws IOException on any error
89+
*/
90+
@SuppressWarnings("unchecked")
91+
public <T> T load(Path pathToFile, Type type, boolean useCache, String overrideExtension) throws IOException {
7792
if (isImmutable(type) || useCache) {
7893
try {
7994
return (T) cache.computeIfAbsent(pathToFile, (path) -> {
8095
try {
81-
return loadWithLoaders(pathToFile, type);
96+
return loadWithLoaders(pathToFile, type, overrideExtension);
8297
} catch (IOException e) {
8398
throw new RuntimeException("Error loading " + pathToFile + " " + e.getMessage(), e);
8499
}
@@ -88,11 +103,11 @@ public <T> T load(Path pathToFile, Type type, boolean useCache) throws IOExcepti
88103
}
89104
}
90105

91-
return loadWithLoaders(pathToFile, type);
106+
return loadWithLoaders(pathToFile, type, overrideExtension);
92107
}
93108

94109
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
95-
private <T> T loadWithLoaders(Path pathToFile, Type type) throws IOException {
110+
private <T> T loadWithLoaders(Path pathToFile, Type type, String overrideExtension) throws IOException {
96111
Path resolved = root.resolve(pathToFile);
97112

98113
var fileExtension = getExtension(resolved);
@@ -101,6 +116,10 @@ private <T> T loadWithLoaders(Path pathToFile, Type type) throws IOException {
101116
? resolved
102117
: resolved.getParent().resolve(pathToFile.getFileName() + extensionToUse);
103118

119+
if (overrideExtension != null && !overrideExtension.isBlank()) {
120+
extensionToUse = overrideExtension;
121+
}
122+
104123
if (!loaders.containsKey(extensionToUse.toLowerCase(Locale.getDefault()))) {
105124
throw new IOException("No loader present for extension " + extensionToUse);
106125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static Object load(TestDataLoader loaderInstance, String name, Type type,
124124
cache = loaderInstance.getImmutableMode() == Immutable.IMMUTABLE;
125125
}
126126

127-
return loaderInstance.load(path, type, cache);
127+
return loaderInstance.load(path, type, cache, testDataAnnotation.as());
128128
}
129129

130130
private static void setup(TestDataLoader loaderInstance, Field field, Object testInstance) throws Exception {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ void whenFilePathInAnnotationWithSlashesThenLoadsIt() throws Exception {
114114
assertThat(bound.getCatchPhrase().getName()).isEqualTo("Gadget");
115115
}
116116

117+
@Test
118+
void canLoadWithOverrideType() throws Exception {
119+
var loader = new TestDataLoader();
120+
121+
class BoundClass {
122+
@TestData(
123+
value = {"loader", "somejson.json"},
124+
as = ".txt")
125+
String somejson;
126+
}
127+
128+
var bound = new BoundClass();
129+
bindAnnotatedFields(loader, bound);
130+
131+
// has loaded as string
132+
assertThat(bound.somejson)
133+
.isEqualTo("{\n" + " \"name\": \"Gadget\",\n" + " \"catchPhrase\": \"GadgetGadget\"\n" + "}");
134+
}
135+
117136
@Test
118137
void whenTheLoaderIsSetToImmutableSubsequentLoadsAreCached() throws Exception {
119138
var loader = new TestDataLoader();
@@ -191,8 +210,6 @@ static class ReceiveStaticLoader {
191210
void canInjectLoaderIntoStaticField() throws Exception {
192211
var loader = new TestDataLoader();
193212

194-
var testObject = new ReceiveStaticLoader();
195-
196213
bindAnnotatedStaticFields(loader, ReceiveStaticLoader.class);
197214

198215
assertThat(ReceiveStaticLoader.testDataLoader).isSameAs(loader);
@@ -202,7 +219,6 @@ void canInjectLoaderIntoStaticField() throws Exception {
202219
void canReadTestDataLoaderFromTestObjectStatic() throws Exception {
203220
var loader = new TestDataLoader();
204221

205-
var testObject = new ReceiveStaticLoader();
206222
ReceiveStaticLoader.testDataLoader = loader;
207223

208224
assertThat(TestDataLoaderAnnotations.getLoaderFromTestClassOrObject(ReceiveStaticLoader.class, null)

0 commit comments

Comments
 (0)