/*
* Copyright 2011 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.persistence.map.impl;
import static org.drools.persistence.util.PersistenceUtil.*;
import static org.drools.runtime.EnvironmentName.ENTITY_MANAGER_FACTORY;
import java.util.HashMap;
import javax.persistence.EntityManagerFactory;
import org.drools.KnowledgeBase;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy;
import org.drools.persistence.jta.JtaTransactionManager;
import org.drools.persistence.util.PersistenceUtil;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JpaBasedPersistenceTest extends MapPersistenceTest {
private static Logger logger = LoggerFactory.getLogger(JPAPlaceholderResolverStrategy.class);
private HashMap<String, Object> context;
private EntityManagerFactory emf;
private JtaTransactionManager txm;
private boolean useTransactions = false;
@Before
public void setUp() throws Exception {
context = PersistenceUtil.setupWithPoolingDataSource(DROOLS_PERSISTENCE_UNIT_NAME);
emf = (EntityManagerFactory) context.get(ENTITY_MANAGER_FACTORY);
if( useTransactions() ) {
useTransactions = true;
Environment env = createEnvironment(context);
Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );
this.txm = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ),
env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),
tm );
}
}
@After
public void tearDown() throws Exception {
PersistenceUtil.tearDown(context);
}
@Override
protected StatefulKnowledgeSession createSession(KnowledgeBase kbase) {
return JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, createEnvironment(context) );
}
@Override
protected StatefulKnowledgeSession disposeAndReloadSession(StatefulKnowledgeSession ksession,
KnowledgeBase kbase) {
int ksessionId = ksession.getId();
ksession.dispose();
return JPAKnowledgeService.loadStatefulKnowledgeSession( ksessionId, kbase, null, createEnvironment(context) );
}
@Override
protected long getSavedSessionsCount() {
logger.info("quering number of saved sessions.");
boolean transactionOwner = false;
if( useTransactions ) {
transactionOwner = txm.begin();
}
long savedSessionsCount = emf.createEntityManager().createQuery( "FROM SessionInfo" ).getResultList().size();
if( useTransactions ) {
txm.commit(transactionOwner);
}
return savedSessionsCount;
}
}