Return-Path: X-Original-To: apmail-incubator-bval-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-bval-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9C9B1450D for ; Thu, 2 Jun 2011 17:52:26 +0000 (UTC) Received: (qmail 47083 invoked by uid 500); 2 Jun 2011 17:52:26 -0000 Delivered-To: apmail-incubator-bval-commits-archive@incubator.apache.org Received: (qmail 47042 invoked by uid 500); 2 Jun 2011 17:52:26 -0000 Mailing-List: contact bval-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: bval-dev@incubator.apache.org Delivered-To: mailing list bval-commits@incubator.apache.org Received: (qmail 47035 invoked by uid 99); 2 Jun 2011 17:52:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Jun 2011 17:52:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Jun 2011 17:52:23 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 820FF238897D; Thu, 2 Jun 2011 17:52:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1130692 - in /incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src: main/java/org/apache/bval/constraints/dynamic/ main/java/org/apache/bval/constraints/dynamic/appender/ main/java/org/apache/bval/constraints/dynamic/validato... Date: Thu, 02 Jun 2011 17:52:03 -0000 To: bval-commits@incubator.apache.org From: mbenson@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110602175203.820FF238897D@eris.apache.org> Author: mbenson Date: Thu Jun 2 17:52:02 2011 New Revision: 1130692 URL: http://svn.apache.org/viewvc?rev=1130692&view=rev Log: add values validators for primitive/wrapper/Number+BigX Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java (with props) incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java (with props) incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java (with props) Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/Values.java incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/AbstractGenericMultivaluedConstraintAppender.java incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/ValuesConstraintAppender.java incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/EnumValuesValidator.java incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/StringValuesValidator.java incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/Values.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/Values.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/Values.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/Values.java Thu Jun 2 17:52:02 2011 @@ -31,23 +31,22 @@ import javax.validation.Payload; import org.apache.bval.constraints.dynamic.NullValue.Appender; import org.apache.bval.constraints.dynamic.appender.ValuesConstraintAppender; +import org.apache.bval.constraints.dynamic.validator.BooleanValuesValidator; +import org.apache.bval.constraints.dynamic.validator.CharacterValuesValidator; import org.apache.bval.constraints.dynamic.validator.EnumValuesValidator; +import org.apache.bval.constraints.dynamic.validator.NumberValuesValidator; import org.apache.bval.constraints.dynamic.validator.StringValuesValidator; import org.apache.bval.jsr303.dynamic.ConstraintAppender; /** *

- * Allows the specification of discrete values either allowed or disallowed for - * a property. This class functions as an exercise of the - * {@link ConstraintAppender} portion of the dynamic bean validation - * implementation. + * Allows the specification of discrete values either allowed or disallowed for a property. This class functions as an + * exercise of the {@link ConstraintAppender} portion of the dynamic bean validation implementation. *

*

- * Because of the type limitations on Java annotations, it is not feasible to - * write an open-ended constraint annotation to allow the specification of - * strongly-typed values. It is feasible, however, to write validators - * for various types assuming that a reliable strategy for string conversion of - * a given type is available. + * Because of the type limitations on Java annotations, it is not feasible to write an open-ended constraint annotation + * to allow the specification of strongly-typed values. It is feasible, however, to write validators for + * various types assuming that a reliable strategy for string conversion of a given type is available. *

