Skip to content

Commit

Permalink
BVTCK-45 Adding tests for conversion of XML-configured annotation att…
Browse files Browse the repository at this point in the history
…ribute values
  • Loading branch information
gunnarmorling committed Mar 10, 2013
1 parent 50a791b commit 9157139
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 30 deletions.
Expand Up @@ -29,7 +29,6 @@
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


/**
* @author Hardy Ferentschik
*/
Expand All @@ -38,23 +37,40 @@
@Target({ METHOD, FIELD, TYPE })
@Retention(RUNTIME)
public @interface ConsistentUserInformation {
public abstract String message() default "User information is not consistent.";
String message() default "User information is not consistent.";

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

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

byte byteParam() default 0;

short shortParam() default 0;

int intParam() default 0;

long longParam() default 0;

float floatParam() default 0;

double doubleParam() default 0;

boolean booleanParam() default false;

public abstract Class<?>[] groups() default { };
char charParam() default 0;

public abstract Class<? extends Payload>[] payload() default { };
String stringParam() default "";

public abstract String stringParam() default "";
Class<?> classParam() default void.class;

public abstract Class<?> classParam() default void.class;
Class<?> unqualifiedClassParam() default void.class;

public abstract String[] stringArrayParam() default { };
String[] stringArrayParam() default { };

public abstract int intParam() default 0;

public abstract Max max() default @Max(value = 10);
Max max() default @Max(value = 10);

public abstract Pattern[] patterns();
Pattern[] patterns();

public abstract UserType userType() default UserType.BUYER;
UserType userType() default UserType.BUYER;
}
@@ -0,0 +1,23 @@
/*
* 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.xmlconfiguration;

/**
* @author Gunnar Morling
*/
public class SuperUser extends User {
}
Expand Up @@ -21,6 +21,7 @@
import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
import javax.validation.Payload;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
Expand All @@ -31,6 +32,7 @@
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import org.hibernate.beanvalidation.tck.util.TestUtil;
Expand All @@ -48,6 +50,8 @@
@SpecVersion(spec = "beanvalidation", version = "1.1.0")
public class XmlConfigurationTest extends Arquillian {

private Validator validator;

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
Expand All @@ -63,15 +67,22 @@ public static WebArchive createTestArchive() {
Order.class,
OrderLine.class,
CreditCard.class,
TestGroup.class
TestGroup.class,
SuperUser.class
)
.withValidationXml( "validation-XmlConfigurationTest.xml" )
.withResource( "user-constraints.xml" )
.withResource( "superuser-constraints.xml" )
.withResource( "order-constraints.xml" )
.withResource( "order-constraints-XmlConfigurationTest.xml" )
.build();
}

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

