Skip to content

Commit

Permalink
BVTCK-32 Adding test for checking that runtime type is used for path …
Browse files Browse the repository at this point in the history
…node creation
  • Loading branch information
gunnarmorling committed Jan 17, 2013
1 parent a8d1099 commit 9095e23
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
Expand Up @@ -16,8 +16,6 @@
*/
package org.hibernate.beanvalidation.tck.tests.validation;

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

/**
Expand All @@ -27,18 +25,15 @@ public class ActorArrayBased extends Actor {
public static final int MAX_ACTOR_SIZE = 100;

@Valid
private Actor[] playedWith = new Actor[MAX_ACTOR_SIZE];
private final Actor[] playedWith = new Actor[MAX_ACTOR_SIZE];

int currentPointer = 0;

public ActorArrayBased(String firstName, String lastName) {
super( firstName, lastName );
}

public List<Actor> getPlayedWith() {
return Arrays.asList( playedWith );
}

@Override
public void addPlayedWith(Actor playedWith) {
if ( currentPointer == MAX_ACTOR_SIZE ) {
throw new RuntimeException( "Exceeded allowed number of actors." );
Expand Down
@@ -0,0 +1,44 @@
/*
* 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.validation;

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

/**
* @author Gunnar Morling
*/
public class ActorCollectionBased extends Actor {

@Valid
private final Collection<Actor> playedWith = new ArrayList<Actor>();

public ActorCollectionBased(String firstName, String lastName) {
super( firstName, lastName );
}

@Override
public void addPlayedWith(Actor playedWith) {
this.playedWith.add( playedWith );
}

@Override
public String toString() {
return super.toString();
}
}
Expand Up @@ -26,16 +26,13 @@
public class ActorListBased extends Actor {

@Valid
private List<Actor> playedWith = new ArrayList<Actor>();
private final List<Actor> playedWith = new ArrayList<Actor>();

public ActorListBased(String firstName, String lastName) {
super( firstName, lastName );
}

public List<Actor> getPlayedWith() {
return playedWith;
}

@Override
public void addPlayedWith(Actor playedWith) {
this.playedWith.add( playedWith );
}
Expand Down
Expand Up @@ -16,13 +16,9 @@
*/
package org.hibernate.beanvalidation.tck.tests.validation;

import java.util.List;

/**
* @author Hardy Ferentschik
*/
public interface PlayedWith {
List<Actor> getPlayedWith();

void addPlayedWith(Actor playedWith);
}
Expand Up @@ -47,10 +47,11 @@
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectNumberOfViolations;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

/**
* Tests for the implementation of <code>Validator</code>.
* Tests for property paths retrieved via {@link ConstraintViolation#getPropertyPath()}.
*
* @author Hardy Ferentschik
*/
Expand All @@ -64,6 +65,7 @@ public static WebArchive createTestArchive() {
.withClasses(
Actor.class,
ActorArrayBased.class,
ActorCollectionBased.class,
ActorListBased.class,
PlayedWith.class,
Person.class,
Expand All @@ -85,13 +87,16 @@ public void testPropertyPathWithConstraintViolationForRootObject() {
Validator validator = TestUtil.getValidatorUnderTest();
Set<ConstraintViolation<VerySpecialClass>> constraintViolations = validator.validate( new VerySpecialClass() );
assertCorrectNumberOfViolations( constraintViolations, 1 );
ConstraintViolation<VerySpecialClass> constraintViolation = constraintViolations.iterator().next();
ConstraintViolation<VerySpecialClass> constraintViolation = constraintViolations.iterator()
.next();

Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator();
assertTrue( nodeIter.hasNext() );
Path.Node node = nodeIter.next();
assertEquals( node.getName(), null );
assertFalse( node.isInIterable() );
assertNull( node.getIndex() );
assertNull( node.getKey() );
assertFalse( nodeIter.hasNext() );
}

Expand All @@ -116,6 +121,8 @@ public void testPropertyPathTraversedObject() {
Path.Node node = nodeIter.next();
assertEquals( node.getName(), "serialNumber" );
assertFalse( node.isInIterable() );
assertNull( node.getIndex() );
assertNull( node.getKey() );
assertFalse( nodeIter.hasNext() );
}

Expand Down Expand Up @@ -163,6 +170,29 @@ public void testPropertyPathWithArray() {
checkActorViolations( constraintViolations );
}

@Test
@SpecAssertions({
@SpecAssertion(section = "5.2", id = "g"),
@SpecAssertion(section = "5.2", id = "h"),
@SpecAssertion(section = "5.2", id = "k"),
@SpecAssertion(section = "5.2", id = "m")
})
public void testPropertyPathWithRuntimeTypeList() {
Validator validator = TestUtil.getValidatorUnderTest();

Actor clint = new ActorCollectionBased( "Clint", "Eastwood" );
Actor morgan = new ActorCollectionBased( "Morgan", null );
Actor charlie = new ActorCollectionBased( "Charlie", "Sheen" );

clint.addPlayedWith( charlie );
charlie.addPlayedWith( clint );
charlie.addPlayedWith( morgan );
morgan.addPlayedWith( charlie );

Set<ConstraintViolation<Actor>> constraintViolations = validator.validate( clint );
checkActorViolations( constraintViolations );
}

@Test
@SpecAssertions({
@SpecAssertion(section = "5.2", id = "g"),
Expand All @@ -185,10 +215,13 @@ public void testPropertyPathWithMap() {
Path.Node node = nodeIter.next();
assertEquals( node.getName(), "actors" );
assertFalse( node.isInIterable() );
assertNull( node.getKey() );
assertNull( node.getIndex() );

node = nodeIter.next();
assertEquals( node.getName(), "lastName" );
assertTrue( node.isInIterable() );
assertEquals( node.getIndex(), null );
assertEquals( node.getKey(), id );

assertFalse( nodeIter.hasNext() );
Expand Down Expand Up @@ -217,10 +250,14 @@ public void testPropertyPathSet() {
Path.Node node = nodeIter.next();
assertEquals( node.getName(), "orders" );
assertFalse( node.isInIterable() );
assertNull( node.getIndex() );
assertNull( node.getKey() );

node = nodeIter.next();
assertEquals( node.getName(), "orderNumber" );
assertTrue( node.isInIterable() );
assertNull( node.getIndex() );
assertNull( node.getKey() );

assertFalse( nodeIter.hasNext() );
}
Expand All @@ -235,16 +272,20 @@ private void checkActorViolations(Set<ConstraintViolation<Actor>> constraintViol
Path.Node node = nodeIter.next();
assertEquals( node.getName(), "playedWith" );
assertFalse( node.isInIterable() );
assertNull( node.getIndex() );
assertNull( node.getKey() );

node = nodeIter.next();
assertEquals( node.getName(), "playedWith" );
assertTrue( node.isInIterable() );
assertEquals( node.getIndex(), new Integer( 0 ) );
assertNull( node.getKey() );

node = nodeIter.next();
assertEquals( node.getName(), "lastName" );
assertTrue( node.isInIterable() );
assertEquals( node.getIndex(), new Integer( 1 ) );
assertNull( node.getKey() );

assertFalse( nodeIter.hasNext() );
}
Expand Down

0 comments on commit 9095e23

Please sign in to comment.