mirror of
https://github.com/Telecominfraproject/wlan-cloud-base.git
synced 2025-10-30 01:52:43 +00:00
added base-tx-tests project with the TransactionManager for use in unit tests
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
<module>../base-hierarchical-datastore</module>
|
||||
<module>../base-jdbc</module>
|
||||
<module>../base-jdbc-tests</module>
|
||||
<module>../base-tx-tests</module>
|
||||
<module>../base-cassandra</module>
|
||||
<module>../base-cassandra-tests</module>
|
||||
<module>../base-job</module>
|
||||
|
||||
@@ -13,9 +13,11 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<groupId>com.telecominfraproject.wlan</groupId>
|
||||
<artifactId>base-tx-tests</artifactId>
|
||||
<version>${tip-wlan-cloud.release.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.transaction.support.SimpleTransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import com.telecominfraproject.wlan.core.server.tx.test.TxTestConfig;
|
||||
import com.telecominfraproject.wlan.server.RemoteTestServer;
|
||||
|
||||
/**
|
||||
@@ -75,6 +76,7 @@ import com.telecominfraproject.wlan.server.RemoteTestServer;
|
||||
"tip.wlan.csrf-enabled=false" })
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
@Import(value = {
|
||||
TxTestConfig.class
|
||||
})
|
||||
public abstract class BaseRemoteTest {
|
||||
|
||||
@@ -130,53 +132,6 @@ public abstract class BaseRemoteTest {
|
||||
// @PropertySource({ "classpath:persistence-${envTarget:dev}.properties" })
|
||||
public static class Config {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
PlatformTransactionManager ptm = new PlatformTransactionManager() {
|
||||
|
||||
{
|
||||
LOG.info("*** Using simulated PlatformTransactionManager");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(TransactionStatus status) throws TransactionException {
|
||||
LOG.info("Simulating Rollback for {}", status);
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit(TransactionStatus status) throws TransactionException {
|
||||
LOG.info("Simulating Commit for {}", status);
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager
|
||||
.getSynchronizations();
|
||||
if (synchronizations != null) {
|
||||
for (TransactionSynchronization synchronization : synchronizations) {
|
||||
synchronization.afterCommit();
|
||||
}
|
||||
}
|
||||
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
|
||||
LOG.info("Simulating getTransaction for {}", definition);
|
||||
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
}
|
||||
TransactionStatus ts = new SimpleTransactionStatus();
|
||||
return ts;
|
||||
}
|
||||
};
|
||||
|
||||
return ptm;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected int getNextCustomerId() {
|
||||
|
||||
21
base-tx-tests/pom.xml
Normal file
21
base-tx-tests/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.telecominfraproject.wlan</groupId>
|
||||
<artifactId>tip-wlan-cloud-root-pom</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../wlan-cloud-root</relativePath>
|
||||
</parent>
|
||||
<artifactId>base-tx-tests</artifactId>
|
||||
<name>base-tx-tests</name>
|
||||
<description>Common classes used by the unit tests that require transaction support.</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.telecominfraproject.wlan.core.server.tx.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.SimpleTransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
/**
|
||||
* @author dtop
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
public class TxTestConfig {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TxTestConfig.class);
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
PlatformTransactionManager ptm = new PlatformTransactionManager() {
|
||||
private ThreadLocal<List<TransactionStatus>> currentTx = new ThreadLocal<>() ;
|
||||
|
||||
{
|
||||
LOG.info("*** Using simulated PlatformTransactionManager");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(TransactionStatus status) throws TransactionException {
|
||||
LOG.debug("Simulating Rollback for {}", status);
|
||||
if(currentTx.get().size() == 1 ) {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
currentTx.remove();
|
||||
} else {
|
||||
currentTx.get().remove(currentTx.get().size() - 1 );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit(TransactionStatus status) throws TransactionException {
|
||||
LOG.debug("Simulating Commit for {}", status);
|
||||
if(currentTx.get().size() == 1 ) {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager
|
||||
.getSynchronizations();
|
||||
if (synchronizations != null) {
|
||||
for (TransactionSynchronization synchronization : synchronizations) {
|
||||
synchronization.afterCommit();
|
||||
}
|
||||
}
|
||||
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
|
||||
currentTx.remove();
|
||||
} else {
|
||||
currentTx.get().remove(currentTx.get().size() - 1 );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
|
||||
LOG.debug("Simulating getTransaction for {}", definition);
|
||||
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
}
|
||||
|
||||
if (currentTx.get() == null) {
|
||||
List<TransactionStatus> txList = new ArrayList<>();
|
||||
TransactionStatus ts = new SimpleTransactionStatus(true);
|
||||
txList.add(ts);
|
||||
currentTx.set(txList);
|
||||
return ts;
|
||||
} else {
|
||||
List<TransactionStatus> txList = currentTx.get();
|
||||
TransactionStatus ts = new SimpleTransactionStatus(false);
|
||||
txList.add(ts);
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
return ptm;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user