@Test
@SpecAssertions({
@SpecAssertion(section = "5.5.6", id = "a"),
Expand All @@ -82,8 +93,6 @@ public static WebArchive createTestArchive() {
@SpecAssertion(section = "8.1.2", id = "a")
})
public void testClassConstraintDefinedInXml() {
Validator validator = TestUtil.getValidatorUnderTest();

User user = new User();
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
Expand Down Expand Up @@ -129,8 +138,6 @@ public void testIgnoreValidationXml() {
@SpecAssertion(section = "8.1.2", id = "a")
})
public void testPropertyConstraintDefinedInXml() {
Validator validator = TestUtil.getValidatorUnderTest();

User user = new User();
user.setConsistent( true );
user.setFirstname( "Wolfeschlegelsteinhausenbergerdorff" );
Expand All @@ -154,8 +161,6 @@ public void testPropertyConstraintDefinedInXml() {
@SpecAssertion(section = "8.1.2", id = "a")
})
public void testFieldConstraintDefinedInXml() {
Validator validator = TestUtil.getValidatorUnderTest();

User user = new User();
user.setConsistent( true );
user.setFirstname( "Wolfgang" );
Expand All @@ -182,8 +187,6 @@ public void testFieldConstraintDefinedInXml() {
@SpecAssertion(section = "8.1.2", id = "a")
})
public void testAnnotationDefinedConstraintApplies() {
Validator validator = TestUtil.getValidatorUnderTest();

User user = new User();
user.setConsistent( true );
user.setPhoneNumber( "police" );
Expand All @@ -209,8 +212,6 @@ public void testAnnotationDefinedConstraintApplies() {
@SpecAssertion(section = "8.1.2", id = "a")
})
public void testCascadingConfiguredInXml() {
Validator validator = TestUtil.getValidatorUnderTest();

User user = new User();
user.setConsistent( true );
CreditCard card = new CreditCard();
Expand All @@ -229,8 +230,6 @@ public void testCascadingConfiguredInXml() {
@Test
@SpecAssertion(section = "5.5.6", id = "o")
public void testMappingFilesAddedViaConfigurationGetAddedToXmlConfiguredMappings() {
Validator validator = TestUtil.getValidatorUnderTest();

assertFalse(
validator.getConstraintsForClass( Order.class ).isBeanConstrained(),
"Without additional mapping Order should be unconstrained"
Expand All @@ -243,7 +242,7 @@ public void testMappingFilesAddedViaConfigurationGetAddedToXmlConfiguredMappings
"/org/hibernate/beanvalidation/tck/tests/xmlconfiguration/order-constraints-XmlConfigurationTest.xml"
)
);
validator = config.buildValidatorFactory().getValidator();
Validator validator = config.buildValidatorFactory().getValidator();

assertTrue(
validator.getConstraintsForClass( Order.class ).isBeanConstrained(),
Expand All @@ -257,10 +256,20 @@ public void testMappingFilesAddedViaConfigurationGetAddedToXmlConfiguredMappings
@SpecAssertion(section = "8.1.1.6", id = "e"),
@SpecAssertion(section = "8.1.1.6", id = "f"),
@SpecAssertion(section = "8.1.1.6", id = "g"),
@SpecAssertion(section = "8.1.1.6", id = "h")
@SpecAssertion(section = "8.1.1.6", id = "h"),
@SpecAssertion(section = "8.1.3", id = "a"),
@SpecAssertion(section = "8.1.3", id = "b"),
@SpecAssertion(section = "8.1.3", id = "c"),
@SpecAssertion(section = "8.1.3", id = "d"),
@SpecAssertion(section = "8.1.3", id = "e"),
@SpecAssertion(section = "8.1.3", id = "f"),
@SpecAssertion(section = "8.1.3", id = "g"),
@SpecAssertion(section = "8.1.3", id = "h"),
@SpecAssertion(section = "8.1.3", id = "i"),
@SpecAssertion(section = "8.1.3", id = "j"),
@SpecAssertion(section = "8.1.3", id = "k")
})
public void testElementConversionInXmlConfiguredConstraint() {
Validator validator = TestUtil.getValidatorUnderTest();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class );
assertTrue( beanDescriptor.isBeanConstrained() );

Expand All @@ -270,8 +279,19 @@ public void testElementConversionInXmlConfiguredConstraint() {
ConstraintDescriptor<?> descriptor = constraintDescriptors.iterator().next();
ConsistentUserInformation constraintAnnotation = (ConsistentUserInformation) descriptor.getAnnotation();

assertEquals( constraintAnnotation.byteParam(), Byte.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.shortParam(), Short.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.intParam(), Integer.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.longParam(), Long.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.floatParam(), Float.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.doubleParam(), Double.MAX_VALUE, "Wrong parameter value" );
assertEquals( constraintAnnotation.booleanParam(), true, "Wrong parameter value" );
assertEquals( constraintAnnotation.charParam(), 'A', "Wrong parameter value" );

assertEquals( constraintAnnotation.stringParam(), "foobar", "Wrong parameter value" );
assertEquals( constraintAnnotation.classParam(), String.class, "Wrong parameter value" );
assertEquals( constraintAnnotation.unqualifiedClassParam(), UserType.class, "Wrong parameter value" );

assertEquals( constraintAnnotation.userType(), UserType.SELLER, "Wrong parameter value" );

assertEquals( constraintAnnotation.stringArrayParam(), new String[] { "foo", "bar" }, "Wrong parameter value" );
Expand All @@ -280,6 +300,15 @@ public void testElementConversionInXmlConfiguredConstraint() {
assertEquals( constraintAnnotation.patterns().length, 2, "Wrong array size" );
}

@Test(expectedExceptions = ValidationException.class)
@SpecAssertion(section = "8.1.3", id = "l")
public void testIllegalAnnotationValueInXmlMappingCausesException() {
Configuration<?> config = TestUtil.getConfigurationUnderTest();
config.addMapping( getStream( "superuser-constraints.xml" ) );
Validator validator = config.buildValidatorFactory().getValidator();

validator.getConstraintsForClass( SuperUser.class );
}

private InputStream getStream(String fileName) {
return this.getClass().getResourceAsStream( fileName );
Expand Down
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>org.hibernate.beanvalidation.tck.tests.xmlconfiguration</default-package>
<bean class="SuperUser" ignore-annotations="false">
<class ignore-annotations="true">
<constraint annotation="ConsistentUserInformation">
<!-- Larger than Byte.MAX_VALUE -->
<element name="byteParam">128</element>
<element name="patterns"/>
</constraint>
</class>
</bean>
</constraint-mappings>
Expand Up @@ -17,15 +17,23 @@
<payload>
<value>org.hibernate.beanvalidation.tck.tests.xmlconfiguration.Error</value>
</payload>
<element name="byteParam">127</element>
<element name="shortParam">32767</element>
<element name="intParam">
<value>2147483647</value>
</element>
<element name="longParam">9223372036854775807</element>
<element name="floatParam">3.4028235E38</element>
<element name="doubleParam">1.7976931348623157E308</element>
<element name="booleanParam">true</element>
<element name="charParam">A</element>
<element name="stringParam">foobar</element>
<element name="classParam">java.lang.String</element>
<element name="unqualifiedClassParam">UserType</element>
<element name="stringArrayParam">
<value>foo</value>
<value>bar</value>
</element>
<element name="intParam">
<value>42</value>
</element>
<element name="patterns">
<annotation>
<element name="regexp">myRegExp1</element>
Expand Down

0 comments on commit 9157139

Please sign in to comment.