No effective change - Modify some tests to use transaction try with resources

This commit is contained in:
Rob Bygrave
2018-03-27 10:47:24 +13:00
parent abbcd35ee6
commit eb96c726fd
8 changed files with 78 additions and 119 deletions
@@ -5,7 +5,7 @@ import org.junit.Before;
import org.tests.model.basic.ResetBasicData;
/**
* Transactional test case. Every test is coverered by a transaction, which is roll backed.
* Transactional test case. Every test is covered by a transaction, which is roll backed.
* So no changes will persist to database.
*
* Use this test case if you modify data in your test case, so that the test does not interfere
@@ -3,11 +3,11 @@ package io.ebeaninternal.server.core;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.junit.Test;
import org.tests.model.basic.Car;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.tests.model.basic.Vehicle;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -23,15 +23,11 @@ public class DefaultServer_getReferenceTest extends BaseTestCase {
ResetBasicData.reset();
Ebean.beginTransaction();
try {
Ebean.execute(() -> {
Customer loaded = Ebean.find(Customer.class, 1);
Customer reference = Ebean.getReference(Customer.class, 1);
assertThat(loaded).isSameAs(reference);
} finally {
Ebean.endTransaction();
}
});
}
@@ -58,16 +54,12 @@ public class DefaultServer_getReferenceTest extends BaseTestCase {
car.setDriver("TestForRef");
Ebean.save(car);
Ebean.beginTransaction();
try {
Vehicle loaded = Ebean.find(Vehicle.class, car.getId());
Vehicle reference = Ebean.getReference(Vehicle.class, car.getId());
assertThat(reference).isSameAs(loaded);
} finally {
Ebean.endTransaction();
}
Ebean.execute(() -> {
Vehicle loaded = Ebean.find(Vehicle.class, car.getId());
Vehicle reference = Ebean.getReference(Vehicle.class, car.getId());
assertThat(reference).isSameAs(loaded);
}
);
Ebean.delete(car);
}
}
@@ -2,12 +2,13 @@ package org.tests.basic;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Transaction;
import org.junit.Assert;
import org.junit.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.EBasicVer;
import org.tests.model.basic.Order;
import org.tests.model.basic.ResetBasicData;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
@@ -18,8 +19,7 @@ public class TestLogTransLogOnError extends BaseTestCase {
ResetBasicData.reset();
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
Ebean.find(Customer.class).findList();
Ebean.find(Order.class).where().gt("id", 1).findList();
@@ -35,13 +35,11 @@ public class TestLogTransLogOnError extends BaseTestCase {
Assert.assertEquals(0, list.size());
// Get here with mysql?
// Assert.assertTrue(false);
txn.commit();
} catch (RuntimeException e) {
// e.printStackTrace();
Assert.assertTrue(true);
} finally {
Ebean.endTransaction();
}
}
@@ -49,8 +47,7 @@ public class TestLogTransLogOnError extends BaseTestCase {
ResetBasicData.reset();
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
Ebean.find(Customer.class).findList();
EBasicVer newBean = new EBasicVer("aName");
@@ -65,14 +62,11 @@ public class TestLogTransLogOnError extends BaseTestCase {
// never get here
Assert.assertTrue(false);
txn.commit();
} catch (RuntimeException e) {
// e.printStackTrace();
Assert.assertTrue(true);
} finally {
Ebean.endTransaction();
}
}
}
@@ -2,11 +2,12 @@ package org.tests.basic;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Transaction;
import org.junit.Assert;
import org.junit.Test;
import org.tests.model.basic.OCar;
import org.tests.model.basic.OEngine;
import org.tests.model.basic.OGearBox;
import org.junit.Assert;
import org.junit.Test;
public class TestMultipleOneToOneIUD extends BaseTestCase {
@@ -25,19 +26,14 @@ public class TestMultipleOneToOneIUD extends BaseTestCase {
car.setName("test car");
car.setEngine(engine);
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
Ebean.save(gearBox);
Ebean.save(car);
Assert.assertNotNull(car.getId());
Assert.assertNotNull(engine.getEngineId());
Assert.assertNotNull(gearBox.getId());
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
txn.commit();
}
OCar c2 = Ebean.find(OCar.class, car.getId());
@@ -29,10 +29,10 @@ public class TestQueryForUpdate extends BaseTestCase {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.forUpdate()
.order().desc("id");
.forUpdate()
.order().desc("id");
query.findList();
query.findList();
if (isSqlServer()) {
assertThat(sqlOf(query)).contains("with (updlock)");
} else {
@@ -48,25 +48,19 @@ public class TestQueryForUpdate extends BaseTestCase {
ResetBasicData.reset();
Ebean.beginTransaction();
try {
Query<Customer> query = Ebean.find(Customer.class)
.forUpdateNoWait()
.order().desc("id");
Query<Customer> query = Ebean.find(Customer.class)
.forUpdateNoWait()
.order().desc("id");
query.findList();
if (isOracle()) {
assertThat(sqlOf(query)).contains("for update nowait");
} else if (isH2()) {
assertThat(sqlOf(query)).contains("for update");
} else if (isSqlServer()) {
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
} else {
assertThat(sqlOf(query)).contains("for update nowait");
}
} finally {
Ebean.endTransaction();
query.findList();
if (isOracle()) {
assertThat(sqlOf(query)).contains("for update nowait");
} else if (isH2()) {
assertThat(sqlOf(query)).contains("for update");
} else if (isSqlServer()) {
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
} else {
assertThat(sqlOf(query)).contains("for update nowait");
}
}
@@ -80,42 +74,36 @@ public class TestQueryForUpdate extends BaseTestCase {
EbeanServer server = Ebean.getDefaultServer();
Ebean.beginTransaction();
try {
Query<Customer> query = Ebean.find(Customer.class)
.forUpdateNoWait()
.setMaxRows(1)
.order().desc("id");
try (Transaction txn = Ebean.beginTransaction()) {
Query<Customer> query = Ebean.find(Customer.class)
.forUpdateNoWait()
.setMaxRows(1)
.order().desc("id");
List<Customer> list = query.findList();
Customer first = list.get(0);
if (isSqlServer()) {
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
} else if (isH2()){
assertThat(sqlOf(query)).contains("for update");
} else {
assertThat(sqlOf(query)).contains("for update nowait");
}
// create a 2nd transaction to test that the
// row is locked and we can't acquire it
Transaction txn2 = server.createTransaction();
try {
logger.info("... attempt another acquire using 2nd transaction");
Query<Customer> query2 =
server.find(Customer.class)
.where().idEq(first.getId())
.forUpdateNoWait();
List<Customer> list = query.findList();
Customer first = list.get(0);
if (isSqlServer()) {
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
} else if (isH2()) {
assertThat(sqlOf(query)).contains("for update");
} else {
assertThat(sqlOf(query)).contains("for update nowait");
}
// create a 2nd transaction to test that the
// row is locked and we can't acquire it
try (Transaction txn2 = server.createTransaction()) {
logger.info("... attempt another acquire using 2nd transaction");
Query<Customer> query2 =
server.find(Customer.class)
.where().idEq(first.getId())
.forUpdateNoWait();
server.findOne(query2, txn2);
assertTrue(false); // never get here
} catch (AcquireLockException e) {
logger.info("... got AcquireLockException " + e);
} finally {
txn2.end();
}
} finally {
Ebean.endTransaction();
server.findOne(query2, txn2);
assertTrue(false); // never get here
} catch (AcquireLockException e) {
logger.info("... got AcquireLockException " + e);
}
txn.commit();
}
}
}
+4 -6
View File
@@ -4,6 +4,7 @@ import io.ebean.BaseTestCase;
import io.ebean.CacheMode;
import io.ebean.Ebean;
import io.ebean.SqlUpdate;
import io.ebean.Transaction;
import io.ebean.Update;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheManager;
@@ -253,8 +254,7 @@ public class TestCacheCollectionIds extends BaseTestCase {
ResetBasicData.reset();
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
OCachedBean cachedBean = new OCachedBean();
cachedBean.setName("helloForUpdate");
@@ -272,9 +272,7 @@ public class TestCacheCollectionIds extends BaseTestCase {
cachedBean.setName("mod2");
Ebean.save(cachedBean);
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
txn.commit();
}
@@ -340,7 +338,7 @@ public class TestCacheCollectionIds extends BaseTestCase {
Assert.assertEquals(1, rows);
// We need to notify the cache manually
Ebean.externalModification("o_order_detail",false, false, true);
Ebean.externalModification("o_order_detail", false, false, true);
// read the order from cache
Order orderFromCache = Ebean.find(Order.class, 1L);
@@ -2,17 +2,17 @@ package org.tests.iud;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.basic.EBasicVer;
import io.ebean.Transaction;
import org.junit.Assert;
import org.junit.Test;
import org.tests.model.basic.EBasicVer;
public class TestInsertUpdateTrans extends BaseTestCase {
@Test
public void test() {
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
EBasicVer e0 = new EBasicVer("onInsert");
e0.setDescription("something");
@@ -34,11 +34,7 @@ public class TestInsertUpdateTrans extends BaseTestCase {
Assert.assertEquals("onUpdate", e1.getName());
Assert.assertEquals("differentFromInsert", e1.getDescription());
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
txn.commit();
}
}
}
@@ -3,6 +3,7 @@ package org.tests.model.basic.xtra;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.SqlUpdate;
import io.ebean.Transaction;
import org.junit.Test;
import java.util.ArrayList;
@@ -11,8 +12,7 @@ public class TestDeleteUnloadedChildren extends BaseTestCase {
private void init() {
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
String sql;
SqlUpdate delete;
@@ -48,9 +48,7 @@ public class TestDeleteUnloadedChildren extends BaseTestCase {
Ebean.save(extendedParent);
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
txn.commit();
}
}
@@ -59,15 +57,12 @@ public class TestDeleteUnloadedChildren extends BaseTestCase {
init();
Ebean.beginTransaction();
try {
try (Transaction txn = Ebean.beginTransaction()) {
EdParent parent = Ebean.find(EdParent.class).where().eq("name", "MyComputer").findOne();
// // Works only if the following statement is included
// int x = parent.getChildren().size();
Ebean.delete(parent);
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
txn.commit();
}
}