Skip to content

Commit

Permalink
[ARQ-1186] Global / default transaction mode can be now configured th…
Browse files Browse the repository at this point in the history
…rough arquillian.xml
  • Loading branch information
bartoszmajsak authored and aslakknutsen committed Nov 16, 2012
1 parent 5da3ee4 commit 6b93887
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ public enum TransactionMode
/**
* Instructs extension to not use transactions for tests execution.
*/
DISABLED;
DISABLED,
/**
* Determined using externalized configuration. COMMIT by default.
*/
DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* <li>{@link TransactionMode#COMMIT} which is the default mode</li>
* <li>{@link TransactionMode#ROLLBACK}</li>
* <li>{@link TransactionMode#DISABLED}</li>
* <li></li>
* </ul>
*
* @author <a href="mailto:bartosz.majsak@gmail.com">Bartosz Majsak</a>
Expand All @@ -45,17 +46,14 @@
@Target(value={TYPE, METHOD})
@Retention(value=RUNTIME)
@Inherited
public @interface Transactional
{
public @interface Transactional {

TransactionMode value() default TransactionMode.DEFAULT;

/**
* The optional name of the manager to be used for handling transaction for
* given test case or method.
*/
String manager() default "";

/**
* The transaction mode.
*/
TransactionMode value() default TransactionMode.COMMIT;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.event.suite.BeforeSuite;
import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
import org.jboss.arquillian.transaction.impl.configuration.TransactionConfiguration;

import java.util.Collections;
Expand All @@ -38,25 +39,15 @@
*/
public class TransactionConfigurationProducer {

/**
* Represents the name of the transaction extension configuration.
*/
public static final String TRANSACTION_EXTENSION = "transaction";

/**
* Represents the name of the configuration property for storing the manager name.
*/
public static final String MANAGER_PROPERTY_NAME = "manager";

/**
* The arquillian descriptor.
*/
public static final String DEFAULT_TRANSACTION_MODE_PROPERTY_NAME = "transactionDefaultMode";

@Inject
private Instance<ArquillianDescriptor> descriptor;

/**
* The instance of transaction configuration.
*/
@Inject
@ApplicationScoped
private InstanceProducer<TransactionConfiguration> configurationInstance;
Expand Down Expand Up @@ -86,6 +77,10 @@ private TransactionConfiguration getConfiguration(ArquillianDescriptor arquillia

TransactionConfiguration configuration = new TransactionConfiguration();
configuration.setManager(extensionProperties.get(MANAGER_PROPERTY_NAME));
final String transactionDefaultMode = extensionProperties.get(DEFAULT_TRANSACTION_MODE_PROPERTY_NAME);
if (transactionDefaultMode != null && transactionDefaultMode.length() > 0) {
configuration.setTransactionDefaultMode(TransactionMode.valueOf(transactionDefaultMode));
}

return configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package org.jboss.arquillian.transaction.impl.configuration;

import org.jboss.arquillian.transaction.api.annotation.TransactionMode;

/**
* The transaction extension configuration.
*
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
* @author <a href="mailto:bartosz.majsak@gmail.com">Bartosz Majsak</a>
*/
public class TransactionConfiguration {

Expand All @@ -30,28 +33,32 @@ public class TransactionConfiguration {
*/
private String manager;

/**
* Default mode used (COMMIT) for tests if not specified otherwise using annotation.
*/
private TransactionMode transactionDefaultMode = TransactionMode.COMMIT;

/**
* Creates new instance of {@link TransactionConfiguration} class.
*/
public TransactionConfiguration() {
// empty constructor
}

/**
* Retrieves the manager name.
*
* @return the manager name
*/
public String getManager() {
return manager;
}

/**
* Sets the manager name.
*
* @param manager the manager name
*/
public void setManager(String manager) {
this.manager = manager;
}

public TransactionMode getTransactionDefaultMode() {
return transactionDefaultMode;
}

public void setTransactionDefaultMode(TransactionMode transactionDefaultMode) {
this.transactionDefaultMode = transactionDefaultMode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.OutputStream;
import java.util.Properties;

import org.jboss.arquillian.transaction.api.annotation.TransactionMode;

/**
* The configuration converter.
*
Expand All @@ -48,6 +50,7 @@ public static String exportToProperties(TransactionConfiguration configuration)

Properties properties = new Properties();
setPropertyValue(properties, "manager", configuration.getManager());
setPropertyValue(properties, "transactionDefaultMode", configuration.getTransactionDefaultMode().name());
properties.store(outputStream, "arquillian-transaction-configuration");

return outputStream.toString();
Expand All @@ -72,6 +75,10 @@ public static TransactionConfiguration importFromProperties(final InputStream in

TransactionConfiguration transactionConfiguration = new TransactionConfiguration();
transactionConfiguration.setManager(getPropertyValue(properties, "manager"));
final String transactionDefaultMode = getPropertyValue(properties, "transactionDefaultMode");
if (transactionDefaultMode != null && transactionDefaultMode.length() > 0) {
transactionConfiguration.setTransactionDefaultMode(TransactionMode.valueOf(transactionDefaultMode));
}
return transactionConfiguration;
} catch (IOException e) {
throw new RuntimeException("Could not import the configuration.", e);
Expand All @@ -92,7 +99,7 @@ private static String getPropertyValue(Properties properties, String propertyNam

String value = properties.getProperty(propertyName);

if ("".equals(value)) {
if ("".equals(value.trim())) {
return null;
}

Expand All @@ -107,7 +114,6 @@ private static String getPropertyValue(Properties properties, String propertyNam
* @param value the property value
*/
private static void setPropertyValue(Properties properties, String propertyName, String value) {

properties.setProperty(propertyName, getPropertyValueOrDefault(value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ private boolean isTransactionEnabled(TestEvent testEvent) {
return transactionMode != null && !TransactionMode.DISABLED.equals(transactionMode);
}

// TODO fix this crap
private boolean isTransactionSupported(TestEvent testEvent) {
boolean runAsClient = RunModeUtils.isRunAsClient(deploymentInstance.get(), testEvent.getTestClass().getJavaClass(), testEvent.getTestMethod());
boolean isLocal = RunModeUtils.isLocalContainer(containerInstance.get());

return runAsClient || isLocal || (!runAsClient && isLocal);
return runAsClient || isLocal;
}


private boolean rollbackRequired(TestEvent testEvent) {
return testRequiresRollbackDueToFailure() || TransactionMode.ROLLBACK.equals(getTransactionMode(testEvent));
}
Expand All @@ -174,6 +174,14 @@ private boolean testRequiresRollbackDueToFailure() {
* @return the transaction mode
*/
private TransactionMode getTransactionMode(TestEvent testEvent) {
TransactionMode result = extractFromTestEvent(testEvent);
if (TransactionMode.DEFAULT.equals(result)) {
result = configurationInstance.get().getTransactionDefaultMode();
}
return result;
}

private TransactionMode extractFromTestEvent(TestEvent testEvent) {
TransactionMode methodLevel = null;
TransactionMode classLevel = null;
final TransactionEnablerLoader transactionEnablerLoader = new TransactionEnablerLoader(serviceLoaderInstance.get());
Expand All @@ -189,6 +197,7 @@ private TransactionMode getTransactionMode(TestEvent testEvent) {
if (methodLevel != null) {
return methodLevel;
}

return classLevel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jboss.arquillian.test.spi.context.ClassContext;
import org.jboss.arquillian.test.spi.event.suite.BeforeSuite;
import org.jboss.arquillian.test.test.AbstractTestTestBase;
import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
import org.jboss.arquillian.transaction.impl.configuration.TransactionConfiguration;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.junit.Before;
Expand Down Expand Up @@ -58,7 +59,7 @@ protected void addExtensions(List<Class<?>> extensions) {
@Before
public void setUp() throws Exception {

ArquillianDescriptor descriptor = Descriptors.importAs(ArquillianDescriptor.class).from(
ArquillianDescriptor descriptor = Descriptors.importAs(ArquillianDescriptor.class).fromStream(
new FileInputStream(new File("src/test/resources", "arquillian.xml")));

bind(ApplicationScoped.class, ArquillianDescriptor.class, descriptor);
Expand All @@ -74,7 +75,8 @@ public void shouldCreateConfiguration() {
getManager().fire(new BeforeSuite());

TransactionConfiguration transactionConfiguration = getManager().resolve(TransactionConfiguration.class);
assertEquals("Invalid transaction manager name.", "testManagerName", transactionConfiguration.getManager());
assertEquals("Wrongly mapped transaction manager name.", "testManagerName", transactionConfiguration.getManager());
assertEquals("Wrongly mapped transaction default mode.", TransactionMode.DISABLED, transactionConfiguration.getTransactionDefaultMode());

getManager().getContext(ClassContext.class).deactivate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.jboss.arquillian.transaction.impl.configuration;

import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
import org.jboss.arquillian.transaction.impl.test.TransactionalTestImpl;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -60,4 +61,9 @@ public void shouldReturnManager() {

assertEquals("Invalid property value.", manager, instance.getManager());
}

@Test
public void shouldHaveDefaultTransactionModeSetToCommitIfNotSpecifiedOtherwise() {
assertEquals("Expecting COMMIT value.", TransactionMode.COMMIT, instance.getTransactionDefaultMode());
}
}
Loading

0 comments on commit 6b93887

Please sign in to comment.