Yine de bu orijinal soruyu cevaplamayacak. Bariz takibe cevap verecektir. Bir dersten önce ve sonra ve bir testten önce ve sonra çalışan bir kural nasıl oluşturulur?
Bunu başarmak için şu kalıbı kullanabilirsiniz:
@ClassRule
public static JPAConnection jpaConnection = JPAConnection.forUITest("my-persistence-unit");
@Rule
public JPAConnection.EntityManager entityManager = jpaConnection.getEntityManager();
(Sınıf) öncesinde, JPAConnection bağlantıyı (Sınıf) kapattıktan sonra bir kez oluşturur.
getEntityManger
JPAConnection
jpa'nın EntityManager'ını uygulayan bir iç sınıfı döndürür ve jpaConnection
. (Test) öncesinde bir işleme başlar (test) sonrasında tekrar geri alır.
Bu iş parçacığı için güvenli değildir, ancak öyle yapılabilir.
Seçilen kod JPAConnection.class
package com.triodos.general.junit;
import com.triodos.log.Logger;
import org.jetbrains.annotations.NotNull;
import org.junit.rules.ExternalResource;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.Metamodel;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkState;
import static com.triodos.dbconn.DB2DriverManager.DRIVERNAME_TYPE4;
import static com.triodos.dbconn.UnitTestProperties.getDatabaseConnectionProperties;
import static com.triodos.dbconn.UnitTestProperties.getPassword;
import static com.triodos.dbconn.UnitTestProperties.getUsername;
import static java.lang.String.valueOf;
import static java.sql.Connection.TRANSACTION_READ_UNCOMMITTED;
public final class JPAConnectionExample extends ExternalResource {
private static final Logger LOG = Logger.getLogger(JPAConnectionExample.class);
@NotNull
public static JPAConnectionExample forUITest(String persistenceUnitName) {
return new JPAConnectionExample(persistenceUnitName)
.setManualEntityManager();
}
private final String persistenceUnitName;
private EntityManagerFactory entityManagerFactory;
private javax.persistence.EntityManager jpaEntityManager = null;
private EntityManager entityManager;
private JPAConnectionExample(String persistenceUnitName) {
this.persistenceUnitName = persistenceUnitName;
}
@NotNull
private JPAConnectionExample setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
return this;
}
@NotNull
private JPAConnectionExample setManualEntityManager() {
return setEntityManager(new RollBackAfterTestEntityManager());
}
@Override
protected void before() {
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, createEntityManagerProperties());
jpaEntityManager = entityManagerFactory.createEntityManager();
}
@Override
protected void after() {
if (jpaEntityManager.getTransaction().isActive()) {
jpaEntityManager.getTransaction().rollback();
}
if(jpaEntityManager.isOpen()) {
jpaEntityManager.close();
}
// Free for garbage collection as an instance
// of EntityManager may be assigned to a static variable
jpaEntityManager = null;
entityManagerFactory.close();
// Free for garbage collection as an instance
// of JPAConnection may be assigned to a static variable
entityManagerFactory = null;
}
private Map<String,String> createEntityManagerProperties(){
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.url", getDatabaseConnectionProperties().getURL());
properties.put("javax.persistence.jtaDataSource", null);
properties.put("hibernate.connection.isolation", valueOf(TRANSACTION_READ_UNCOMMITTED));
properties.put("hibernate.connection.username", getUsername());
properties.put("hibernate.connection.password", getPassword());
properties.put("hibernate.connection.driver_class", DRIVERNAME_TYPE4);
properties.put("org.hibernate.readOnly", valueOf(true));
return properties;
}
@NotNull
public EntityManager getEntityManager(){
checkState(entityManager != null);
return entityManager;
}
private final class RollBackAfterTestEntityManager extends EntityManager {
@Override
protected void before() throws Throwable {
super.before();
jpaEntityManager.getTransaction().begin();
}
@Override
protected void after() {
super.after();
if (jpaEntityManager.getTransaction().isActive()) {
jpaEntityManager.getTransaction().rollback();
}
}
}
public abstract class EntityManager extends ExternalResource implements javax.persistence.EntityManager {
@Override
protected void before() throws Throwable {
checkState(jpaEntityManager != null, "JPAConnection was not initialized. Is it a @ClassRule? Did the test runner invoke the rule?");
// Safety-close, if failed to close in setup
if (jpaEntityManager.getTransaction().isActive()) {
jpaEntityManager.getTransaction().rollback();
LOG.error("EntityManager encountered an open transaction at the start of a test. Transaction has been closed but should have been closed in the setup method");
}
}
@Override
protected void after() {
checkState(jpaEntityManager != null, "JPAConnection was not initialized. Is it a @ClassRule? Did the test runner invoke the rule?");
}
@Override
public final void persist(Object entity) {
jpaEntityManager.persist(entity);
}
@Override
public final <T> T merge(T entity) {
return jpaEntityManager.merge(entity);
}
@Override
public final void remove(Object entity) {
jpaEntityManager.remove(entity);
}
@Override
public final <T> T find(Class<T> entityClass, Object primaryKey) {
return jpaEntityManager.find(entityClass, primaryKey);
}
@Override
public final <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) {
return jpaEntityManager.find(entityClass, primaryKey, properties);
}
@Override
public final <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
return jpaEntityManager.find(entityClass, primaryKey, lockMode);
}
@Override
public final <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
return jpaEntityManager.find(entityClass, primaryKey, lockMode, properties);
}
@Override
public final <T> T getReference(Class<T> entityClass, Object primaryKey) {
return jpaEntityManager.getReference(entityClass, primaryKey);
}
@Override
public final void flush() {
jpaEntityManager.flush();
}
@Override
public final void setFlushMode(FlushModeType flushMode) {
jpaEntityManager.setFlushMode(flushMode);
}
@Override
public final FlushModeType getFlushMode() {
return jpaEntityManager.getFlushMode();
}
@Override
public final void lock(Object entity, LockModeType lockMode) {
jpaEntityManager.lock(entity, lockMode);
}
@Override
public final void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
jpaEntityManager.lock(entity, lockMode, properties);
}
@Override
public final void refresh(Object entity) {
jpaEntityManager.refresh(entity);
}
@Override
public final void refresh(Object entity, Map<String, Object> properties) {
jpaEntityManager.refresh(entity, properties);
}
@Override
public final void refresh(Object entity, LockModeType lockMode) {
jpaEntityManager.refresh(entity, lockMode);
}
@Override
public final void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties) {
jpaEntityManager.refresh(entity, lockMode, properties);
}
@Override
public final void clear() {
jpaEntityManager.clear();
}
@Override
public final void detach(Object entity) {
jpaEntityManager.detach(entity);
}
@Override
public final boolean contains(Object entity) {
return jpaEntityManager.contains(entity);
}
@Override
public final LockModeType getLockMode(Object entity) {
return jpaEntityManager.getLockMode(entity);
}
@Override
public final void setProperty(String propertyName, Object value) {
jpaEntityManager.setProperty(propertyName, value);
}
@Override
public final Map<String, Object> getProperties() {
return jpaEntityManager.getProperties();
}
@Override
public final Query createQuery(String qlString) {
return jpaEntityManager.createQuery(qlString);
}
@Override
public final <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
return jpaEntityManager.createQuery(criteriaQuery);
}
@Override
public final <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
return jpaEntityManager.createQuery(qlString, resultClass);
}
@Override
public final Query createNamedQuery(String name) {
return jpaEntityManager.createNamedQuery(name);
}
@Override
public final <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
return jpaEntityManager.createNamedQuery(name, resultClass);
}
@Override
public final Query createNativeQuery(String sqlString) {
return jpaEntityManager.createNativeQuery(sqlString);
}
@Override
public final Query createNativeQuery(String sqlString, Class resultClass) {
return jpaEntityManager.createNativeQuery(sqlString, resultClass);
}
@Override
public final Query createNativeQuery(String sqlString, String resultSetMapping) {
return jpaEntityManager.createNativeQuery(sqlString, resultSetMapping);
}
@Override
public final void joinTransaction() {
jpaEntityManager.joinTransaction();
}
@Override
public final <T> T unwrap(Class<T> cls) {
return jpaEntityManager.unwrap(cls);
}
@Override
public final Object getDelegate() {
return jpaEntityManager.getDelegate();
}
@Override
public final void close() {
jpaEntityManager.close();
}
@Override
public final boolean isOpen() {
return jpaEntityManager.isOpen();
}
@Override
public final EntityTransaction getTransaction() {
return jpaEntityManager.getTransaction();
}
@Override
public final EntityManagerFactory getEntityManagerFactory() {
return jpaEntityManager.getEntityManagerFactory();
}
@Override
public final CriteriaBuilder getCriteriaBuilder() {
return jpaEntityManager.getCriteriaBuilder();
}
@Override
public final Metamodel getMetamodel() {
return jpaEntityManager.getMetamodel();
}
}
}