Index: src/test/java/org/test/QueryTest.java =================================================================== --- src/test/java/org/test/QueryTest.java (revision 0) +++ src/test/java/org/test/QueryTest.java (revision 1007) @@ -0,0 +1,130 @@ +package org.test; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.Query; + +import junit.framework.TestCase; + +import org.drools.KnowledgeBase; +import org.drools.KnowledgeBaseFactory; +import org.drools.builder.KnowledgeBuilder; +import org.drools.builder.KnowledgeBuilderError; +import org.drools.builder.KnowledgeBuilderErrors; +import org.drools.builder.KnowledgeBuilderFactory; +import org.drools.builder.ResourceType; +import org.drools.io.ResourceFactory; +import org.drools.persistence.jpa.JPAKnowledgeService; +import org.drools.persistence.processinstance.VariablePersistenceStrategyFactory; +import org.drools.runtime.Environment; +import org.drools.runtime.EnvironmentName; +import org.drools.runtime.StatefulKnowledgeSession; +import org.drools.runtime.process.WorkflowProcessInstance; + +import bitronix.tm.resource.jdbc.PoolingDataSource; + +/** + * This is a sample file to launch a process. + */ +public class QueryTest extends TestCase { + + static EntityManagerFactory emf; + + static KnowledgeBase kbase; + + static Environment env; + + StatefulKnowledgeSession ksession; + + static { + + VariablePersistenceStrategyFactory.getVariablePersistenceStrategy() + .setPersister("java.lang.String", + "org.plugtree.labs.variablepersistence.StringVariablePersister"); + + // define the datasource + initH2(); + + // create the entity manager factory and register it in the environment + emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa"); + env = KnowledgeBaseFactory.newEnvironment(); + env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); + + + // load up the knowledge base + kbase = readKnowledgeBase(); +} + + @Override + protected void setUp() throws Exception { + ksession = JPAKnowledgeService + .newStatefulKnowledgeSession(kbase, null, env); + } + + @Override + protected void tearDown() throws Exception { + if (ksession != null) { + ksession.dispose(); + } + } + + public void testQuery() throws Exception { + + // start two processes + startProcess(ksession, "red"); + startProcess(ksession, "blue"); + + // count processes and variables + assertEquals(new Long(2), getCount("ProcessInstanceCount")); + assertEquals(new Long(4), getCount("VariableInstanceCount")); + assertEquals(new Long(1), getCount("BlueProcessInstanceCount")); + } + + private static WorkflowProcessInstance startProcess( + StatefulKnowledgeSession ksession, String color) { + Map vars = new HashMap(); + vars.put("color", color); + vars.put("size", "large"); + WorkflowProcessInstance p1 = (WorkflowProcessInstance) ksession + .startProcess("org.plugtree.labs.droolsflow", vars); + return p1; + } + + private static Long getCount(String query) { + Query namedQuery = emf.createEntityManager().createNamedQuery(query); + Long count = (Long) namedQuery.getSingleResult(); + return count; + } + + public static void initH2() { + PoolingDataSource ds = new PoolingDataSource(); + ds.setUniqueName("jdbc/testDS1"); + ds.setClassName("org.h2.jdbcx.JdbcDataSource"); + ds.setMaxPoolSize(3); + ds.setAllowLocalTransactions(true); + ds.getDriverProperties().put("user", "sa"); + ds.getDriverProperties().put("password", "sasa"); + ds.getDriverProperties().put("URL", "jdbc:h2:mem:"); + ds.init(); + } + + private static KnowledgeBase readKnowledgeBase() { + KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + kbuilder.add(ResourceFactory.newClassPathResource("VariablePersistenceStrategyProcess.rf"), + ResourceType.DRF); + KnowledgeBuilderErrors errors = kbuilder.getErrors(); + if (errors.size() > 0) { + for (KnowledgeBuilderError error : errors) { + System.err.println(error); + } + throw new IllegalArgumentException("Could not parse knowledge."); + } + KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); + kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); + return kbase; + } + +} \ No newline at end of file Index: src/test/resources/jndi.properties =================================================================== --- src/test/resources/jndi.properties (revision 0) +++ src/test/resources/jndi.properties (revision 1007) @@ -0,0 +1 @@ +java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory Index: src/main/resources/META-INF/persistence.xml =================================================================== --- src/main/resources/META-INF/persistence.xml (revision 995) +++ src/main/resources/META-INF/persistence.xml (working copy) @@ -29,7 +29,9 @@ - + + + Index: src/main/resources/META-INF/orm.xml =================================================================== --- src/main/resources/META-INF/orm.xml (revision 995) +++ src/main/resources/META-INF/orm.xml (working copy) @@ -13,14 +13,37 @@ :type in elements(processInstanceInfo.eventTypes) - + +