Skip to content

Commit 153bf60

Browse files
committed
introducing ResourceLoader testsupport class instead of repeated resource-loading code
1 parent 3f335a0 commit 153bf60

10 files changed

+67
-59
lines changed

core/src/test/java/org/everit/json/schema/ArraySchemaTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929

3030
public class ArraySchemaTest {
3131

32-
private static final JSONObject ARRAYS = new JSONObject(new JSONTokener(
33-
ArraySchemaTest.class.getResourceAsStream("/org/everit/jsonvalidator/arraytestcases.json")));
32+
private static final ResourceLoader loader = ResourceLoader.DEFAULT;
33+
34+
private static final JSONObject ARRAYS = loader.readObj("arraytestcases.json");
3435

3536
@Test
3637
public void additionalItemsSchema() {
@@ -170,21 +171,16 @@ public void equalsVerifier() {
170171
.verify();
171172
}
172173

173-
private JSONObject read(final String fileName) {
174-
return new JSONObject(new JSONTokener(
175-
getClass().getResourceAsStream("/org/everit/jsonvalidator/tostring/" + fileName)));
176-
}
177-
178174
@Test
179175
public void toStringTest() {
180-
JSONObject rawSchemaJson = read("arrayschema-list.json");
176+
JSONObject rawSchemaJson = loader.readObj("tostring/arrayschema-list.json");
181177
String actual = SchemaLoader.load(rawSchemaJson).toString();
182178
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
183179
}
184180

185181
@Test
186182
public void toStringAdditionalItems() {
187-
JSONObject rawSchemaJson = read("arrayschema-list.json");
183+
JSONObject rawSchemaJson = loader.readObj("tostring/arrayschema-list.json");
188184
rawSchemaJson.remove("items");
189185
rawSchemaJson.put("additionalItems", false);
190186
String actual = SchemaLoader.load(rawSchemaJson).toString();
@@ -193,15 +189,15 @@ public void toStringAdditionalItems() {
193189

194190
@Test
195191
public void toStringNoExplicitType() {
196-
JSONObject rawSchemaJson = read("arrayschema-list.json");
192+
JSONObject rawSchemaJson = loader.readObj("tostring/arrayschema-list.json");
197193
rawSchemaJson.remove("type");
198194
String actual = SchemaLoader.load(rawSchemaJson).toString();
199195
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
200196
}
201197

202198
@Test
203199
public void toStringTupleSchema() {
204-
JSONObject rawSchemaJson = read("arrayschema-tuple.json");
200+
JSONObject rawSchemaJson = loader.readObj("tostring/arrayschema-tuple.json");
205201
String actual = SchemaLoader.load(rawSchemaJson).toString();
206202
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
207203
}

core/src/test/java/org/everit/json/schema/ObjectSchemaTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424

2525
public class ObjectSchemaTest {
2626

27-
private static final JSONObject OBJECTS = new JSONObject(new JSONTokener(
28-
ObjectSchemaTest.class
29-
.getResourceAsStream("/org/everit/jsonvalidator/objecttestcases.json")));
27+
private static final JSONObject OBJECTS = ResourceLoader.DEFAULT.readObj("objecttestcases.json");
3028

3129
@Test
3230
public void additionalPropertiesOnEmptyObject() {

core/src/test/java/org/everit/json/schema/PointerBubblingTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
public class PointerBubblingTest {
2525

26-
private final JSONObject allSchemas = new JSONObject(new JSONTokener(
27-
getClass().getResourceAsStream("/org/everit/jsonvalidator/testschemas.json")));
26+
private static final ResourceLoader loader = ResourceLoader.DEFAULT;
27+
28+
private final JSONObject allSchemas = loader.readObj("testschemas.json");
2829

2930
private final Schema rectangleSchema = SchemaLoader
3031
.load(allSchemas.getJSONObject("pointerResolution"));
3132

32-
private final JSONObject testInputs = new JSONObject(new JSONTokener(
33-
getClass().getResourceAsStream("/org/everit/jsonvalidator/objecttestcases.json")));
33+
private final JSONObject testInputs = loader.readObj("objecttestcases.json");
3434

3535
@Test
3636
public void rectangleMultipleFailures() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.everit.json.schema;
2+
3+
import org.json.JSONObject;
4+
import org.json.JSONTokener;
5+
6+
import java.io.InputStream;
7+
8+
import static java.lang.String.format;
9+
import static java.util.Objects.requireNonNull;
10+
11+
public class ResourceLoader {
12+
13+
public static final ResourceLoader DEFAULT = new ResourceLoader("/org/everit/jsonvalidator/");
14+
15+
private final String rootPath;
16+
17+
public ResourceLoader(String rootPath) {
18+
this.rootPath = requireNonNull(rootPath, "rootPath cannot be null");
19+
}
20+
21+
public JSONObject readObj(String relPath) {
22+
InputStream stream = getStream(relPath);
23+
return new JSONObject(new JSONTokener(stream));
24+
}
25+
26+
public InputStream getStream(String relPath) {
27+
String absPath = rootPath + relPath;
28+
InputStream rval = getClass().getResourceAsStream(absPath);
29+
if (rval == null) {
30+
throw new IllegalArgumentException(
31+
format("failed to load resource by relPath [%s].\n"
32+
+ "InputStream by path [%s] is null", relPath, absPath));
33+
}
34+
return rval;
35+
}
36+
37+
}

core/src/test/java/org/everit/json/schema/ValidationExceptionTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,11 @@ public void testToJSON() {
192192
ValidationException subject =
193193
new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a/b"),
194194
"exception message", Collections.emptyList(), "type");
195-
JSONObject expected = readFile("/org/everit/jsonvalidator/exception-to-json.json");
195+
JSONObject expected = ResourceLoader.DEFAULT.readObj("exception-to-json.json");
196196
JSONObject actual = subject.toJSON();
197197
Assert.assertTrue(ObjectComparator.deepEquals(expected, actual));
198198
}
199199

200-
private JSONObject readFile(final String absPath) {
201-
return new JSONObject(new JSONTokener(
202-
getClass().getResourceAsStream(absPath)));
203-
}
204-
205200
@Test
206201
public void toJSONNullPointerToViolation() {
207202
ValidationException subject =
@@ -222,7 +217,7 @@ public void toJSONWithCauses() {
222217
ValidationException subject =
223218
new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a"),
224219
"exception message", Arrays.asList(cause), "type");
225-
JSONObject expected = readFile("/org/everit/jsonvalidator/exception-to-json-with-causes.json");
220+
JSONObject expected = ResourceLoader.DEFAULT.readObj("exception-to-json-with-causes.json");
226221
JSONObject actual = subject.toJSON();
227222
Assert.assertTrue(ObjectComparator.deepEquals(expected, actual));
228223
}

core/src/test/java/org/everit/json/schema/loader/CustomFormatValidatorTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.everit.json.schema.loader;
1717

1818
import org.everit.json.schema.FormatValidator;
19+
import org.everit.json.schema.ResourceLoader;
1920
import org.everit.json.schema.ValidationException;
2021
import org.json.JSONObject;
2122
import org.json.JSONTokener;
@@ -26,6 +27,8 @@
2627

2728
public class CustomFormatValidatorTest {
2829

30+
private final ResourceLoader loader = ResourceLoader.DEFAULT;
31+
2932
static class EvenCharNumValidator implements FormatValidator {
3033

3134
@Override
@@ -38,19 +41,14 @@ public Optional<String> validate(final String subject) {
3841
}
3942
}
4043

41-
private JSONObject read(final String path) {
42-
return new JSONObject(new JSONTokener(getClass().getResourceAsStream(path)));
43-
}
44-
4544
@Test
4645
public void test() {
4746
SchemaLoader schemaLoader = SchemaLoader.builder()
48-
.schemaJson(read("/org/everit/jsonvalidator/customformat-schema.json"))
47+
.schemaJson(loader.readObj("customformat-schema.json"))
4948
.addFormatValidator("evenlength", new EvenCharNumValidator())
5049
.build();
5150
try {
52-
schemaLoader.load().build()
53-
.validate(read("/org/everit/jsonvalidator/customformat-data.json"));
51+
schemaLoader.load().build().validate(loader.readObj("customformat-data.json"));
5452
Assert.fail("did not throw exception");
5553
} catch (ValidationException ve) {
5654
}

core/src/test/java/org/everit/json/schema/loader/DefinesPropertyTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.everit.json.schema.loader;
1717

1818
import org.everit.json.schema.ObjectSchema;
19+
import org.everit.json.schema.ResourceLoader;
1920
import org.everit.json.schema.Schema;
2021
import org.json.JSONObject;
2122
import org.json.JSONTokener;
@@ -27,14 +28,7 @@
2728

2829
public class DefinesPropertyTest {
2930

30-
private static JSONObject ALL_SCHEMAS;
31-
32-
@BeforeClass
33-
public static void before() {
34-
InputStream stream = DefinesPropertyTest.class.getResourceAsStream(
35-
"/org/everit/jsonvalidator/testschemas.json");
36-
ALL_SCHEMAS = new JSONObject(new JSONTokener(stream));
37-
}
31+
private static JSONObject ALL_SCHEMAS = ResourceLoader.DEFAULT.readObj("testschemas.json");
3832

3933
private JSONObject get(final String schemaName) {
4034
return ALL_SCHEMAS.getJSONObject(schemaName);
@@ -110,8 +104,8 @@ public void objectEscape() {
110104
@Test
111105
public void testOfTest() {
112106
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternPropsAndSchemaDeps"));
113-
JSONObject input = new JSONObject(new JSONTokener(
114-
getClass().getResourceAsStream("/org/everit/jsonvalidator/objecttestcases.json")))
107+
JSONObject input = ResourceLoader.DEFAULT
108+
.readObj("objecttestcases.json")
115109
.getJSONObject("validOfPatternPropsAndSchemaDeps");
116110
actual.validate(input);
117111
}

core/src/test/java/org/everit/json/schema/loader/ExtendTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
package org.everit.json.schema.loader;
1717

1818
import org.everit.json.schema.ObjectComparator;
19+
import org.everit.json.schema.ResourceLoader;
1920
import org.json.JSONObject;
2021
import org.json.JSONTokener;
2122
import org.junit.Assert;
2223
import org.junit.Test;
2324

2425
public class ExtendTest {
2526

27+
private static final ResourceLoader loader = ResourceLoader.DEFAULT;
28+
2629
private static JSONObject OBJECTS;
2730

2831
static {
29-
OBJECTS = new JSONObject(new JSONTokener(
30-
ExtendTest.class.getResourceAsStream("/org/everit/jsonvalidator/merge-testcases.json")));
32+
OBJECTS = loader.readObj("merge-testcases.json");
3133
}
3234

3335
@Test

core/src/test/java/org/everit/json/schema/loader/ResolutionScopeTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.everit.json.schema.loader;
1717

18+
import org.everit.json.schema.ResourceLoader;
1819
import org.everit.json.schema.loader.internal.DefaultSchemaClient;
1920
import org.json.JSONObject;
2021
import org.json.JSONTokener;
@@ -24,13 +25,7 @@
2425

2526
public class ResolutionScopeTest {
2627

27-
private static JSONObject ALL_SCHEMAS;
28-
29-
static {
30-
InputStream stream = SchemaLoaderTest.class.getResourceAsStream(
31-
"/org/everit/jsonvalidator/testschemas.json");
32-
ALL_SCHEMAS = new JSONObject(new JSONTokener(stream));
33-
}
28+
private static JSONObject ALL_SCHEMAS = ResourceLoader.DEFAULT.readObj("testschemas.json");
3429

3530
private JSONObject get(final String schemaName) {
3631
return ALL_SCHEMAS.getJSONObject(schemaName);

core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@
3434

3535
public class SchemaLoaderTest {
3636

37-
private static JSONObject ALL_SCHEMAS;
38-
39-
@BeforeClass
40-
public static void before() {
41-
InputStream stream = SchemaLoaderTest.class.getResourceAsStream(
42-
"/org/everit/jsonvalidator/testschemas.json");
43-
ALL_SCHEMAS = new JSONObject(new JSONTokener(stream));
44-
}
37+
private static JSONObject ALL_SCHEMAS = ResourceLoader.DEFAULT.readObj("testschemas.json");
4538

4639
@Test
4740
public void additionalItemSchema() {

0 commit comments

Comments
 (0)