Add @BaseUnitTest annotation and minor changes

This commit is contained in:
Andrea Boriero 2021-01-22 13:46:45 +01:00 committed by Jan Schatteman
parent f9937f66be
commit a5d60b3b7e
4 changed files with 70 additions and 39 deletions

View File

@ -12,6 +12,7 @@ import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.persistence.RollbackException; import javax.persistence.RollbackException;
@ -31,14 +32,21 @@ import static org.junit.jupiter.api.Assertions.fail;
) )
public class BeanValidationTest { public class BeanValidationTest {
@AfterEach
public void tearDown(EntityManagerFactoryScope scope){
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from CupHolder" )
);
}
@Test @Test
public void testBeanValidationIntegrationOnFlush(EntityManagerFactoryScope scope) { public void testBeanValidationIntegrationOnFlush(EntityManagerFactoryScope scope) {
scope.inEntityManager( scope.inTransaction(
entityManager -> { entityManager -> {
CupHolder ch = new CupHolder(); CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) ); ch.setRadius( new BigDecimal( "12" ) );
ch.setTitle( "foo" ); ch.setTitle( "foo" );
entityManager.getTransaction().begin();
try { try {
entityManager.persist(ch); entityManager.persist(ch);
entityManager.flush(); entityManager.flush();
@ -51,41 +59,37 @@ public class BeanValidationTest {
entityManager.getTransaction().getRollbackOnly(), entityManager.getTransaction().getRollbackOnly(),
"A constraint violation exception should mark the transaction for rollback" "A constraint violation exception should mark the transaction for rollback"
); );
entityManager.getTransaction().rollback();
entityManager.clear();
} }
); );
} }
@Test @Test
public void testBeanValidationIntegrationOnCommit(EntityManagerFactoryScope scope) { public void testBeanValidationIntegrationOnCommit(EntityManagerFactoryScope scope) {
scope.inEntityManager( try {
entityManager -> { scope.inTransaction(
CupHolder ch = new CupHolder(); entityManager -> {
ch.setRadius(new BigDecimal("9")); CupHolder ch = new CupHolder();
ch.setTitle("foo"); ch.setRadius( new BigDecimal( "9" ) );
entityManager.getTransaction().begin(); ch.setTitle( "foo" );
entityManager.persist(ch); entityManager.persist( ch );
entityManager.flush(); entityManager.flush();
try {
ch.setRadius(new BigDecimal("12")); ch.setRadius( new BigDecimal( "12" ) );
entityManager.getTransaction().commit();
fail("invalid object should not be persisted");
} }
catch (RollbackException e) { );
final Throwable cve = e.getCause(); fail( "invalid object should not be persisted" );
assertTrue(cve instanceof ConstraintViolationException); }
assertEquals(1, ((ConstraintViolationException) cve).getConstraintViolations().size()); catch (RollbackException e) {
} final Throwable cve = e.getCause();
entityManager.close(); assertTrue( cve instanceof ConstraintViolationException );
} assertEquals( 1, ( (ConstraintViolationException) cve ).getConstraintViolations().size() );
); }
} }
@Test @Test
@RequiresDialect(H2Dialect.class) @RequiresDialect(H2Dialect.class)
public void testTitleColumnHasExpectedLength(EntityManagerFactoryScope scope) { public void testTitleColumnHasExpectedLength(EntityManagerFactoryScope scope) {
scope.inEntityManager( scope.inTransaction(
entityManager -> { entityManager -> {
int len = (Integer) entityManager.createNativeQuery( int len = (Integer) entityManager.createNativeQuery(
"select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = 'CUPHOLDER' and c.COLUMN_NAME = 'TITLE'" "select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = 'CUPHOLDER' and c.COLUMN_NAME = 'TITLE'"

View File

@ -17,12 +17,14 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.HibernatePersistenceProvider; import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.hibernate.test.jpa.xml.versions.JpaXsdVersionsTest; import org.hibernate.test.jpa.xml.versions.JpaXsdVersionsTest;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
@ -31,15 +33,17 @@ import static org.junit.jupiter.api.Assertions.assertSame;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
@BaseUnitTest
public class ValidatorFactory2PhaseInjectionTest {
private ValidatorFactory vf; private ValidatorFactory vf;
@Before @BeforeEach
public void before() { public void before() {
vf = Validation.byDefaultProvider().configure().buildValidatorFactory(); vf = Validation.byDefaultProvider().configure().buildValidatorFactory();
} }
@After @AfterEach
public void after() { public void after() {
if ( vf != null ) { if ( vf != null ) {
vf.close(); vf.close();
@ -58,9 +62,9 @@ public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
}, },
Collections.emptyMap() Collections.emptyMap()
); );
emfb.withValidatorFactory( vf ); emfb.withValidatorFactory( vf );
EntityManagerFactory emf = emfb.build(); EntityManagerFactory emf = emfb.build();
try { try {
assertSame( vf, emf.getProperties().get( AvailableSettings.JPA_VALIDATION_FACTORY ) ); assertSame( vf, emf.getProperties().get( AvailableSettings.JPA_VALIDATION_FACTORY ) );
} }

View File

@ -20,7 +20,8 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.service.spi.ServiceException; import org.hibernate.service.spi.ServiceException;
import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl; import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
import org.junit.Test; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/** /**
* Test to verify that a dump configuration error results in an exception being * Test to verify that a dump configuration error results in an exception being
@ -31,14 +32,16 @@ import org.junit.Test;
*/ */
public class BootFailureTest { public class BootFailureTest {
@Test(expected = ServiceException.class) @Test
public void exceptionOnIllegalPUTest() { public void exceptionOnIllegalPUTest() {
bootstrapPersistenceUnit( "IntentionallyBroken" ); Assertions.assertThrows( ServiceException.class, () ->
bootstrapPersistenceUnit( "IntentionallyBroken" ) );
} }
@Test(expected = ServiceException.class) @Test
public void exceptionOnIllegalPUWithoutProviderTest() { public void exceptionOnIllegalPUWithoutProviderTest() {
bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" ); Assertions.assertThrows( ServiceException.class, () ->
bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" ) );
} }
private void bootstrapPersistenceUnit(final String puName) { private void bootstrapPersistenceUnit(final String puName) {

View File

@ -0,0 +1,20 @@
package org.hibernate.testing.orm.junit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
@Inherited
@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
@ExtendWith( FailureExpectedExtension.class )
@ExtendWith( ExpectedExceptionExtension.class )
@ExtendWith( DialectFilterExtension.class )
public @interface BaseUnitTest {
}