Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-12097 - EntityManagerFactory open/closed checks per JPA spec
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/ClosedFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ); | ||
} | ||
} | ||
} |