Skip to content

Commit 349616a

Browse files
committed
added ValidationExceptionTest#getMessageAfterPrepend() test, added javadoc
1 parent ca9c5e1 commit 349616a

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ public class ValidationException extends RuntimeException {
3232
* create {@code ValidationException}s, handling the case of multiple violations occuring during
3333
* validation.
3434
*
35-
* <p>
3635
* <ul>
3736
* <li>If {@code failures} is empty, then it doesn't do anything</li>
3837
* <li>If {@code failures} contains 1 exception instance, then that will be thrown</li>
3938
* <li>Otherwise a new exception instance will be created, its {@link #getViolatedSchema()
4039
* violated schema} will be {@code rootFailingSchema}, and its {@link #getCausingExceptions()
4140
* causing exceptions} will be the {@code failures} list</li>
4241
* </ul>
43-
* </p>
4442
*
43+
* @param rootFailingSchema
44+
* the schema which detected the {@code failures}
45+
* @param failures
46+
* list containing validation failures to be thrown by this method
4547
*/
4648
public static void throwFor(final Schema rootFailingSchema,
4749
final List<ValidationException> failures) {
@@ -63,6 +65,11 @@ public static void throwFor(final Schema rootFailingSchema,
6365

6466
/**
6567
* Deprecated, use {@code ValidationException(Schema, Class<?>, Object)} instead.
68+
*
69+
* @param expectedType
70+
* the expected type
71+
* @param actualValue
72+
* the violating value
6673
*/
6774
@Deprecated
6875
public ValidationException(final Class<?> expectedType, final Object actualValue) {
@@ -71,6 +78,13 @@ public ValidationException(final Class<?> expectedType, final Object actualValue
7178

7279
/**
7380
* Constructor.
81+
*
82+
* @param violatedSchema
83+
* the schema instance which detected the schema violation
84+
* @param expectedType
85+
* the expected type
86+
* @param actualValue
87+
* the violating value
7488
*/
7589
public ValidationException(final Schema violatedSchema, final Class<?> expectedType,
7690
final Object actualValue) {
@@ -87,12 +101,30 @@ private ValidationException(final Schema rootFailingSchema,
87101
causingExceptions);
88102
}
89103

104+
/**
105+
* Constructor.
106+
*
107+
* @param violatedSchema
108+
* the schema instance which detected the schema violation
109+
* @param message
110+
* the readable exception message
111+
*/
90112
public ValidationException(final Schema violatedSchema, final String message) {
91113
this(violatedSchema, new StringBuilder("#"), message, Collections.emptyList());
92114
}
93115

94116
/***
95117
* Constructor.
118+
*
119+
* @param violatedSchema
120+
* the schema instance which detected the schema violation
121+
* @param pointerToViolation
122+
* a JSON pointer denoting the part of the document which violates the schema
123+
* @param message
124+
* the readable exception message
125+
* @param causingExceptions
126+
* a (possibly empty) list of validation failures. It is used if multiple schema
127+
* violations are found by violatedSchema
96128
*/
97129
public ValidationException(final Schema violatedSchema, final StringBuilder pointerToViolation,
98130
final String message,
@@ -125,6 +157,11 @@ public List<ValidationException> getCausingExceptions() {
125157
return causingExceptions;
126158
}
127159

160+
@Override
161+
public String getMessage() {
162+
return getPointerToViolation() + ": " + super.getMessage();
163+
}
164+
128165
public String getPointerToViolation() {
129166
return pointerToViolation.toString();
130167
}
@@ -162,7 +199,7 @@ public ValidationException prepend(final String fragment, final Schema violatedS
162199
List<ValidationException> prependedCausingExceptions = causingExceptions.stream()
163200
.map(exc -> exc.prepend(fragment))
164201
.collect(Collectors.toList());
165-
return new ValidationException(newPointer, violatedSchema, getMessage(),
202+
return new ValidationException(newPointer, violatedSchema, super.getMessage(),
166203
prependedCausingExceptions);
167204
}
168205

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ public void multipleSchemaDepViolation() {
8888
.getCausingExceptions().get(1);
8989
Assert.assertEquals("#/billing_name", billingNameFailure.getPointerToViolation());
9090
Assert.assertEquals(billingNameSchema, billingNameFailure.getViolatedSchema());
91-
9291
Assert.assertEquals("#", ageFailure.getPointerToViolation());
93-
Assert.assertEquals("required key [age] not found", ageFailure.getMessage());
92+
Assert.assertEquals("#: required key [age] not found", ageFailure.getMessage());
9493
}
9594
}
9695

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ private ValidationException createDummyException(final String pointer) {
3737
"stuff went wrong", Collections.emptyList());
3838
}
3939

40+
@Test
41+
public void getMessageAfterPrepend() {
42+
ValidationException subject = createDummyException("#/a").prepend("obj");
43+
System.out.println(subject.getMessage());
44+
Assert.assertEquals("#/obj/a: stuff went wrong", subject.getMessage());
45+
}
46+
4047
@Test(expected = NullPointerException.class)
4148
public void nullPointerFragmentFailure() {
4249
new ValidationException(BooleanSchema.INSTANCE, Boolean.class, 2).prepend(null,
@@ -92,7 +99,7 @@ public void throwForMultipleFailures() {
9299
Assert.fail("did not throw exception for 2 input exceptions");
93100
} catch (ValidationException e) {
94101
Assert.assertSame(rootSchema, e.getViolatedSchema());
95-
Assert.assertEquals("2 schema violations found", e.getMessage());
102+
Assert.assertEquals("#: 2 schema violations found", e.getMessage());
96103
List<ValidationException> causes = e.getCausingExceptions();
97104
Assert.assertEquals(2, causes.size());
98105
Assert.assertSame(input1, causes.get(0));

0 commit comments

Comments
 (0)