[hibernate-commits] Hibernate SVN: r16763 - in beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests: validation and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jun 11 11:54:30 EDT 2009


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
+ */
+ at Artifact(artifactType = ArtifactType.JSR303)
+ at 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
+ */
+ at Documented
+ at Constraint(validatedBy = DummyValidator.class)
+ at Target({ METHOD, FIELD })
+ at 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




More information about the hibernate-commits mailing list