Skip to content

Commit

Permalink
BVTCK-42 Adding/marking validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Mar 7, 2013
1 parent a3788ce commit d361fb3
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 2 deletions.
Expand Up @@ -290,6 +290,7 @@ public void testGraphValidationWithList() {
@Test
@SpecAssertions({
@SpecAssertion(section = "3.4", id = "r"),
@SpecAssertion(section = "4.1.3", id = "i"),
@SpecAssertion(section = "4.1.3", id = "d")
})
public void testGraphValidationWithArray() {
Expand Down
@@ -0,0 +1,189 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and/or its affiliates, 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.beanvalidation.tck.tests.validation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import javax.validation.Validator;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import org.hibernate.beanvalidation.tck.util.TestUtil;
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.testng.Assert.assertEquals;

/**
* @author Gunnar Morling
*/
@SpecVersion(spec = "beanvalidation", version = "1.1.0")
public class ValueAccessStrategyTest extends Arquillian {

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClass( ValueAccessStrategyTest.class )
.build();
}

private Validator validator;

@Test
@SpecAssertion(section = "4.2", id = "a")
public void testValidatedObjectIsPassedToValidatorOfClassLevelConstraint() {
Person person = new Person();
validator.validate( person );

assertEquals( ValidPerson.ValidPersonValidator.validatedValue, person );
}

@Test
@SpecAssertion(section = "4.2", id = "a")
public void testValueFromFieldIsPassedToValidatorOfFieldLevelConstraint() {
Person person = new Person();
validator.validate( person );

assertEquals(
ValidFirstName.ValidFirstNameValidator.validatedValue,
"Bob",
"Expected value to be retrieved from field."
);
}

@Test
@SpecAssertion(section = "4.2", id = "a")
public void testValueFromGetterIsPassedToValidatorOfPropertyLevelConstraint() {
Person person = new Person();
validator.validate( person );

assertEquals( ValidName.Validator.validatedValue, "Billy", "Expected value to be retrieved from getter." );
}

@BeforeMethod
public void setupValidator() {
validator = TestUtil.getValidatorUnderTest();
}

@ValidPerson
private static class Person {

@ValidFirstName
private final String firstName = "Bob";

@ValidName
public String getFirstName() {
return "Billy";
}
}

@Constraint(validatedBy = ValidPerson.ValidPersonValidator.class)
@Documented
@Target({ METHOD, CONSTRUCTOR, FIELD, TYPE })
@Retention(RUNTIME)
public @interface ValidPerson {
String message() default "{ValidPerson.message}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

public static class ValidPersonValidator implements ConstraintValidator<ValidPerson, Person> {

private static Person validatedValue;

@Override
public void initialize(ValidPerson constraintAnnotation) {
}

@Override
public boolean isValid(Person value, ConstraintValidatorContext context) {
validatedValue = value;
return false;
}
}
}

@Constraint(validatedBy = ValidFirstName.ValidFirstNameValidator.class)
@Documented
@Target({ METHOD, CONSTRUCTOR, FIELD, TYPE })
@Retention(RUNTIME)
public @interface ValidFirstName {
String message() default "{ValidFirstName.message}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

public static class ValidFirstNameValidator implements ConstraintValidator<ValidFirstName, String> {

private static String validatedValue;

@Override
public void initialize(ValidFirstName constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
validatedValue = value;
return false;
}
}
}

@Constraint(validatedBy = ValidName.Validator.class)
@Documented
@Target({ METHOD, CONSTRUCTOR, FIELD, TYPE })
@Retention(RUNTIME)
public @interface ValidName {
String message() default "{ValidFirstName.message}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

public static class Validator implements ConstraintValidator<ValidName, String> {

private static String validatedValue;

@Override
public void initialize(ValidName constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
validatedValue = value;
return false;
}
}
}
}
Expand Up @@ -145,7 +145,10 @@ public void testTypeOfContainedValueIsDeterminedAtRuntime() {
}

@Test
@SpecAssertion(section = "4.1.3", id = "f")
@SpecAssertions({
@SpecAssertion(section = "4.1.3", id = "f"),
@SpecAssertion(section = "4.1.3", id = "i")
})
public void testContainedSet() {
MultiCage cage = new MultiCage();
cage.addAnimal( new Zebra( null ) );
Expand All @@ -162,7 +165,10 @@ public void testContainedSet() {
}

@Test
@SpecAssertion(section = "4.1.3", id = "c")
@SpecAssertions({
@SpecAssertion(section = "4.1.3", id = "c"),
@SpecAssertion(section = "4.1.3", id = "i")
})
public void testContainedIterable() {
GameReserve<Zebra> reserve = new GameReserve<Zebra>();
Herd<Zebra> zebraHerd = new Herd<Zebra>();
Expand Down Expand Up @@ -209,6 +215,7 @@ public void testTypeOfContainedValuesIsDeterminedAtRuntime() {
@Test
@SpecAssertions({
@SpecAssertion(section = "4.1.3", id = "h"),
@SpecAssertion(section = "4.1.3", id = "i"),
@SpecAssertion(section = "4.1.3", id = "j")
})
public void testContainedMap() {
Expand Down

0 comments on commit d361fb3

Please sign in to comment.