No effective change - add extra test coverage on DefaultTransactionThreadLocal

This commit is contained in:
Rob Bygrave
2018-03-26 23:31:31 +13:00
parent 74e0a16531
commit db5a2b194d
@@ -2,26 +2,80 @@ package io.ebeaninternal.server.transaction;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Transaction;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.Platform;
import io.ebeaninternal.api.SpiTransaction;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
public class DefaultTransactionThreadLocalTest extends BaseTestCase {
@ForPlatform({Platform.H2})
@Test
public void get() throws Exception {
public void get() {
Ebean.execute(() -> {
SpiTransaction txn = DefaultTransactionThreadLocal.get("h2");
assertNotNull(txn);
});
if (isH2()) {
Ebean.execute(() -> {
SpiTransaction txn = DefaultTransactionThreadLocal.get("h2");
assertNotNull(txn);
});
// thread local should be set to null
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
// thread local should be set to null
@ForPlatform({Platform.H2})
@Test
public void tryWithResources_expect_scopeCleanup() {
try (Transaction transaction = Ebean.beginTransaction()) {
assertNotNull(transaction);
SpiTransaction txn = DefaultTransactionThreadLocal.get("h2");
assertSame(txn, transaction);
try (Transaction nested = Ebean.beginTransaction()) {
assertNotNull(nested);
SpiTransaction txnNested = DefaultTransactionThreadLocal.get("h2");
assertSame(txnNested, nested);
}
assertNotNull(DefaultTransactionThreadLocal.get("h2"));
}
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
@ForPlatform({Platform.H2})
@Test
public void afterCommit_expect_scopeCleanup() {
try (Transaction transaction = Ebean.beginTransaction()) {
transaction.commit();
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
@ForPlatform({Platform.H2})
@Test
public void afterRollback_expect_scopeCleanup() {
try (Transaction transaction = Ebean.beginTransaction()) {
transaction.rollback();
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
assertNull(DefaultTransactionThreadLocal.get("h2"));
}
@ForPlatform({Platform.H2})
@Test
public void end_withoutActiveTransaction_isFine() {
assertNull(Ebean.currentTransaction());
Ebean.endTransaction();
}
}