Skip to content

Commit

Permalink
BVTCK-48 Adding more integration tests for executable validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Mar 17, 2013
1 parent 2d3c19b commit 4f328b4
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 1 deletion.
@@ -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.executable;

import javax.inject.Inject;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;

/**
* @author Gunnar Morling
*/
public class AnotherBookingService {

public interface IgnoredValidationGroup {
}

private static int invocationCount = 0;

private static String name;

@Inject
@ValidAnotherBookingService
@Null(groups = IgnoredValidationGroup.class)
public AnotherBookingService(@Size(min = 5) @Null(groups = IgnoredValidationGroup.class) String name) {
this.name = name;
invocationCount++;
}

public static String getName() {
return name;
}

public static int getInvocationCount() {
return invocationCount;
}
}
@@ -0,0 +1,43 @@
/*
* 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.executable;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;

/**
* @author Gunnar Morling
*/
public class BookingService {

public interface IgnoredValidationGroup {
}

private int invocationCount = 0;

@DecimalMin("10001")
@Null(groups = IgnoredValidationGroup.class)
public String placeBooking(@Size(min = 5) @Null(groups = IgnoredValidationGroup.class) String name) {
invocationCount++;
return name;
}

public int getInvocationCount() {
return invocationCount;
}
}
@@ -0,0 +1,30 @@
/*
* 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.executable;

import javax.validation.Valid;

/**
* @author Gunnar Morling
*/
public class CalendarServiceWithCascadingReturnValue {

@Valid
public Event createValidEvent() {
return new Event();
}
}
Expand Up @@ -16,9 +16,17 @@
*/
package org.hibernate.beanvalidation.tck.tests.integration.cdi.executable;

import javax.validation.constraints.NotNull;

/**
* @author Gunnar Morling
*/
public class Event {

@NotNull
private String name;

public String getName() {
return name;
}
}
Expand Up @@ -20,6 +20,7 @@
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.validation.ConstraintViolationException;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
Expand All @@ -36,6 +37,7 @@
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;

import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintTypes;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail;
Expand All @@ -50,6 +52,18 @@ public class ExecutableValidationTest extends Arquillian {
@Inject
private CalendarService calendar;

@Inject
private CalendarServiceWithCascadingReturnValue cascadingCalendar;

@Inject
private BookingService bookingService;

@Inject
private Instance<AnotherBookingService> anotherBookingService;

@Inject
private NameProducer nameProducer;

@Inject
private AnnotatedCalendarService annotatedCalendar;

Expand Down Expand Up @@ -114,6 +128,23 @@ public void testReturnValueValidationOfConstrainedMethod() {
}
}

@Test
@SpecAssertions({
@SpecAssertion(section = "10.1.2", id = "a"),
@SpecAssertion(section = "10.1.2", id = "b"),
@SpecAssertion(section = "10.1.2", id = "c"),
@SpecAssertion(section = "10.3", id = "a")
})
public void testCascadedReturnValueValidationOfConstrainedMethod() {
try {
cascadingCalendar.createValidEvent();
fail( "Method invocation should have caused a ConstraintViolationException" );
}
catch ( ConstraintViolationException e ) {
assertCorrectConstraintTypes( e.getConstraintViolations(), NotNull.class );
}
}

@Test
@SpecAssertions({
@SpecAssertion(section = "10.1.2", id = "a"),
Expand All @@ -135,6 +166,7 @@ public void testGettersAreNotValidatedByDefault() {
})
public void testParameterValidationOfConstrainedConstructor() {
try {
nameProducer.setName( "Bob" );
userServiceInstance.get();
fail( "Constructor invocation should have caused a ConstraintViolationException" );
}
Expand Down Expand Up @@ -225,4 +257,81 @@ public void testValidationOfConstrainedMethodOnInterfaceAnnotatedWithValidateOnE
// success; the constraint is invalid, but no violation exception is
// expected since the executable type is not given in @ValidateOnExecution
}

@Test
@SpecAssertions({
@SpecAssertion(section = "5.4", id = "a"),
@SpecAssertion(section = "5.4", id = "b")
})
public void testMethodValidationInvokesParameterAndReturnValueValidationUsingDefaultGroup() {
//parameter constraint is violated
try {
bookingService.placeBooking( "9999" );
fail( "Method invocation should have caused a ConstraintViolationException" );
}
catch ( ConstraintViolationException e ) {
assertCorrectConstraintTypes( e.getConstraintViolations(), Size.class );
}

//method should not be invoked
assertEquals( bookingService.getInvocationCount(), 0 );

//parameter constraint is valid, but return value constraint is violated
try {
bookingService.placeBooking( "10000" );
fail( "Method invocation should have caused a ConstraintViolationException" );
}
catch ( ConstraintViolationException e ) {
assertCorrectConstraintTypes( e.getConstraintViolations(), DecimalMin.class );
}

//method should have been invoked
assertEquals( bookingService.getInvocationCount(), 1 );

//valid invocation
String booking = bookingService.placeBooking( "10001" );
assertEquals( booking, "10001" );

assertEquals( bookingService.getInvocationCount(), 2 );
}

@Test
@SpecAssertions({
@SpecAssertion(section = "5.4", id = "a"),
@SpecAssertion(section = "5.4", id = "b")
})
public void testConstructorValidationInvokesParameterAndReturnValueValidationUsingDefaultGroup() {
nameProducer.setName( "9999" );
//parameter constraint is violated
try {
anotherBookingService.get();
fail( "Constructor invocation should have caused a ConstraintViolationException" );
}
catch ( ConstraintViolationException e ) {
assertCorrectConstraintTypes( e.getConstraintViolations(), Size.class );
}

//constructor should not be invoked
assertEquals( AnotherBookingService.getInvocationCount(), 0 );

//parameter constraint is valid, but return value constraint is violated
nameProducer.setName( "10000" );
try {
anotherBookingService.get();
fail( "Constructor invocation should have caused a ConstraintViolationException" );
}
catch ( ConstraintViolationException e ) {
assertCorrectConstraintTypes( e.getConstraintViolations(), ValidAnotherBookingService.class );
}

//constructor should have been invoked
assertEquals( AnotherBookingService.getInvocationCount(), 1 );

//valid invocation
nameProducer.setName( "10001" );
AnotherBookingService instance = anotherBookingService.get();
assertNotNull( instance );

assertEquals( AnotherBookingService.getInvocationCount(), 2 );
}
}
Expand Up @@ -16,15 +16,25 @@
*/
package org.hibernate.beanvalidation.tck.tests.integration.cdi.executable;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;

/**
* @author Gunnar Morling
*/
@ApplicationScoped
public class NameProducer {

private String name = "Bob";

@Produces
@Dependent
public String getName() {
return "Bob";
return name;
}

public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,58 @@
/*
* 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.executable;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
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
*/
@Documented
@Constraint(validatedBy = ValidAnotherBookingService.Validator.class)
@Target({ METHOD, CONSTRUCTOR, TYPE, FIELD })
@Retention(RUNTIME)
public @interface ValidAnotherBookingService {
String message() default "default message";

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

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

public static class Validator implements ConstraintValidator<ValidAnotherBookingService, AnotherBookingService> {

@Override
public void initialize(ValidAnotherBookingService parameters) {
}

@Override
public boolean isValid(AnotherBookingService service, ConstraintValidatorContext constraintValidatorContext) {
return Integer.parseInt( AnotherBookingService.getName() ) > 10000;
}
}
}

0 comments on commit 4f328b4

Please sign in to comment.