Skip to content

Commit

Permalink
Re-enabled additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Jul 26, 2021
1 parent 6b4f475 commit 3f7044e
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-->

<hibernate-mapping package="org.hibernate.test.optlock">
<hibernate-mapping package="org.hibernate.orm.test.optlock">

<class name="Document" entity-name="LockDirty" table="Document" optimistic-lock="dirty" dynamic-update="true">
<id name="id">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

//$Id: Document.java 7552 2005-07-19 18:51:24Z oneovthafew $
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
* 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.optlock;


import javax.persistence.PersistenceException;

import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SQLServerDialect;

import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests relating to the optimistic-lock mapping option.
*
* @author Gavin King
* @author Steve Ebersole
*/
@RequiresDialectFeature(
feature = DialectFeatureChecks.DoesRepeatableReadNotCauseReadersToBlockWritersCheck.class,
comment = "potential deadlock"
)
@DomainModel(
xmlMappings = "org/hibernate/orm/test/optlock/Document.hbm.xml"
)
@SessionFactory
public class OptimisticLockTest {

@AfterEach
public void tearDown(SessionFactoryScope scope){
scope.inTransaction(
session ->
{
session.createQuery( "delete from LockDirty" ).executeUpdate();
session.createQuery( "delete from LockAll" ).executeUpdate();
}
);

}

@Test
public void testOptimisticLockDirty(SessionFactoryScope scope) {
testUpdateOptimisticLockFailure( "LockDirty", scope );
}

@Test
public void testOptimisticLockAll(SessionFactoryScope scope) {
testUpdateOptimisticLockFailure( "LockAll", scope );
}

@Test
public void testOptimisticLockDirtyDelete(SessionFactoryScope scope) {
testDeleteOptimisticLockFailure( "LockDirty", scope );
}

@Test
public void testOptimisticLockAllDelete(SessionFactoryScope scope) {
testDeleteOptimisticLockFailure( "LockAll", scope );
}

private void testUpdateOptimisticLockFailure(String entityName, SessionFactoryScope scope) {
Document doc = scope.fromTransaction(
session -> {
Document document = new Document();
document.setTitle( "Hibernate in Action" );
document.setAuthor( "Bauer et al" );
document.setSummary( "Very boring book about persistence" );
document.setText( "blah blah yada yada yada" );
document.setPubDate( new PublicationDate( 2004 ) );
session.save( entityName, document );
return document;
}
);

scope.inTransaction(
mainSession -> {
Document document = (Document) mainSession.get( entityName, doc.getId() );

scope.inTransaction(
otherSession -> {
Document otherDoc = (Document) otherSession.get( entityName, document.getId() );
otherDoc.setSummary( "A modern classic" );
}
);

try {
document.setSummary( "A machiavellian achievement of epic proportions" );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch (PersistenceException e) {
// expected
checkException( mainSession, e, scope );
}
mainSession.clear();
}
);

scope.inTransaction(
session -> {
Document document = (Document) session.load( entityName, doc.getId() );
session.delete( entityName, document );
}
);
}

private void testDeleteOptimisticLockFailure(String entityName, SessionFactoryScope scope) {
Document doc = scope.fromTransaction(
session -> {
Document document = new Document();
document.setTitle( "Hibernate in Action" );
document.setAuthor( "Bauer et al" );
document.setSummary( "Very boring book about persistence" );
document.setText( "blah blah yada yada yada" );
document.setPubDate( new PublicationDate( 2004 ) );
session.save( entityName, document );
session.flush();
document.setSummary( "A modern classic" );
session.flush();
document.getPubDate().setMonth( Integer.valueOf( 3 ) );
session.flush();
return document;
}
);

scope.inTransaction(
mainSession -> {
Document document = (Document) mainSession.get( entityName, doc.getId() );

scope.inTransaction(
otherSession -> {
Document otherDoc = (Document) otherSession.get( entityName, document.getId() );
otherDoc.setSummary( "my other summary" );
otherSession.flush();
}
);

try {
mainSession.delete( document );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch (StaleObjectStateException e) {
// expected
}
catch (PersistenceException e) {
// expected
checkException( mainSession, e, scope );
}
mainSession.clear();
}
);

scope.inTransaction(
session -> {
Document document = (Document) session.load( entityName, doc.getId() );
session.delete( entityName, document );
}
);
}

private void checkException(Session mainSession, PersistenceException e, SessionFactoryScope scope) {
final Throwable cause = e.getCause();
if ( cause instanceof JDBCException ) {
Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect();
if ( dialect instanceof SQLServerDialect && ( (JDBCException) cause ).getErrorCode() == 3960 ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation.
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
else if ( dialect instanceof CockroachDialect && ( (JDBCException) cause ).getSQLState().equals(
"40001" ) ) {
// CockroachDB always runs in SERIALIZABLE isolation, and uses SQL state 40001 to indicate
// serialization failure.
}
else {
throw e;
}
}
else if ( !( cause instanceof StaleObjectStateException ) && !( cause instanceof StaleStateException ) ) {
fail( "expected StaleObjectStateException or StaleStateException exception but is " + cause );
}
}

}

Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.LockModeType;
import javax.persistence.Version;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class OptimisticLockWithGloballyQuotedIdentifierTest extends BaseCoreFunctionalTestCase {

@Override
protected void configure(Configuration configuration) {
configuration.setProperty( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true" );
}
@DomainModel(
annotatedClasses = {
OptimisticLockWithGloballyQuotedIdentifierTest.Person.class
}
)
@SessionFactory
@ServiceRegistry(
settings = @Setting( name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true")
)
public class OptimisticLockWithGloballyQuotedIdentifierTest {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class };
}

@Before
public void setUp() {
inTransaction(
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person person = new Person( "1", "Fabiana" );
session.persist( person );
}
);
}

@After
public void tearDown() {
inTransaction(
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}

@Test
public void testHqlQueryWithOptimisticLock() {
inTransaction(
public void testHqlQueryWithOptimisticLock(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "from Person e", Person.class )
.setLockMode( LockModeType.OPTIMISTIC )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.LockModeType;
import javax.persistence.Version;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class OptimisticLockWithQuotedVersionTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = OptimisticLockWithQuotedVersionTest.Person.class
)
@SessionFactory
public class OptimisticLockWithQuotedVersionTest {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class };
}

@Before
public void setUp() {
inTransaction(
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person person = new Person( "1", "Fabiana" );
session.persist( person );
}
);
}

@After
public void tearDown() {
inTransaction(
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}

@Test
public void testHqlQueryWithOptimisticLock() {
inTransaction(
public void testHqlQueryWithOptimisticLock(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "from Person e", Person.class )
.setLockMode( LockModeType.OPTIMISTIC )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

//$Id: PublicationDate.java 7556 2005-07-19 23:22:27Z oneovthafew $
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;


public class PublicationDate {
Expand Down
Loading

0 comments on commit 3f7044e

Please sign in to comment.