Skip to content

Commit

Permalink
HV-836 Adding a dedicated XML configured cascaded validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Dec 2, 2013
1 parent a3084fe commit f29d8e7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
@@ -0,0 +1,37 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, 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.xml;

import javax.validation.constraints.NotNull;

/**
* @author Hardy Ferentschik
*/
public class Part {
@NotNull
String partId;

public String getPartId() {
return partId;
}

public void setPartId(String partId) {
this.partId = partId;
}
}


@@ -0,0 +1,53 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, 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.xml;

import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;

/**
* @author Hardy Ferentschik
*/
public class System {
private String name;

private List<Part> parts;

public System() {
parts = new ArrayList<Part>();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Valid
public List<Part> getParts() {
return parts;
}

public void addPart(Part part) {
parts.add( part );
}
}


Expand Up @@ -27,20 +27,21 @@
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import javax.validation.executable.ExecutableType;
import javax.validation.groups.Default;
import javax.validation.metadata.MethodDescriptor;

import org.testng.annotations.Test;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.defs.SizeDef;
import org.hibernate.validator.testutil.TestForIssue;
import org.hibernate.validator.testutil.ValidatorUtil;
import org.testng.annotations.Test;

import static org.hibernate.validator.internal.util.CollectionHelper.asSet;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectConstraintTypes;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectConstraintViolationMessages;
import static org.testng.Assert.assertEquals;

Expand Down Expand Up @@ -309,6 +310,23 @@ public void run() {
);
}

@Test
@TestForIssue(jiraKey = "HV-836")
public void testCascadedValidation() {
final Configuration<?> configuration = ValidatorUtil.getConfiguration();
configuration.addMapping( XmlMappingTest.class.getResourceAsStream( "cascaded-validation-mapping.xml" ) );

final ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
final Validator validator = validatorFactory.getValidator();

System system = new System();
system.addPart( new Part() );
Set<ConstraintViolation<System>> violations = validator.validate( system );

assertEquals( violations.size(), 1 );
assertCorrectConstraintTypes( violations, NotNull.class );
}

/**
* Executes the given runnable, using the specified file as replacement for
* {@code META-INF/validation.xml}.
Expand Down
@@ -0,0 +1,16 @@
<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.0.xsd">

<default-package>org.hibernate.validator.internal.xml</default-package>

<bean class="org.hibernate.validator.test.internal.xml.System" ignore-annotations="true">
<getter name="parts">
<valid/>
</getter>
</bean>

<bean class="org.hibernate.validator.test.internal.xml.Part" ignore-annotations="false"/>
</constraint-mappings>

0 comments on commit f29d8e7

Please sign in to comment.