Re-enabled additional tests
This commit is contained in:
parent
8dbac8d44c
commit
e07e6d4105
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.tm;
|
package org.hibernate.orm.test.tm;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
|
@ -4,9 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.tm;
|
package org.hibernate.orm.test.tm;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
|
@ -19,36 +17,38 @@ import org.hibernate.HibernateException;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
|
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
|
||||||
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
|
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.engine.spi.ActionQueue;
|
import org.hibernate.engine.spi.ActionQueue;
|
||||||
import org.hibernate.engine.spi.SessionImplementor;
|
import org.hibernate.engine.spi.SessionImplementor;
|
||||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
||||||
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
|
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
|
||||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
|
||||||
|
|
||||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.hibernate.testing.orm.junit.ExtraAssertions.assertTyping;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Chris Cranford
|
* @author Chris Cranford
|
||||||
*/
|
*/
|
||||||
public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase {
|
public class JtaAfterCompletionTest extends BaseSessionFactoryFunctionalTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
return new Class<?>[] { SimpleEntity.class };
|
return new Class<?>[] { SimpleEntity.class };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addSettings(Map settings) {
|
protected void applySettings(StandardServiceRegistryBuilder builder) {
|
||||||
super.addSettings( settings );
|
TestingJtaBootstrap.prepare( builder.getSettings() );
|
||||||
TestingJtaBootstrap.prepare( settings );
|
builder.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
|
||||||
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
|
builder.applySetting( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" );
|
||||||
settings.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -63,7 +63,7 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase
|
||||||
|
|
||||||
Session session = null;
|
Session session = null;
|
||||||
try {
|
try {
|
||||||
session = openSession();
|
session = sessionFactory().openSession();
|
||||||
|
|
||||||
SimpleEntity entity = new SimpleEntity( "Hello World" );
|
SimpleEntity entity = new SimpleEntity( "Hello World" );
|
||||||
session.save( entity );
|
session.save( entity );
|
||||||
|
@ -104,9 +104,10 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase
|
||||||
try {
|
try {
|
||||||
// Wait for the transaction to be rolled back by the Reaper thread.
|
// Wait for the transaction to be rolled back by the Reaper thread.
|
||||||
final Transaction transaction = TestingJtaPlatformImpl.transactionManager().getTransaction();
|
final Transaction transaction = TestingJtaPlatformImpl.transactionManager().getTransaction();
|
||||||
while ( transaction.getStatus() != Status.STATUS_ROLLEDBACK )
|
while ( transaction.getStatus() != Status.STATUS_ROLLEDBACK ) {
|
||||||
Thread.sleep( 10 );
|
Thread.sleep( 10 );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
// we aren't concerned with this.
|
// we aren't concerned with this.
|
||||||
}
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||||
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
|
*/
|
||||||
|
package org.hibernate.orm.test.tm;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.transaction.RollbackException;
|
||||||
|
import javax.transaction.Status;
|
||||||
|
import javax.transaction.TransactionManager;
|
||||||
|
|
||||||
|
import org.hibernate.JDBCException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
import org.hibernate.orm.test.resource.transaction.jta.JtaPlatformStandardTestingImpl;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
||||||
|
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
|
||||||
|
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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Steve Ebersole
|
||||||
|
*/
|
||||||
|
public class JtaBeforeCompletionFailureTest extends BaseSessionFactoryFunctionalTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class[] getAnnotatedClasses() {
|
||||||
|
return new Class[] { SimpleEntity.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void applySettings(StandardServiceRegistryBuilder builder) {
|
||||||
|
TestingJtaBootstrap.prepare( builder.getSettings() );
|
||||||
|
builder.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
inTransaction(
|
||||||
|
session ->
|
||||||
|
session.save( newEntity( 1 ) )
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown() {
|
||||||
|
inTransaction(
|
||||||
|
session ->
|
||||||
|
session.createQuery( "delete SimpleEntity" ).executeUpdate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-9888")
|
||||||
|
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
|
||||||
|
final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
|
||||||
|
assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() );
|
||||||
|
|
||||||
|
// begin the transaction ("CMT" style)
|
||||||
|
tm.begin();
|
||||||
|
|
||||||
|
try (Session session = sessionFactory().openSession()) {
|
||||||
|
|
||||||
|
session.save( newEntity( 2 ) );
|
||||||
|
|
||||||
|
// complete the transaction ("CMT" style) - this leads to the managed flush
|
||||||
|
// which should lead to the UK violation
|
||||||
|
try {
|
||||||
|
tm.commit();
|
||||||
|
fail( "Expecting a failure from JTA commit" );
|
||||||
|
}
|
||||||
|
catch (RollbackException expected) {
|
||||||
|
boolean violationExceptionFound = false;
|
||||||
|
Throwable cause = expected;
|
||||||
|
while ( cause != null ) {
|
||||||
|
if ( cause instanceof JDBCException ) {
|
||||||
|
violationExceptionFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cause = cause.getCause();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !violationExceptionFound ) {
|
||||||
|
fail( "Did not find JDBCException in JTA RollbackException chain" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SimpleEntity newEntity(int id) {
|
||||||
|
// since "key" is reused, should violate the UK
|
||||||
|
return new SimpleEntity( id, "key", "name" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "SimpleEntity")
|
||||||
|
public static class SimpleEntity {
|
||||||
|
@Id
|
||||||
|
public Integer id;
|
||||||
|
@Column(unique = true, name = "entity_key")
|
||||||
|
public String key;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public SimpleEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleEntity(Integer id, String key, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.key = key;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,135 +0,0 @@
|
||||||
/*
|
|
||||||
* Hibernate, Relational Persistence for Idiomatic Java
|
|
||||||
*
|
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
|
||||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
|
||||||
*/
|
|
||||||
package org.hibernate.test.tm;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.transaction.RollbackException;
|
|
||||||
import javax.transaction.Status;
|
|
||||||
import javax.transaction.TransactionManager;
|
|
||||||
|
|
||||||
import org.hibernate.JDBCException;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
|
||||||
import org.hibernate.engine.spi.SessionImplementor;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
|
||||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
|
||||||
import org.hibernate.orm.test.resource.transaction.jta.JtaPlatformStandardTestingImpl;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Steve Ebersole
|
|
||||||
*/
|
|
||||||
public class JtaBeforeCompletionFailureTest extends BaseNonConfigCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Class[] getAnnotatedClasses() {
|
|
||||||
return new Class[] { SimpleEntity.class };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void addSettings(Map settings) {
|
|
||||||
super.addSettings( settings );
|
|
||||||
TestingJtaBootstrap.prepare( settings );
|
|
||||||
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-9888" )
|
|
||||||
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
// set up test data
|
|
||||||
|
|
||||||
Session session = openSession();
|
|
||||||
session.getTransaction().begin();
|
|
||||||
session.save( newEntity( 1 ) );
|
|
||||||
session.getTransaction().commit();
|
|
||||||
session.close();
|
|
||||||
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
// do the test
|
|
||||||
|
|
||||||
final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
|
|
||||||
assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() );
|
|
||||||
|
|
||||||
// begin the transaction ("CMT" style)
|
|
||||||
tm.begin();
|
|
||||||
|
|
||||||
session = openSession();
|
|
||||||
|
|
||||||
session.save( newEntity( 2 ) );
|
|
||||||
|
|
||||||
// complete the transaction ("CMT" style) - this leads to the managed flush
|
|
||||||
// which should lead to the UK violation
|
|
||||||
try {
|
|
||||||
tm.commit();
|
|
||||||
fail( "Expecting a failure from JTA commit" );
|
|
||||||
}
|
|
||||||
catch (RollbackException expected) {
|
|
||||||
log.info( "Test encountered expected JTA RollbackException; looking for nested JDBCException", expected );
|
|
||||||
boolean violationExceptionFound = false;
|
|
||||||
Throwable cause = expected;
|
|
||||||
while ( cause != null ) {
|
|
||||||
if ( cause instanceof JDBCException ) {
|
|
||||||
log.info( "Found JDBCException, assuming related to UK violation", cause );
|
|
||||||
violationExceptionFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
cause = cause.getCause();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !violationExceptionFound ) {
|
|
||||||
fail( "Did not find JDBCException in JTA RollbackException chain" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( !( (SessionImplementor) session ).isClosed() ) {
|
|
||||||
session.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
// clean up test data
|
|
||||||
|
|
||||||
session = openSession();
|
|
||||||
session.getTransaction().begin();
|
|
||||||
session.createQuery( "delete SimpleEntity" ).executeUpdate();
|
|
||||||
session.getTransaction().commit();
|
|
||||||
session.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private SimpleEntity newEntity(int id) {
|
|
||||||
// since "key" is reused, should violate the UK
|
|
||||||
return new SimpleEntity( id, "key", "name" );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Entity(name = "SimpleEntity")
|
|
||||||
public static class SimpleEntity {
|
|
||||||
@Id
|
|
||||||
public Integer id;
|
|
||||||
@Column(unique = true, name = "entity_key")
|
|
||||||
public String key;
|
|
||||||
public String name;
|
|
||||||
|
|
||||||
public SimpleEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public SimpleEntity(Integer id, String key, String name) {
|
|
||||||
this.id = id;
|
|
||||||
this.key = key;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue