Skip to content

Commit

Permalink
BVTCK-45 Adding test for injection into validator
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling authored and hferentschik committed Mar 14, 2013
1 parent 8803e4a commit 9689583
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.integration.cdi.factory;

import java.util.Set;
import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.ValidatorFactory;

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.Test;

import org.hibernate.beanvalidation.tck.util.Groups;
import org.hibernate.beanvalidation.tck.util.IntegrationTest;
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;

import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintViolationMessages;

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

@Inject
private ValidatorFactory defaultValidatorFactory;

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClassPackage( ConstraintValidatorInjectionTest.class )
.withEmptyBeansXml()
.build();
}


@Test(groups = Groups.FAILING_ON_AS)
@SpecAssertion(section = "10.1.1", id = "b")
public void testDependencyInjectionIntoConstraintValidator() {
Set<ConstraintViolation<Foo>> violations = defaultValidatorFactory.getValidator().validate( new Foo() );

assertCorrectConstraintViolationMessages( violations, "Hello, bar!", "Good morning, qux!" );
}

private static class Foo {
@GreetingConstraint(name = "bar")
public String bar;

@GreetingConstraint(name = "qux")
public Integer qux;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.integration.cdi.factory;

/**
* @author Gunnar Morling
*/
public class Greeter {

public final static String MESSAGE = "Hello, %s!";

public final static String FORMAL_MESSAGE = "Good morning, %s!";

public String greet(String name) {
return String.format( MESSAGE, name );
}

public String greetFormally(String name) {
return String.format( FORMAL_MESSAGE, name );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.integration.cdi.factory;

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

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;

/**
* @author Gunnar Morling
*/
@Constraint(validatedBy = { GreetingConstraintValidatorForString.class, GreetingConstraintValidatorForInteger.class })
@Documented
@Target({ METHOD, FIELD, TYPE })
@Retention(RUNTIME)
public @interface GreetingConstraint {
String message() default "my custom constraint";

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

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

String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.integration.cdi.factory;

import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* A validator which makes use of constructor injection.
*
* @author Gunnar Morling
*/
public class GreetingConstraintValidatorForInteger implements ConstraintValidator<GreetingConstraint, Integer> {

private final Greeter greeter;
private String name;

@Inject
public GreetingConstraintValidatorForInteger(Greeter greeter) {
this.greeter = greeter;
}

@Override
public void initialize(GreetingConstraint constraintAnnotation) {
this.name = constraintAnnotation.name();
}

@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate( greeter.greetFormally( name ) ).addConstraintViolation();

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.integration.cdi.factory;

import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* @author Gunnar Morling
*/
public class GreetingConstraintValidatorForString implements ConstraintValidator<GreetingConstraint, String> {

@Inject
private Greeter greeter;

private String name;

public GreetingConstraintValidatorForString() {
}

@Override
public void initialize(GreetingConstraint constraintAnnotation) {
this.name = constraintAnnotation.name();
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate( greeter.greet( name ) ).addConstraintViolation();

return false;
}
}

0 comments on commit 9689583

Please sign in to comment.