diff --git a/test-utils/pom.xml b/test-utils/pom.xml index 40bbacd52..4623fcdbe 100644 --- a/test-utils/pom.xml +++ b/test-utils/pom.xml @@ -60,5 +60,17 @@ assertj-core provided + + + + org.testng + testng + test + + + org.easymock + easymock + test + diff --git a/test-utils/src/test/java/org/hibernate/validator/testutil/PreconfiguredConstraintValidatorFactoryTest.java b/test-utils/src/test/java/org/hibernate/validator/testutil/PreconfiguredConstraintValidatorFactoryTest.java new file mode 100644 index 000000000..9e9a65e34 --- /dev/null +++ b/test-utils/src/test/java/org/hibernate/validator/testutil/PreconfiguredConstraintValidatorFactoryTest.java @@ -0,0 +1,75 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.validator.testutil; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.easymock.EasyMock.*; + +import java.util.Map; + +import jakarta.validation.ConstraintValidatorFactory; +import jakarta.validation.ValidatorFactory; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class PreconfiguredConstraintValidatorFactoryTest { + + private ConstraintValidatorFactory delegatedConstraintValidatorFactory; + + @BeforeMethod + public void setUp() { + delegatedConstraintValidatorFactory = createMock( ConstraintValidatorFactory.class ); + } + + @Test + public void testGetInstanceWithPreconfiguredValidator() { + CountValidationCallsValidator constraintValidator = new CountValidationCallsValidator(); + + PreconfiguredConstraintValidatorFactory constraintValidatorFactory = PreconfiguredConstraintValidatorFactory.builder() + .delegated( delegatedConstraintValidatorFactory ) + .defaultValidators( Map.of( CountValidationCallsValidator.class, constraintValidator ) ) + .build(); + + assertThat( constraintValidatorFactory.getInstance( CountValidationCallsValidator.class ) ) + .isEqualTo( constraintValidator ); + } + + @Test + public void testGetInstanceWithDefaultValidator() { + CountValidationCallsValidator constraintValidator = new CountValidationCallsValidator(); + + expect( delegatedConstraintValidatorFactory.getInstance( CountValidationCallsValidator.class ) ).andReturn( constraintValidator ); + + PreconfiguredConstraintValidatorFactory constraintValidatorFactory = PreconfiguredConstraintValidatorFactory.builder() + .delegated( delegatedConstraintValidatorFactory ) + .build(); + + replay( delegatedConstraintValidatorFactory ); + + assertThat( constraintValidatorFactory.getInstance( CountValidationCallsValidator.class ) ) + .isEqualTo( constraintValidator ); + + verify( delegatedConstraintValidatorFactory ); + } + + @Test + public void testReleaseInstance() { + CountValidationCallsValidator constraintValidator = new CountValidationCallsValidator(); + + delegatedConstraintValidatorFactory.releaseInstance( constraintValidator ); + + PreconfiguredConstraintValidatorFactory constraintValidatorFactory = PreconfiguredConstraintValidatorFactory.builder() + .delegated( delegatedConstraintValidatorFactory ) + .build(); + + replay( delegatedConstraintValidatorFactory ); + + constraintValidatorFactory.releaseInstance( constraintValidator ); + + verify( delegatedConstraintValidatorFactory ); + } +}