Skip to content

Commit

Permalink
Added AutoCloseable tests for EntityManagerFactory and EntityManager.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Oct 7, 2021
1 parent 8bf4f2d commit 4eba244
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/com/sun/ts/tests/jpa/core/entityManager/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.LockModeType;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.Persistence;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.Query;
import jakarta.persistence.StoredProcedureQuery;
Expand Down Expand Up @@ -276,6 +277,43 @@ public boolean verifyListEmployees(List<Employee> expected,
return result;
}

/*
* @testName: autoCloseableTest
*
* @assertion_ids: PERSISTENCE:JAVADOC:N/A
*
* @test_Strategy: Create EntityManager in try with resources block
* and verify whether it's open inside and outside of the try block.
*/
public void autoCloseableTest() throws Fault {
boolean pass = true;
EntityManager em = null;
try (final EntityManagerFactory emfLocal
= Persistence.createEntityManagerFactory(getPersistenceUnitName(), getPersistenceUnitProperties())) {
try (final EntityManager emLocal = emfLocal.createEntityManager()) {
em = emLocal;
if (em == null) {
TestUtil.logErr("createEntityManager() returned a null result");
pass = false;
}
if (em.isOpen() == false) {
TestUtil.logErr("EntityManager isOpen() returned false in try block");
pass = false;
}
} finally {
if (em.isOpen() == true) {
TestUtil.logErr("EntityManager isOpen() returned true outside try block");
pass = false;
}
}
} catch (Throwable t) {
throw new Fault("autoCloseableTest failed with Exception", t);
}
if (!pass) {
throw new Fault("autoCloseableTest failed");
}
}

/*
* @testName: mergeTest
*
Expand Down
41 changes: 40 additions & 1 deletion src/com/sun/ts/tests/jpa/core/entityManagerFactory/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import com.sun.ts.lib.util.TestUtil;
import com.sun.ts.tests.jpa.common.PMClientBase;

import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Persistence;
import jakarta.persistence.PersistenceUnitUtil;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
Expand Down Expand Up @@ -96,12 +98,49 @@ public void cleanup() throws Fault {
super.cleanup();
}

/*
* @testName: autoCloseableTest
*
* @assertion_ids: PERSISTENCE:JAVADOC:N/A;
*
* @test_Strategy: Create EntityManagerFactory in try with resources block
* and verify whether it's open inside and outside of the try block.
*/
@SetupMethod(name = "setupNoData")
@CleanupMethod(name = "cleanupNoData")
public void autoCloseableTest() throws Fault {
boolean pass = true;
EntityManagerFactory emf = null;
try (final EntityManagerFactory emfLocal
= Persistence.createEntityManagerFactory(getPersistenceUnitName())) {
emf = emfLocal;
if (emf == null) {
TestUtil.logErr("createEntityManagerFactory(String) returned a null result");
pass = false;
}
if (emf.isOpen() == false) {
TestUtil.logErr("EntityManagerFactory isOpen() returned false in try block");
pass = false;
}
} catch (Throwable t) {
throw new Fault("autoCloseableTest failed with Exception", t);
} finally {
if (emf.isOpen() == true) {
TestUtil.logErr("EntityManagerFactory isOpen() returned true outside try block");
pass = false;
}
}
if (!pass) {
throw new Fault("autoCloseableTest failed");
}
}

/*
* @testName: getMetamodelTest
*
* @assertion_ids: PERSISTENCE:JAVADOC:340;
*
* @test_Strategy: Get a MetaModel Object from the EntityManagerFactory an
* @test_Strategy: Get a MetaModel Object from the EntityManagerFactory and
* make sure it is not null
*/
@SetupMethod(name = "setupNoData")
Expand Down

0 comments on commit 4eba244

Please sign in to comment.