Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. /*
  2.  * Copyright 2017 Red Hat, Inc. and/or its affiliates.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package org.drools.persistence.kie.persistence.session;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import javax.naming.InitialContext;
  23. import javax.transaction.UserTransaction;
  24.  
  25. import org.drools.persistence.util.DroolsPersistenceUtil;
  26. import org.junit.After;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.kie.api.KieBase;
  30. import org.kie.api.KieServices;
  31. import org.kie.api.io.ResourceType;
  32. import org.kie.api.runtime.Environment;
  33. import org.kie.api.runtime.EnvironmentName;
  34. import org.kie.api.runtime.KieSession;
  35. import org.kie.internal.utils.KieHelper;
  36.  
  37. import static org.drools.persistence.util.DroolsPersistenceUtil.DROOLS_PERSISTENCE_UNIT_NAME;
  38. import static org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment;
  39. import static org.junit.Assert.assertEquals;
  40.  
  41. public class SimpleJpaPersistentStatefulSessionWithOOPathTest {
  42.  
  43.     private Map<String, Object> context;
  44.     private Environment env;
  45.  
  46.     @Before
  47.     public void setUp() throws Exception {
  48.         context = DroolsPersistenceUtil.setupWithPoolingDataSource( DROOLS_PERSISTENCE_UNIT_NAME );
  49.         env = createEnvironment(context);
  50.         env.set( EnvironmentName.USE_PESSIMISTIC_LOCKING, true );
  51.     }
  52.  
  53.     @After
  54.     public void tearDown() throws Exception {
  55.         DroolsPersistenceUtil.cleanUp(context);
  56.     }
  57.  
  58.     @Test
  59.     public void testUserTransactionsOOpath() throws Exception {
  60.         testUserTransactions( true );
  61.     }
  62.  
  63.     @Test
  64.     public void testUserTransactionsNoOOpath() throws Exception {
  65.         testUserTransactions( false );
  66.     }
  67.  
  68.     private void testUserTransactions(boolean oopath) throws Exception {
  69.         String str = "package org.kie.test\n" +
  70.                      "global java.util.List list\n" +
  71.                      "rule rule1\n" +
  72.                      "when\n" +
  73.                      ( oopath ? " $i: Integer( /intValue{this > 0})\n" : " $i : Integer(intValue > 0)\n" ) +
  74.                      "then\n" +
  75.                      " list.add( $i );\n" +
  76.                      "end\n";
  77.  
  78.         KieBase kbase = new KieHelper().addContent( str, ResourceType.DRL ).build();
  79.  
  80.         UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
  81.         ut.begin();
  82.         KieSession ksession = KieServices.get().getStoreServices().newKieSession( kbase, null, env );
  83.         ut.commit();
  84.  
  85.         final List<?> list = new ArrayList<>();
  86.  
  87.         // insert and commit
  88.         ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
  89.         ut.begin();
  90.         ksession.setGlobal( "list",
  91.                             list );
  92.         ksession.insert( 1 );
  93.         ksession.insert( 2 );
  94.         ksession.fireAllRules();
  95.         ut.commit();
  96.  
  97.         // insert and rollback
  98.         ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
  99.         ut.begin();
  100.         ksession.insert(3);
  101.         ut.rollback();
  102.  
  103.         // check we rolled back the state changes from the 3rd insert
  104.         ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
  105.         ut.begin();
  106.         ksession.fireAllRules();
  107.         ut.commit();
  108.         assertEquals( 2,
  109.                       list.size() );
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement