Skip to content

Commit c17db30

Browse files
committed
migrated StringSchema to new ViolationException constructor
1 parent 8b31e1f commit c17db30

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ public Pattern getPattern() {
111111
private void testLength(final String subject) {
112112
int actualLength = subject.length();
113113
if (minLength != null && actualLength < minLength.intValue()) {
114-
throw new ValidationException("expected minLength: " + minLength + ", actual: "
114+
throw new ValidationException(this, "expected minLength: " + minLength + ", actual: "
115115
+ actualLength);
116116
}
117117
if (maxLength != null && actualLength > maxLength.intValue()) {
118-
throw new ValidationException("expected maxLength: " + maxLength + ", actual: "
118+
throw new ValidationException(this, "expected maxLength: " + maxLength + ", actual: "
119119
+ actualLength);
120120
}
121121
}
122122

123123
private void testPattern(final String subject) {
124124
if (pattern != null && !pattern.matcher(subject).find()) {
125-
throw new ValidationException(String.format("string [%s] does not match pattern %s",
125+
throw new ValidationException(this, String.format("string [%s] does not match pattern %s",
126126
subject, pattern.pattern()));
127127
}
128128
}
@@ -131,7 +131,7 @@ private void testPattern(final String subject) {
131131
public void validate(final Object subject) {
132132
if (!(subject instanceof String)) {
133133
if (requiresString) {
134-
throw new ValidationException(String.class, subject);
134+
throw new ValidationException(this, String.class, subject);
135135
}
136136
} else {
137137
String stringSubject = (String) subject;

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

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

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

2220
public class StringSchemaTest {
2321

24-
@Test(expected = ValidationException.class)
22+
@Test
2523
public void maxLength() {
26-
StringSchema.builder().maxLength(3).build().validate("foobar");
24+
StringSchema subject = StringSchema.builder().maxLength(3).build();
25+
TestSupport.exceptFailure(subject, "foobar");
2726
}
2827

29-
@Test(expected = ValidationException.class)
28+
@Test
3029
public void minLength() {
31-
StringSchema.builder().minLength(2).build().validate("a");
30+
StringSchema subject = StringSchema.builder().minLength(2).build();
31+
TestSupport.exceptFailure(subject, "a");
3232
}
3333

3434
@Test
35-
public void success() {
36-
StringSchema.builder().build().validate("foo");
35+
public void notRequiresString() {
36+
StringSchema.builder().requiresString(false).build().validate(2);
3737
}
3838

39-
@Test(expected = ValidationException.class)
40-
public void typeFailure() {
41-
StringSchema.builder().build().validate(null);
39+
@Test
40+
public void patternFailure() {
41+
StringSchema subject = StringSchema.builder().pattern("^a*$").build();
42+
TestSupport.exceptFailure(subject, "abc");
4243
}
4344

4445
@Test
4546
public void patternSuccess() {
4647
StringSchema.builder().pattern("^a*$").build().validate("aaaa");
4748
}
4849

49-
@Test(expected = ValidationException.class)
50-
public void patternFailure() {
51-
StringSchema.builder().pattern("^a*$").build().validate("abc");
50+
@Test
51+
public void success() {
52+
StringSchema.builder().build().validate("foo");
5253
}
5354

5455
@Test
55-
public void notRequiresString() {
56-
StringSchema.builder().requiresString(false).build().validate(2);
56+
public void typeFailure() {
57+
TestSupport.exceptFailure(StringSchema.builder().build(), null);
5758
}
5859
}

0 commit comments

Comments
 (0)