Hibernate SVN: r16764 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-06-11 15:44:59 -0400 (Thu, 11 Jun 2009)
New Revision: 16764
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
Log:
HV-169 concurrency problem
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11 15:54:30 UTC (rev 16763)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11 19:44:59 UTC (rev 16764)
@@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Constraint;
import javax.validation.ConstraintDefinitionException;
import javax.validation.ConstraintValidator;
@@ -83,10 +84,10 @@
public class ConstraintHelper {
private final Map<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<?, ?>>>> builtinConstraints =
- new HashMap<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<?, ?>>>>();
+ new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<?, ?>>>>();
private final Map<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>> constraintValidatorDefinitons =
- new HashMap<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>>();
+ new ConcurrentHashMap<Class<? extends Annotation>, List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>>();
public ConstraintHelper() {
@@ -275,7 +276,7 @@
*
* @return <code>true</code> if the annotation fulfills the above condtions, <code>false</code> otherwise.
*/
- public boolean isConstraintAnnotation(Annotation annotation) {
+ public static boolean isConstraintAnnotation(Annotation annotation) {
Constraint constraint = annotation.annotationType()
.getAnnotation( Constraint.class );
15 years, 5 months
Hibernate SVN: r16763 - in beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests: validation and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 11:54:30 -0400 (Thu, 11 Jun 2009)
New Revision: 16763
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/Dummy.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java
Log:
Added more tests from Hibernate Validator
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-06-11 15:54:30 UTC (rev 16763)
@@ -0,0 +1,122 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.jsr303.tck.tests.validation.validatorcontext;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import org.testng.annotations.Test;
+import org.jboss.testharness.AbstractTest;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.ArtifactType;
+import org.jboss.testharness.impl.packaging.Classes;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
+public class ConstraintValidatorContextTest extends AbstractTest {
+
+ @Test
+ public void testNoCustomization() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ DummyValidator.disableDefaultError( false );
+ DummyValidator.setErrorMessages( null );
+
+ DummyBean bean = new DummyBean( "foobar" );
+
+ Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "dummy message", constraintViolation.getMessage(), "Wrong message" );
+ }
+
+ /**
+ * @todo Is this the right behaviour? The spec is not quite clear about this.
+ */
+ @Test
+ public void testDisableDefaultErrorWithoutCustomError() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ DummyValidator.disableDefaultError( true );
+ Map<String, String> errors = new HashMap<String, String>();
+ DummyValidator.setErrorMessages( errors );
+
+ DummyBean bean = new DummyBean( "foobar" );
+
+ Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
+ }
+
+ @Test
+ public void testDisableDefaultErrorWithCustomErrors() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ DummyValidator.disableDefaultError( true );
+ Map<String, String> errors = new HashMap<String, String>();
+ errors.put( "message1", "property1" );
+ DummyValidator.setErrorMessages( errors );
+
+ DummyBean bean = new DummyBean( "foobar" );
+
+ Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "message1", constraintViolation.getMessage(), "Wrong message" );
+ assertEquals( "property1", constraintViolation.getPropertyPath(), "Wrong property" );
+ }
+
+ @Test
+ public void testNestedValidation() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ DummyValidator.disableDefaultError( false );
+ DummyValidator.setErrorMessages( null );
+
+ DummyBean bean = new DummyBean( "foo" );
+ bean.setNestedDummy( new DummyBean( "bar" ) );
+
+ Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
+ assertEquals( constraintViolations.size(), 2, "Wrong number of constraints" );
+ boolean validatedNestedBean = false;
+ for ( ConstraintViolation<DummyBean> violation : constraintViolations ) {
+
+ if ( violation.getPropertyPath().equals( "value" ) ) {
+ assertEquals( "dummy message", violation.getMessage(), "Wrong message" );
+ }
+ else if ( violation.getPropertyPath().equals( "nestedDummy.value" ) ) {
+ assertEquals( "dummy message", violation.getMessage(), "Wrong message" );
+ validatedNestedBean = true;
+ }
+ else {
+ fail( "Wrong property " + violation.getMessage() );
+ }
+ }
+ assertTrue( validatedNestedBean );
+ }
+}
Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/Dummy.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/Dummy.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/Dummy.java 2009-06-11 15:54:30 UTC (rev 16763)
@@ -0,0 +1,39 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.jsr303.tck.tests.validation.validatorcontext;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Documented
+@Constraint(validatedBy = DummyValidator.class)
+@Target({ METHOD, FIELD })
+@Retention(RUNTIME)
+public @interface Dummy {
+ String message() default "dummy message";
+
+ Class<?>[] groups() default { };
+}
\ No newline at end of file
Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/Dummy.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java 2009-06-11 15:54:30 UTC (rev 16763)
@@ -0,0 +1,40 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.jsr303.tck.tests.validation.validatorcontext;
+
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DummyBean {
+
+ @Dummy
+ String value;
+
+ @Valid
+ DummyBean nestedDummy;
+
+ public DummyBean(String value) {
+ this.value = value;
+ }
+
+ public void setNestedDummy(DummyBean nestedDummy) {
+ this.nestedDummy = nestedDummy;
+ }
+}
Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java 2009-06-11 15:54:30 UTC (rev 16763)
@@ -0,0 +1,58 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.jsr303.tck.tests.validation.validatorcontext;
+
+import java.util.Map;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DummyValidator implements ConstraintValidator<Dummy, String> {
+
+ private static boolean disableDefaultError;
+
+ private static Map<String, String> errorMessages;
+
+
+ public void initialize(Dummy parameters) {
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
+ if ( disableDefaultError ) {
+ constraintValidatorContext.disableDefaultError();
+ }
+
+ if ( errorMessages != null ) {
+ for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) {
+ constraintValidatorContext.addError( entry.getKey(), entry.getValue() );
+ }
+ }
+
+ return false;
+ }
+
+ public static void disableDefaultError(boolean b) {
+ disableDefaultError = b;
+ }
+
+ public static void setErrorMessages(Map<String, String> errorMessages) {
+ DummyValidator.errorMessages = errorMessages;
+ }
+}
\ No newline at end of file
Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
15 years, 5 months
Hibernate SVN: r16762 - in beanvalidation/trunk: validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 11:53:57 -0400 (Thu, 11 Jun 2009)
New Revision: 16762
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/constraints/DecimalMax.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java
Log:
Enabled tests for DecimalMin and DecimalMax
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/constraints/DecimalMax.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/constraints/DecimalMax.java 2009-06-11 15:51:21 UTC (rev 16761)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/constraints/DecimalMax.java 2009-06-11 15:53:57 UTC (rev 16762)
@@ -38,17 +38,17 @@
* <li><code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>,
* and their respective wrappers</li>
* </ul>
- * Note that <code>double</code> and <code>float</code> are not supported due to rounding errors
+ * Note that <code>double</code> and <code>float</code> are not supported due to rounding errors
* (some providers might provide some approximative support)
* <p/>
- * <code>null</code> elements are considered valid
+ * <code>null</code> elements are considered valid.
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
-@Constraint(validatedBy = {})
+@Constraint(validatedBy = { })
public @interface DecimalMax {
String message() default "{javax.validation.constraints.DecimalMax.message}";
@@ -57,20 +57,21 @@
/**
* The String representation of the max value according to the
* BigDecimal string representation
+ *
* @return value the element must be lower or equal to
*/
String value();
/**
* Defines several @DecimalMax annotations on the same element
- * @see {@link DecimalMax}
*
* @author Emmanuel Bernard
+ * @see {@link DecimalMax}
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
- @interface List {
+ @interface List {
DecimalMax[] value();
}
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java 2009-06-11 15:51:21 UTC (rev 16761)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java 2009-06-11 15:53:57 UTC (rev 16762)
@@ -31,6 +31,8 @@
import javax.validation.Validator;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
@@ -47,7 +49,6 @@
import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ArtifactType;
import org.jboss.testharness.impl.packaging.Classes;
-import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.jsr303.tck.util.TestUtil;
@@ -265,24 +266,105 @@
assertCorrectNumberOfViolations( constraintViolations, 0 );
}
- @Test(enabled = false)
+ @Test
@SpecAssertions({
@SpecAssertion(section = "6", id = "a"),
@SpecAssertion(section = "6", id = "i")
})
public void testDecimalMinConstraint() {
Validator validator = TestUtil.getDefaultValidator();
- fail();
+ DecimalMinDummyEntity dummy = new DecimalMinDummyEntity();
+
+ Set<ConstraintViolation<DecimalMinDummyEntity>> constraintViolations = validator.validate( dummy );
+ // only the min constraints on the primitive values should fail. Object values re still null and should pass per spec
+ assertCorrectNumberOfViolations( constraintViolations, 4 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "bytePrimitive", "intPrimitive", "longPrimitive", "shortPrimitive" }
+ );
+
+
+ dummy.intPrimitive = 101;
+ dummy.longPrimitive = 1001;
+ dummy.bytePrimitive = 111;
+ dummy.shortPrimitive = 142;
+
+ dummy.intObject = Integer.valueOf( "100" );
+ dummy.longObject = Long.valueOf( "0" );
+ dummy.byteObject = Byte.parseByte( "-1" );
+ dummy.shortObject = Short.parseShort( "3" );
+ dummy.bigDecimal = BigDecimal.valueOf( 100.9 );
+ dummy.bigInteger = BigInteger.valueOf( 100 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 6 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "byteObject", "intObject", "longObject", "shortObject", "bigDecimal", "bigInteger" }
+ );
+
+ dummy.intObject = Integer.valueOf( "101" );
+ dummy.longObject = Long.valueOf( "12345" );
+ dummy.byteObject = Byte.parseByte( "102" );
+ dummy.shortObject = Short.parseShort( "111" );
+ dummy.bigDecimal = BigDecimal.valueOf( 101.1 );
+ dummy.bigInteger = BigInteger.valueOf( 101 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
}
- @Test(enabled = false)
+ @Test
@SpecAssertions({
@SpecAssertion(section = "6", id = "a"),
@SpecAssertion(section = "6", id = "j")
})
public void testDecimalMaxConstraint() {
Validator validator = TestUtil.getDefaultValidator();
- fail();
+ DecimalMaxDummyEntity dummy = new DecimalMaxDummyEntity();
+
+ dummy.intPrimitive = 102;
+ dummy.longPrimitive = 1234;
+ dummy.bytePrimitive = 102;
+ dummy.shortPrimitive = 102;
+
+ Set<ConstraintViolation<DecimalMaxDummyEntity>> constraintViolations = validator.validate( dummy );
+ // only the max constraints on the primitive values should fail. Object values re still null and should pass per spec
+ assertCorrectNumberOfViolations( constraintViolations, 4 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "bytePrimitive", "intPrimitive", "longPrimitive", "shortPrimitive" }
+ );
+
+
+ dummy.intPrimitive = 101;
+ dummy.longPrimitive = 100;
+ dummy.bytePrimitive = 99;
+ dummy.shortPrimitive = 42;
+
+ dummy.intObject = Integer.valueOf( "102" );
+ dummy.longObject = Long.valueOf( "12345" );
+ dummy.byteObject = Byte.parseByte( "111" );
+ dummy.shortObject = Short.parseShort( "1234" );
+ dummy.bigDecimal = BigDecimal.valueOf( 102 );
+ dummy.bigInteger = BigInteger.valueOf( 102 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 6 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "byteObject", "intObject", "longObject", "shortObject", "bigDecimal", "bigInteger" }
+ );
+
+ dummy.intObject = Integer.valueOf( "101" );
+ dummy.longObject = Long.valueOf( "100" );
+ dummy.byteObject = Byte.parseByte( "100" );
+ dummy.shortObject = Short.parseShort( "101" );
+ dummy.bigDecimal = BigDecimal.valueOf( 100.9 );
+ dummy.bigInteger = BigInteger.valueOf( 100 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
}
@Test
@@ -606,6 +688,70 @@
Long longObject;
}
+ class DecimalMaxDummyEntity {
+ @DecimalMax("101.000000000")
+ BigDecimal bigDecimal;
+
+ @DecimalMax("1.01E+2")
+ BigInteger bigInteger;
+
+ @DecimalMax("101")
+ byte bytePrimitive;
+
+ @DecimalMax("101")
+ short shortPrimitive;
+
+ @DecimalMax("101")
+ int intPrimitive;
+
+ @DecimalMax("101")
+ long longPrimitive;
+
+ @DecimalMax("101")
+ Byte byteObject;
+
+ @DecimalMax("101")
+ Short shortObject;
+
+ @DecimalMax("101")
+ Integer intObject;
+
+ @DecimalMax("101")
+ Long longObject;
+ }
+
+ class DecimalMinDummyEntity {
+ @DecimalMin("101.000000000")
+ BigDecimal bigDecimal;
+
+ @DecimalMin("1.01E+2")
+ BigInteger bigInteger;
+
+ @DecimalMin("101")
+ byte bytePrimitive;
+
+ @DecimalMin("101")
+ short shortPrimitive;
+
+ @DecimalMin("101")
+ int intPrimitive;
+
+ @DecimalMin("101")
+ long longPrimitive;
+
+ @DecimalMin("101")
+ Byte byteObject;
+
+ @DecimalMin("101")
+ Short shortObject;
+
+ @DecimalMin("101")
+ Integer intObject;
+
+ @DecimalMin("101")
+ Long longObject;
+ }
+
class SizeDummyEntity {
@Size(min = 1, max = 1)
String string;
15 years, 5 months
Hibernate SVN: r16761 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validation/engine and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 11:51:21 -0400 (Thu, 11 Jun 2009)
New Revision: 16761
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java
Log:
HV-168 Added validators for DecimalMin and DecimalMax
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMax;
+
+/**
+ * Check that the number being validated is less than or equal to the maximum
+ * value specified.
+ *
+ * @author Hardy Ferentschik
+ */
+public class DecimalMaxValidatorForNumber implements ConstraintValidator<DecimalMax, Number> {
+
+ private BigDecimal maxValue;
+
+ public void initialize(DecimalMax maxValue) {
+ try {
+ this.maxValue = new BigDecimal( maxValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( maxValue.value() + " does not represent a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext) {
+
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+
+ if ( value instanceof BigDecimal ) {
+ return ( ( BigDecimal ) value ).compareTo( maxValue ) != 1;
+ }
+ else if ( value instanceof BigInteger ) {
+ return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( maxValue ) != 1;
+ }
+ else {
+ return ( new BigDecimal( value.doubleValue() ).compareTo( maxValue ) ) != 1;
+ }
+ }
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -0,0 +1,57 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMax;
+
+/**
+ * Check that the String being validated represents a number, and has a value
+ * less than or equal to the maximum value specified.
+ *
+ * @author Alaa Nassef
+ */
+public class DecimalMaxValidatorForString implements ConstraintValidator<DecimalMax, String> {
+
+ private BigDecimal maxValue;
+
+ public void initialize(DecimalMax maxValue) {
+ try {
+ this.maxValue = new BigDecimal( maxValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( maxValue.value() + " does not represent a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+ try {
+ return new BigDecimal( value ).compareTo( maxValue ) != 1;
+ }
+ catch ( NumberFormatException nfe ) {
+ return false;
+ }
+ }
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMin;
+
+/**
+ * Check that the number being validated is less than or equal to the maximum
+ * value specified.
+ *
+ * @author Hardy Ferentschik
+ */
+public class DecimalMinValidatorForNumber implements ConstraintValidator<DecimalMin, Number> {
+
+ private BigDecimal minValue;
+
+ public void initialize(DecimalMin minValue) {
+ try {
+ this.minValue = new BigDecimal( minValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( minValue.value() + " does not represent a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext) {
+
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+
+ if ( value instanceof BigDecimal ) {
+ return ( ( BigDecimal ) value ).compareTo( minValue ) != -1;
+ }
+ else if ( value instanceof BigInteger ) {
+ return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( minValue ) != -1;
+ }
+ else {
+ return ( new BigDecimal( value.doubleValue() ).compareTo( minValue ) ) != -1;
+ }
+ }
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -0,0 +1,54 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMin;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DecimalMinValidatorForString implements ConstraintValidator<DecimalMin, String> {
+
+ private BigDecimal minValue;
+
+ public void initialize(DecimalMin minValue) {
+ try {
+ this.minValue = new BigDecimal( minValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( minValue.value() + " does not represent a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+ try {
+ return new BigDecimal( value ).compareTo( minValue ) != -1;
+ }
+ catch ( NumberFormatException nfe ) {
+ return false;
+ }
+ }
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11 10:39:51 UTC (rev 16760)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -39,6 +39,8 @@
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
import org.hibernate.validation.constraints.impl.AssertFalseValidator;
import org.hibernate.validation.constraints.impl.AssertTrueValidator;
@@ -66,6 +68,10 @@
import org.hibernate.validation.constraints.impl.SizeValidatorForCollection;
import org.hibernate.validation.constraints.impl.SizeValidatorForMap;
import org.hibernate.validation.constraints.impl.SizeValidatorForString;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForString;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForString;
import org.hibernate.validation.util.ReflectionHelper;
/**
@@ -94,6 +100,21 @@
builtinConstraints.put( AssertTrue.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
+ constraintList.add( DecimalMaxValidatorForNumber.class );
+ constraintList.add( DecimalMaxValidatorForString.class );
+ builtinConstraints.put( DecimalMax.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
+ constraintList.add( DecimalMinValidatorForNumber.class );
+ constraintList.add( DecimalMinValidatorForString.class );
+ builtinConstraints.put( DecimalMin.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
+ constraintList.add( DigitsValidatorForString.class );
+ constraintList.add( DigitsValidatorForNumber.class );
+ builtinConstraints.put( Digits.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
constraintList.add( FutureValidatorForCalendar.class );
constraintList.add( FutureValidatorForDate.class );
builtinConstraints.put( Future.class, constraintList );
@@ -122,9 +143,8 @@
builtinConstraints.put( Past.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
- constraintList.add( DigitsValidatorForString.class );
- constraintList.add( DigitsValidatorForNumber.class );
- builtinConstraints.put( Digits.class, constraintList );
+ constraintList.add( PatternValidator.class );
+ builtinConstraints.put( Pattern.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
constraintList.add( SizeValidatorForString.class );
@@ -139,10 +159,6 @@
constraintList.add( SizeValidatorForArraysOfInt.class );
constraintList.add( SizeValidatorForArraysOfLong.class );
builtinConstraints.put( Size.class, constraintList );
-
- constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
- constraintList.add( PatternValidator.class );
- builtinConstraints.put( Pattern.class, constraintList );
}
public List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> getBuiltInConstraints(Class<? extends Annotation> annotationType) {
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java 2009-06-11 10:39:51 UTC (rev 16760)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -19,11 +19,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.Max;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -35,21 +38,51 @@
*/
public class MaxValidatorForNumberTest {
- private static MaxValidatorForNumber constraint;
+ @Test
+ public void testIsValidMax() {
- @BeforeClass
- public static void init() {
AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.max}" );
Max m = AnnotationFactory.create( descriptor );
- constraint = new MaxValidatorForNumber();
+ MaxValidatorForNumber constraint = new MaxValidatorForNumber();
constraint.initialize( m );
+ testMaxValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMax() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "15.0E0" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ constraint.initialize( m );
+ testMaxValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMaxValidator(ConstraintValidator<?, Number> constraint) {
byte b = 1;
Byte bWrapper = 127;
assertTrue( constraint.isValid( null, null ) );
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java 2009-06-11 10:39:51 UTC (rev 16760)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -17,36 +17,69 @@
*/
package org.hibernate.validation.constraints.impl;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.Max;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
/**
- * @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MaxValidatorForStringTest {
- private static MaxValidatorForString constraint;
+ @Test
+ public void testIsValidMax() {
- @BeforeClass
- public static void init() {
AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.max}" );
Max m = AnnotationFactory.create( descriptor );
- constraint = new MaxValidatorForString();
+ MaxValidatorForString constraint = new MaxValidatorForString();
constraint.initialize( m );
+ testMaxValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMax() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "15.0E0" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForString constraint = new DecimalMaxValidatorForString();
+ constraint.initialize( m );
+ testMaxValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMaxValidator(ConstraintValidator<?, String> constraint) {
assertTrue( constraint.isValid( null, null ) );
assertTrue( constraint.isValid( "15", null ) );
assertTrue( constraint.isValid( "15.0", null ) );
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java 2009-06-11 10:39:51 UTC (rev 16760)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -19,11 +19,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -35,21 +38,49 @@
*/
public class MinValidatorForNumberTest {
- private static MinValidatorForNumber constraint;
-
- @BeforeClass
- public static void init() {
+ @Test
+ public void testIsValidMinValidator() {
AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.min}" );
Min m = AnnotationFactory.create( descriptor );
- constraint = new MinValidatorForNumber();
+ MinValidatorForNumber constraint = new MinValidatorForNumber();
constraint.initialize( m );
+ testMinValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMinValidator() {
+ AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "1500E-2" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ constraint.initialize( m );
+ testMinValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMinValidator(ConstraintValidator<?, Number> constraint) {
byte b = 1;
Byte bWrapper = 127;
assertTrue( constraint.isValid( null, null ) );
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java 2009-06-11 10:39:51 UTC (rev 16760)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java 2009-06-11 15:51:21 UTC (rev 16761)
@@ -17,11 +17,14 @@
*/
package org.hibernate.validation.constraints.impl;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -29,24 +32,53 @@
/**
* @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MinValidatorForStringTest {
- private static MinValidatorForString constraint;
-
- @BeforeClass
- public static void init() {
+ @Test
+ public void testIsValidMinValidator() {
AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.min}" );
Min m = AnnotationFactory.create( descriptor );
- constraint = new MinValidatorForString();
+ MinValidatorForString constraint = new MinValidatorForString();
constraint.initialize( m );
+ testMinValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMinValidator() {
+ AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "1500E-2" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForString constraint = new DecimalMinValidatorForString();
+ constraint.initialize( m );
+ testMinValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMinValidator(ConstraintValidator<?, String> constraint) {
assertTrue( constraint.isValid( null, null ) );
assertTrue( constraint.isValid( "20", null ) );
assertTrue( constraint.isValid( "15", null ) );
@@ -57,5 +89,4 @@
//number format exception
assertFalse( constraint.isValid( "15l", null ) );
}
-
}
15 years, 5 months
Hibernate SVN: r16760 - validator/trunk/hibernate-validator/src/test/suite.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 06:39:51 -0400 (Thu, 11 Jun 2009)
New Revision: 16760
Modified:
validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
Log:
updated testsuite
Modified: validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
===================================================================
--- validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml 2009-06-11 10:24:15 UTC (rev 16759)
+++ validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml 2009-06-11 10:39:51 UTC (rev 16760)
@@ -4,14 +4,15 @@
<test name="Unit tests">
<packages>
<package name="org.hibernate.validation.bootstrap"/>
- <package name="org.hibernate.validation.constraints"/>
- <package name="org.hibernate.validation.constraints.validatorcontext"/>
+ <package name="org.hibernate.validation.constraints.impl"/>
+ <package name="org.hibernate.validation.constraints.custom"/>
<package name="org.hibernate.validation.engine"/>
<package name="org.hibernate.validation.engine.constraintcomposition"/>
<package name="org.hibernate.validation.engine.graphnavigation"/>
<package name="org.hibernate.validation.engine.groups"/>
<package name="org.hibernate.validation.engine.metadata"/>
- <package name="org.hibernate.validation.engine.validatorresolution"/>
+ <package name="org.hibernate.validation.engine.inheritance"/>
+ <package name="org.hibernate.validation.engine.resolver"/>
<package name="org.hibernate.validation.engine.xml"/>
<package name="org.hibernate.validation.util"/>
<package name="org.hibernate.validation.util.annotationfactory"/>
15 years, 5 months
Hibernate SVN: r16759 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validation/engine and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 06:24:15 -0400 (Thu, 11 Jun 2009)
New Revision: 16759
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java
Log:
HV-167 NotEmpty is now a composition of @NotNull and @Size
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -24,16 +24,20 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
+import javax.validation.ReportAsSingleViolation;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
-import org.hibernate.validation.constraints.impl.NotEmptyValidator;
-
/**
* @author Emmanuel Bernard
*/
@Documented
-@Constraint(validatedBy = NotEmptyValidator.class)
+@Constraint(validatedBy = {})
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
+@ReportAsSingleViolation
+@NotNull
+@Size(min=1)
public @interface NotEmpty {
String message() default "{org.hibernate.validation.constraints.NotEmpty.message}";
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -107,26 +107,33 @@
* @param <V> Type of the value to be validated.
*/
public <T, V> void validateConstraints(V value, Type type, ExecutionContext<T> executionContext, List<ConstraintViolation<T>> constraintViolations) {
+ // first validate composing constraints
for ( ConstraintTree<?> tree : getChildren() ) {
tree.validateConstraints( value, type, executionContext, constraintViolations );
}
- if ( log.isTraceEnabled() ) {
- log.trace( "Validating value {} against constraint defined by {}", value, descriptor );
- }
- ConstraintValidator<A, V> validator = getInitalizedValidator(
- value, type, executionContext.getConstraintValidatorFactory()
- );
ConstraintValidatorContextImpl constraintValidatorContext = new ConstraintValidatorContextImpl(
executionContext.peekParentPath(), executionContext.peekProperty(), descriptor
);
- if ( !validator.isValid( value, constraintValidatorContext ) ) {
- constraintViolations.addAll(
- executionContext.createConstraintViolations(
- value, constraintValidatorContext
- )
+
+ // we could have a composing constraint which does not need its own validator.
+ if ( !descriptor.getConstraintValidatorClasses().isEmpty() ) {
+ if ( log.isTraceEnabled() ) {
+ log.trace( "Validating value {} against constraint defined by {}", value, descriptor );
+ }
+ ConstraintValidator<A, V> validator = getInitalizedValidator(
+ value, type, executionContext.getConstraintValidatorFactory()
);
+
+ if ( !validator.isValid( value, constraintValidatorContext ) ) {
+ constraintViolations.addAll(
+ executionContext.createConstraintViolations(
+ value, constraintValidatorContext
+ )
+ );
+ }
}
+
if ( reportAsSingleViolation() && constraintViolations.size() > 0 ) {
constraintViolations.clear();
final String message = ( String ) getParent().getDescriptor().getAttributes().get( "message" );
@@ -150,6 +157,7 @@
*
* @return A initalized constraint validator matching the type of the value to be validated.
*/
+ @SuppressWarnings("unchecked")
private <V> ConstraintValidator<A, V> getInitalizedValidator(V value, Type type, ConstraintValidatorFactory constraintFactory) {
Class<? extends ConstraintValidator<?, ?>> validatorClass = findMatchingValidatorClass( value, type );
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -75,7 +75,7 @@
Set<ConstraintViolation<Customer>> constraintViolations = validator.validate( customer );
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
ConstraintViolation<Customer> constraintViolation = constraintViolations.iterator().next();
- assertEquals( "may not be null", constraintViolation.getMessage(), "Wrong message" );
+ assertEquals( "may not be empty", constraintViolation.getMessage(), "Wrong message" );
// get a new factory using a custom configuration
configuration = Validation.byDefaultProvider().configure();
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -17,21 +17,17 @@
*/
package org.hibernate.validation.engine;
-import javax.validation.constraints.NotNull;
-
import org.hibernate.validation.constraints.NotEmpty;
/**
* @author Hardy Ferentschik
*/
public interface Person {
- @NotNull
@NotEmpty
String getFirstName();
String getMiddleName();
- @NotNull
@NotEmpty
String getLastName();
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -32,7 +32,6 @@
@NotEmpty(groups = Last.class)
private String firstName;
- @NotNull(groups = First.class)
@NotEmpty(groups = First.class)
private String lastName;
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -31,7 +31,6 @@
* @author Hardy Ferentschik
*/
public class Book {
- @NotNull(groups = First.class)
@NotEmpty(groups = First.class)
private String title;
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java 2009-06-11 10:23:04 UTC (rev 16758)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java 2009-06-11 10:24:15 UTC (rev 16759)
@@ -118,7 +118,7 @@
constraintViolations = validator.validate( book, Book.All.class );
ConstraintViolation constraintViolation = constraintViolations.iterator().next();
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- assertEquals( "may not be null", constraintViolation.getMessage(), "Wrong message" );
+ assertEquals( "may not be empty", constraintViolation.getMessage(), "Wrong message" );
assertEquals( constraintViolation.getRootBean(), book, "Wrong root entity" );
assertEquals( constraintViolation.getInvalidValue(), book.getTitle(), "Wrong value" );
assertEquals( "title", constraintViolation.getPropertyPath(), "Wrong propertyName" );
15 years, 5 months
Hibernate SVN: r16758 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validation/constraints and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 06:23:04 -0400 (Thu, 11 Jun 2009)
New Revision: 16758
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertFalseValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertTrueValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForCalendarTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForDateTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/LengthValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NotNullValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NullValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForCalendarTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForDateTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PatternValidatorTest.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/NotEmptyValidator.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertFalseValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertTrueValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForCalendarTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForDateTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotNullValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NullValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForCalendarTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForDateTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/validatorcontext/
Log:
Moved validator tests to be consistent with the package location of the actual tests classes
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/NotEmptyValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/NotEmptyValidator.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/NotEmptyValidator.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,41 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints.impl;
-
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
-
-import org.hibernate.validation.constraints.NotEmpty;
-
-/**
- * @author Hardy Ferentschik
- * @todo Extend to not only support strings, but also collections and maps. Needs to be specified first though.
- */
-public class NotEmptyValidator implements ConstraintValidator<NotEmpty, String> {
-
- public void initialize(NotEmpty parameters) {
- }
-
- public boolean isValid(String object, ConstraintValidatorContext constraintValidatorContext) {
- if ( object == null ) {
- return true;
- }
- int length = object.length();
- return length > 0;
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertFalseValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertFalseValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertFalseValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,47 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.AssertFalseValidator;
-
-/**
- * @author Alaa Nassef
- */
-public class AssertFalseValidatorTest {
-
- private static AssertFalseValidator constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new AssertFalseValidator();
- }
-
- @Test
- public void testIsValid() {
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( false, null ) );
- assertTrue( constraint.isValid( Boolean.FALSE, null ) );
- assertFalse( constraint.isValid( true, null ) );
- assertFalse( constraint.isValid( Boolean.TRUE, null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertTrueValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertTrueValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertTrueValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,48 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.AssertTrueValidator;
-
-/**
- * @author Alaa Nassef
- */
-public class AssertTrueValidatorTest {
-
- private static AssertTrueValidator constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new AssertTrueValidator();
- }
-
- @Test
- public void testIsValid() {
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( true, null ) );
- assertTrue( constraint.isValid( Boolean.TRUE, null ) );
- assertFalse( constraint.isValid( false, null ) );
- assertFalse( constraint.isValid( Boolean.FALSE, null ) );
- }
-}
-
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForNumberTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForNumberTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,109 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.math.BigDecimal;
-import javax.validation.ValidationException;
-import javax.validation.constraints.Digits;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.DigitsValidatorForNumber;
-
-/**
- * @author Alaa Nassef
- * @author Hardy Ferentschik
- */
-public class DigitsValidatorForNumberTest {
-
- @Test
- public void testIsValid() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", 5 );
- descriptor.setValue( "fraction", 2 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
- constraint.initialize( p );
-
-
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( Byte.valueOf( "0" ), null ) );
- assertTrue( constraint.isValid( Double.valueOf( "500.2" ), null ) );
-
- assertTrue( constraint.isValid( new BigDecimal( "-12345.12" ), null ) );
- assertFalse( constraint.isValid( new BigDecimal( "-123456.12" ), null ) );
- assertFalse( constraint.isValid( new BigDecimal( "-123456.123" ), null ) );
- assertFalse( constraint.isValid( new BigDecimal( "-12345.123" ), null ) );
- assertFalse( constraint.isValid( new BigDecimal( "12345.123" ), null ) );
-
- assertTrue( constraint.isValid( Float.valueOf( "-000000000.22" ), null ) );
- assertFalse( constraint.isValid( Integer.valueOf( "256874" ), null ) );
- assertFalse( constraint.isValid( Double.valueOf( "12.0001" ), null ) );
- }
-
- @Test
- public void testIsValidZeroLength() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", 0 );
- descriptor.setValue( "fraction", 0 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
- constraint.initialize( p );
-
-
- assertTrue( constraint.isValid( null, null ) );
- assertFalse( constraint.isValid( Byte.valueOf( "0" ), null ) );
- assertFalse( constraint.isValid( Double.valueOf( "500.2" ), null ) );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeIntegerLength() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", -1 );
- descriptor.setValue( "fraction", 1 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
- constraint.initialize( p );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeFractionLength() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", 1 );
- descriptor.setValue( "fraction", -1 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
- constraint.initialize( p );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForStringTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForStringTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,91 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import javax.validation.ValidationException;
-import javax.validation.constraints.Digits;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.DigitsValidatorForString;
-
-/**
- * @author Alaa Nassef
- */
-public class DigitsValidatorForStringTest {
-
- private static DigitsValidatorForString constraint;
-
- @BeforeClass
- public static void init() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", 5 );
- descriptor.setValue( "fraction", 2 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- constraint = new DigitsValidatorForString();
- constraint.initialize( p );
- }
-
- @Test
- public void testIsValid() {
-
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( "0", null ) );
- assertTrue( constraint.isValid( "500.2", null ) );
- assertTrue( constraint.isValid( "-12456.22", null ) );
- assertTrue( constraint.isValid( "-000000000.22", null ) );
- //should throw number format exception
- assertFalse( constraint.isValid( "", null ) );
- assertFalse( constraint.isValid( "256874.0", null ) );
- assertFalse( constraint.isValid( "12.0001", null ) );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeIntegerLength() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", -1 );
- descriptor.setValue( "fraction", 1 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForString constraint = new DigitsValidatorForString();
- constraint.initialize( p );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeFractionLength() {
-
- AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
- descriptor.setValue( "integer", 1 );
- descriptor.setValue( "fraction", -1 );
- descriptor.setValue( "message", "{validator.digits}" );
- Digits p = AnnotationFactory.create( descriptor );
-
- DigitsValidatorForString constraint = new DigitsValidatorForString();
- constraint.initialize( p );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForCalendarTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForCalendarTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForCalendarTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,65 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.util.Calendar;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.FutureValidatorForCalendar;
-
-/**
- * @author Alaa Nassef
- * @author Hardy Ferentschik
- */
-public class FutureValidatorForCalendarTest {
-
- private static FutureValidatorForCalendar constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new FutureValidatorForCalendar();
- }
-
- @Test
- public void testIsValid() {
- Calendar futureDate = getFutureDate();
- Calendar pastDate = getPastDate();
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( futureDate, null ) );
- assertFalse( constraint.isValid( pastDate, null ) );
- }
-
- private Calendar getFutureDate() {
- Calendar cal = Calendar.getInstance();
- int year = cal.get( Calendar.YEAR );
- cal.set( Calendar.YEAR, year + 1 );
- return cal;
- }
-
- private Calendar getPastDate() {
- Calendar cal = Calendar.getInstance();
- int year = cal.get( Calendar.YEAR );
- cal.set( Calendar.YEAR, year - 1 );
- return cal;
- }
-
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForDateTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForDateTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForDateTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,62 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.util.Date;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.FutureValidatorForDate;
-
-public class FutureValidatorForDateTest {
-
- private static FutureValidatorForDate constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new FutureValidatorForDate();
- }
-
- @Test
- public void testIsValid() {
- Date futureDate = getFutureDate();
- Date pastDate = getPastDate();
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( futureDate, null ) );
- assertFalse( constraint.isValid( new Date(), null ) );
- assertFalse( constraint.isValid( pastDate, null ) );
- }
-
- private Date getFutureDate() {
- Date date = new Date();
- long timeStamp = date.getTime();
- date.setTime( timeStamp + 31557600000l );
- return date;
- }
-
- private Date getPastDate() {
- Date date = new Date();
- long timeStamp = date.getTime();
- date.setTime( timeStamp - 31557600000l );
- return date;
- }
-
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,89 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import javax.validation.ValidationException;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.LengthValidator;
-
-/**
- * Tests the <code>LengthConstraint</code>.
- *
- * @author Hardy Ferentschik
- */
-public class LengthValidatorTest {
-
- @Test
- public void testIsValid() {
- AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
- descriptor.setValue( "min", 1 );
- descriptor.setValue( "max", 3 );
- descriptor.setValue( "message", "{validator.length}" );
- Length l = AnnotationFactory.create( descriptor );
- LengthValidator constraint = new LengthValidator();
- constraint.initialize( l );
- assertTrue( constraint.isValid( null, null ) );
- assertFalse( constraint.isValid( "", null ) );
- assertTrue( constraint.isValid( "f", null ) );
- assertTrue( constraint.isValid( "fo", null ) );
- assertTrue( constraint.isValid( "foo", null ) );
- assertFalse( constraint.isValid( "foobar", null ) );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeMinValue() {
- AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
- descriptor.setValue( "min", -1 );
- descriptor.setValue( "max", 1 );
- descriptor.setValue( "message", "{validator.length}" );
- Length p = AnnotationFactory.create( descriptor );
-
- LengthValidator constraint = new LengthValidator();
- constraint.initialize( p );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeMaxValue() {
- AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
- descriptor.setValue( "min", 1 );
- descriptor.setValue( "max", -1 );
- descriptor.setValue( "message", "{validator.length}" );
- Length p = AnnotationFactory.create( descriptor );
-
- LengthValidator constraint = new LengthValidator();
- constraint.initialize( p );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testNegativeLength() {
- AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
- descriptor.setValue( "min", 5 );
- descriptor.setValue( "max", 4 );
- descriptor.setValue( "message", "{validator.length}" );
- Length p = AnnotationFactory.create( descriptor );
-
- LengthValidator constraint = new LengthValidator();
- constraint.initialize( p );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForNumberTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForNumberTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,71 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import javax.validation.constraints.Max;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.MaxValidatorForNumber;
-
-/**
- * @author Alaa Nassef
- * @author Hardy Ferentschik
- */
-public class MaxValidatorForNumberTest {
-
- private static MaxValidatorForNumber constraint;
-
- @BeforeClass
- public static void init() {
- AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
- descriptor.setValue( "value", 15l );
- descriptor.setValue( "message", "{validator.max}" );
- Max m = AnnotationFactory.create( descriptor );
-
- constraint = new MaxValidatorForNumber();
- constraint.initialize( m );
- }
-
- @Test
- public void testIsValid() {
- byte b = 1;
- Byte bWrapper = 127;
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( b, null ) );
- assertTrue( constraint.isValid( 15l, null ) );
- assertTrue( constraint.isValid( 15, null ) );
- assertTrue( constraint.isValid( 15.0, null ) );
- assertTrue( constraint.isValid( BigDecimal.valueOf( -156000000000.0 ), null ) );
- assertTrue( constraint.isValid( BigInteger.valueOf( -10000000l ), null ) );
- assertTrue( constraint.isValid( 10, null ) );
- assertTrue( constraint.isValid( 14.99, null ) );
- assertTrue( constraint.isValid( -14.99, null ) );
- assertFalse( constraint.isValid( 20, null ) );
- assertFalse( constraint.isValid( bWrapper, null ) );
- assertFalse( constraint.isValid( BigDecimal.valueOf( 156000000000.0 ), null ) );
- assertFalse( constraint.isValid( BigInteger.valueOf( 10000000l ), null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForStringTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForStringTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,61 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import javax.validation.constraints.Max;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.MaxValidatorForString;
-
-/**
- * @author Alaa Nassef
- */
-public class MaxValidatorForStringTest {
-
- private static MaxValidatorForString constraint;
-
- @BeforeClass
- public static void init() {
- AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
- descriptor.setValue( "value", 15l );
- descriptor.setValue( "message", "{validator.max}" );
- Max m = AnnotationFactory.create( descriptor );
-
- constraint = new MaxValidatorForString();
- constraint.initialize( m );
- }
-
- @Test
- public void testIsValid() {
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( "15", null ) );
- assertTrue( constraint.isValid( "15.0", null ) );
- assertTrue( constraint.isValid( "10", null ) );
- assertTrue( constraint.isValid( "14.99", null ) );
- assertTrue( constraint.isValid( "-14.99", null ) );
- assertFalse( constraint.isValid( "20", null ) );
- //number format exception
- assertFalse( constraint.isValid( "15l", null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForNumberTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForNumberTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,71 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import javax.validation.constraints.Min;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.MinValidatorForNumber;
-
-/**
- * @author Alaa Nassef
- * @author Hardy Ferentschik
- */
-public class MinValidatorForNumberTest {
-
- private static MinValidatorForNumber constraint;
-
- @BeforeClass
- public static void init() {
- AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
- descriptor.setValue( "value", 15l );
- descriptor.setValue( "message", "{validator.min}" );
- Min m = AnnotationFactory.create( descriptor );
-
- constraint = new MinValidatorForNumber();
- constraint.initialize( m );
- }
-
- @Test
- public void testIsValid() {
- byte b = 1;
- Byte bWrapper = 127;
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( bWrapper, null ) );
- assertTrue( constraint.isValid( 20, null ) );
- assertTrue( constraint.isValid( 15l, null ) );
- assertTrue( constraint.isValid( 15, null ) );
- assertTrue( constraint.isValid( 15.0, null ) );
- assertTrue( constraint.isValid( BigDecimal.valueOf( 156000000000.0 ), null ) );
- assertTrue( constraint.isValid( BigInteger.valueOf( 10000000l ), null ) );
- assertFalse( constraint.isValid( b, null ) );
- assertFalse( constraint.isValid( BigDecimal.valueOf( -156000000000.0 ), null ) );
- assertFalse( constraint.isValid( BigInteger.valueOf( -10000000l ), null ) );
- assertFalse( constraint.isValid( 10, null ) );
- assertFalse( constraint.isValid( 14.99, null ) );
- assertFalse( constraint.isValid( -14.99, null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForStringTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForStringTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,62 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import javax.validation.constraints.Min;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.MinValidatorForString;
-
-/**
- * @author Alaa Nassef
- */
-public class MinValidatorForStringTest {
-
- private static MinValidatorForString constraint;
-
- @BeforeClass
- public static void init() {
- AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
- descriptor.setValue( "value", 15l );
- descriptor.setValue( "message", "{validator.min}" );
- Min m = AnnotationFactory.create( descriptor );
-
- constraint = new MinValidatorForString();
- constraint.initialize( m );
- }
-
- @Test
- public void testIsValid() {
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( "20", null ) );
- assertTrue( constraint.isValid( "15", null ) );
- assertTrue( constraint.isValid( "15.0", null ) );
- assertFalse( constraint.isValid( "10", null ) );
- assertFalse( constraint.isValid( "14.99", null ) );
- assertFalse( constraint.isValid( "-14.99", null ) );
- //number format exception
- assertFalse( constraint.isValid( "15l", null ) );
- }
-
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,48 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.NotEmptyValidator;
-
-/**
- * @author Hardy Ferentschik
- */
-public class NotEmptyValidatorTest {
-
- private static NotEmptyValidator constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new NotEmptyValidator();
- }
-
- @Test
- public void testIsValid() {
-
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( "foo", null ) );
- assertTrue( constraint.isValid( " ", null ) );
-
- assertFalse( constraint.isValid( "", null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotNullValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotNullValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotNullValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,38 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.NotNullValidator;
-
-/**
- * @author Hardy Ferentschik
- */
-public class NotNullValidatorTest {
-
- @Test
- public void testIsValid() {
- NotNullValidator constraint = new NotNullValidator();
-
- assertFalse( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( new Object(), null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NullValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NullValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NullValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,44 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.NullValidator;
-
-/**
- * @author Alaa Nassef
- */
-public class NullValidatorTest {
-
- private static NullValidator constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new NullValidator();
- }
-
- @Test
- public void testIsValid() {
- assertTrue( constraint.isValid( null, null ) );
- assertFalse( constraint.isValid( new Object(), null ) );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForCalendarTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForCalendarTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForCalendarTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,64 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.util.Calendar;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.PastValidatorForCalendar;
-
-/**
- * @author Alaa Nassef
- * @author Hardy Ferentschik
- */
-public class PastValidatorForCalendarTest {
-
- private static PastValidatorForCalendar constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new PastValidatorForCalendar();
- }
-
- @Test
- public void testIsValid() {
- Calendar futureDate = getFutureDate();
- Calendar pastDate = getPastDate();
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( pastDate, null ) );
- assertFalse( constraint.isValid( futureDate, null ) );
- }
-
- private Calendar getFutureDate() {
- Calendar cal = Calendar.getInstance();
- int year = cal.get( Calendar.YEAR );
- cal.set( Calendar.YEAR, year + 1 );
- return cal;
- }
-
- private Calendar getPastDate() {
- Calendar cal = Calendar.getInstance();
- int year = cal.get( Calendar.YEAR );
- cal.set( Calendar.YEAR, year - 1 );
- return cal;
- }
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForDateTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForDateTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForDateTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,62 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.util.Date;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.constraints.impl.PastValidatorForDate;
-
-public class PastValidatorForDateTest {
-
- private static PastValidatorForDate constraint;
-
- @BeforeClass
- public static void init() {
- constraint = new PastValidatorForDate();
- }
-
- @Test
- public void testIsValid() {
- Date futureDate = getFutureDate();
- Date pastDate = getPastDate();
- assertTrue( constraint.isValid( null, null ) );
- assertTrue( constraint.isValid( pastDate, null ) );
- assertFalse( constraint.isValid( new Date(), null ) );
- assertFalse( constraint.isValid( futureDate, null ) );
- }
-
- private Date getFutureDate() {
- Date date = new Date();
- long timeStamp = date.getTime();
- date.setTime( timeStamp + 31557600000l );
- return date;
- }
-
- private Date getPastDate() {
- Date date = new Date();
- long timeStamp = date.getTime();
- date.setTime( timeStamp - 31557600000l );
- return date;
- }
-
-}
Deleted: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternValidatorTest.java 2009-06-11 10:21:57 UTC (rev 16757)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -1,62 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import javax.validation.ValidationException;
-import javax.validation.constraints.Pattern;
-
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.Test;
-
-import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
-import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.constraints.impl.PatternValidator;
-
-/**
- * @author Hardy Ferentschik
- */
-public class PatternValidatorTest {
-
- @Test
- public void testIsValid() {
- AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class );
- descriptor.setValue( "regexp", "foobar" );
- descriptor.setValue( "message", "{validator.pattern}" );
- Pattern p = AnnotationFactory.create( descriptor );
-
- PatternValidator constraint = new PatternValidator();
- constraint.initialize( p );
-
- assertTrue( constraint.isValid( null, null ) );
- assertFalse( constraint.isValid( "", null ) );
- assertFalse( constraint.isValid( "bla bla", null ) );
- assertFalse( constraint.isValid( "This test is not foobar", null ) );
- }
-
- @Test(expectedExceptions = ValidationException.class)
- public void testInvalidRegularExpression() {
- AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class );
- descriptor.setValue( "regexp", "(unbalanced parentheses" );
- descriptor.setValue( "message", "{validator.pattern}" );
- Pattern p = AnnotationFactory.create( descriptor );
-
- PatternValidator constraint = new PatternValidator();
- constraint.initialize( p );
- }
-}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertFalseValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertFalseValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertFalseValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertFalseValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,45 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * @author Alaa Nassef
+ */
+public class AssertFalseValidatorTest {
+
+ private static AssertFalseValidator constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new AssertFalseValidator();
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( false, null ) );
+ assertTrue( constraint.isValid( Boolean.FALSE, null ) );
+ assertFalse( constraint.isValid( true, null ) );
+ assertFalse( constraint.isValid( Boolean.TRUE, null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertTrueValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/AssertTrueValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertTrueValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/AssertTrueValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,46 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * @author Alaa Nassef
+ */
+public class AssertTrueValidatorTest {
+
+ private static AssertTrueValidator constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new AssertTrueValidator();
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( true, null ) );
+ assertTrue( constraint.isValid( Boolean.TRUE, null ) );
+ assertFalse( constraint.isValid( false, null ) );
+ assertFalse( constraint.isValid( Boolean.FALSE, null ) );
+ }
+}
+
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForNumberTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForNumberTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForNumberTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,108 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import javax.validation.ValidationException;
+import javax.validation.constraints.Digits;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ * @author Hardy Ferentschik
+ */
+public class DigitsValidatorForNumberTest {
+
+ @Test
+ public void testIsValid() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", 5 );
+ descriptor.setValue( "fraction", 2 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
+ constraint.initialize( p );
+
+
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( Byte.valueOf( "0" ), null ) );
+ assertTrue( constraint.isValid( Double.valueOf( "500.2" ), null ) );
+
+ assertTrue( constraint.isValid( new BigDecimal( "-12345.12" ), null ) );
+ assertFalse( constraint.isValid( new BigDecimal( "-123456.12" ), null ) );
+ assertFalse( constraint.isValid( new BigDecimal( "-123456.123" ), null ) );
+ assertFalse( constraint.isValid( new BigDecimal( "-12345.123" ), null ) );
+ assertFalse( constraint.isValid( new BigDecimal( "12345.123" ), null ) );
+
+ assertTrue( constraint.isValid( Float.valueOf( "-000000000.22" ), null ) );
+ assertFalse( constraint.isValid( Integer.valueOf( "256874" ), null ) );
+ assertFalse( constraint.isValid( Double.valueOf( "12.0001" ), null ) );
+ }
+
+ @Test
+ public void testIsValidZeroLength() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", 0 );
+ descriptor.setValue( "fraction", 0 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
+ constraint.initialize( p );
+
+
+ assertTrue( constraint.isValid( null, null ) );
+ assertFalse( constraint.isValid( Byte.valueOf( "0" ), null ) );
+ assertFalse( constraint.isValid( Double.valueOf( "500.2" ), null ) );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeIntegerLength() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", -1 );
+ descriptor.setValue( "fraction", 1 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
+ constraint.initialize( p );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeFractionLength() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", 1 );
+ descriptor.setValue( "fraction", -1 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForNumber constraint = new DigitsValidatorForNumber();
+ constraint.initialize( p );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForStringTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/DigitsValidatorForStringTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForStringTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/DigitsValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,90 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import javax.validation.ValidationException;
+import javax.validation.constraints.Digits;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ */
+public class DigitsValidatorForStringTest {
+
+ private static DigitsValidatorForString constraint;
+
+ @BeforeClass
+ public static void init() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", 5 );
+ descriptor.setValue( "fraction", 2 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ constraint = new DigitsValidatorForString();
+ constraint.initialize( p );
+ }
+
+ @Test
+ public void testIsValid() {
+
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( "0", null ) );
+ assertTrue( constraint.isValid( "500.2", null ) );
+ assertTrue( constraint.isValid( "-12456.22", null ) );
+ assertTrue( constraint.isValid( "-000000000.22", null ) );
+ //should throw number format exception
+ assertFalse( constraint.isValid( "", null ) );
+ assertFalse( constraint.isValid( "256874.0", null ) );
+ assertFalse( constraint.isValid( "12.0001", null ) );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeIntegerLength() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", -1 );
+ descriptor.setValue( "fraction", 1 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForString constraint = new DigitsValidatorForString();
+ constraint.initialize( p );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeFractionLength() {
+
+ AnnotationDescriptor<Digits> descriptor = new AnnotationDescriptor<Digits>( Digits.class );
+ descriptor.setValue( "integer", 1 );
+ descriptor.setValue( "fraction", -1 );
+ descriptor.setValue( "message", "{validator.digits}" );
+ Digits p = AnnotationFactory.create( descriptor );
+
+ DigitsValidatorForString constraint = new DigitsValidatorForString();
+ constraint.initialize( p );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForCalendarTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForCalendarTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForCalendarTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForCalendarTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.util.Calendar;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * @author Alaa Nassef
+ * @author Hardy Ferentschik
+ */
+public class FutureValidatorForCalendarTest {
+
+ private static FutureValidatorForCalendar constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new FutureValidatorForCalendar();
+ }
+
+ @Test
+ public void testIsValid() {
+ Calendar futureDate = getFutureDate();
+ Calendar pastDate = getPastDate();
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( futureDate, null ) );
+ assertFalse( constraint.isValid( pastDate, null ) );
+ }
+
+ private Calendar getFutureDate() {
+ Calendar cal = Calendar.getInstance();
+ int year = cal.get( Calendar.YEAR );
+ cal.set( Calendar.YEAR, year + 1 );
+ return cal;
+ }
+
+ private Calendar getPastDate() {
+ Calendar cal = Calendar.getInstance();
+ int year = cal.get( Calendar.YEAR );
+ cal.set( Calendar.YEAR, year - 1 );
+ return cal;
+ }
+
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForDateTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/FutureValidatorForDateTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForDateTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorForDateTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,60 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.util.Date;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class FutureValidatorForDateTest {
+
+ private static FutureValidatorForDate constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new FutureValidatorForDate();
+ }
+
+ @Test
+ public void testIsValid() {
+ Date futureDate = getFutureDate();
+ Date pastDate = getPastDate();
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( futureDate, null ) );
+ assertFalse( constraint.isValid( new Date(), null ) );
+ assertFalse( constraint.isValid( pastDate, null ) );
+ }
+
+ private Date getFutureDate() {
+ Date date = new Date();
+ long timeStamp = date.getTime();
+ date.setTime( timeStamp + 31557600000l );
+ return date;
+ }
+
+ private Date getPastDate() {
+ Date date = new Date();
+ long timeStamp = date.getTime();
+ date.setTime( timeStamp - 31557600000l );
+ return date;
+ }
+
+}
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/FutureValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,45 @@
+// $Id: FutureValidatorTest.java 16528 2009-05-11 12:18:22Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import static org.testng.Assert.assertEquals;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.constraints.DateHolder;
+import org.hibernate.validation.util.TestUtil;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class FutureValidatorTest {
+
+ /**
+ * HV-158
+ */
+ @Test
+ public void testFutureAndPast() {
+ Validator validator = TestUtil.getValidator();
+ DateHolder dateHolder = new DateHolder();
+ Set<ConstraintViolation<DateHolder>> constraintViolations = validator.validate( dateHolder );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/LengthValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/LengthValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/LengthValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,89 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import javax.validation.ValidationException;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.constraints.Length;
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * Tests the <code>LengthConstraint</code>.
+ *
+ * @author Hardy Ferentschik
+ */
+public class LengthValidatorTest {
+
+ @Test
+ public void testIsValid() {
+ AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
+ descriptor.setValue( "min", 1 );
+ descriptor.setValue( "max", 3 );
+ descriptor.setValue( "message", "{validator.length}" );
+ Length l = AnnotationFactory.create( descriptor );
+ LengthValidator constraint = new LengthValidator();
+ constraint.initialize( l );
+ assertTrue( constraint.isValid( null, null ) );
+ assertFalse( constraint.isValid( "", null ) );
+ assertTrue( constraint.isValid( "f", null ) );
+ assertTrue( constraint.isValid( "fo", null ) );
+ assertTrue( constraint.isValid( "foo", null ) );
+ assertFalse( constraint.isValid( "foobar", null ) );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeMinValue() {
+ AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
+ descriptor.setValue( "min", -1 );
+ descriptor.setValue( "max", 1 );
+ descriptor.setValue( "message", "{validator.length}" );
+ Length p = AnnotationFactory.create( descriptor );
+
+ LengthValidator constraint = new LengthValidator();
+ constraint.initialize( p );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeMaxValue() {
+ AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
+ descriptor.setValue( "min", 1 );
+ descriptor.setValue( "max", -1 );
+ descriptor.setValue( "message", "{validator.length}" );
+ Length p = AnnotationFactory.create( descriptor );
+
+ LengthValidator constraint = new LengthValidator();
+ constraint.initialize( p );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNegativeLength() {
+ AnnotationDescriptor<Length> descriptor = new AnnotationDescriptor<Length>( Length.class );
+ descriptor.setValue( "min", 5 );
+ descriptor.setValue( "max", 4 );
+ descriptor.setValue( "message", "{validator.length}" );
+ Length p = AnnotationFactory.create( descriptor );
+
+ LengthValidator constraint = new LengthValidator();
+ constraint.initialize( p );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForNumberTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,70 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.constraints.Max;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ * @author Hardy Ferentschik
+ */
+public class MaxValidatorForNumberTest {
+
+ private static MaxValidatorForNumber constraint;
+
+ @BeforeClass
+ public static void init() {
+ AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
+ descriptor.setValue( "value", 15l );
+ descriptor.setValue( "message", "{validator.max}" );
+ Max m = AnnotationFactory.create( descriptor );
+
+ constraint = new MaxValidatorForNumber();
+ constraint.initialize( m );
+ }
+
+ @Test
+ public void testIsValid() {
+ byte b = 1;
+ Byte bWrapper = 127;
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( b, null ) );
+ assertTrue( constraint.isValid( 15l, null ) );
+ assertTrue( constraint.isValid( 15, null ) );
+ assertTrue( constraint.isValid( 15.0, null ) );
+ assertTrue( constraint.isValid( BigDecimal.valueOf( -156000000000.0 ), null ) );
+ assertTrue( constraint.isValid( BigInteger.valueOf( -10000000l ), null ) );
+ assertTrue( constraint.isValid( 10, null ) );
+ assertTrue( constraint.isValid( 14.99, null ) );
+ assertTrue( constraint.isValid( -14.99, null ) );
+ assertFalse( constraint.isValid( 20, null ) );
+ assertFalse( constraint.isValid( bWrapper, null ) );
+ assertFalse( constraint.isValid( BigDecimal.valueOf( 156000000000.0 ), null ) );
+ assertFalse( constraint.isValid( BigInteger.valueOf( 10000000l ), null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MaxValidatorForStringTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,60 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import javax.validation.constraints.Max;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ */
+public class MaxValidatorForStringTest {
+
+ private static MaxValidatorForString constraint;
+
+ @BeforeClass
+ public static void init() {
+ AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>( Max.class );
+ descriptor.setValue( "value", 15l );
+ descriptor.setValue( "message", "{validator.max}" );
+ Max m = AnnotationFactory.create( descriptor );
+
+ constraint = new MaxValidatorForString();
+ constraint.initialize( m );
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( "15", null ) );
+ assertTrue( constraint.isValid( "15.0", null ) );
+ assertTrue( constraint.isValid( "10", null ) );
+ assertTrue( constraint.isValid( "14.99", null ) );
+ assertTrue( constraint.isValid( "-14.99", null ) );
+ assertFalse( constraint.isValid( "20", null ) );
+ //number format exception
+ assertFalse( constraint.isValid( "15l", null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForNumberTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,70 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.constraints.Min;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ * @author Hardy Ferentschik
+ */
+public class MinValidatorForNumberTest {
+
+ private static MinValidatorForNumber constraint;
+
+ @BeforeClass
+ public static void init() {
+ AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
+ descriptor.setValue( "value", 15l );
+ descriptor.setValue( "message", "{validator.min}" );
+ Min m = AnnotationFactory.create( descriptor );
+
+ constraint = new MinValidatorForNumber();
+ constraint.initialize( m );
+ }
+
+ @Test
+ public void testIsValid() {
+ byte b = 1;
+ Byte bWrapper = 127;
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( bWrapper, null ) );
+ assertTrue( constraint.isValid( 20, null ) );
+ assertTrue( constraint.isValid( 15l, null ) );
+ assertTrue( constraint.isValid( 15, null ) );
+ assertTrue( constraint.isValid( 15.0, null ) );
+ assertTrue( constraint.isValid( BigDecimal.valueOf( 156000000000.0 ), null ) );
+ assertTrue( constraint.isValid( BigInteger.valueOf( 10000000l ), null ) );
+ assertFalse( constraint.isValid( b, null ) );
+ assertFalse( constraint.isValid( BigDecimal.valueOf( -156000000000.0 ), null ) );
+ assertFalse( constraint.isValid( BigInteger.valueOf( -10000000l ), null ) );
+ assertFalse( constraint.isValid( 10, null ) );
+ assertFalse( constraint.isValid( 14.99, null ) );
+ assertFalse( constraint.isValid( -14.99, null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/MinValidatorForStringTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,61 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import javax.validation.constraints.Min;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ */
+public class MinValidatorForStringTest {
+
+ private static MinValidatorForString constraint;
+
+ @BeforeClass
+ public static void init() {
+ AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>( Min.class );
+ descriptor.setValue( "value", 15l );
+ descriptor.setValue( "message", "{validator.min}" );
+ Min m = AnnotationFactory.create( descriptor );
+
+ constraint = new MinValidatorForString();
+ constraint.initialize( m );
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( "20", null ) );
+ assertTrue( constraint.isValid( "15", null ) );
+ assertTrue( constraint.isValid( "15.0", null ) );
+ assertFalse( constraint.isValid( "10", null ) );
+ assertFalse( constraint.isValid( "14.99", null ) );
+ assertFalse( constraint.isValid( "-14.99", null ) );
+ //number format exception
+ assertFalse( constraint.isValid( "15l", null ) );
+ }
+
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NotNullValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotNullValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NotNullValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NotNullValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,36 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class NotNullValidatorTest {
+
+ @Test
+ public void testIsValid() {
+ NotNullValidator constraint = new NotNullValidator();
+
+ assertFalse( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( new Object(), null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NullValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NullValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NullValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/NullValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,42 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * @author Alaa Nassef
+ */
+public class NullValidatorTest {
+
+ private static NullValidator constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new NullValidator();
+ }
+
+ @Test
+ public void testIsValid() {
+ assertTrue( constraint.isValid( null, null ) );
+ assertFalse( constraint.isValid( new Object(), null ) );
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForCalendarTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForCalendarTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForCalendarTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForCalendarTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,64 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.util.Calendar;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.constraints.impl.PastValidatorForCalendar;
+
+/**
+ * @author Alaa Nassef
+ * @author Hardy Ferentschik
+ */
+public class PastValidatorForCalendarTest {
+
+ private static PastValidatorForCalendar constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new PastValidatorForCalendar();
+ }
+
+ @Test
+ public void testIsValid() {
+ Calendar futureDate = getFutureDate();
+ Calendar pastDate = getPastDate();
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( pastDate, null ) );
+ assertFalse( constraint.isValid( futureDate, null ) );
+ }
+
+ private Calendar getFutureDate() {
+ Calendar cal = Calendar.getInstance();
+ int year = cal.get( Calendar.YEAR );
+ cal.set( Calendar.YEAR, year + 1 );
+ return cal;
+ }
+
+ private Calendar getPastDate() {
+ Calendar cal = Calendar.getInstance();
+ int year = cal.get( Calendar.YEAR );
+ cal.set( Calendar.YEAR, year - 1 );
+ return cal;
+ }
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForDateTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PastValidatorForDateTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForDateTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PastValidatorForDateTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,60 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.util.Date;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class PastValidatorForDateTest {
+
+ private static PastValidatorForDate constraint;
+
+ @BeforeClass
+ public static void init() {
+ constraint = new PastValidatorForDate();
+ }
+
+ @Test
+ public void testIsValid() {
+ Date futureDate = getFutureDate();
+ Date pastDate = getPastDate();
+ assertTrue( constraint.isValid( null, null ) );
+ assertTrue( constraint.isValid( pastDate, null ) );
+ assertFalse( constraint.isValid( new Date(), null ) );
+ assertFalse( constraint.isValid( futureDate, null ) );
+ }
+
+ private Date getFutureDate() {
+ Date date = new Date();
+ long timeStamp = date.getTime();
+ date.setTime( timeStamp + 31557600000l );
+ return date;
+ }
+
+ private Date getPastDate() {
+ Date date = new Date();
+ long timeStamp = date.getTime();
+ date.setTime( timeStamp - 31557600000l );
+ return date;
+ }
+
+}
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PatternValidatorTest.java (from rev 16755, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PatternValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/PatternValidatorTest.java 2009-06-11 10:23:04 UTC (rev 16758)
@@ -0,0 +1,61 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import javax.validation.ValidationException;
+import javax.validation.constraints.Pattern;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PatternValidatorTest {
+
+ @Test
+ public void testIsValid() {
+ AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class );
+ descriptor.setValue( "regexp", "foobar" );
+ descriptor.setValue( "message", "{validator.pattern}" );
+ Pattern p = AnnotationFactory.create( descriptor );
+
+ PatternValidator constraint = new PatternValidator();
+ constraint.initialize( p );
+
+ assertTrue( constraint.isValid( null, null ) );
+ assertFalse( constraint.isValid( "", null ) );
+ assertFalse( constraint.isValid( "bla bla", null ) );
+ assertFalse( constraint.isValid( "This test is not foobar", null ) );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testInvalidRegularExpression() {
+ AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class );
+ descriptor.setValue( "regexp", "(unbalanced parentheses" );
+ descriptor.setValue( "message", "{validator.pattern}" );
+ Pattern p = AnnotationFactory.create( descriptor );
+
+ PatternValidator constraint = new PatternValidator();
+ constraint.initialize( p );
+ }
+}
15 years, 5 months
Hibernate SVN: r16757 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 06:21:57 -0400 (Thu, 11 Jun 2009)
New Revision: 16757
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
Log:
changed an error message
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-06-11 10:20:11 UTC (rev 16756)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-06-11 10:21:57 UTC (rev 16757)
@@ -68,7 +68,7 @@
//we now have all bind from a type to its resolution at one level
Type validatorType = ( ( ParameterizedType ) constraintValidatorType ).getActualTypeArguments()[VALIDATOR_TYPE_INDEX];
if ( validatorType == null ) {
- throw new ValidationException( "Null is an invalid type for a constraint validator." );
+ throw new ValidationException( "null is an invalid type for a constraint validator." );
}
else if ( validatorType instanceof GenericArrayType ) {
validatorType = TypeUtils.getArrayType( TypeUtils.getComponentType( validatorType ) );
15 years, 5 months
Hibernate SVN: r16756 - in validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints: impl and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-11 06:20:11 -0400 (Thu, 11 Jun 2009)
New Revision: 16756
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/SizeValidatorTest.java
Log:
HV-127
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/SizeValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/SizeValidatorTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/SizeValidatorTest.java 2009-06-11 10:20:11 UTC (rev 16756)
@@ -0,0 +1,172 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints.impl;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.Size;
+
+import static org.testng.Assert.assertTrue;
+import static org.testng.AssertJUnit.assertFalse;
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+
+/**
+ * @author Alaa Nassef
+ */
+public class SizeValidatorTest {
+
+
+ @Test
+ public void testIsValidObjectArray() throws Exception {
+ ConstraintValidator<Size, Object[]> validator = getValidator( SizeValidatorForArray.class );
+ assertSizes( validator, Object[].class );
+ }
+
+ @Test
+ public void testIsValidBooleanArray() throws Exception {
+ ConstraintValidator<Size, boolean[]> validator = getValidator( SizeValidatorForArraysOfBoolean.class );
+ assertSizes( validator, boolean[].class );
+ }
+
+ @Test
+ public void testIsValidByteArray() throws Exception {
+ ConstraintValidator<Size, byte[]> validator = getValidator( SizeValidatorForArraysOfByte.class );
+ assertSizes( validator, byte[].class );
+ }
+
+ @Test
+ public void testIsValidCharArray() throws Exception {
+ ConstraintValidator<Size, char[]> validator = getValidator( SizeValidatorForArraysOfChar.class );
+ assertSizes( validator, char[].class );
+ }
+
+ @Test
+ public void testIsValidDoubleArray() throws Exception {
+ ConstraintValidator<Size, double[]> validator = getValidator( SizeValidatorForArraysOfDouble.class );
+ assertSizes( validator, double[].class );
+ }
+
+ @Test
+ public void testIsValidFloatArray() throws Exception {
+ ConstraintValidator<Size, float[]> validator = getValidator( SizeValidatorForArraysOfFloat.class );
+ assertSizes( validator, float[].class );
+ }
+
+ @Test
+ public void testIsValidIntArray() throws Exception {
+ ConstraintValidator<Size, int[]> validator = getValidator( SizeValidatorForArraysOfInt.class );
+ assertSizes( validator, int[].class );
+ }
+
+ @Test
+ public void testIsValidLongArray() throws Exception {
+ ConstraintValidator<Size, long[]> validator = getValidator( SizeValidatorForArraysOfLong.class );
+ assertSizes( validator, long[].class );
+ }
+
+ @Test
+ public void testIsValidShortArray() throws Exception {
+ ConstraintValidator<Size, short[]> validator = getValidator( SizeValidatorForArraysOfShort.class );
+ assertSizes( validator, short[].class );
+ }
+
+ @Test
+ public void testIsValidCollection() throws Exception {
+ ConstraintValidator<Size, Collection> validator = getValidator( SizeValidatorForCollection.class );
+
+ assertTrue( validator.isValid( null, null ) );
+
+ Collection<String> collection = new ArrayList<String>();
+ assertFalse( validator.isValid( collection, null ) );
+
+ collection.add( "firstItem" );
+ assertTrue( validator.isValid( collection, null ) );
+
+ collection.add( "secondItem" );
+ assertTrue( validator.isValid( collection, null ) );
+
+ collection.add( "thirdItem" );
+ assertFalse( validator.isValid( collection, null ) );
+ }
+
+ @Test
+ public void testIsValidMap() throws Exception {
+ ConstraintValidator<Size, Map> validator = getValidator( SizeValidatorForMap.class );
+
+ assertTrue( validator.isValid( null, null ) );
+
+ Map<String, String> map = new HashMap<String, String>();
+ assertFalse( validator.isValid( map, null ) );
+
+ map.put( "key1", "firstItem" );
+ assertTrue( validator.isValid( map, null ) );
+
+ map.put( "key3", "secondItem" );
+ assertTrue( validator.isValid( map, null ) );
+
+ map.put( "key2", "thirdItem" );
+ assertFalse( validator.isValid( map, null ) );
+ }
+
+ @Test
+ public void testIsValiString() throws Exception {
+ ConstraintValidator<Size, String> validator = getValidator( SizeValidatorForString.class );
+
+ assertTrue( validator.isValid( null, null ) );
+ assertFalse( validator.isValid( "", null ) );
+ assertTrue( validator.isValid( "a", null ) );
+ assertTrue( validator.isValid( "ab", null ) );
+ assertFalse( validator.isValid( "abc", null ) );
+ }
+
+ private <T> ConstraintValidator<Size, T> getValidator(Class<?> validatorClass) throws Exception {
+ AnnotationDescriptor<Size> descriptor = new AnnotationDescriptor<Size>( Size.class );
+ descriptor.setValue( "min", 1 );
+ descriptor.setValue( "max", 2 );
+ descriptor.setValue( "message", "{validator.max}" );
+ Size m = AnnotationFactory.create( descriptor );
+ @SuppressWarnings("unchecked")
+ ConstraintValidator<Size, T> validator = ( ConstraintValidator<Size, T> ) validatorClass.newInstance();
+ validator.initialize( m );
+ return validator;
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> void assertSizes(ConstraintValidator<Size, T> validator, Class<T> arrayType) {
+ assertTrue( validator.isValid( null, null ) );
+
+ T array = ( T ) Array.newInstance( arrayType.getComponentType(), 0 );
+ assertFalse( validator.isValid( array, null ) );
+
+ array = ( T ) Array.newInstance( arrayType.getComponentType(), 1 );
+ assertTrue( validator.isValid( array, null ) );
+
+ array = ( T ) Array.newInstance( arrayType.getComponentType(), 2 );
+ assertTrue( validator.isValid( array, null ) );
+
+ array = ( T ) Array.newInstance( arrayType.getComponentType(), 3 );
+ assertFalse( validator.isValid( array, null ) );
+ }
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/SizeValidatorTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
15 years, 5 months
Hibernate SVN: r16755 - in search/trunk/src: main/java/org/hibernate/search/filter and 4 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-06-10 20:43:09 -0400 (Wed, 10 Jun 2009)
New Revision: 16755
Added:
search/trunk/src/main/java/org/hibernate/search/filter/FullTextFilterImplementor.java
search/trunk/src/main/java/org/hibernate/search/filter/ShardSensitiveOnlyFilter.java
search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategy.java
search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategyTest.java
Modified:
search/trunk/src/main/docbook/en-US/modules/configuration.xml
search/trunk/src/main/docbook/en-US/modules/query.xml
search/trunk/src/main/java/org/hibernate/search/filter/ChainedFilter.java
search/trunk/src/main/java/org/hibernate/search/query/FullTextFilterImpl.java
search/trunk/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java
search/trunk/src/main/java/org/hibernate/search/store/IdHashShardingStrategy.java
search/trunk/src/main/java/org/hibernate/search/store/IndexShardingStrategy.java
search/trunk/src/main/java/org/hibernate/search/store/NotShardedStrategy.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/UselessShardingStrategy.java
Log:
HSEARCH-251 Query on a shard subset based on a filter activation
Modified: search/trunk/src/main/docbook/en-US/modules/configuration.xml
===================================================================
--- search/trunk/src/main/docbook/en-US/modules/configuration.xml 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/docbook/en-US/modules/configuration.xml 2009-06-11 00:43:09 UTC (rev 16755)
@@ -206,21 +206,33 @@
<section id="search-configuration-directory-sharding" revision="1">
<title>Sharding indexes</title>
- <para>In some extreme cases involving huge indexes (in size), it is
- necessary to split (shard) the indexing data of a given entity type into
- several Lucene indexes. This solution is not recommended until you reach
- significant index sizes and index update times are slowing the application
- down. The main drawback of index sharding is that searches will end up
- being slower since more files have to be opened for a single search. In
+ <para>In some cases, it is necessary to split (shard) the indexing data of
+ a given entity type into several Lucene indexes. This solution is not
+ recommended unless there is a pressing need because by default, searches
+ will be slower as all shards have to be opened for a single search. In
other words don't do it until you have problems :)</para>
- <para>Despite this strong warning, Hibernate Search allows you to index a
- given entity type into several sub indexes. Data is sharded into the
- different sub indexes thanks to an
- <classname>IndexShardingStrategy</classname>. By default, no sharding
- strategy is enabled, unless the number of shards is configured. To
- configure the number of shards use the following property</para>
+ <para>For example, sharding may be desirable if:</para>
+ <itemizedlist>
+ <listitem>
+ <para>A single index is so huge that index update times are slowing
+ the application down.</para>
+ </listitem>
+
+ <listitem>
+ <para>A typical search will only hit a sub-set of the index, such as
+ when data is naturally segmented by customer, region or
+ application.</para>
+ </listitem>
+ </itemizedlist>
+
+ <para>Hibernate Search allows you to index a given entity type into
+ several sub indexes. Data is sharded into the different sub indexes thanks
+ to an <classname>IndexShardingStrategy</classname>. By default, no
+ sharding strategy is enabled, unless the number of shards is configured.
+ To configure the number of shards use the following property</para>
+
<example>
<title>Enabling index sharding by specifying nbr_of_shards for a
specific index</title>
@@ -243,10 +255,21 @@
<programlisting>hibernate.search.<indexName>.sharding_strategy my.shardingstrategy.Implementation</programlisting>
</example>
+ <para>Using a custom <classname>IndexShardingStrategy</classname>
+ implementation, it's possible to define what shard a given entity is
+ indexed to. </para>
+
+ <para>It also allows for optimizing searches by selecting which shard to
+ run the query onto. By activating a filter (see <xref
+ linkend="query-filter-shard" />), a sharding strategy can select a subset
+ of the shards used to answer a query
+ (<classname>IndexShardingStrategy.getDirectoryProvidersForQuery</classname>)
+ and thus speed up the query execution.</para>
+
<para>Each shard has an independent directory provider configuration as
described in <xref linkend="search-configuration-directory" />. The
- DirectoryProvider default name for the previous example are
- <literal><indexName>.0</literal> to
+ <classname>DirectoryProvider</classname> default name for the previous
+ example are <literal><indexName>.0</literal> to
<literal><indexName>.4</literal>. In other words, each shard has the
name of it's owning index followed by <constant>.</constant> (dot) and its
index number.</para>
@@ -367,14 +390,15 @@
<entry>Out of the box support for the Apache Lucene back end and
the JMS back end. Default to <literal>lucene</literal>. Supports
- also <literal>jms</literal> and <literal>blackhole</literal>.</entry>
+ also <literal>jms</literal> and
+ <literal>blackhole</literal>.</entry>
</row>
<row>
<entry><literal>hibernate.search.worker.execution</literal></entry>
- <entry>Supports synchronous and asynchronous execution. Default
- to <literal><literal>sync</literal></literal>. Supports also
+ <entry>Supports synchronous and asynchronous execution. Default to
+ <literal><literal>sync</literal></literal>. Supports also
<literal>async</literal>.</entry>
</row>
@@ -445,8 +469,8 @@
<section>
<title>Slave nodes</title>
- <para>Every index update operation is sent to a JMS queue. Index querying
- operations are executed on a local index copy.</para>
+ <para>Every index update operation is sent to a JMS queue. Index
+ querying operations are executed on a local index copy.</para>
<example>
<title>JMS Slave configuration</title>
@@ -605,8 +629,9 @@
<para>To enable Hibernate Search in Hibernate Core (ie. if you don't use
Hibernate Annotations), add the
<literal>FullTextIndexEventListener</literal> for the following six
- Hibernate events and also add it after the default
- <literal>DefaultFlushEventListener</literal>, as in the following example.</para>
+ Hibernate events and also add it after the default
+ <literal>DefaultFlushEventListener</literal>, as in the following
+ example.</para>
<example>
<title>Explicitly enabling Hibernate Search by configuring the
@@ -768,13 +793,13 @@
terms.</para> <para>This silently truncates large documents,
excluding from the index all terms that occur further in the
document. If you know your source documents are large, be sure to
- set this value high enough to accommodate the expected size. If you
- set it to Integer.MAX_VALUE, then the only limit is your memory,
- but you should anticipate an OutOfMemoryError. </para> <para>If
- setting this value in <literal>batch</literal> differently than in
- <literal>transaction</literal> you may get different data (and
- results) in your index depending on the indexing
- mode.</para></entry>
+ set this value high enough to accommodate the expected size. If
+ you set it to Integer.MAX_VALUE, then the only limit is your
+ memory, but you should anticipate an OutOfMemoryError. </para>
+ <para>If setting this value in <literal>batch</literal>
+ differently than in <literal>transaction</literal> you may get
+ different data (and results) in your index depending on the
+ indexing mode.</para></entry>
<entry>10000</entry>
</row>
@@ -852,24 +877,26 @@
</tbody>
</tgroup>
</table>
-
- <para>To tune the indexing speed it might be useful to time the
- object loading from database in isolation from the writes to the index.
- To achieve this set the <literal>blackhole</literal> as worker backend and start
- you indexing routines.
- This backend does not disable Hibernate Search: it will still generate the needed
- changesets to the index, but will discard them instead of flushing them to the index.
- As opposite to setting the <literal>hibernate.search.indexing_strategy</literal>
- to <literal>manual</literal> when using <literal>blackhole</literal> it will possibly load
- more data to rebuild the index from associated entities.</para>
-
+
+ <para>To tune the indexing speed it might be useful to time the object
+ loading from database in isolation from the writes to the index. To
+ achieve this set the <literal>blackhole</literal> as worker backend and
+ start you indexing routines. This backend does not disable Hibernate
+ Search: it will still generate the needed changesets to the index, but
+ will discard them instead of flushing them to the index. As opposite to
+ setting the <literal>hibernate.search.indexing_strategy</literal> to
+ <literal>manual</literal> when using <literal>blackhole</literal> it will
+ possibly load more data to rebuild the index from associated
+ entities.</para>
+
<programlisting>hibernate.search.worker.backend blackhole</programlisting>
-
- <para>The recommended approach is to focus first on optimizing the object loading, and then
- use the timings you achieve as a baseline to tune the indexing process.</para>
- <para>The <literal>blackhole</literal> backend is not meant to be used in production, only
- as a tool to identify indexing bottlenecks.</para>
-
+
+ <para>The recommended approach is to focus first on optimizing the object
+ loading, and then use the timings you achieve as a baseline to tune the
+ indexing process.</para>
+
+ <para>The <literal>blackhole</literal> backend is not meant to be used in
+ production, only as a tool to identify indexing bottlenecks.</para>
</section>
<section id="search-configuration-directory-lockfactories" revision="1">
@@ -883,6 +910,8 @@
for most cases, but it's possible to specify for each index managed by
Hibernate Search which LockingFactory you want to use.</para>
+
+
<para>Some of these locking strategies require a filesystem level lock and
may be used even on RAM based indexes, but this is not recommended and of
no practical use.</para>
@@ -976,7 +1005,7 @@
</tgroup>
</table></para>
- Configuration example:
+ Configuration example:
<programlisting>hibernate.search.default.locking_strategy simple
hibernate.search.Animals.locking_strategy native
@@ -988,4 +1017,4 @@
</section>
-</chapter>
+</chapter>
\ No newline at end of file
Modified: search/trunk/src/main/docbook/en-US/modules/query.xml
===================================================================
--- search/trunk/src/main/docbook/en-US/modules/query.xml 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/docbook/en-US/modules/query.xml 2009-06-11 00:43:09 UTC (rev 16755)
@@ -345,8 +345,8 @@
</listitem>
<listitem>
- <para>FullTextQuery.OBJECT_CLASS: returns the class of the
- indexed entity.</para>
+ <para>FullTextQuery.OBJECT_CLASS: returns the class of the indexed
+ entity.</para>
</listitem>
<listitem>
@@ -545,7 +545,7 @@
</section>
</section>
- <section>
+ <section id="query-filter">
<title>Filters</title>
<para>Apache Lucene has a powerful feature that allows to filter query
@@ -833,6 +833,105 @@
time spent to execute the query)</para>
</listitem>
</itemizedlist>
+
+ <section id="query-filter-shard">
+ <title>Using filters in a sharded environment</title>
+
+ <para>It is possible, in a sharded environment to execute queries on a
+ subset of the available shards. This can be done in two steps:</para>
+
+ <itemizedlist>
+ <listitem>
+ <para>create a sharding strategy that does select a subset of
+ <classname>DirectoryProvider</classname>s depending on sone filter
+ configuration</para>
+ </listitem>
+
+ <listitem>
+ <para>activate the proper filter at query time</para>
+ </listitem>
+ </itemizedlist>
+
+ <para>Let's first look at an example of sharding strategy that query on
+ a specific customer shard if the customer filter is activated.</para>
+
+ <programlisting>public class CustomerShardingStrategy implements IndexShardingStrategy {
+
+ // stored DirectoryProviders in a array indexed by customerID
+ private DirectoryProvider<?>[] providers;
+
+ public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
+ this.providers = providers;
+ }
+
+ public DirectoryProvider<?>[] getDirectoryProvidersForAllShards() {
+ return providers;
+ }
+
+ public DirectoryProvider<?> getDirectoryProviderForAddition(Class<?> entity, Serializable id, String idInString, Document document) {
+ Integer customerID = Integer.parseInt(document.getField("customerID").stringValue());
+ return providers[customerID];
+ }
+
+ public DirectoryProvider<?>[] getDirectoryProvidersForDeletion(Class<?> entity, Serializable id, String idInString) {
+ return getDirectoryProvidersForAllShards();
+ }
+
+<emphasis role="bold"> /**
+ * Optimization; don't search ALL shards and union the results; in this case, we
+ * can be certain that all the data for a particular customer Filter is in a single
+ * shard; simply return that shard by customerID.
+ */
+ public DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] filters) {
+ FFullTextFilter filter = getCustomerFilter(filters, "customer");
+ if (filter == null) {
+ return getDirectoryProvidersForAllShards();
+ }
+ else {
+ return new DirectoryProvider[] { providers[Integer.parseInt(filter.getParameter("customerID").toString())] };
+ }
+ }
+
+ private FullTextFilter getFilter(FullTextFilterImplementor[] filters, String name) {
+ for (FullTextFilterImplementor filter: filters) {
+ if (filter.getName().equals(name)) return filter;
+ }
+ return null;
+ }</emphasis>
+
+}</programlisting>
+
+ <para>In this example, if the filter named <literal>customer</literal>
+ is present, we make sure to only use the shard dedicated to this
+ customer. Otherwise, we return all shards. A given Sharding strategy can
+ react to one or more filters and depends on their parameters.</para>
+
+ <para>The second step is simply to activate the filter at query time.
+ While the filter can be a regular filter (as defined in <xref
+ linkend="query-filter" />) which also filters Lucene results after the
+ query, you can make use of a special filter that will only be passed to
+ the sharding strategy and otherwise ignored for the rest of the query.
+ Simply use the <classname>ShardSensitiveOnlyFilter</classname> class
+ when declaring your filter.</para>
+
+ <programlisting>@Entity @Indexed
+<emphasis role="bold">@FullTextFilterDef(name="customer", impl=ShardSensitiveOnlyFilter.class)</emphasis>
+public class Customer {
+ ...
+}
+
+
+FullTextQuery query = ftEm.createFullTextQuery(luceneQuery, Customer.class);
+<emphasis role="bold">query.enableFulltextFilter("customer").setParameter("CustomerID", 5);</emphasis>
+@SuppressWarnings("unchecked")
+List<Customer> results = query.getResultList();</programlisting>
+
+ <para>Note that by using the
+ <classname>ShardSensitiveOnlyFilter</classname>, you do not have to
+ implement any Lucene filter. Using filters and sharding strategy
+ reacting to these filters is recommended to speed up queries in a
+ sharded environment.</para>
+ </section>
</section>
<section>
@@ -866,4 +965,4 @@
run Lucene specific queries. Check <xref linkend="search-lucene-native" />
for more information.</para>
</section>
-</chapter>
+</chapter>
\ No newline at end of file
Modified: search/trunk/src/main/java/org/hibernate/search/filter/ChainedFilter.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/filter/ChainedFilter.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/filter/ChainedFilter.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -30,6 +30,10 @@
this.chainedFilters.add( filter );
}
+ public boolean isEmpty() {
+ return chainedFilters.size() == 0;
+ }
+
public BitSet bits(IndexReader reader) throws IOException {
throw new UnsupportedOperationException();
}
Added: search/trunk/src/main/java/org/hibernate/search/filter/FullTextFilterImplementor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/filter/FullTextFilterImplementor.java (rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/filter/FullTextFilterImplementor.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -0,0 +1,15 @@
+package org.hibernate.search.filter;
+
+import org.hibernate.search.FullTextFilter;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public interface FullTextFilterImplementor extends FullTextFilter {
+ /**
+ * Returns the Filter name
+ */
+ String getName();
+
+ //TODO should we expose Map<String, Object> getParameters()
+}
Added: search/trunk/src/main/java/org/hibernate/search/filter/ShardSensitiveOnlyFilter.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/filter/ShardSensitiveOnlyFilter.java (rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/filter/ShardSensitiveOnlyFilter.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -0,0 +1,12 @@
+package org.hibernate.search.filter;
+
+/**
+ * When using this class in @FullTextFilterDef.impl, Hibernate Search
+ * considers the filter to be only influencing the sharding strategy.
+ *
+ * This filter is not applied on the results of the Lucene query.
+ *
+ * @author Emmanuel Bernard
+ */
+public interface ShardSensitiveOnlyFilter {
+}
Modified: search/trunk/src/main/java/org/hibernate/search/query/FullTextFilterImpl.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/query/FullTextFilterImpl.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/query/FullTextFilterImpl.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -5,11 +5,12 @@
import java.util.Map;
import org.hibernate.search.FullTextFilter;
+import org.hibernate.search.filter.FullTextFilterImplementor;
/**
* @author Emmanuel Bernard
*/
-public class FullTextFilterImpl implements FullTextFilter {
+public class FullTextFilterImpl implements FullTextFilterImplementor {
private final Map<String, Object> parameters = new HashMap<String, Object>();
private String name;
Modified: search/trunk/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -54,9 +54,12 @@
import org.hibernate.search.filter.ChainedFilter;
import org.hibernate.search.filter.FilterKey;
import org.hibernate.search.filter.StandardFilterKey;
+import org.hibernate.search.filter.FullTextFilterImplementor;
+import org.hibernate.search.filter.ShardSensitiveOnlyFilter;
import org.hibernate.search.reader.ReaderProvider;
import static org.hibernate.search.reader.ReaderProviderHelper.getIndexReaders;
import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.IndexShardingStrategy;
import org.hibernate.search.util.ContextHelper;
import static org.hibernate.search.util.FilterCacheModeTypeHelper.cacheInstance;
import static org.hibernate.search.util.FilterCacheModeTypeHelper.cacheResults;
@@ -84,6 +87,7 @@
private Integer resultSize;
private Sort sort;
private Filter filter;
+ private Filter userFilter;
private Criteria criteria;
private String[] indexProjection;
private Set<String> idFieldNames;
@@ -92,7 +96,9 @@
private SearchFactoryImplementor searchFactoryImplementor;
private Map<String, FullTextFilterImpl> filterDefinitions;
private int fetchSize = 1;
+ private static final FullTextFilterImplementor[] EMPTY_FULL_TEXT_FILTER_IMPLEMENTOR = new FullTextFilterImplementor[0];
+
/**
* Constructs a <code>FullTextQueryImpl</code> instance.
*
@@ -127,7 +133,7 @@
* {@inheritDoc}
*/
public FullTextQuery setFilter(Filter filter) {
- this.filter = filter;
+ this.userFilter = filter;
return this;
}
@@ -399,20 +405,27 @@
}
private void buildFilters() {
- if ( filterDefinitions == null || filterDefinitions.size() == 0 ) {
- return; // there is nothing to do if we don't have any filter definitions
+ ChainedFilter chainedFilter = null;
+ if ( ! ( filterDefinitions == null || filterDefinitions.size() == 0 ) ) {
+ chainedFilter = new ChainedFilter();
+ for ( FullTextFilterImpl fullTextFilter : filterDefinitions.values() ) {
+ Filter filter = buildLuceneFilter( fullTextFilter );
+ if (filter != null) chainedFilter.addFilter( filter );
+ }
}
- ChainedFilter chainedFilter = new ChainedFilter();
- for ( FullTextFilterImpl fullTextFilter : filterDefinitions.values() ) {
- Filter filter = buildLuceneFilter( fullTextFilter );
- chainedFilter.addFilter( filter );
+ if ( userFilter != null ) {
+ //chainedFilter is not always necessary here but the code is easier to read
+ if (chainedFilter == null) chainedFilter = new ChainedFilter();
+ chainedFilter.addFilter( userFilter );
}
- if ( filter != null ) {
- chainedFilter.addFilter( filter );
+ if ( chainedFilter == null || chainedFilter.isEmpty() ) {
+ filter = null;
}
- filter = chainedFilter;
+ else {
+ filter = chainedFilter;
+ }
}
/**
@@ -430,6 +443,10 @@
* as FilterCachingStrategy ensure a memory barrier between concurrent thread calls
*/
FilterDef def = searchFactoryImplementor.getFilterDefinition( fullTextFilter.getName() );
+ //def can never be null, ti's guarded by enableFullTextFilter(String)
+
+ if ( isPreQueryFilterOnly(def) ) return null;
+
Object instance = createFilterInstance( fullTextFilter, def );
FilterKey key = createFilterKey( def, instance );
@@ -449,6 +466,10 @@
return filter;
}
+ private boolean isPreQueryFilterOnly(FilterDef def) {
+ return def.getImpl().equals( ShardSensitiveOnlyFilter.class );
+ }
+
private Filter createFilter(FilterDef def, Object instance) {
Filter filter;
if ( def.getFactoryMethod() != null ) {
@@ -633,7 +654,7 @@
*/
private IndexSearcher buildSearcher(SearchFactoryImplementor searchFactoryImplementor) {
Map<Class<?>, DocumentBuilderIndexedEntity<?>> builders = searchFactoryImplementor.getDocumentBuildersIndexedEntities();
- List<DirectoryProvider> directories = new ArrayList<DirectoryProvider>();
+ List<DirectoryProvider> targetedDirectories = new ArrayList<DirectoryProvider>();
Set<String> idFieldNames = new HashSet<String>();
Similarity searcherSimilarity = null;
@@ -653,9 +674,7 @@
idFieldNames.add( builder.getIdKeywordName() );
allowFieldSelectionInProjection = allowFieldSelectionInProjection && builder.allowFieldSelectionInProjection();
}
- final DirectoryProvider[] directoryProviders = builder.getDirectoryProviderSelectionStrategy()
- .getDirectoryProvidersForAllShards();
- populateDirectories( directories, directoryProviders );
+ populateDirectories( targetedDirectories, builder );
}
classesAndSubclasses = null;
}
@@ -679,10 +698,8 @@
idFieldNames.add( builder.getIdKeywordName() );
allowFieldSelectionInProjection = allowFieldSelectionInProjection && builder.allowFieldSelectionInProjection();
}
- final DirectoryProvider[] directoryProviders = builder.getDirectoryProviderSelectionStrategy()
- .getDirectoryProvidersForAllShards();
searcherSimilarity = checkSimilarity( searcherSimilarity, builder );
- populateDirectories( directories, directoryProviders );
+ populateDirectories( targetedDirectories, builder );
}
this.classesAndSubclasses = involvedClasses;
}
@@ -691,7 +708,7 @@
//compute optimization needClassFilterClause
//if at least one DP contains one class that is not part of the targeted classesAndSubclasses we can't optimize
if ( classesAndSubclasses != null ) {
- for ( DirectoryProvider dp : directories ) {
+ for ( DirectoryProvider dp : targetedDirectories ) {
final Set<Class<?>> classesInDirectoryProvider = searchFactoryImplementor.getClassesInDirectoryProvider(
dp
);
@@ -712,7 +729,7 @@
}
//set up the searcher
- final DirectoryProvider[] directoryProviders = directories.toArray( new DirectoryProvider[directories.size()] );
+ final DirectoryProvider[] directoryProviders = targetedDirectories.toArray( new DirectoryProvider[targetedDirectories.size()] );
IndexSearcher is = new IndexSearcher(
searchFactoryImplementor.getReaderProvider().openReader(
directoryProviders
@@ -722,7 +739,19 @@
return is;
}
- private void populateDirectories(List<DirectoryProvider> directories, DirectoryProvider[] directoryProviders) {
+ private void populateDirectories(List<DirectoryProvider> directories, DocumentBuilderIndexedEntity builder) {
+ final IndexShardingStrategy indexShardingStrategy = builder.getDirectoryProviderSelectionStrategy();
+ final DirectoryProvider[] directoryProviders;
+ if ( filterDefinitions != null && !filterDefinitions.isEmpty() ) {
+ directoryProviders = indexShardingStrategy.getDirectoryProvidersForQuery(
+ filterDefinitions.values().toArray( new FullTextFilterImplementor[filterDefinitions.size()] )
+ );
+ }
+ else {
+ //no filter get all shards
+ directoryProviders = indexShardingStrategy.getDirectoryProvidersForQuery( EMPTY_FULL_TEXT_FILTER_IMPLEMENTOR );
+ }
+
for ( DirectoryProvider provider : directoryProviders ) {
if ( !directories.contains( provider ) ) {
directories.add( provider );
Modified: search/trunk/src/main/java/org/hibernate/search/store/IdHashShardingStrategy.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/store/IdHashShardingStrategy.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/store/IdHashShardingStrategy.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -6,6 +6,8 @@
import org.apache.lucene.document.Document;
+import org.hibernate.search.filter.FullTextFilterImplementor;
+
/**
* This implementation use idInString as the hashKey.
*
@@ -31,6 +33,10 @@
return new DirectoryProvider[] { providers[hashKey( idInString )] };
}
+ public DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] fullTextFilters) {
+ return getDirectoryProvidersForAllShards();
+ }
+
private int hashKey(String key) {
// reproduce the hashCode implementation of String as documented in the javadoc
// to be safe cross Java version (in case it changes some day)
Modified: search/trunk/src/main/java/org/hibernate/search/store/IndexShardingStrategy.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/store/IndexShardingStrategy.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/store/IndexShardingStrategy.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -6,6 +6,8 @@
import org.apache.lucene.document.Document;
+import org.hibernate.search.filter.FullTextFilterImplementor;
+
/**
* Defines how a given virtual index shards data into different DirectoryProviders
*
@@ -32,4 +34,13 @@
* id and idInString can be null. If null, all the directory providers containing entity types should be returned
*/
DirectoryProvider<?>[] getDirectoryProvidersForDeletion(Class<?> entity, Serializable id, String idInString);
+
+ /**
+ * return the set of DirectoryProvider(s) where the entities matching the filters are stored
+ * this optional optimization allows queries to hit a subset of all shards, which may be useful for some datasets
+ * if this optimization is not needed, return getDirectoryProvidersForAllShards()
+ *
+ * fullTextFilters can be empty if no filter is applied
+ */
+ DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] fullTextFilters);
}
Modified: search/trunk/src/main/java/org/hibernate/search/store/NotShardedStrategy.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/store/NotShardedStrategy.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/main/java/org/hibernate/search/store/NotShardedStrategy.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -6,6 +6,7 @@
import org.apache.lucene.document.Document;
import org.hibernate.annotations.common.AssertionFailure;
+import org.hibernate.search.filter.FullTextFilterImplementor;
/**
* @author Emmanuel Bernard
@@ -31,4 +32,8 @@
return directoryProvider;
}
+ public DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] fullTextFilters) {
+ return directoryProvider;
+ }
+
}
Modified: search/trunk/src/test/java/org/hibernate/search/test/configuration/UselessShardingStrategy.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/configuration/UselessShardingStrategy.java 2009-06-10 21:48:21 UTC (rev 16754)
+++ search/trunk/src/test/java/org/hibernate/search/test/configuration/UselessShardingStrategy.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -8,6 +8,7 @@
import org.apache.lucene.document.Document;
import org.hibernate.search.store.DirectoryProvider;
import org.hibernate.search.store.IndexShardingStrategy;
+import org.hibernate.search.filter.FullTextFilterImplementor;
/**
* Used to test the configuration of a third-party strategy
@@ -27,6 +28,10 @@
return null;
}
+ public DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] fullTextFilters) {
+ return null;
+ }
+
public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
Enumeration<?> propertyNames = properties.propertyNames();
int counter;
Added: search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategy.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategy.java (rev 0)
+++ search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategy.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -0,0 +1,65 @@
+package org.hibernate.search.test.shards;
+
+import java.io.Serializable;
+import java.util.Properties;
+
+import org.apache.lucene.document.Document;
+
+import org.hibernate.search.FullTextFilter;
+import org.hibernate.search.filter.FullTextFilterImplementor;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.IndexShardingStrategy;
+
+/**
+ * Shards an index containing data for multiple customers by customerID. customerID is
+ * provided as a property on all indexes entities, and is also defined as a Filter.
+ *
+ * The number of shards should be configured to be MAX(customerID).
+ *
+ * @author Chase Seibert
+ */
+public class CustomerShardingStrategy implements IndexShardingStrategy {
+
+ // stored DirectoryProviders in a array indexed by customerID
+ private DirectoryProvider<?>[] providers;
+
+ public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
+ this.providers = providers;
+ }
+
+ public DirectoryProvider<?>[] getDirectoryProvidersForAllShards() {
+ return providers;
+ }
+
+ public DirectoryProvider<?> getDirectoryProviderForAddition(Class<?> entity, Serializable id, String idInString, Document document) {
+ Integer customerID = Integer.parseInt(document.getField("customerID").stringValue());
+ return providers[customerID];
+ }
+
+ public DirectoryProvider<?>[] getDirectoryProvidersForDeletion(Class<?> entity, Serializable id, String idInString) {
+ return getDirectoryProvidersForAllShards();
+ }
+
+ /**
+ * Optimization; don't search ALL shards and union the results; in this case, we
+ * can be certain that all the data for a particular customer Filter is in a single
+ * shard; simply return that shard by customerID.
+ */
+ public DirectoryProvider<?>[] getDirectoryProvidersForQuery(FullTextFilterImplementor[] filters) {
+ FullTextFilter filter = getCustomerFilter(filters, "customer");
+ if (filter == null) {
+ return getDirectoryProvidersForAllShards();
+ }
+ else {
+ return new DirectoryProvider[] { providers[Integer.parseInt(filter.getParameter("customerID").toString())] };
+ }
+ }
+
+ private FullTextFilter getCustomerFilter(FullTextFilterImplementor[] filters, String name) {
+ for (FullTextFilterImplementor filter: filters) {
+ if (filter.getName().equals(name)) return filter;
+ }
+ return null;
+ }
+
+}
Added: search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategyTest.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategyTest.java (rev 0)
+++ search/trunk/src/test/java/org/hibernate/search/test/shards/CustomerShardingStrategyTest.java 2009-06-11 00:43:09 UTC (rev 16755)
@@ -0,0 +1,56 @@
+package org.hibernate.search.test.shards;
+
+import junit.framework.TestCase;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.hibernate.search.query.FullTextFilterImpl;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.RAMDirectoryProvider;
+
+/**
+ * @author Chase Seibert
+ */
+public class CustomerShardingStrategyTest extends TestCase {
+
+ private CustomerShardingStrategy shardStrategy;
+
+ protected void setUp() throws Exception {
+ shardStrategy = new CustomerShardingStrategy();
+
+ // initilaize w/ 10 shards
+ shardStrategy.initialize( null, new DirectoryProvider[] {
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider(),
+ new RAMDirectoryProvider()
+ } );
+ }
+
+ public void testGetDirectoryProvidersForQuery() {
+
+ FullTextFilterImpl filter = new FullTextFilterImpl();
+ filter.setName("customer");
+ filter.setParameter("customerID", 5);
+
+ // customerID == 5 should correspond to just a single shard instance
+ DirectoryProvider[] providers = shardStrategy.getDirectoryProvidersForQuery(new FullTextFilterImpl[] { filter });
+ assertTrue(providers.length == 1);
+
+ // create a dummy document for the same customerID, and make sure the shard it would be
+ // indexed on matches the shard returned by getDirectoryProvidersForQuery()
+ Document document = new Document();
+ document.add(new Field("customerID", "5", Field.Store.NO, Field.Index.UN_TOKENIZED));
+
+ assertTrue(providers[0].equals(
+ shardStrategy.getDirectoryProviderForAddition(null, null, null, document)
+ ));
+ }
+
+}
\ No newline at end of file
15 years, 5 months