Skip to content

Commit

Permalink
test: fix DbUpdateTest.insertUpdateNoCheck()
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed May 21, 2024
1 parent 2ac2c80 commit eb58de8
Showing 1 changed file with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -257,41 +259,25 @@ void insertUpdate() throws Exception {
var session = getSession();
var tm = createTransactionManagerOcc(session);
tm.execute(tranasction -> {
// insert
try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) {
int count = tranasction.executeAndGetCount(ps, insertEntity);
assertUpdateCount(1, count);
}

// update
try (var ps = session.createStatement(sql)) {
int count = tranasction.executeAndGetCount(ps);
assertUpdateCount(1, count);
try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); //
var updatePs = session.createStatement(sql)) {
// insert
int insertCount = tranasction.executeAndGetCount(insertPs, insertEntity);
assertUpdateCount(1, insertCount);
// update
int updateCount = tranasction.executeAndGetCount(updatePs);
assertUpdateCount(1, updateCount);
}

// select
try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) {
var list = tranasction.executeAndGetList(ps);
assertEquals(SIZE + 1, list.size());
for (var entity : list) {
if (entity.getFoo().equals(insertEntity.getFoo())) {
assertEquals(789L, entity.getBar());
} else {
assertEquals((long) entity.getFoo(), entity.getBar());
}
}
assertInsertUpdate(insertEntity, 1, list);
}
});

var list = selectAllFromTest();
assertEquals(SIZE + 1, list.size());
for (var entity : list) {
if (entity.getFoo().equals(insertEntity.getFoo())) {
assertEquals(789L, entity.getBar());
} else {
assertEquals((long) entity.getFoo(), entity.getBar());
}
}
assertInsertUpdate(insertEntity, 1, list);
}

@Test
Expand All @@ -302,39 +288,46 @@ void insertUpdateNoCheck() throws Exception {
+ " bar = 789" //
+ " where foo = " + insertEntity.getFoo();

var updateCount = new AtomicInteger();
var session = getSession();
var tm = createTransactionManagerOcc(session);
tm.execute(tranasction -> {
// insert
try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) {
ps.execute(tranasction, insertEntity); // not get-count
}
// executeの結果を確認せずに次のSQLを実行すると、同一トランザクション内でもSQLの実行順序が保証されないらしい

// update
try (var ps = session.createStatement(sql)) {
ps.execute(tranasction); // not get-count
try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); //
var updatePs = session.createStatement(sql)) {
// executeの結果を確認せずに次のSQLを実行すると、同一トランザクション内でもSQLの実行順序が保証されない
// insert
var insertResult = insertPs.execute(tranasction, insertEntity);
// update
var updateResult = updatePs.execute(tranasction);

updateCount.set(updateResult.getUpdateCount());
if (updateCount.get() != 1) {
LOG.info("insertUpdateNoCheck().updateCount={}", updateCount);
assertEquals(0, updateCount.get());
}
assertEquals(1, insertResult.getUpdateCount());
}

// select
try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) {
var list = tranasction.executeAndGetList(ps);
assertEquals(SIZE + 1, list.size());
for (var entity : list) {
if (entity.getFoo().equals(insertEntity.getFoo())) {
assertEquals(789L, entity.getBar());
} else {
assertEquals((long) entity.getFoo(), entity.getBar());
}
}
assertInsertUpdate(insertEntity, updateCount.get(), list);
}
});

var list = selectAllFromTest();
assertInsertUpdate(insertEntity, updateCount.get(), list);
}

private static void assertInsertUpdate(TestEntity insertEntity, int updateCount, List<TestEntity> list) {
assertEquals(SIZE + 1, list.size());
for (var entity : list) {
if (entity.getFoo().equals(insertEntity.getFoo())) {
assertEquals(789L, entity.getBar());
if (updateCount == 1) {
assertEquals(789L, entity.getBar());
} else {
assertEquals(insertEntity.getBar(), entity.getBar());
}
} else {
assertEquals((long) entity.getFoo(), entity.getBar());
}
Expand Down

0 comments on commit eb58de8

Please sign in to comment.