From 93b4caa8bf391c7cea89b284b6c9301bee7c4692 Mon Sep 17 00:00:00 2001 From: "Rafael M. Pestano" Date: Thu, 3 Aug 2017 07:48:41 -0300 Subject: [PATCH] fix(#14): Resolves TransactionProviderNotFoundException when no TransactionHandler is found (#15) --- .../client/ClientSideTransactionHandler.java | 13 +++++++++++ .../TransactionProviderProducer.java | 3 +-- .../ClientSideTransactionHandlerTestCase.java | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/client/ClientSideTransactionHandler.java b/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/client/ClientSideTransactionHandler.java index 9fd805d..f4709f1 100644 --- a/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/client/ClientSideTransactionHandler.java +++ b/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/client/ClientSideTransactionHandler.java @@ -21,12 +21,15 @@ import org.jboss.arquillian.container.spi.client.deployment.Deployment; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.Inject; +import org.jboss.arquillian.core.spi.ServiceLoader; import org.jboss.arquillian.core.spi.context.ApplicationContext; import org.jboss.arquillian.test.spi.event.suite.TestEvent; import org.jboss.arquillian.transaction.impl.lifecycle.ModeChecker; import org.jboss.arquillian.transaction.impl.lifecycle.TransactionHandler; +import org.jboss.arquillian.transaction.spi.provider.TransactionProvider; public class ClientSideTransactionHandler extends TransactionHandler { + @Inject private Instance deploymentInstance; @@ -36,8 +39,18 @@ public class ClientSideTransactionHandler extends TransactionHandler { @Inject private Instance applicationContextInstance; + @Inject + private Instance serviceLoaderInstance; + @Override public boolean isTransactionSupported(TestEvent testEvent) { + + final TransactionProvider transactionProvider = + serviceLoaderInstance.get().onlyOne(TransactionProvider.class); + if (transactionProvider == null) { + return false; + } + return new ModeChecker(deploymentInstance.get(), containerInstance.get()).isClientMode(testEvent); } } diff --git a/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/lifecycle/TransactionProviderProducer.java b/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/lifecycle/TransactionProviderProducer.java index 844f5ff..4cadc36 100644 --- a/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/lifecycle/TransactionProviderProducer.java +++ b/impl-base/src/main/java/org/jboss/arquillian/transaction/impl/lifecycle/TransactionProviderProducer.java @@ -44,8 +44,7 @@ public void registerTransactionProvider(@Observes(precedence = 100) Before befor final TransactionProvider transactionProvider = serviceLoaderInstance.get().onlyOne(TransactionProvider.class); if (transactionProvider == null) { - throw new TransactionProviderNotFoundException( - "Transaction provider for given test case has not been found."); + return; } transactionProviderProducer.set(transactionProvider); } catch (IllegalStateException e) { diff --git a/impl-base/src/test/java/org/jboss/arquillian/transaction/impl/lifecycle/ClientSideTransactionHandlerTestCase.java b/impl-base/src/test/java/org/jboss/arquillian/transaction/impl/lifecycle/ClientSideTransactionHandlerTestCase.java index 3c731be..8aad469 100644 --- a/impl-base/src/test/java/org/jboss/arquillian/transaction/impl/lifecycle/ClientSideTransactionHandlerTestCase.java +++ b/impl-base/src/test/java/org/jboss/arquillian/transaction/impl/lifecycle/ClientSideTransactionHandlerTestCase.java @@ -205,6 +205,28 @@ public void shouldNotStartTransaction() throws Exception { getManager().getContext(ClassContext.class).deactivate(); } + @Test + public void shouldNotStartTransactionWhenNoTransactionProividerIsFound() throws Exception { + + when(mockServiceLoader.onlyOne(TransactionProvider.class)).thenReturn(null); + + getManager().getContext(ClassContext.class).activate(TestClass.class); + + Object instance = new TestClass(); + Method testMethod = instance.getClass().getMethod("defaultTest"); + + getManager().fire(new org.jboss.arquillian.test.spi.event.suite.Before(instance, testMethod)); + + // checks if the transaction context hasn't been created + verifyZeroInteractions(mockTransactionContext); + + // verifies that the transaction hasn't been started + verifyZeroInteractions(mockTransactionProvider); + + getManager().getContext(ClassContext.class).deactivate(); + } + + @Test public void shouldRollbackTransaction() throws Exception {