Skip to content

Commit

Permalink
HV-373 test for group conversions configured in xml
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Feb 18, 2013
1 parent 1076981 commit 54a8cc7
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public HibernateValidatorConfiguration parameterNameProvider(ParameterNameProvid
}

public final HibernateValidatorConfiguration addMapping(InputStream stream) {
Contracts.assertNotNull( stream, MESSAGES.parameterMustNotBeNull( "stream" ) );
Contracts.assertNotNull( stream, MESSAGES.inputStreamCannotBeNull() );

validationBootstrapParameters.addMapping( stream.markSupported() ? stream : new BufferedInputStream( stream ) );
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public interface Messages {

@Message(value = "The created instance must not be null.", format = Message.Format.NO_FORMAT)
String validatedConstructorCreatedInstanceMustNotBeNull();

@Message(value = "The input stream for #addMappging() cannot be null.")
String inputStreamCannotBeNull();
}


Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javax.validation.groups.ConvertGroup;
import javax.validation.groups.Default;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.hibernate.validator.internal.util.CollectionHelper.asSet;
Expand All @@ -41,14 +40,11 @@
*
* @author Gunnar Morling
*/
public class GroupConversionTest {
public abstract class AbstractGroupConversionTest {

private Validator validator;
protected Validator validator;

@BeforeMethod
public void setupValidator() {
validator = getValidator();
}
public abstract void setupValidator();

@Test
public void groupConversionOnField() {
Expand All @@ -64,7 +60,6 @@ public void groupConversionOnGetter() {

@Test
public void groupConversionOnParameter() throws Exception {

Set<ConstraintViolation<User6>> violations = getValidator().forExecutables().validateParameters(
new User6(),
User6.class.getMethod( "setAddresses", List.class ),
Expand All @@ -76,7 +71,6 @@ public void groupConversionOnParameter() throws Exception {

@Test
public void groupConversionOnReturnValue() throws Exception {

Set<ConstraintViolation<User7>> violations = getValidator().forExecutables().validateReturnValue(
new User7(),
User7.class.getMethod( "findAddresses" ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, 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.validator.test.internal.engine.groups.conversion;

import org.testng.annotations.BeforeMethod;

import static org.hibernate.validator.testutil.ValidatorUtil.getValidator;

/**
* Integrative test for annotation based group conversion.
*
* @author Hardy Ferentschik
*/
public class AnnotationBasedGroupConversionTest extends AbstractGroupConversionTest {
@BeforeMethod
public void setupValidator() {
validator = getValidator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, 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.validator.test.internal.engine.groups.conversion;

import javax.validation.Configuration;
import javax.validation.ValidatorFactory;

import org.testng.annotations.BeforeMethod;

import org.hibernate.validator.testutil.ValidatorUtil;

/**
* Integrative test for XML configured group conversion.
*
* @author Hardy Ferentschik
*/
public class XmlBasedGroupConversionTest extends AbstractGroupConversionTest {
@BeforeMethod
public void setupValidator() {
Configuration<?> configuration = ValidatorUtil.getConfiguration();
configuration.addMapping(
XmlBasedGroupConversionTest.class.getResourceAsStream(
"group-conversion-mapping.xml"
)
);
ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
validator = validatorFactory.getValidator();
}


public void conversionFromSequenceCausesException() {
Configuration<?> configuration = ValidatorUtil.getConfiguration();
configuration.addMapping(
XmlBasedGroupConversionTest.class.getResourceAsStream(
"invalid-group-conversion-mapping.xml"
)
);
ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
validator = validatorFactory.getValidator();

super.conversionFromSequenceCausesException();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<constraint-mappings
xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd" version="1.1">

<default-package>org.hibernate.validator.test.internal.engine.groups.conversion</default-package>

<bean class="AbstractGroupConversionTest$User1" ignore-annotations="true">
<field name="addresses">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
</field>
</bean>

<bean class="AbstractGroupConversionTest$User2" ignore-annotations="true">
<getter name="addresses">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
</getter>
</bean>

<bean class="AbstractGroupConversionTest$User3" ignore-annotations="true">
<getter name="addresses">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
<convert-group from="AbstractGroupConversionTest$Complete" to="AbstractGroupConversionTest$FullPostal"/>
</getter>
</bean>

<bean class="AbstractGroupConversionTest$User4" ignore-annotations="true">
<getter name="addresses">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
<convert-group from="AbstractGroupConversionTest$Complete" to="AbstractGroupConversionTest$FullPostal"/>
</getter>
</bean>

<bean class="AbstractGroupConversionTest$User5" ignore-annotations="true">
<getter name="addresses">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
</getter>
<getter name="phoneNumber">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicNumber"/>
</getter>
</bean>

<bean class="AbstractGroupConversionTest$User6" ignore-annotations="true">
<method name="setAddresses">
<parameter type="java.util.List">
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
</parameter>
</method>
</bean>

<bean class="AbstractGroupConversionTest$User7" ignore-annotations="true">
<method name="findAddresses">
<return-value>
<valid/>
<convert-group from="javax.validation.groups.Default" to="AbstractGroupConversionTest$BasicPostal"/>
</return-value>
</method>
</bean>

<bean class="AbstractGroupConversionTest$Address" ignore-annotations="true">
<field name="street1">
<constraint annotation="javax.validation.constraints.NotNull">
<groups>
<value>AbstractGroupConversionTest$BasicPostal</value>
</groups>
</constraint>
</field>
<field name="street2">
<constraint annotation="javax.validation.constraints.NotNull">
</constraint>
</field>
<field name="zipCode">
<constraint annotation="javax.validation.constraints.Size">
<groups>
<value>AbstractGroupConversionTest$BasicPostal</value>
</groups>
<element name="min">3</element>
</constraint>
</field>
<field name="doorCode">
<constraint annotation="javax.validation.constraints.Size">
<groups>
<value>AbstractGroupConversionTest$FullPostal</value>
</groups>
<element name="max">2</element>
</constraint>
</field>
</bean>
<bean class="AbstractGroupConversionTest$PhoneNumber" ignore-annotations="true">
<field name="number">
<constraint annotation="javax.validation.constraints.NotNull">
<groups>
<value>AbstractGroupConversionTest$BasicNumber</value>
</groups>
</constraint>
</field>
</bean>

</constraint-mappings>

0 comments on commit 54a8cc7

Please sign in to comment.