StringSchema extends Schema { public static Builder builder() { return new Builder(); } StringSchema();  StringSchema(final Builder builder); static Builder builder(); Integer getMaxLength(); Integer getMinLength(); java.util.regex.Pattern getPattern(); @Override boolean equals(Object o); FormatValidator getFormatValidator(); @Override int hashCode(); boolean requireString();  }
@Test public void formatFailure() { StringSchema subject = buildWithLocation(StringSchema.builder() .formatValidator(subj -> Optional.of("violation"))); TestSupport.failureOf(subject) .expectedKeyword("format") .input("string") .expect(); }
@Test public void formatSuccess() { StringSchema subject = StringSchema.builder().formatValidator(subj -> Optional.empty()).build(); subject.validate("string"); }
@Test public void maxLength() { StringSchema subject = buildWithLocation(StringSchema.builder().maxLength(3)); TestSupport.failureOf(subject) .expectedKeyword("maxLength") .input("foobar") .expect(); }
@Test public void minLength() { StringSchema subject = buildWithLocation(StringSchema.builder().minLength(2)); TestSupport.failureOf(subject) .expectedKeyword("minLength") .input("a") .expect(); }
@Test public void multipleViolations() { try { StringSchema.builder().minLength(3).maxLength(1).pattern("^b.*").build().validate("ab"); Assert.fail(); } catch (ValidationException e) { Assert.assertEquals(3, e.getCausingExceptions().size()); } }
@Test public void notRequiresString() { StringSchema.builder().requiresString(false).build().validate(2); }
@Test public void patternFailure() { StringSchema subject = buildWithLocation(StringSchema.builder().pattern("^a*$")); TestSupport.failureOf(subject).expectedKeyword("pattern").input("abc").expect(); }
@Test public void patternSuccess() { StringSchema.builder().pattern("^a*$").build().validate("aaaa"); }
@Test public void success() { StringSchema.builder().build().validate("foo"); }
@Test public void typeFailure() { TestSupport.failureOf(StringSchema.builder()) .expectedKeyword("type") .input(null) .expect(); }
@Test(expected = ValidationException.class) public void issue38Pattern() { StringSchema.builder().requiresString(true).pattern("\\+?\\d+").build().validate("aaa"); }
@Test public void toString_ReadOnlyWriteOnly() { Schema subject = StringSchema.builder().readOnly(true).writeOnly(false).build(); JSONObject actual = new JSONObject(subject.toString()); JSONObject expected = ResourceLoader.DEFAULT.readObj("tostring/stringschema-readonly-true-writeonly-false.json"); assertThat(actual, sameJsonAs(expected)); }
@Test public void requiresString_nullable() { Schema subject = StringSchema.builder().requiresString(true).nullable(true).build(); subject.validate(JSONObject.NULL); }