Modified: aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlStatusTest.java URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlStatusTest.java?rev=1740210&r1=1740209&r2=1740210&view=diff ============================================================================== --- aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlStatusTest.java (original) +++ aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlStatusTest.java Wed Apr 20 23:09:10 2016 @@ -3,43 +3,31 @@ package org.apache.aries.tx.control.serv import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.osgi.service.transaction.control.TransactionStatus.ACTIVE; import static org.osgi.service.transaction.control.TransactionStatus.MARKED_ROLLBACK; import static org.osgi.service.transaction.control.TransactionStatus.NO_TRANSACTION; -import java.util.HashMap; -import java.util.Map; - import javax.transaction.xa.XAException; -import org.apache.aries.tx.control.service.xa.impl.TransactionControlImpl; import org.apache.geronimo.transaction.manager.GeronimoTransactionManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import org.osgi.service.coordinator.Coordination; -import org.osgi.service.coordinator.Coordinator; -import org.osgi.service.coordinator.Participant; import org.osgi.service.transaction.control.LocalResource; import org.osgi.service.transaction.control.ResourceProvider; +import org.osgi.service.transaction.control.TransactionContext; @RunWith(MockitoJUnitRunner.class) public class TransactionControlStatusTest { @Mock - Coordinator coordinator; - @Mock - Coordination coordination1; - @Mock - Coordination coordination2; - - @Mock ResourceProvider<Object> testProvider; @Mock LocalResource testResource; @@ -47,24 +35,17 @@ public class TransactionControlStatusTes TransactionControlImpl txControl; Object resource = new Object(); - Map<Class<?>, Object> variables1; - Map<Class<?>, Object> variables2; @Before public void setUp() throws XAException { resource = new Object(); - variables1 = new HashMap<>(); - variables2 = new HashMap<>(); - - Mockito.when(coordination1.getVariables()).thenReturn(variables1); - Mockito.when(coordination2.getVariables()).thenReturn(variables2); - txControl = new TransactionControlImpl(new GeronimoTransactionManager(), coordinator); + txControl = new TransactionControlImpl(new GeronimoTransactionManager()); } @Test - public void testGetRollbackOnlyUnscopedNoCoord() { + public void testGetRollbackOnlyUnscoped() { try { txControl.getRollbackOnly(); fail("Should not be able to get rollback only"); @@ -74,7 +55,7 @@ public class TransactionControlStatusTes } @Test - public void testSetRollbackOnlyUnscopedNoCoord() { + public void testSetRollbackOnlyUnscoped() { try { txControl.setRollbackOnly(); fail("Should not be able to set rollback only"); @@ -84,80 +65,15 @@ public class TransactionControlStatusTes } @Test - public void testTranChecksUnscopedNoCoord() { + public void testTranChecksUnscoped() { assertFalse(txControl.activeTransaction()); assertFalse(txControl.activeScope()); assertNull(txControl.getCurrentContext()); } - private void setupExistingCoordination() { - Mockito.when(coordinator.peek()).thenReturn(coordination1); - Mockito.when(coordination1.getVariables()).thenReturn(variables1); - } - - @Test - public void testGetRollbackOnlyUnscopedWithCoordination() { - setupExistingCoordination(); - - try { - txControl.getRollbackOnly(); - fail("Should not be able to get rollback only"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void testSetRollbackOnlyUnscopedWithCoordination() { - setupExistingCoordination(); - - - try { - txControl.setRollbackOnly(); - fail("Should not be able to set rollback only"); - } catch (IllegalStateException e) { - - } - } - - @Test - public void testTranChecksUnscopedWithCoordination() { - - setupExistingCoordination(); - - assertFalse(txControl.activeTransaction()); - assertFalse(txControl.activeScope()); - assertNull(txControl.getCurrentContext()); - } - - private void setupCoordinatorForSingleTransaction() { - setupCoordinatorForSingleTransaction(null); - } - - private void setupCoordinatorForSingleTransaction(Coordination existing) { - - Mockito.when(coordinator.peek()).thenReturn(existing); - - Mockito.when(coordinator.begin(Mockito.anyString(), Mockito.anyLong())) - .then(i -> { - Mockito.when(coordinator.peek()).thenReturn(coordination1); - return coordination1; - }); - - - Mockito.doAnswer(i -> Mockito.when(coordinator.peek()).thenReturn(existing)) - .when(coordination1).end(); - Mockito.doAnswer(i -> Mockito.when(coordinator.peek()).thenReturn(existing) != null) - .when(coordination1).fail(Mockito.any(Throwable.class)); - - Mockito.when(coordination1.getVariables()).thenReturn(variables1); - } - @Test public void testGetRollbackOnlyScoped() { - setupCoordinatorForSingleTransaction(); txControl.notSupported(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); try { txControl.getRollbackOnly(); fail("Should not be able to get or set rollback when there is no transaction"); @@ -169,10 +85,7 @@ public class TransactionControlStatusTes @Test public void testSetRollbackOnlyScoped() { - setupCoordinatorForSingleTransaction(); - txControl.notSupported(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); try { txControl.setRollbackOnly(); fail("Should not be able to get or set rollback when there is no transaction"); @@ -185,7 +98,6 @@ public class TransactionControlStatusTes @Test public void testTranChecksScoped() { - setupCoordinatorForSingleTransaction(); txControl.notSupported(() -> { assertFalse(txControl.activeTransaction()); assertTrue(txControl.activeScope()); @@ -197,53 +109,43 @@ public class TransactionControlStatusTes } @Test - public void testGetRollbackOnlyScopedExistingCoordination() { - setupCoordinatorForSingleTransaction(coordination2); + public void testInheritNotSupported() { txControl.notSupported(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); - try { - txControl.getRollbackOnly(); - fail("Should not be able to get or set rollback when there is no transaction"); - } catch (IllegalStateException ise) { - } - return null; - }); + TransactionContext currentContext = txControl.getCurrentContext(); + return txControl.notSupported(() -> { + assertSame(currentContext, txControl.getCurrentContext()); + return null; + }); + }); } - + @Test - public void testSetRollbackOnlyScopedExistingCoordination() { - setupCoordinatorForSingleTransaction(coordination2); - + public void testInheritNotSupportedSupports() { txControl.notSupported(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); - try { - txControl.setRollbackOnly(); - fail("Should not be able to get or set rollback when there is no transaction"); - } catch (IllegalStateException ise) { - } - return null; + TransactionContext currentContext = txControl.getCurrentContext(); + return txControl.supports(() -> { + assertSame(currentContext, txControl.getCurrentContext()); + return null; + }); }); } - + @Test - public void testTranChecksScopedExistingCoordination() { - - setupCoordinatorForSingleTransaction(coordination2); + public void testInheritNotSupportedRequired() { txControl.notSupported(() -> { - assertFalse(txControl.activeTransaction()); - assertTrue(txControl.activeScope()); - assertNotNull(txControl.getCurrentContext()); - assertEquals(NO_TRANSACTION, txControl.getCurrentContext().getTransactionStatus()); - + TransactionContext currentContext = txControl.getCurrentContext(); + txControl.required(() -> { + assertNotSame(currentContext, txControl.getCurrentContext()); + return null; + }); + assertSame(currentContext, txControl.getCurrentContext()); return null; }); } @Test public void testGetRollbackOnlyActive() { - setupCoordinatorForSingleTransaction(); txControl.required(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); assertFalse(txControl.getRollbackOnly()); return null; }); @@ -251,14 +153,10 @@ public class TransactionControlStatusTes @Test public void testSetRollbackOnlyActive() { - setupCoordinatorForSingleTransaction(); - txControl.required(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); assertFalse(txControl.getRollbackOnly()); txControl.setRollbackOnly(); assertTrue(txControl.getRollbackOnly()); - return null; }); } @@ -266,7 +164,6 @@ public class TransactionControlStatusTes @Test public void testTranChecksActive() { - setupCoordinatorForSingleTransaction(); txControl.required(() -> { assertTrue(txControl.activeTransaction()); assertTrue(txControl.activeScope()); @@ -281,42 +178,36 @@ public class TransactionControlStatusTes } @Test - public void testGetRollbackOnlyActiveExistingCoordination() { - setupCoordinatorForSingleTransaction(coordination2); + public void testInheritSupports() { txControl.required(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); - assertFalse(txControl.getRollbackOnly()); - return null; - }); + TransactionContext currentContext = txControl.getCurrentContext(); + return txControl.supports(() -> { + assertSame(currentContext, txControl.getCurrentContext()); + return null; + }); + }); } - + @Test - public void testSetRollbackOnlyActiveExistingCoordination() { - setupCoordinatorForSingleTransaction(coordination2); - + public void testInheritRequired() { txControl.required(() -> { - Mockito.verify(coordination1).addParticipant(Mockito.any(Participant.class)); - assertFalse(txControl.getRollbackOnly()); - txControl.setRollbackOnly(); - assertTrue(txControl.getRollbackOnly()); - - return null; + TransactionContext currentContext = txControl.getCurrentContext(); + return txControl.required(() -> { + assertSame(currentContext, txControl.getCurrentContext()); + return null; + }); }); } - + @Test - public void testTranChecksActiveExistingCoordination() { - - setupCoordinatorForSingleTransaction(coordination2); + public void testInheritRequiredNotSupported() { txControl.required(() -> { - assertTrue(txControl.activeTransaction()); - assertTrue(txControl.activeScope()); - assertNotNull(txControl.getCurrentContext()); - assertEquals(ACTIVE, txControl.getCurrentContext().getTransactionStatus()); - - txControl.setRollbackOnly(); - assertEquals(MARKED_ROLLBACK, txControl.getCurrentContext().getTransactionStatus()); - + TransactionContext currentContext = txControl.getCurrentContext(); + txControl.notSupported(() -> { + assertNotSame(currentContext, txControl.getCurrentContext()); + return null; + }); + assertSame(currentContext, txControl.getCurrentContext()); return null; }); }
Modified: aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionLifecycleTest.java URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionLifecycleTest.java?rev=1740210&r1=1740209&r2=1740210&view=diff ============================================================================== --- aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionLifecycleTest.java (original) +++ aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionLifecycleTest.java Wed Apr 20 23:09:10 2016 @@ -5,21 +5,14 @@ import static org.junit.Assert.assertFal import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.util.HashMap; -import java.util.Map; - import javax.transaction.xa.XAException; -import org.apache.aries.tx.control.service.xa.impl.TransactionControlImpl; import org.apache.geronimo.transaction.manager.GeronimoTransactionManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import org.osgi.service.coordinator.Coordination; -import org.osgi.service.coordinator.Coordinator; import org.osgi.service.transaction.control.LocalResource; import org.osgi.service.transaction.control.ResourceProvider; @@ -27,65 +20,15 @@ import org.osgi.service.transaction.cont public class TransactionLifecycleTest { @Mock - Coordinator coordinator; - @Mock - Coordination coordination1; - @Mock - Coordination coordination2; - - @Mock ResourceProvider<Object> testProvider; @Mock LocalResource testResource; TransactionControlImpl txControl; - Map<Class<?>, Object> variables1; - Map<Class<?>, Object> variables2; - @Before public void setUp() throws XAException { - variables1 = new HashMap<>(); - variables2 = new HashMap<>(); - - setupCoordinations(); - - txControl = new TransactionControlImpl(new GeronimoTransactionManager(), coordinator); - } - - /** - * Allow up to two Coordinations to be happening - */ - private void setupCoordinations() { - Mockito.when(coordinator.begin(Mockito.anyString(), Mockito.anyLong())).then(i -> { - Mockito.when(coordinator.peek()).thenReturn(coordination1); - return coordination1; - }).then(i -> { - Mockito.when(coordinator.peek()).thenReturn(coordination2); - return coordination2; - }).thenThrow(new IllegalStateException("Only two coordinations at a time in the test")); - - Mockito.when(coordination1.getVariables()).thenReturn(variables1); - Mockito.when(coordination1.getId()).thenReturn(42L); - Mockito.doAnswer(i -> { - Mockito.when(coordinator.peek()).thenReturn(null); - return null; - }).when(coordination1).end(); - Mockito.doAnswer(i -> { - Mockito.when(coordinator.peek()).thenReturn(null); - return null; - }).when(coordination1).fail(Mockito.any(Throwable.class)); - - Mockito.when(coordination2.getVariables()).thenReturn(variables2); - Mockito.when(coordination2.getId()).thenReturn(43L); - Mockito.doAnswer(i -> { - Mockito.when(coordinator.peek()).thenReturn(coordination1); - return null; - }).when(coordination2).end(); - Mockito.doAnswer(i -> { - Mockito.when(coordinator.peek()).thenReturn(coordination1); - return null; - }).when(coordination2).fail(Mockito.any(Throwable.class)); + txControl = new TransactionControlImpl(new GeronimoTransactionManager()); } @Test