* * @version $Rev$ $Date$ @@ -55,7 +54,8 @@ import org.apache.bval.jsr303.dynamic.Co @Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER }) @Retention(RUNTIME) -@Constraint(validatedBy = { StringValuesValidator.class, EnumValuesValidator.class }) +@Constraint(validatedBy = { StringValuesValidator.class, EnumValuesValidator.class, NumberValuesValidator.class, + BooleanValuesValidator.class, CharacterValuesValidator.class }) @ConstraintAppender.Use(ValuesConstraintAppender.class) public @interface Values { /** @@ -86,8 +86,7 @@ public @interface Values { Class[] payload() default {}; /** - * List of {@link Values} annotation; typically not used directly, but - * utilized by {@link Appender}. + * List of {@link Values} annotation; typically not used directly, but utilized by {@link Appender}. */ @Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER }) @@ -103,17 +102,17 @@ public @interface Values { } /** - * Labeled values constraint for those occasions where it is necessary for a - * human-consumable label to accompany a data value. Arguably more properly - * the concern of the view technology of an application, this constraint is - * a concession to the concept that there may be times when it is - * appropriate to pair a value and its label so low as an application's - * business/validation layer. + * Labeled values constraint for those occasions where it is necessary for a human-consumable label to accompany a + * data value. Arguably more properly the concern of the view technology of an application, this constraint is a + * concession to the concept that there may be times when it is appropriate to pair a value and its label so low as + * an application's business/validation layer. */ @Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER }) @Retention(RUNTIME) - @Constraint(validatedBy = { StringValuesValidator.Labeled.class, EnumValuesValidator.Labeled.class }) + @Constraint(validatedBy = { StringValuesValidator.Labeled.class, EnumValuesValidator.Labeled.class, + NumberValuesValidator.Labeled.class, BooleanValuesValidator.Labeled.class, + CharacterValuesValidator.Labeled.class }) @ConstraintAppender.Use(ValuesConstraintAppender.Labeled.class) public @interface Labeled { @@ -181,8 +180,7 @@ public @interface Values { } /** - * Describes the intended handling of values specified by - * {@link Values#value()}. + * Describes the intended handling of values specified by {@link Values#value()}. */ public enum Rule { INCLUDE, EXCLUDE; Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/AbstractGenericMultivaluedConstraintAppender.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/AbstractGenericMultivaluedConstraintAppender.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/AbstractGenericMultivaluedConstraintAppender.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/AbstractGenericMultivaluedConstraintAppender.java Thu Jun 2 17:52:02 2011 @@ -101,7 +101,9 @@ public abstract class AbstractGenericMul boolean foundSingles = false; for (Annotation a : collection) { if (multipleConstraintType.isInstance(a)) { - return ArrayUtils.contains(getValue((M) a), constraint); + @SuppressWarnings("unchecked") + boolean result = ArrayUtils.contains(getValue((M) a), constraint); + return result; } foundSingles |= constraintType.isInstance(a); } Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/ValuesConstraintAppender.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/ValuesConstraintAppender.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/ValuesConstraintAppender.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/appender/ValuesConstraintAppender.java Thu Jun 2 17:52:02 2011 @@ -19,11 +19,8 @@ package org.apache.bval.constraints.dyna import javax.validation.Payload; import org.apache.bval.constraints.dynamic.Values; -import org.apache.bval.constraints.dynamic.Values.Labeled; -import org.apache.bval.constraints.dynamic.Values.List; -import org.apache.bval.constraints.dynamic.Values.Strategy; import org.apache.bval.constraints.dynamic.Values.Rule; -import org.apache.bval.constraints.dynamic.Values.Labeled.Item; +import org.apache.bval.constraints.dynamic.Values.Strategy; import org.apache.bval.jsr303.dynamic.ConstraintAppender; import org.apache.commons.proxy2.stub.StubConfigurer; Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java?rev=1130692&view=auto ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java (added) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java Thu Jun 2 17:52:02 2011 @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.constraints.dynamic.validator; + +/** + * Boolean Values validator. + * + * @version $Rev$ $Date$ + */ +public class BooleanValuesValidator extends AbstractValuesValidator.Plain { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Boolean value, String choice) { + return value.toString().equals(choice); + } + + /** + * Labeled Boolean Values validator. + */ + public static class Labeled extends AbstractValuesValidator.Labeled { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Boolean value, String choice) { + return value.toString().equals(choice); + } + + } +} Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/BooleanValuesValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java?rev=1130692&view=auto ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java (added) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java Thu Jun 2 17:52:02 2011 @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.constraints.dynamic.validator; + +/** + * Character values validator. + * + * @version $Rev$ $Date$ + */ +public class CharacterValuesValidator extends AbstractValuesValidator.Plain { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Character value, String choice) { + return choice.length() == 1 && value.equals(choice.charAt(0)); + } + + /** + * Labeled Character values validator. + */ + public static class Labeled extends AbstractValuesValidator.Labeled { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Character value, String choice) { + return choice.length() == 1 && value.equals(choice.charAt(0)); + } + + } +} Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/CharacterValuesValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/EnumValuesValidator.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/EnumValuesValidator.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/EnumValuesValidator.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/EnumValuesValidator.java Thu Jun 2 17:52:02 2011 @@ -17,7 +17,6 @@ package org.apache.bval.constraints.dynamic.validator; import org.apache.bval.constraints.dynamic.Values; -import org.apache.bval.constraints.dynamic.Values.Labeled; /** * Validates {@link Values} annotations against enum constants. Added: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java?rev=1130692&view=auto ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java (added) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java Thu Jun 2 17:52:02 2011 @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.constraints.dynamic.validator; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * Number Values validator. + * + * @version $Rev$ $Date$ + */ +public class NumberValuesValidator extends AbstractValuesValidator.Plain { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Number value, String choice) { + return test(value, choice); + } + + /** + * Labeled Number Values validator. + */ + public static class Labeled extends AbstractValuesValidator.Labeled { + + /** + * {@inheritDoc} + */ + @Override + protected boolean matches(Number value, String choice) { + return test(value, choice); + } + + } + + private static boolean test(Number value, String choice) { + try { + // BigDecimal/BigInteger !final; test class/name + if (value.getClass().getName().equals(BigDecimal.class.getName())) { + return new BigDecimal(choice).equals(value); + } + if (value.getClass().getName().equals(BigInteger.class.getName())) { + return new BigInteger(choice).equals(value); + } + if (value instanceof Double) { + return Double.valueOf(choice).equals(value); + } + if (value instanceof Float) { + return Float.valueOf(choice).equals(value); + } + if (value instanceof Long) { + return Long.valueOf(choice).equals(value); + } + if (value instanceof Integer) { + return Integer.valueOf(choice).equals(value); + } + if (value instanceof Short) { + return Short.valueOf(choice).equals(value); + } + if (value instanceof Byte) { + return Byte.valueOf(choice).equals(value); + } + } catch (NumberFormatException e) { + // ignore + } + // by default, simply compare toString() + return value.toString().equals(choice); + } +} Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/NumberValuesValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/StringValuesValidator.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/StringValuesValidator.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/StringValuesValidator.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/constraints/dynamic/validator/StringValuesValidator.java Thu Jun 2 17:52:02 2011 @@ -20,7 +20,6 @@ import javax.validation.ConstraintValida import javax.validation.Payload; import org.apache.bval.constraints.dynamic.Values; -import org.apache.bval.constraints.dynamic.Values.Labeled; import org.apache.commons.lang3.StringUtils; /** Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java?rev=1130692&r1=1130691&r2=1130692&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java (original) +++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java Thu Jun 2 17:52:02 2011 @@ -20,6 +20,8 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.assertNull; import java.lang.annotation.ElementType; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Locale; import javax.validation.ConstraintValidator; @@ -125,11 +127,133 @@ public class ValuesConstraintValidationT String labeledIgnoreCaseTrimExclude; } + public static class PrimitiveBean { + @Values({ "0", "1", "2" }) + byte b; + @Values.Labeled({ @Item(label = "zero", value = "0"), @Item(label = "one", value = "1"), + @Item(label = "two", value = "2") }) + short s; + @Values({ "0", "1", "2" }) + int i; + @Values.Labeled({//@formatter:off + @Item(label = "zero", value = "0"), + @Item(label = "one", value = "1"), + @Item(label = "two", value = "2") + })//@formatter:on + long l; + @Values(value = { "Infinity", "-Infinity", "NaN" }, rule = Rule.EXCLUDE) + float f; + @Values.Labeled({ @Item(label = "zero", value = "0.0"), @Item(label = "one", value = "1.0"), + @Item(label = "two", value = "2.0") }) + double d; + @Values({ "true" }) + boolean bool; + @Values.Labeled({ @Item(label = "'a'", value = "a"), @Item(label = "'b'", value = "b"), + @Item(label = "'c'", value = "c") }) + char c; + + /** + * Create a new ValuesConstraintValidationTest.PrimitiveBean instance. + */ + public PrimitiveBean() { + reset(); + } + + void reset() { + b = 0; + s = 0; + i = 0; + l = 0L; + f = 0.0f; + d = 0.0d; + bool = true; + c = 'a'; + } + } + + public static class WrapperBean { + @Values({ "0", "1", "2" }) + Byte b; + @Values.Labeled({ @Item(label = "zero", value = "0"), @Item(label = "one", value = "1"), + @Item(label = "two", value = "2") }) + Short s; + @Values({ "0", "1", "2" }) + Integer i; + @Values.Labeled({//@formatter:off + @Item(label = "zero", value = "0"), + @Item(label = "one", value = "1"), + @Item(label = "two", value = "2") + })//@formatter:on + Long l; + @Values(value = { "Infinity", "-Infinity", "NaN" }, rule = Rule.EXCLUDE) + Float f; + @Values.Labeled({ @Item(label = "zero", value = "0.0"), @Item(label = "one", value = "1.0"), + @Item(label = "two", value = "2.0") }) + Double d; + @Values({ "true" }) + Boolean bool; + @Values.Labeled({ @Item(label = "'a'", value = "a"), @Item(label = "'b'", value = "b"), + @Item(label = "'c'", value = "c") }) + Character c; + + /** + * Create a new ValuesConstraintValidationTest.WrapperBean instance. + */ + public WrapperBean() { + clear(); + } + + void clear() { + b = null; + s = null; + i = null; + l = null; + f = null; + d = null; + bool = null; + c = null; + } + + void copyFrom(PrimitiveBean prototype) { + b = prototype.b; + s = prototype.s; + i = prototype.i; + l = prototype.l; + f = prototype.f; + d = prototype.d; + bool = prototype.bool; + c = prototype.c; + } + } + + public static class BigNumberBean { + @Values.Labeled({ @Item(label = "B", value = "1"), @Item(label = "K", value = "1024"), + @Item(label = "M", value = "1048576"), @Item(label = "G", value = "1073741824") }) + BigInteger bigI; + @Values({ "1024e0", "1024e-1", "1024e-2", "1024e-3" }) + BigDecimal bigD; + + /** + * Create a new ValuesConstraintValidationTest.BigNumberBean instance. + */ + public BigNumberBean() { + clear(); + } + + void clear() { + bigI = null; + bigD = null; + } + } + private Validator validator; private DynamicValidatorContext validatorContext; private final AnnotationFactory annotationFactory = new AnnotationFactory(); private TestBean testBean; private StringBean stringBean; + private PrimitiveBean primitiveBean; + private WrapperBean wrapperBean; + private BigNumberBean bigNumberBean; @Before public void setup() { @@ -145,6 +269,9 @@ public class ValuesConstraintValidationT validator = validatorContext.getValidator(); testBean = new TestBean(); stringBean = new StringBean(); + primitiveBean = new PrimitiveBean(); + wrapperBean = new WrapperBean(); + bigNumberBean = new BigNumberBean(); } @Test @@ -717,6 +844,56 @@ public class ValuesConstraintValidationT .getConstraintDescriptors().size()); } + @Test + public void testValidatePrimitiveValues() throws Exception { + assertEquals(0, validator.validate(primitiveBean).size()); + primitiveBean.bool = false; + assertEquals(1, validator.validate(primitiveBean).size()); + primitiveBean.reset(); + primitiveBean.f = Float.POSITIVE_INFINITY; + assertEquals(1, validator.validate(primitiveBean).size()); + primitiveBean.f = Float.NEGATIVE_INFINITY; + assertEquals(1, validator.validate(primitiveBean).size()); + primitiveBean.f = Float.NaN; + assertEquals(1, validator.validate(primitiveBean).size()); + } + + @Test + public void testValidateWrapperValues() throws Exception { + assertEquals(0, validator.validate(wrapperBean).size()); + wrapperBean.copyFrom(primitiveBean); + assertEquals(0, validator.validate(wrapperBean).size()); + wrapperBean.c = 'x'; + assertEquals(1, validator.validate(wrapperBean).size()); + wrapperBean.copyFrom(primitiveBean); + wrapperBean.f = Float.POSITIVE_INFINITY; + assertEquals(1, validator.validate(wrapperBean).size()); + wrapperBean.f = Float.NEGATIVE_INFINITY; + assertEquals(1, validator.validate(wrapperBean).size()); + wrapperBean.f = Float.NaN; + assertEquals(1, validator.validate(wrapperBean).size()); + } + + @Test + public void testValidateBigNumberValues() throws Exception { + assertEquals(0, validator.validate(bigNumberBean).size()); + BigInteger ki = BigInteger.valueOf(2).pow(10); + for (int n = 0; n < 4; n++) { + bigNumberBean.bigI = ki.pow(n); + assertEquals(0, validator.validate(bigNumberBean).size()); + bigNumberBean.bigI = bigNumberBean.bigI.add(BigInteger.ONE); + assertEquals(1, validator.validate(bigNumberBean).size()); + } + bigNumberBean.clear(); + BigDecimal kd = new BigDecimal(2).pow(10); + for (int n = 0; n < 4; n++) { + bigNumberBean.bigD = kd.divide(BigDecimal.TEN.pow(n)); + assertEquals(0, validator.validate(bigNumberBean).size()); + bigNumberBean.bigD = bigNumberBean.bigD.add(BigDecimal.ONE); + assertEquals(1, validator.validate(bigNumberBean).size()); + } + } + public interface X { }