Skip to content

Commit

Permalink
HHH-12097 - EntityManagerFactory open/closed checks per JPA spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Dec 13, 2017
1 parent 25ba7bd commit 4095962
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
Expand Up @@ -627,6 +627,7 @@ public CriteriaBuilder getCriteriaBuilder() {

@Override
public MetamodelImplementor getMetamodel() {
validateNotClosed();
return metamodel;
}

Expand Down Expand Up @@ -734,6 +735,10 @@ public Type getReferencedPropertyType(String className, String propertyName)
*/
public void close() throws HibernateException {
if ( isClosed ) {
if ( sessionFactoryOptions.isJpaBootstrap() ) {
throw new IllegalStateException( "EntityManagerFactory is closed" );
}

LOG.trace( "Already closed" );
return;
}
Expand Down
@@ -0,0 +1,128 @@
/*
* 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.jpa.compliance;

import java.util.Collections;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.spi.SessionFactoryBuilderImplementor;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "12097")
public class ClosedFactoryTests extends BaseUnitTestCase {
@Test
public void testClosedChecks() {
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources().buildMetadata()
.getSessionFactoryBuilder();
factoryBuilder.markAsJpaBootstrap();
final SessionFactory sf = factoryBuilder.build();

sf.close();

assertTrue( sf.isClosed() );

// we now have a closed SF (EMF)... test the closed checks in various methods

try {
sf.getCache();
fail( "#getCache did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#getCache failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.getMetamodel();
fail( "#getMetamodel did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#getMetamodel failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.getCriteriaBuilder();
fail( "#getCriteriaBuilder did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#getCriteriaBuilder failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.getProperties();
fail( "#getProperties did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#getProperties failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.getPersistenceUnitUtil();
fail( "#getPersistenceUnitUtil did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#getPersistenceUnitUtil failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.close();
fail( "#close did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#close failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.createEntityManager();
fail( "#createEntityManager did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#createEntityManager failed, but not with the expected IllegalStateException : " + e.toString() );
}

try {
sf.createEntityManager( Collections.emptyMap() );
fail( "#createEntityManager(Map) did not fail" );
}
catch (IllegalStateException expected) {
// this is the expected outcome
}
catch (Exception e) {
fail( "#createEntityManager(Map) failed, but not with the expected IllegalStateException : " + e.toString() );
}
}
}

0 comments on commit 4095962

Please sign in to comment.