Skip to content

Commit b699c0a

Browse files
committed
adding facility to specify an issue test to be run in early failing mode
1 parent 08f89c6 commit b699c0a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

tests/src/test/java/org/everit/json/schema/IssueTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,19 @@ private void initJetty(final File documentRoot) {
6969
}
7070
}
7171

72+
private static JSONObject fileAsJson(File file) {
73+
try {
74+
return new JSONObject(new JSONTokener(new FileInputStream(file)));
75+
} catch (FileNotFoundException e) {
76+
throw new UncheckedIOException(e);
77+
}
78+
}
79+
7280
private Schema loadSchema() {
7381
Optional<File> schemaFile = fileByName("schema.json");
7482
try {
7583
if (schemaFile.isPresent()) {
76-
JSONObject schemaObj = new JSONObject(
77-
new JSONTokener(new FileInputStream(schemaFile.get())));
84+
JSONObject schemaObj = fileAsJson(schemaFile.get());
7885
return SchemaLoader.load(schemaObj);
7986
}
8087
throw new RuntimeException(issueDir.getCanonicalPath() + "/schema.json is not found");
@@ -99,18 +106,29 @@ public void test() {
99106
stopJetty();
100107
}
101108

109+
private Validator createValidator() {
110+
Validator.ValidatorBuilder builder = Validator.builder();
111+
fileByName("validator-config.json").map(file -> fileAsJson(file))
112+
.map(json -> json.getBoolean("failEarly"))
113+
.filter(bool -> Boolean.TRUE.equals(bool))
114+
.ifPresent(t -> builder.failEarly());
115+
return builder.build();
116+
}
117+
102118
private void validate(final File file, final Schema schema, final boolean shouldBeValid) {
103119
ValidationException thrown = null;
104120

105121
Object subject = loadJsonFile(file);
106122

107123
try {
108-
schema.validate(subject);
124+
Validator validator = createValidator();
125+
validator.performValidation(schema, subject);
109126
} catch (ValidationException e) {
110127
thrown = e;
111128
}
112129

113130
if (shouldBeValid && thrown != null) {
131+
thrown.getAllMessages().forEach(System.out::println);
114132
StringBuilder failureBuilder = new StringBuilder("validation failed with: " + thrown);
115133
for (ValidationException e : thrown.getCausingExceptions()) {
116134
failureBuilder.append("\n\t").append(e.getMessage());

0 commit comments

Comments
 (0)