TransactionHandler implements ExchangeHandler { @Override public void handleMessage(Exchange exchange) throws TransactionFailureException { if (_transactionManager == null) { return; } Property prop = exchange.getContext().getProperty(BEFORE_INVOKED_PROPERTY, Scope.EXCHANGE); if (prop != null && Boolean.class.cast(prop.getValue())) { handleAfter(exchange); } else { exchange.getContext().setProperty(BEFORE_INVOKED_PROPERTY, Boolean.TRUE, Scope.EXCHANGE).addLabels(BehaviorLabel.TRANSIENT.label()); handleBefore(exchange); } } TransactionHandler(); @Override void handleMessage(Exchange exchange); @Override void handleFault(Exchange exchange);  }
@Test public void incompatibleRequirements_PropagatesAndSuspends() { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.SUSPENDS_TRANSACTION); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void incompatibleRequirements_managedGlobalAndmanagedLocal() { PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void incompatibleRequirements_managedGlobalAndNoManaged() { PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); PolicyUtil.require(exchange, TransactionPolicy.NO_MANAGED_TRANSACTION); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void incompatibleRequirements_managedLocalAndNoManaged() { PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL); PolicyUtil.require(exchange, TransactionPolicy.NO_MANAGED_TRANSACTION); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void incompatibleRequirements_PropagatesAndManagedLocal() { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void incompatibleRequirements_PropagatesAndNoManaged() { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.NO_MANAGED_TRANSACTION); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to incompatible policy"); }
@Test public void propagatesRequiredButNoTransaction() { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); exchange.setPhase(ExchangePhase.IN); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { System.out.println(handlerEx.toString()); return; } Assert.fail("Expected a handler exception due to invalid policy"); }
@Test public void propagateProvidedButNotRequired() throws Exception { PolicyUtil.provide(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); exchange.setPhase(ExchangePhase.IN); tm.begin(); try { handler.handleMessage(exchange); } catch (HandlerException handlerEx) { Assert.fail("Exception not expected when transaction is provided but not required: " + handlerEx); } }
@Test public void propagateRequiredAndProvided() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.provide(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); exchange.setPhase(ExchangePhase.IN); tm.begin(); Transaction tx = tm.getTransaction(); handler.handleMessage(exchange); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Transaction tx2 = tm.getTransaction(); Assert.assertEquals(tx, tx2); Assert.assertEquals(Status.STATUS_ACTIVE, tx2.getStatus()); }
@Test public void propagateRequiredButNotProvidedWithManagedGlobalRequired() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); exchange.setPhase(ExchangePhase.IN); Assert.assertEquals(null, tm.getTransaction()); handler.handleMessage(exchange); Transaction tx = tm.getTransaction(); Assert.assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.PROPAGATES_TRANSACTION)); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL)); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertEquals(Status.STATUS_COMMITTED, tx.getStatus()); }
@Test public void managedLocalRequiredWithNoTransactionProvided() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL); exchange.setPhase(ExchangePhase.IN); Assert.assertEquals(null, tm.getTransaction()); handler.handleMessage(exchange); Transaction tx = tm.getTransaction(); Assert.assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL)); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertEquals(Status.STATUS_COMMITTED, tx.getStatus()); }
@Test public void transactionRolledbackByHandler() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.PROPAGATES_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); exchange.setPhase(ExchangePhase.IN); Assert.assertEquals(null, tm.getTransaction()); handler.handleMessage(exchange); Transaction tx = tm.getTransaction(); Assert.assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.PROPAGATES_TRANSACTION)); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL)); tx.setRollbackOnly(); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertEquals(Status.STATUS_ROLLEDBACK, tx.getStatus()); }
@Test public void suspendAndManagedGlobalRequiredButNoTransaction() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.SUSPENDS_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); exchange.setPhase(ExchangePhase.IN); handler.handleMessage(exchange); Transaction tx2 = tm.getTransaction(); Assert.assertEquals(Status.STATUS_ACTIVE, tx2.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.SUSPENDS_TRANSACTION)); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL)); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertEquals(Status.STATUS_COMMITTED, tx2.getStatus()); }
@Test public void suspendAndManagedGlobalRequired() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.SUSPENDS_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL); exchange.setPhase(ExchangePhase.IN); tm.begin(); Transaction tx1 = tm.getTransaction(); handler.handleMessage(exchange); Transaction tx2 = tm.getTransaction(); Assert.assertNotSame(tx1, tx2); Assert.assertEquals(Status.STATUS_ACTIVE, tx1.getStatus()); Assert.assertEquals(Status.STATUS_ACTIVE, tx2.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.SUSPENDS_TRANSACTION)); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_GLOBAL)); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertEquals(tx1, tm.getTransaction()); Assert.assertEquals(Status.STATUS_ACTIVE, tx1.getStatus()); Assert.assertEquals(Status.STATUS_COMMITTED, tx2.getStatus()); }
@Test public void suspendsAndManagedLocalRequired() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.SUSPENDS_TRANSACTION); PolicyUtil.require(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL); exchange.setPhase(ExchangePhase.IN); tm.begin(); Transaction tx1 = tm.getTransaction(); handler.handleMessage(exchange); Transaction tx2 = tm.getTransaction(); Assert.assertNotSame(tx1, tx2); Assert.assertEquals(Status.STATUS_ACTIVE, tx1.getStatus()); Assert.assertEquals(Status.STATUS_ACTIVE, tx2.getStatus()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.SUSPENDS_TRANSACTION)); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.MANAGED_TRANSACTION_LOCAL)); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Transaction tx3 = tm.getTransaction(); Assert.assertEquals(tx1, tx3); Assert.assertEquals(Status.STATUS_COMMITTED, tx2.getStatus()); Assert.assertEquals(Status.STATUS_ACTIVE, tx3.getStatus()); }
@Test public void suspendRequiredButNoTransaction() throws Exception { PolicyUtil.require(exchange, TransactionPolicy.SUSPENDS_TRANSACTION); exchange.setPhase(ExchangePhase.IN); handler.handleMessage(exchange); Assert.assertNull(tm.getTransaction()); exchange.setPhase(ExchangePhase.OUT); handler.handleMessage(exchange); Assert.assertNull(tm.getTransaction()); Assert.assertTrue(PolicyUtil.isProvided(exchange, TransactionPolicy.SUSPENDS_TRANSACTION)); }