Skip to content

Commit 8b31e1f

Browse files
committed
migrated NumberSchema to new ViolationException constructor
1 parent f4bda90 commit 8b31e1f

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@ public NumberSchema(final Builder builder) {
125125
private void checkMaximum(final double subject) {
126126
if (maximum != null) {
127127
if (exclusiveMaximum && maximum.doubleValue() <= subject) {
128-
throw new ValidationException(subject + " is not lower than " + maximum);
128+
throw new ValidationException(this, subject + " is not lower than " + maximum);
129129
} else if (maximum.doubleValue() < subject) {
130-
throw new ValidationException(subject + " is not lower or equal to " + maximum);
130+
throw new ValidationException(this, subject + " is not lower or equal to " + maximum);
131131
}
132132
}
133133
}
134134

135135
private void checkMinimum(final double subject) {
136136
if (minimum != null) {
137137
if (exclusiveMinimum && subject <= minimum.doubleValue()) {
138-
throw new ValidationException(subject + " is not higher than " + minimum);
138+
throw new ValidationException(this, subject + " is not higher than " + minimum);
139139
} else if (subject < minimum.doubleValue()) {
140-
throw new ValidationException(subject + " is not higher or equal to " + minimum);
140+
throw new ValidationException(this, subject + " is not higher or equal to " + minimum);
141141
}
142142
}
143143
}
@@ -147,7 +147,7 @@ private void checkMultipleOf(final double subject) {
147147
BigDecimal remainder = BigDecimal.valueOf(subject).remainder(
148148
BigDecimal.valueOf(multipleOf.doubleValue()));
149149
if (remainder.compareTo(BigDecimal.ZERO) != 0) {
150-
throw new ValidationException(subject + " is not a multiple of " + multipleOf);
150+
throw new ValidationException(this, subject + " is not a multiple of " + multipleOf);
151151
}
152152
}
153153
}
@@ -180,11 +180,11 @@ public boolean requiresInteger() {
180180
public void validate(final Object subject) {
181181
if (!(subject instanceof Number)) {
182182
if (requiresNumber) {
183-
throw new ValidationException(Number.class, subject);
183+
throw new ValidationException(this, Number.class, subject);
184184
}
185185
} else {
186186
if (!(subject instanceof Integer) && requiresInteger) {
187-
throw new ValidationException(Integer.class, subject);
187+
throw new ValidationException(this, Integer.class, subject);
188188
}
189189
double intSubject = ((Number) subject).doubleValue();
190190
checkMinimum(intSubject);

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,76 @@
1515
*/
1616
package org.everit.json.schema;
1717

18-
import org.everit.json.schema.NumberSchema;
19-
import org.everit.json.schema.ValidationException;
2018
import org.junit.Test;
2119

2220
public class NumberSchemaTest {
2321

24-
@Test(expected = ValidationException.class)
22+
@Test
2523
public void exclusiveMinimum() {
26-
NumberSchema.builder().minimum(10.0).exclusiveMinimum(true).build().validate(10);
24+
NumberSchema subject = NumberSchema.builder().minimum(10.0).exclusiveMinimum(true).build();
25+
TestSupport.exceptFailure(subject, 10);
2726
}
2827

29-
@Test(expected = ValidationException.class)
28+
@Test
3029
public void maximum() {
31-
NumberSchema.builder().maximum(20.0).build().validate(21);
30+
NumberSchema subject = NumberSchema.builder().maximum(20.0).build();
31+
TestSupport.exceptFailure(subject, 21);
3232
}
3333

34-
@Test(expected = ValidationException.class)
34+
@Test
3535
public void maximumExclusive() {
36-
NumberSchema.builder().maximum(20.0).exclusiveMaximum(true).build().validate(20);
36+
NumberSchema subject = NumberSchema.builder().maximum(20.0).exclusiveMaximum(true).build();
37+
TestSupport.exceptFailure(subject, 20);
3738
}
3839

39-
@Test(expected = ValidationException.class)
40+
@Test
4041
public void minimumFailure() {
41-
NumberSchema.builder().minimum(10.0).build().validate(9);
42+
NumberSchema subject = NumberSchema.builder().minimum(10.0).build();
43+
TestSupport.exceptFailure(subject, 9);
4244
}
4345

44-
@Test(expected = ValidationException.class)
46+
@Test
4547
public void multipleOfFailure() {
46-
NumberSchema.builder().multipleOf(10).build().validate(15);
48+
NumberSchema subject = NumberSchema.builder().multipleOf(10).build();
49+
TestSupport.exceptFailure(subject, 15);
4750
}
4851

4952
@Test
5053
public void notRequiresNumber() {
5154
NumberSchema.builder().requiresNumber(false).build().validate("foo");
5255
}
5356

54-
@Test(expected = ValidationException.class)
57+
@Test
5558
public void requiresIntegerFailure() {
56-
NumberSchema.builder().requiresInteger(true).build().validate(new Float(10.2));
59+
NumberSchema subject = NumberSchema.builder().requiresInteger(true).build();
60+
TestSupport.exceptFailure(subject, 10.2f);
5761
}
5862

5963
@Test
6064
public void requiresIntegerSuccess() {
6165
NumberSchema.builder().requiresInteger(true).build().validate(10);
6266
}
6367

68+
@Test
69+
public void smallMultipleOf() {
70+
NumberSchema.builder()
71+
.multipleOf(0.0001)
72+
.build().validate(0.0075);
73+
}
74+
6475
@Test
6576
public void success() {
6677
NumberSchema.builder()
67-
.minimum(10.0)
68-
.maximum(11.0)
69-
.exclusiveMaximum(true)
70-
.multipleOf(10)
71-
.build().validate(10.0);
78+
.minimum(10.0)
79+
.maximum(11.0)
80+
.exclusiveMaximum(true)
81+
.multipleOf(10)
82+
.build().validate(10.0);
7283
}
7384

7485
@Test(expected = ValidationException.class)
7586
public void typeFailure() {
7687
NumberSchema.builder().build().validate(null);
7788
}
7889

79-
@Test
80-
public void smallMultipleOf() {
81-
NumberSchema.builder()
82-
.multipleOf(0.0001)
83-
.build().validate(0.0075);
84-
}
85-
8690
}

0 commit comments

Comments
 (0)