HHH-13672 Add test for issue

This commit is contained in:
Andrea Boriero 2019-10-17 13:52:35 +01:00 committed by Sanne Grinovero
parent d83acfa50f
commit 43402ea51c
2 changed files with 193 additions and 6 deletions

View File

@ -0,0 +1,187 @@
/*
* 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.stateless;
import java.util.function.Consumer;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
public class StatelessSessionPersistentContextTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { TestEntity.class, OtherEntity.class };
}
@Test
@TestForIssue(jiraKey = "HHH-13672")
public void testStatelessSessionPersistenceContextIsCleared() {
TestEntity testEntity = new TestEntity();
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
testEntity.setName( "Fab" );
OtherEntity otherEntity = new OtherEntity();
otherEntity.setName( "other" );
testEntity.setOtherEntity( otherEntity );
statelessSession.insert( otherEntity );
statelessSession.insert( testEntity );
}
);
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
statelessSession.get( TestEntity.class, testEntity.getId() );
}
);
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
TestEntity p2 = (TestEntity) statelessSession.get( TestEntity.class, testEntity.getId() );
p2.setName( "Fabulous" );
statelessSession.update( p2 );
}
);
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
TestEntity testEntity1 = (TestEntity) statelessSession.createQuery(
"select p from TestEntity p where id = :id" )
.setParameter( "id", testEntity.getId() )
.uniqueResult();
testEntity1.getOtherEntity();
}
);
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
statelessSession.refresh( testEntity );
}
);
consumeAndCheckPersistenceContextIsClosed(
statelessSession -> {
statelessSession.delete( testEntity );
}
);
}
private void consumeAndCheckPersistenceContextIsClosed(Consumer<StatelessSession> consumer) {
Transaction transaction = null;
StatelessSession statelessSession = sessionFactory().openStatelessSession();
try {
transaction = statelessSession.beginTransaction();
consumer.accept( statelessSession );
transaction.commit();
}
catch (Exception e) {
if ( transaction != null && transaction.isActive() ) {
transaction.rollback();
}
throw e;
}
finally {
statelessSession.close();
}
assertThatPersistenContextIsCleared( statelessSession );
}
private void assertThatPersistenContextIsCleared(StatelessSession ss) {
PersistenceContext persistenceContextInternal = ( (SharedSessionContractImplementor) ss ).getPersistenceContextInternal();
assertTrue(
"StatelessSession: PersistenceContext has not been cleared",
persistenceContextInternal.getEntitiesByKey().isEmpty()
);
assertTrue(
"StatelessSession: PersistenceContext has not been cleared",
persistenceContextInternal.getCollectionsByKey().isEmpty()
);
}
@Entity(name = "TestEntity")
@Table(name = "TestEntity")
public static class TestEntity {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn
private OtherEntity otherEntity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OtherEntity getOtherEntity() {
return otherEntity;
}
public void setOtherEntity(OtherEntity otherEntity) {
this.otherEntity = otherEntity;
}
}
@Entity(name = "OtherEntity")
@Table(name = "OtherEntity")
public static class OtherEntity {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -50,15 +50,15 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase {
doc.setText("blah blah blah .... blah blay");
ss.update(doc);
tx.commit();
Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs");
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
ScrollableResults sr = ss.createQuery("from Document where text is not null")
.scroll(ScrollMode.FORWARD_ONLY);
sr.next();
@ -66,17 +66,17 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase {
sr.close();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createSQLQuery("select * from Document")
.addEntity(Document.class)
.uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
doc2 = (Document) ss.createCriteria(Document.class).uniqueResult();
assertEquals("Blahs", doc2.getName());
assertEquals(doc.getText(), doc2.getText());
sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY);
sr.next();
doc2 = (Document) sr.get(0);