Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2017 Red Hat, Inc. and/or its affiliates.
- *
- * 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.kie.persistence.session;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import javax.naming.InitialContext;
- import javax.transaction.UserTransaction;
- import org.drools.persistence.util.DroolsPersistenceUtil;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.kie.api.KieBase;
- import org.kie.api.KieServices;
- import org.kie.api.io.ResourceType;
- import org.kie.api.runtime.Environment;
- import org.kie.api.runtime.EnvironmentName;
- import org.kie.api.runtime.KieSession;
- import org.kie.internal.utils.KieHelper;
- import static org.drools.persistence.util.DroolsPersistenceUtil.DROOLS_PERSISTENCE_UNIT_NAME;
- import static org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment;
- import static org.junit.Assert.assertEquals;
- public class SimpleJpaPersistentStatefulSessionWithOOPathTest {
- private Map<String, Object> context;
- private Environment env;
- @Before
- public void setUp() throws Exception {
- context = DroolsPersistenceUtil.setupWithPoolingDataSource( DROOLS_PERSISTENCE_UNIT_NAME );
- env = createEnvironment(context);
- env.set( EnvironmentName.USE_PESSIMISTIC_LOCKING, true );
- }
- @After
- public void tearDown() throws Exception {
- DroolsPersistenceUtil.cleanUp(context);
- }
- @Test
- public void testUserTransactionsOOpath() throws Exception {
- testUserTransactions( true );
- }
- @Test
- public void testUserTransactionsNoOOpath() throws Exception {
- testUserTransactions( false );
- }
- private void testUserTransactions(boolean oopath) throws Exception {
- String str = "package org.kie.test\n" +
- "global java.util.List list\n" +
- "rule rule1\n" +
- "when\n" +
- ( oopath ? " $i: Integer( /intValue{this > 0})\n" : " $i : Integer(intValue > 0)\n" ) +
- "then\n" +
- " list.add( $i );\n" +
- "end\n";
- KieBase kbase = new KieHelper().addContent( str, ResourceType.DRL ).build();
- UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
- ut.begin();
- KieSession ksession = KieServices.get().getStoreServices().newKieSession( kbase, null, env );
- ut.commit();
- final List<?> list = new ArrayList<>();
- // insert and commit
- ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
- ut.begin();
- ksession.setGlobal( "list",
- list );
- ksession.insert( 1 );
- ksession.insert( 2 );
- ksession.fireAllRules();
- ut.commit();
- // insert and rollback
- ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
- ut.begin();
- ksession.insert(3);
- ut.rollback();
- // check we rolled back the state changes from the 3rd insert
- ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
- ut.begin();
- ksession.fireAllRules();
- ut.commit();
- assertEquals( 2,
- list.size() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement