Skip to content

Commit 125f8fb

Browse files
committed
removed ValidationException#multipleFailures() and replaced with ValidationException#throwFor(), tests added, ObjectSchema modified accordingly
1 parent 9437a90 commit 125f8fb

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,7 @@ public void validate(final Object subject) {
390390
failures.addAll(testPropertyDependencies(objSubject));
391391
failures.addAll(testSchemaDependencies(objSubject));
392392
failures.addAll(testPatternProperties(objSubject));
393-
if (failures.size() == 1) {
394-
throw failures.get(0);
395-
} else if (failures.size() > 1) {
396-
throw ValidationException.multipleFailures(this, failures);
397-
}
393+
ValidationException.throwFor(this, failures);
398394
}
399395
}
400396

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@
2626
public class ValidationException extends RuntimeException {
2727
private static final long serialVersionUID = 6192047123024651924L;
2828

29-
public static final ValidationException multipleFailures(final Schema rootFailingSchema,
30-
final List<ValidationException> causingExceptions) {
31-
return new ValidationException(rootFailingSchema, new ArrayList<>(causingExceptions));
29+
public static void throwFor(final Schema rootFailingSchema,
30+
final List<ValidationException> failures) {
31+
int failureCount = failures.size();
32+
if (failureCount == 0) {
33+
return;
34+
} else if (failureCount == 1) {
35+
throw failures.get(0);
36+
} else {
37+
throw new ValidationException(rootFailingSchema, new ArrayList<>(failures));
38+
}
3239
}
3340

3441
private final StringBuilder pointerToViolation;

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
*/
1616
package org.everit.json.schema;
1717

18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
1822
import org.junit.Assert;
1923
import org.junit.Test;
2024

2125
public class ValidationExceptionTest {
2226

27+
private final Schema rootSchema = ObjectSchema.builder().build();
28+
2329
@Test
2430
public void constructorNullSchema() {
2531
new ValidationException(null, Boolean.class, 2);
@@ -53,4 +59,37 @@ public void testConstructor() {
5359
Assert.assertEquals("#", exc.getPointerToViolation());
5460
}
5561

62+
@Test
63+
public void throwForMultipleFailures() {
64+
ValidationException input1 = new ValidationException(NullSchema.INSTANCE, "msg1");
65+
ValidationException input2 = new ValidationException(BooleanSchema.INSTANCE, "msg2");
66+
try {
67+
ValidationException.throwFor(rootSchema, Arrays.asList(input1, input2));
68+
Assert.fail("did not throw exception for 2 input exceptions");
69+
} catch (ValidationException e) {
70+
Assert.assertSame(rootSchema, e.getViolatedSchema());
71+
Assert.assertEquals("2 schema violations found", e.getMessage());
72+
List<ValidationException> causes = e.getCausingExceptions();
73+
Assert.assertEquals(2, causes.size());
74+
Assert.assertSame(input1, causes.get(0));
75+
Assert.assertSame(input2, causes.get(1));
76+
}
77+
}
78+
79+
@Test
80+
public void throwForNoFailure() {
81+
ValidationException.throwFor(rootSchema, Collections.emptyList());
82+
}
83+
84+
@Test
85+
public void throwForSingleFailure() {
86+
ValidationException input = new ValidationException(NullSchema.INSTANCE, "msg");
87+
try {
88+
ValidationException.throwFor(rootSchema, Arrays.asList(input));
89+
Assert.fail("did not throw exception for single failure");
90+
} catch (ValidationException actual) {
91+
Assert.assertSame(input, actual);
92+
}
93+
}
94+
5695
}

0 commit comments

Comments
 (0)