All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.beanvalidation.tck.tests.util.ConstraintViolationAssertTest Maven / Gradle / Ivy

The newest version!
/**
 * Jakarta Bean Validation TCK
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.beanvalidation.tck.tests.util;

import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.assertThat;
import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.pathWith;
import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.violationOf;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.beanvalidation.tck.tests.AbstractTCKTest;
import org.hibernate.beanvalidation.tck.util.TestUtil;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import org.testng.annotations.Test;

/**
 * Tests for {@link org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.ConstraintViolationSetAssert}.
 *
 * @author Marko Bekhta
 * @author Guillaume Smet
 */
public class ConstraintViolationAssertTest extends AbstractTCKTest {

	@Deployment
	public static WebArchive createTestArchive() {
		return webArchiveBuilder()
				.withTestClass( ConstraintViolationAssertTest.class )
				.withClass( Foo.class )
				.build();
	}

	@Test
	public void testConstraintTypeCorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testConstraintTypeIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( Min.class )
		);
	}

	@Test
	public void testMessageCorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withMessage( "message" )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testMessageIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withMessage( "wrong message" )
		);
	}

	@Test
	public void testRootBeanClassCorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withRootBeanClass( Foo.class )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testRootBeanClassIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withRootBeanClass( ConstraintViolationAssertTest.class )
		);
	}

	@Test
	public void testInvalidValueCorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withInvalidValue( null )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testInvalidValueIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withInvalidValue( "not null" )
		);
	}

	@Test
	public void testPropertyCorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withProperty( "string" )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testPropertyIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withProperty( "wrongPropertyName" )
		);
	}

	@Test
	public void testPropertyPathCorrect() throws Exception {
		Set> violations = TestUtil.getValidatorUnderTest().forExecutables()
				.validateReturnValue( new Foo( null ), Foo.class.getDeclaredMethod( "bar" ), null );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class )
						.withPropertyPath( pathWith()
								.method( "bar" )
								.returnValue()
						)
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testPropertyPathIncorrect() throws Exception {
		Set> violations = TestUtil.getValidatorUnderTest().forExecutables()
				.validateReturnValue( new Foo( null ), Foo.class.getDeclaredMethod( "bar" ), null );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class )
						.withPropertyPath( pathWith()
								.method( "bar" )
								.property( "nonExistingProperty" )
								.returnValue()
						)
		);
	}

	@Test
	public void testLeafBeanCorrect() {
		Foo foo = new Foo( null );
		Set> violations = TestUtil.getValidatorUnderTest().validate( foo );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withLeafBean( foo )
		);
	}

	@Test(expectedExceptions = AssertionError.class)
	public void testLeafBeanIncorrect() {
		Set> violations = TestUtil.getValidatorUnderTest().validate( new Foo( null ) );
		assertThat( violations ).containsOnlyViolations(
				violationOf( NotNull.class ).withLeafBean( "not the leaf bean" )
		);
	}

	private static class Foo {

		@NotNull(message = "message")
		private final String string;

		public Foo(String string) {
			this.string = string;
		}

		@NotNull
		public String bar() {
			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy