Skip to content

Commit

Permalink
improve initialization of @bytesize to be strict, and more #761
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshikawaa committed Oct 19, 2017
1 parent 545070a commit b2f3da6
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@
* Supported types are:
* </p>
* <ul>
* <li>{@code String}</li>
* <li>{@code CharSequence}</li>
* </ul>
* <p>
* {@code null} elements are considered valid. Determine the byte length By encoding the string in the specified
* {@link ByteMax#charset()}. If not specify, encode with charset {@code "UTF-8"}. If specify a charset that can not be used, it
* is thrown {@link IllegalArgumentException}(wrapped in {@link ValidationException}).
* {@link ByteMax#charset()}. If not specify, encode with charset {@code "UTF-8"}.
* An {@link IllegalArgumentException}(wrapped in {@link ValidationException}) is thrown if specify
* {@link ByteMax#charset()} that can not be used or specify {@link ByteMax#value()} that is negative value.
* </p>
* @since 5.1.0
* @see ByteMaxValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@
* Supported types are:
* </p>
* <ul>
* <li>{@code String}</li>
* <li>{@code CharSequence}</li>
* </ul>
* <p>
* {@code null} elements are considered valid. Determine the byte length By encoding the string in the specified
* {@link ByteMin#charset()}. If not specify, encode with charset {@code "UTF-8"}. If specify a charset that can not be used, it
* is thrown {@link IllegalArgumentException}(wrapped in {@link ValidationException}).
* {@link ByteMin#charset()}. If not specify, encode with charset {@code "UTF-8"}.
* An {@link IllegalArgumentException}(wrapped in {@link ValidationException}) is thrown if specify
* {@link ByteMin#charset()} that can not be used or specify {@link ByteMin#value()} that is negative value.
* </p>
* @since 5.1.0
* @see ByteMinValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@
* The annotated element must be a {@link CharSequence}({@link String}, {@link StringBuilder}, etc ...) whose byte length must
* be between the specified minimum and maximum.
* <p>
* This is an annotation combining the functions {@link ByteMin} and {@link ByteMax}. Compared to using two annotations,
* the advantage is that overhead can be reduced by getting byte length at a time.
* </p>
* <p>
* Supported types are:
* </p>
* <ul>
* <li>{@code String}</li>
* <li>{@code CharSequence}</li>
* </ul>
* <p>
* {@code null} elements are considered valid. Determine the byte length By encoding the string in the specified
* {@link ByteSize#charset()}. If not specify, encode with charset {@code "UTF-8"}. If specify a charset that can not be used, it
* is thrown {@link IllegalArgumentException}(wrapped in {@link ValidationException}).
* {@link ByteSize#charset()}. If not specify, encode with charset {@code "UTF-8"}.
* An {@link IllegalArgumentException}(wrapped in {@link ValidationException}) is thrown if specify
* {@link ByteSize#charset()} that can not be used or specify {@link ByteSize#min()} or {@link ByteSize#max()}
* that is negative or specify {@link ByteSize#max()} that lower than {@link ByteSize#min()} value.
* </p>
* @since 5.1.0
* @since 5.4.0
* @see ByteSizeValidator
*/
@Documented
Expand Down Expand Up @@ -77,12 +83,12 @@
/**
* @return value the element's byte length must be higher or equal to
*/
long min();
long min() default 0;

/**
* @return value the element's byte length must be lower or equal to
*/
long max();
long max() default Long.MAX_VALUE;

/**
* @return the charset name used in parse to a string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
* </ul>
* <p>
* {@code null} elements are considered valid. If any of the specified two properties is {@code null}, are considered valid. If
* specify two properties of different types, are considered invalid. If specify a property not {@link Comparable}, it is thrown
* {@link IllegalArgumentException}(wrapped in {@link ValidationException}).
* specify two properties of different types, are considered invalid.
* An {@link IllegalArgumentException}(wrapped in {@link ValidationException}) is thrown if specify a property not {@link Comparable}.
* </p>
* @since 5.1.0
* @see CompareValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ByteMaxValidator implements
/**
* Initialize validator.
* @param constraintAnnotation annotation instance for a given constraint declaration
* @throws IllegalArgumentException failed to get a charset by name.
* @throws IllegalArgumentException failed to get a charset by name, or value is invalid.
* @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
Expand All @@ -61,6 +61,10 @@ public void initialize(ByteMax constraintAnnotation) {
throw reportFailedToInitialize(e);
}
max = constraintAnnotation.value();
if (max < 0) {
throw reportFailedToInitialize(new IllegalArgumentException("value["
+ max + "] must not be negative value."));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ByteMinValidator implements
/**
* Initialize validator.
* @param constraintAnnotation annotation instance for a given constraint declaration
* @throws IllegalArgumentException failed to get a charset by name.
* @throws IllegalArgumentException failed to get a charset by name, or value is invalid.
* @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
Expand All @@ -61,6 +61,10 @@ public void initialize(ByteMin constraintAnnotation) {
throw reportFailedToInitialize(e);
}
min = constraintAnnotation.value();
if (min < 0) {
throw reportFailedToInitialize(new IllegalArgumentException("value["
+ min + "] must not be negative value."));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
/**
* Constraint validator class of {@link ByteSize} annotation.
* <p>
* Validate the {@link CharSequence}({@link String}, {@link StringBuilder}, etc ...) whose byte length must
* be between the specified minimum and maximum. Determine the byte length By encoding the string in the specified charset.
* Validate the {@link CharSequence}({@link String}, {@link StringBuilder}, etc ...) whose byte length must be between the
* specified minimum and maximum. Determine the byte length By encoding the string in the specified charset.
* </p>
* @since 5.1.0
* @since 5.4.0
* @see ConstraintValidator
* @see ByteSize
*/
Expand All @@ -55,7 +55,7 @@ public class ByteSizeValidator implements
/**
* Initialize validator.
* @param constraintAnnotation annotation instance for a given constraint declaration
* @throws IllegalArgumentException failed to get a charset by name, or max is lower than min.
* @throws IllegalArgumentException failed to get a charset by name, or min and max are invalid.
* @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
Expand All @@ -67,6 +67,14 @@ public void initialize(ByteSize constraintAnnotation) {
}
min = constraintAnnotation.min();
max = constraintAnnotation.max();
if (min < 0) {
throw reportFailedToInitialize(new IllegalArgumentException("min["
+ min + "] must not be negative value."));
}
if (max < 0) {
throw reportFailedToInitialize(new IllegalArgumentException("max["
+ max + "] must not be negative value."));
}
if (max < min) {
throw reportFailedToInitialize(new IllegalArgumentException("max["
+ max + "] must be higher or equal to min[" + min + "]."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,41 @@ public static void beforeClass() {

/**
* set {@code ExpectedException} for failed to initialize.
* @param cls expected inner exception.
* @param causeType expected type of inner exception.
*/
protected void setExpectedFailedToInitialize(Class<?> cls) {
protected void setExpectedFailedToInitialize(
Class<? extends Throwable> causeType) {
thrown.expect(ValidationException.class);
thrown.expectCause(allOf(Matchers.<Throwable> instanceOf(
IllegalArgumentException.class), hasProperty("message", is(
MESSAGE_INITIALIZE_ERROR)), hasProperty("cause",
Matchers.<Throwable> instanceOf(cls))));
Matchers.<Throwable> instanceOf(causeType))));
}

/**
* set {@code ExpectedException} for failed to initialize.
* @param cls expected inner exception.
* @param causeType expected type of inner exception.
* @param message expected message of inner exception.
*/
protected void setExpectedFailedToInitialize(Class<?> cls, String message) {
protected void setExpectedFailedToInitialize(
Class<? extends Throwable> causeType, String message) {
thrown.expect(ValidationException.class);
thrown.expectCause(allOf(Matchers.<Throwable> instanceOf(
IllegalArgumentException.class), hasProperty("message", is(
MESSAGE_INITIALIZE_ERROR)), hasProperty("cause", allOf(
Matchers.<Throwable> instanceOf(cls),
Matchers.<Throwable> instanceOf(causeType),
hasProperty("message", is(message))))));
}

/**
* set {@code ExpectedException} for type not support.
* @param cls expected not support type.
* @param causeType expected not support type.
*/
protected void setExpectedTypeNotSupport(Class<?> cls) {
protected void setExpectedTypeNotSupport(Class<?> causeType) {
thrown.expect(ValidationException.class);
thrown.expectCause(allOf(Matchers.<Throwable> instanceOf(
IllegalArgumentException.class), hasProperty("message", is(
String.format(MESSAGE_NOTSUPPORT_ERROR, cls
String.format(MESSAGE_NOTSUPPORT_ERROR, causeType
.getName())))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ public void testSpecifyIllegalCharset() throws Throwable {
}

/**
* specify negative value. expected {@code ValidationException} caused by {@code IllegalArgumentException} that message is
* {@code failed to initialize validator by invalid argument} and nested by {@code IllegalArgumentException} that message is
* {@code value[-1] must not be negative value.}.
* @throws Throwable
*/
@Test
public void testSpecifyNegativeValue() throws Throwable {
setExpectedFailedToInitialize(IllegalArgumentException.class,
"value[-1] must not be negative value.");

validator.validate(form, NegativeValue.class);
}

/**
* specify not support type. expected {@code UnexpectedTypeException}
* @throws Throwable
*/
Expand All @@ -156,6 +170,12 @@ private static interface IllegalCharset {
};

/**
* Validation group value negative.
*/
private static interface NegativeValue {
};

/**
* Validation group unexpected type.
*/
private static interface UnexpectedType {
Expand All @@ -166,7 +186,8 @@ public class ByteMaxTestForm {
@ByteMax(value = 6, charset = "shift-jis", groups = {
SpecifyCharset.class }),
@ByteMax(value = 6, charset = "illegal-charset", groups = {
IllegalCharset.class }) })
IllegalCharset.class }), @ByteMax(value = -1, groups = {
NegativeValue.class }) })
private String stringProperty;

@ByteMax(6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public void testSpecifyIllegalCharset() throws Throwable {
}

/**
* specify negative value. expected {@code ValidationException} caused by {@code IllegalArgumentException} that message is
* {@code failed to initialize validator by invalid argument} and nested by {@code IllegalArgumentException} that message is
* {@code value[-1] must not be negative value.}.
* @throws Throwable
*/
@Test
public void testSpecifyNegativeValue() throws Throwable {
setExpectedFailedToInitialize(IllegalArgumentException.class,
"value[-1] must not be negative value.");

validator.validate(form, NegativeValue.class);
}

/**
* specify not support type. expected {@code UnexpectedTypeException}
* @throws Throwable
*/
Expand All @@ -157,6 +171,12 @@ private static interface IllegalCharset {
};

/**
* Validation group value negative.
*/
private static interface NegativeValue {
};

/**
* Validation group unexpected type.
*/
private static interface UnexpectedType {
Expand All @@ -167,7 +187,8 @@ public class ByteMinTestForm {
@ByteMin(value = 6, charset = "shift-jis", groups = {
SpecifyCharset.class }),
@ByteMin(value = 6, charset = "illegal-charset", groups = {
IllegalCharset.class }) })
IllegalCharset.class }), @ByteMin(value = -1, groups = {
NegativeValue.class }) })
private String stringProperty;

@ByteMin(6)
Expand Down
Loading

0 comments on commit b2f3da6

Please sign in to comment.