Skip to content

Commit

Permalink
perf
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Sep 15, 2016
1 parent 24887d8 commit fb37de8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static com.github.dbunit.rules.util.EntityManagerProvider.em;
import static com.github.dbunit.rules.util.EntityManagerProvider.isEntityManagerActive;
import static com.github.dbunit.rules.util.EntityManagerProvider.tx;

/**
* Created by pestano on 27/08/16.
Expand Down Expand Up @@ -114,7 +115,9 @@ public void beforeTestExecution(TestExtensionContext testExtensionContext) throw
boolean isTransactional = dataSetConfig.isTransactional();
if (isTransactional) {
if (isEntityManagerActive()) {
em().getTransaction().begin();
if(!tx().isActive()){
em().getTransaction().begin();
}
} else{
Connection connection = executor.getConnectionHolder().getConnection();
connection.setAutoCommit(false);
Expand Down Expand Up @@ -179,12 +182,24 @@ public void afterTestExecution(TestExtensionContext testExtensionContext) throws
DataSetConfig datasetConfig = testExtensionContext.getStore(namespace).get(DATASET_CONFIG_STORE, DataSetConfig.class);
boolean isTransactional = datasetConfig.isTransactional();
if (isTransactional) {
if(isEntityManagerActive()){
em().getTransaction().commit();
} else{
Connection connection = executor.getConnectionHolder().getConnection();
connection.commit();
connection.setAutoCommit(false);
try {
if (isEntityManagerActive()) {
if(tx().isActive()){
tx().commit();
}
} else {
Connection connection = executor.getConnectionHolder().getConnection();
connection.commit();
connection.setAutoCommit(false);
}
}catch (Exception e){
if(isEntityManagerActive()){
tx().rollback();
} else{
Connection connection = executor.getConnectionHolder().getConnection();
connection.setAutoCommit(false);
connection.setReadOnly(true);
}
}
}
executor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true), expectedDataSet.ignoreCols());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public void shouldUpdateUser() {
}

@Test
@DataSet(value = "users.yml", transactional = true, cleanAfter = true)
@DataSet(value = "usersWithTweet.yml", transactional = true, cleanBefore = true)
@ExpectedDataSet("expectedUser.yml")
public void shouldDeleteUser() {
User user = (User) em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("@realpestano");
em().remove(user.getTweets().get(0));
em().remove(user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private ConnectionHolder getConnection(){
}

@Test
@DataSet("users.yml")
@DataSet(value = "users.yml",cleanBefore = true)
public void shouldListUsers() {
List<User> users = em().createQuery("select u from User u").getResultList();
assertThat(users).isNotNull().isNotEmpty().hasSize(2);
Expand Down Expand Up @@ -63,12 +63,13 @@ public void shouldUpdateUser() {
}

@Test
@DataSet(value = "users.yml", transactional = true)
@DataSet(value = "usersWithTweet.yml", transactional = true)
@ExpectedDataSet("expectedUser.yml")
public void shouldDeleteUser() {
User user = (User) em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("@realpestano");
em().remove(user.getTweets().get(0));
em().remove(user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.dbunit.rules.api.configuration.DBUnit;
import com.github.dbunit.rules.api.dataset.DataSet;
import com.github.dbunit.rules.api.dataset.ExpectedDataSet;
import org.assertj.core.api.Assertions;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -22,7 +23,7 @@
*/
@ExtendWith(DBUnitExtension.class)
@RunWith(JUnitPlatform.class)
@DBUnit(cacheConnection=false,cacheTableNames = false, url = "jdbc:hsqldb:mem:flyway;DB_CLOSE_DELAY=-1", driver = "org.hsqldb.jdbcDriver", user = "sa")
@DBUnit(url = "jdbc:hsqldb:mem:flyway;DB_CLOSE_DELAY=-1", driver = "org.hsqldb.jdbcDriver", user = "sa")
public class FlywayIt {

private static Flyway flyway;
Expand All @@ -32,7 +33,6 @@ public class FlywayIt {

@BeforeAll
public static void initDB(){

flyway = new Flyway();
flyway.setDataSource("jdbc:hsqldb:mem:flyway;DB_CLOSE_DELAY=-1", "sa", "");
flyway.setLocations("filesystem:src/test/resources/migration");
Expand All @@ -41,7 +41,7 @@ public static void initDB(){
}

@Test
@DataSet("users.yml")
@DataSet(value = "users.yml",executorId = "flyway")
public void shouldListUsers() throws SQLException {
try (Statement stmt = flyway.getDataSource().getConnection().createStatement()) {
ResultSet resultSet = stmt.executeQuery("select * from user u order by id");
Expand All @@ -51,7 +51,7 @@ public void shouldListUsers() throws SQLException {
}

@Test
@DataSet(cleanBefore = true, transactional = true)
@DataSet(cleanBefore = true, transactional = true,executorId = "flyway")
@ExpectedDataSet(value = "usersInserted.yml")
public void shouldInserUsers() throws SQLException {
Connection connection = flyway.getDataSource().getConnection();
Expand Down

0 comments on commit fb37de8

Please sign in to comment.