package org.drools.persistence.map.impl;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.base.MapGlobalResolver;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;

public class JpaBasedPersistenceTest extends MapPersistenceTest {

    private static PoolingDataSource ds1;
    
    @BeforeClass
    public static void setUp() throws Exception {
        ds1 = new PoolingDataSource();
        ds1.setUniqueName( "jdbc/testDS1" );
        ds1.setClassName( "org.h2.jdbcx.JdbcDataSource" );
        ds1.setMaxPoolSize( 3 );
        ds1.setAllowLocalTransactions( true );
        ds1.getDriverProperties().put( "user",
                                       "sa" );
        ds1.getDriverProperties().put( "password",
                                       "sasa" );
        ds1.getDriverProperties().put( "URL",
                                       "jdbc:h2:mem:mydb" );
        ds1.init();

    }
    
    @AfterClass
    public static void tearDown() throws Exception {
        ds1.close();
    }
    
    @Override
    protected StatefulKnowledgeSession createSession(KnowledgeBase kbase) {
        return JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, createEnvironment() );
    }

    @Override
    protected StatefulKnowledgeSession disposeAndReloadSession(StatefulKnowledgeSession ksession,
                                                               KnowledgeBase kbase) {
        long ksessionId = ksession.getId();
        ksession.dispose();
        return JPAKnowledgeService.loadStatefulKnowledgeSession( ksessionId, kbase, null, createEnvironment() );
    }

    @Override
    protected long getSavedSessionsCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    private Environment createEnvironment(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory( "org.drools.persistence.jpa" );
        Environment env = KnowledgeBaseFactory.newEnvironment();
        env.set( EnvironmentName.ENTITY_MANAGER_FACTORY,
                 emf );
        env.set( EnvironmentName.TRANSACTION_MANAGER,
                 TransactionManagerServices.getTransactionManager() );
        env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
        
        return env;
    }
}

@@ -35,16 +35,56 @@
 .impl;%0A%0A
+import javax.persistence.EntityManager;%0A
 import j
@@ -465,16 +465,40 @@
 ession;%0A
+import org.junit.After;%0A
 import o
@@ -518,16 +518,41 @@
 rClass;%0A
+import org.junit.Before;%0A
 import o
@@ -753,22 +753,16 @@
 private 
-static
  Pooling
@@ -777,16 +777,54 @@
 ce ds1;%0A
+    private EntityManagerFactory emf;%0A
     %0A   
@@ -831,21 +831,16 @@
  @Before
-Class
 %0A    pub
@@ -839,31 +839,24 @@
 %0A    public 
-static 
 void setUp()
@@ -1419,16 +1419,101 @@
 init();%0A
+        emf = Persistence.createEntityManagerFactory( %22org.drools.persistence.jpa%22 );
 %0A    %7D%0A 
@@ -1526,21 +1526,16 @@
   @After
-Class
 %0A    pub
@@ -1542,15 +1542,8 @@
 lic 
-static 
 void
@@ -1565,32 +1565,53 @@
 ows Exception %7B%0A
+        emf.close();%0A
         ds1.clos
@@ -2284,219 +2284,189 @@
     
-// TODO Auto-generated method stub%0A        return 0
+System.out.println(%22quering%22)
 ;%0A    
-%7D%0A%0A
     
-private Environment createEnvironment()%7B%0A        EntityManagerFactory emf = Persistence.cre
+return emf.createEntityManager().createQuery( %22FROM SessionInfo%22 ).getResultList().size();%0A    %7D%0A%0A    priv
 ate
+ 
 En
-tityManagerFactory( %22org.drools.persistence.jpa%22 );
+vironment createEnvironment()%7B
 %0A   

fix jpa based test for setting up new environment on each run