Skip to content

Commit

Permalink
HV-1921: Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shark300 committed Nov 11, 2024
1 parent d48de85 commit fad87b8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@
<artifactId>assertj-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- test -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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;
private ValidatorFactory defaultValidationFactory;

@BeforeMethod
@SuppressWarnings("unchecked")
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 );
}
}

0 comments on commit fad87b8

Please sign in to comment.