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.RequiresDialect;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import javax.persistence.RollbackException;
@ -31,14 +32,21 @@ import static org.junit.jupiter.api.Assertions.fail;
)
public class BeanValidationTest {
@AfterEach
public void tearDown(EntityManagerFactoryScope scope){
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from CupHolder" )
);
}
@Test
public void testBeanValidationIntegrationOnFlush(EntityManagerFactoryScope scope) {
scope.inEntityManager(
scope.inTransaction(
entityManager -> {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
ch.setTitle( "foo" );
entityManager.getTransaction().begin();
try {
entityManager.persist(ch);
entityManager.flush();
@ -51,41 +59,37 @@ public class BeanValidationTest {
entityManager.getTransaction().getRollbackOnly(),
"A constraint violation exception should mark the transaction for rollback"
);
entityManager.getTransaction().rollback();
entityManager.clear();
}
);
}
@Test
public void testBeanValidationIntegrationOnCommit(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("9"));
ch.setTitle("foo");
entityManager.getTransaction().begin();
entityManager.persist(ch);
entityManager.flush();
try {
ch.setRadius(new BigDecimal("12"));
entityManager.getTransaction().commit();
fail("invalid object should not be persisted");
try {
scope.inTransaction(
entityManager -> {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "9" ) );
ch.setTitle( "foo" );
entityManager.persist( ch );
entityManager.flush();
ch.setRadius( new BigDecimal( "12" ) );
}
catch (RollbackException e) {
final Throwable cve = e.getCause();
assertTrue(cve instanceof ConstraintViolationException);
assertEquals(1, ((ConstraintViolationException) cve).getConstraintViolations().size());
}
entityManager.close();
}
);
);
fail( "invalid object should not be persisted" );
}
catch (RollbackException e) {
final Throwable cve = e.getCause();
assertTrue( cve instanceof ConstraintViolationException );
assertEquals( 1, ( (ConstraintViolationException) cve ).getConstraintViolations().size() );
}
}
@Test
@RequiresDialect(H2Dialect.class)
public void testTitleColumnHasExpectedLength(EntityManagerFactoryScope scope) {
scope.inEntityManager(
scope.inTransaction(
entityManager -> {
int len = (Integer) entityManager.createNativeQuery(
"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.boot.spi.Bootstrap;
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.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -31,15 +33,17 @@ import static org.junit.jupiter.api.Assertions.assertSame;
*
* @author Steve Ebersole
*/
public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
@BaseUnitTest
public class ValidatorFactory2PhaseInjectionTest {
private ValidatorFactory vf;
@Before
@BeforeEach
public void before() {
vf = Validation.byDefaultProvider().configure().buildValidatorFactory();
}
@After
@AfterEach
public void after() {
if ( vf != null ) {
vf.close();
@ -58,9 +62,9 @@ public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase {
},
Collections.emptyMap()
);
emfb.withValidatorFactory( vf );
emfb.withValidatorFactory( vf );
EntityManagerFactory emf = emfb.build();
EntityManagerFactory emf = emfb.build();
try {
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.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
@ -31,14 +32,16 @@ import org.junit.Test;
*/
public class BootFailureTest {
@Test(expected = ServiceException.class)
@Test
public void exceptionOnIllegalPUTest() {
bootstrapPersistenceUnit( "IntentionallyBroken" );
Assertions.assertThrows( ServiceException.class, () ->
bootstrapPersistenceUnit( "IntentionallyBroken" ) );
}
@Test(expected = ServiceException.class)
@Test
public void exceptionOnIllegalPUWithoutProviderTest() {
bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" );
Assertions.assertThrows( ServiceException.class, () ->
bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" ) );
}
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 {
}