Skip to content

Commit c5f41d4

Browse files
committed
initial implementation of EarlyFailingFailureReporter
* changing ValidationFailureReporter to be an abstract class instead of interface; tracking the current schema here * moving some failure() overloads into ValidationFailureReporter from CollectingFailureReporter * moving some of the #inContextOfSchema() into ValidationFailureReporter, just to track the context schema change * implementing and testing EarlyFailingFailureReporter
1 parent ff194eb commit c5f41d4

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

core/src/main/java/org/everit/json/schema/CollectingFailureReporter.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
package org.everit.json.schema;
22

3-
import static java.util.Objects.requireNonNull;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

8-
class CollectingFailureReporter implements ValidationFailureReporter {
6+
class CollectingFailureReporter extends ValidationFailureReporter {
97

108
private List<ValidationException> failures = new ArrayList<>(1);
119

12-
private Schema schema;
13-
1410
CollectingFailureReporter(Schema schema) {
15-
this.schema = requireNonNull(schema, "schema cannot be null");
16-
}
17-
18-
@Override
19-
public void failure(String message, String keyword) {
20-
failures.add(new ValidationException(schema, message, keyword, schema.getSchemaLocation()));
21-
}
22-
23-
@Override
24-
public void failure(Class<?> expectedType, Object actualValue) {
25-
failures.add(new ValidationException(schema, expectedType, actualValue, "type", schema.getSchemaLocation()));
11+
super(schema);
2612
}
2713

2814
@Override
@@ -35,12 +21,8 @@ public void validationFinished() {
3521
}
3622

3723
public ValidationException inContextOfSchema(Schema schema, Runnable task) {
38-
requireNonNull(schema, "schema cannot be null");
3924
int failureCountBefore = failures.size();
40-
Schema origSchema = this.schema;
41-
this.schema = schema;
42-
task.run();
43-
this.schema = origSchema;
25+
super.inContextOfSchema(schema, task);
4426
int failureCountAfter = failures.size(), newFailureCount = failureCountAfter - failureCountBefore;
4527
if (newFailureCount == 0) {
4628
return null;

core/src/main/java/org/everit/json/schema/EarlyFailingFailureReporter.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
package org.everit.json.schema;
22

3-
public class EarlyFailingFailureReporter implements ValidationFailureReporter {
4-
5-
@Override public void failure(String message, String keyword) {
6-
7-
}
8-
9-
@Override public void failure(Class<?> expectedType, Object actualValue) {
3+
public class EarlyFailingFailureReporter extends ValidationFailureReporter {
104

5+
public EarlyFailingFailureReporter(Schema schema) {
6+
super(schema);
117
}
128

139
@Override public void failure(ValidationException exc) {
14-
15-
}
16-
17-
@Override public ValidationException inContextOfSchema(Schema schema, Runnable task) {
18-
return null;
10+
throw exc;
1911
}
2012

2113
@Override public void validationFinished() {
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
package org.everit.json.schema;
22

3+
import static java.util.Objects.requireNonNull;
4+
35
/**
46
* Internal interface receiving validation failures. Implementations are supposed to throw or collect {@link ValidationException} instances.
57
* <p>
6-
* The validation always happens in the context of some "current schema", tracked by implementations. This {@link Schema} instance will
8+
* The validation always happens in the context of some "current schema". This {@link Schema} instance will
79
* be the {@link ValidationException#getViolatedSchema() violated schema} of the {@code ValidationException}s created.
810
* </p>
911
*/
10-
interface ValidationFailureReporter {
12+
abstract class ValidationFailureReporter {
13+
14+
protected Schema schema;
15+
16+
ValidationFailureReporter(Schema schema) {
17+
this.schema = requireNonNull(schema, "schema cannot be null");
18+
}
1119

12-
void failure(String message, String keyword);
20+
void failure(String message, String keyword) {
21+
failure(new ValidationException(schema, message, keyword, schema.getSchemaLocation()));
22+
}
1323

14-
void failure(Class<?> expectedType, Object actualValue);
24+
void failure(Class<?> expectedType, Object actualValue) {
25+
failure(new ValidationException(schema, expectedType, actualValue, "type", schema.getSchemaLocation()));
26+
}
1527

16-
void failure(ValidationException exc);
28+
abstract void failure(ValidationException exc);
1729

18-
ValidationException inContextOfSchema(Schema schema, Runnable task);
30+
ValidationException inContextOfSchema(Schema schema, Runnable task) {
31+
requireNonNull(schema, "schema cannot be null");
32+
Schema origSchema = this.schema;
33+
this.schema = schema;
34+
task.run();
35+
this.schema = origSchema;
36+
return null;
37+
}
1938

20-
void validationFinished();
39+
abstract void validationFinished();
2140
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.everit.json.schema;
2+
3+
import static org.junit.Assert.assertSame;
4+
import static org.junit.Assert.fail;
5+
6+
import org.junit.Test;
7+
8+
public class EarlyFailingFailureReporterTest {
9+
10+
private EarlyFailingFailureReporter createSubject() {
11+
return new EarlyFailingFailureReporter(NullSchema.INSTANCE);
12+
}
13+
14+
@Test
15+
public void testFailure() {
16+
EarlyFailingFailureReporter subject = createSubject();
17+
ValidationException input = new ValidationException(BooleanSchema.INSTANCE, Boolean.class, "string");
18+
try {
19+
subject.failure(input);
20+
fail();
21+
} catch (ValidationException e) {
22+
assertSame(input, e);
23+
}
24+
}
25+
26+
@Test
27+
public void testValidationFinished() {
28+
// should be no-op
29+
createSubject().validationFinished();
30+
}
31+
32+
}

0 commit comments

Comments
 (0)