-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1686 from arjantijms/cdi_entitymanagerfactory_test
Add test for EntityManagerFactory injection using CDI
- Loading branch information
Showing
4 changed files
with
135 additions
and
42 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
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
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
93 changes: 72 additions & 21 deletions
93
jpa/platform-tests/src/main/java/ee/jakarta/tck/persistence/ee/cdi/TestBeanEM.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 |
---|---|---|
@@ -1,70 +1,121 @@ | ||
package ee.jakarta.tck.persistence.ee.cdi; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.EntityManagerFactory; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import static java.util.logging.Level.SEVERE; | ||
|
||
/** | ||
* A request scoped bean that uses the CTS-EM and CTS-EM-NOTX persistence units | ||
* A request scoped bean that uses the CTS-EM and CTS-EM2 persistence units | ||
*/ | ||
@RequestScoped | ||
public class TestBeanEM { | ||
Logger logger = Logger.getLogger(TestBeanEM.class.getName()); | ||
|
||
//@PersistenceContext(unitName = "CTS-EM") | ||
@Inject | ||
@CtsEmQualifier | ||
private EntityManager entityManager1; | ||
|
||
@Inject | ||
@CtsEm2Qualifier | ||
private EntityManager entityManager2; | ||
|
||
@Inject | ||
private EntityManager em1; | ||
//@PersistenceContext(unitName = "CTS-EM2") | ||
@CtsEmQualifier | ||
private EntityManagerFactory entityManagerFactory1; | ||
|
||
@Inject | ||
private EntityManager em2; | ||
@CtsEm2Qualifier | ||
private EntityManagerFactory entityManagerFactory2; | ||
|
||
public void logMsg(String msg) { | ||
logger.info(msg); | ||
} | ||
|
||
public void logTrace(String msg) { | ||
logger.fine(msg); | ||
} | ||
|
||
public void logErr(String msg) { | ||
logger.severe(msg); | ||
} | ||
|
||
public void logErr(String msg, Throwable e) { | ||
logger.log(java.util.logging.Level.SEVERE, msg, e); | ||
logger.log(SEVERE, msg, e); | ||
} | ||
|
||
public void injectEmUsingQualifier() throws Exception { | ||
public void injectEntityManagerUsingQualifier() throws Exception { | ||
boolean pass1 = false; | ||
boolean pass2 = false; | ||
|
||
try { | ||
logMsg( "Test CtsEmNoTxQualifier"); | ||
if (em1 != null) { | ||
logTrace( "Received non-null EntityManager"); | ||
logMsg("Test @CtsEmQualifier"); | ||
if (entityManager1 != null) { | ||
logTrace("Received non-null EntityManager"); | ||
pass1 = true; | ||
} else { | ||
logErr( "Received null EntityManager"); | ||
logErr("Received null EntityManager"); | ||
} | ||
} catch (Exception e) { | ||
logErr( "Received unexpected exception", e); | ||
logErr("Received unexpected exception", e); | ||
} | ||
|
||
try { | ||
logMsg( "Test CtsEmNoTxQualifier"); | ||
if (em2 != null) { | ||
logTrace( "Received non-null EntityManager"); | ||
logMsg("Test @CtsEm2Qualifier"); | ||
if (entityManager2 != null) { | ||
logTrace("Received non-null EntityManager"); | ||
pass2 = true; | ||
} else { | ||
logErr( "Received null EntityManager"); | ||
logErr("Received null EntityManager"); | ||
} | ||
} catch (Exception e) { | ||
logErr( "Received unexpected exception", e); | ||
logErr("Received unexpected exception", e); | ||
} | ||
|
||
if (!pass1 || !pass2) { | ||
throw new Exception("createEntityManagerSynchronizationTypeMapTest failed"); | ||
throw new Exception("injectEntityManagerUsingQualifier failed"); | ||
} | ||
logMsg( "Test PASSED"); | ||
|
||
logMsg("Test PASSED"); | ||
} | ||
|
||
public void injectEntityManagerFactoryUsingQualifier() throws Exception { | ||
boolean pass1 = false; | ||
boolean pass2 = false; | ||
|
||
try { | ||
logMsg("Test @CtsEmQualifier"); | ||
if (entityManagerFactory1 != null) { | ||
logTrace("Received non-null EntityManagerFactory1"); | ||
pass1 = true; | ||
} else { | ||
logErr("Received null EntityManagerFactory1"); | ||
} | ||
} catch (Exception e) { | ||
logErr("Received unexpected exception", e); | ||
} | ||
|
||
try { | ||
logMsg("Test @CtsEm2Qualifier"); | ||
if (entityManagerFactory2 != null) { | ||
logTrace("Received non-null EntityManagerFactory2"); | ||
pass2 = true; | ||
} else { | ||
logErr("Received null EntityManagerFactory2"); | ||
} | ||
} catch (Exception e) { | ||
logErr("Received unexpected exception", e); | ||
} | ||
|
||
if (!pass1 || !pass2) { | ||
throw new Exception("injectEntityManagerFactoryUsingQualifier failed"); | ||
} | ||
|
||
logMsg("Test PASSED"); | ||
} | ||
|
||
} |