-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BVTCK-45 Adding test for injection into validator
- Loading branch information
1 parent
8803e4a
commit 9689583
Showing
5 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...te/beanvalidation/tck/tests/integration/cdi/factory/ConstraintValidatorInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/factory/Greeter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...va/org/hibernate/beanvalidation/tck/tests/integration/cdi/factory/GreetingConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
50 changes: 50 additions & 0 deletions
50
...anvalidation/tck/tests/integration/cdi/factory/GreetingConstraintValidatorForInteger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...eanvalidation/tck/tests/integration/cdi/factory/GreetingConstraintValidatorForString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |