Remove custom DB transaction code from BriarIntegrationTest

parent 87701e5f
......@@ -62,7 +62,7 @@ public class BlogManagerIntegrationTest
blog1 = blogFactory.createBlog(author1);
rssBlog = blogFactory.createFeedBlog(rssAuthor);
withinTransaction(db0, txn -> blogManager0.addBlog(txn, rssBlog));
db0.transaction(false, txn -> blogManager0.addBlog(txn, rssBlog));
}
@Override
......@@ -330,7 +330,7 @@ public class BlogManagerIntegrationTest
cHeader = h;
}
}
assertTrue(cHeader != null);
assertNotNull(cHeader);
// another comment on the comment
String comment2 = "This is a comment on a comment.";
......
......@@ -298,20 +298,20 @@ public class IntroductionIntegrationTest
Group g1 = introductionManager0.getContactGroup(introducee1);
Group g2 = introductionManager0.getContactGroup(introducee2);
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db0, txn -> introductionManager0
db0.transactionWithResult(true, txn -> introductionManager0
.getMessageHeaders(txn, contactId1From0));
assertEquals(2, messages.size());
assertGroupCount(messageTracker0, g1.getId(), 2, 1);
messages = withinTransactionReturns(db0,
messages = db0.transactionWithResult(true,
txn -> introductionManager0.getMessageHeaders(txn, contactId2From0));
assertEquals(2, messages.size());
assertGroupCount(messageTracker0, g2.getId(), 2, 1);
messages = withinTransactionReturns(db1,
messages = db1.transactionWithResult(true,
txn -> introductionManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, messages.size());
assertGroupCount(messageTracker1, g1.getId(), 2, 1);
// introducee2 should also have the decline response of introducee1
messages = withinTransactionReturns(db2,
messages = db2.transactionWithResult(true,
txn -> introductionManager2.getMessageHeaders(txn, contactId0From2));
assertEquals(3, messages.size());
assertGroupCount(messageTracker2, g2.getId(), 3, 2);
......@@ -364,18 +364,18 @@ public class IntroductionIntegrationTest
.contactExists(author1.getId(), author2.getId()));
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db0, txn -> introductionManager0
db0.transactionWithResult(true, txn -> introductionManager0
.getMessageHeaders(txn, contactId1From0));
assertEquals(2, messages.size());
messages = withinTransactionReturns(db0,
messages = db0.transactionWithResult(true,
txn -> introductionManager0
.getMessageHeaders(txn, contactId2From0));
assertEquals(2, messages.size());
messages = withinTransactionReturns(db1,
messages = db1.transactionWithResult(true,
txn -> introductionManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(3, messages.size());
messages = withinTransactionReturns(db2,
messages = db2.transactionWithResult(true,
txn -> introductionManager2
.getMessageHeaders(txn, contactId0From2));
assertEquals(3, messages.size());
......@@ -524,19 +524,19 @@ public class IntroductionIntegrationTest
Group g1 = introductionManager0.getContactGroup(introducee1);
Group g2 = introductionManager0.getContactGroup(introducee2);
assertEquals(2, withinTransactionReturns(db0,
assertEquals(2, db0.transactionWithResult(true,
txn -> introductionManager0.getMessageHeaders(txn, contactId1From0))
.size());
assertGroupCount(messageTracker0, g1.getId(), 2, 1);
assertEquals(2, withinTransactionReturns(db0,
assertEquals(2, db0.transactionWithResult(true,
txn -> introductionManager0.getMessageHeaders(txn, contactId2From0))
.size());
assertGroupCount(messageTracker0, g2.getId(), 2, 1);
assertEquals(3, withinTransactionReturns(db1,
assertEquals(3, db1.transactionWithResult(true,
txn -> introductionManager1.getMessageHeaders(txn, contactId0From1))
.size());
assertGroupCount(messageTracker1, g1.getId(), 3, 2);
assertEquals(3, withinTransactionReturns(db2,
assertEquals(3, db2.transactionWithResult(true,
txn -> introductionManager2.getMessageHeaders(txn, contactId0From2))
.size());
assertGroupCount(messageTracker2, g2.getId(), 3, 2);
......@@ -562,7 +562,7 @@ public class IntroductionIntegrationTest
assertFalse(listener1.requestReceived);
// make really sure we don't have that request
assertTrue(withinTransactionReturns(db1,
assertTrue(db1.transactionWithResult(true,
txn -> introductionManager1.getMessageHeaders(txn, contactId0From1))
.isEmpty());
......@@ -997,7 +997,7 @@ public class IntroductionIntegrationTest
AcceptMessage m = visitor.visit(message);
// replace original response with modified one
withinTransaction(db0, txn -> {
db0.transaction(false, txn -> {
db0.removeMessage(txn, message.getMessageId());
Message msg = c0.getMessageEncoder()
.encodeAcceptMessage(m.getGroupId(), m.getTimestamp(),
......@@ -1103,22 +1103,22 @@ public class IntroductionIntegrationTest
private void assertDefaultUiMessages() throws DbException {
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db0, txn -> introductionManager0
db0.transactionWithResult(true, txn -> introductionManager0
.getMessageHeaders(txn, contactId1From0));
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = withinTransactionReturns(db0,
messages = db0.transactionWithResult(true,
txn -> introductionManager0.getMessageHeaders(txn, contactId2From0));
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = withinTransactionReturns(db1,
messages = db1.transactionWithResult(true,
txn -> introductionManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = withinTransactionReturns(db2,
messages = db2.transactionWithResult(true,
txn -> introductionManager2.getMessageHeaders(txn, contactId0From2));
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
......@@ -1302,8 +1302,8 @@ public class IntroductionIntegrationTest
private IntroductionRequest getIntroductionRequest(DatabaseComponent db,
IntroductionManager manager, ContactId contactId)
throws DbException {
Collection<PrivateMessageHeader> messages = withinTransactionReturns(db,
txn -> manager.getMessageHeaders(txn, contactId));
Collection<PrivateMessageHeader> messages = db.transactionWithResult(
true, txn -> manager.getMessageHeaders(txn, contactId));
for (PrivateMessageHeader im : messages) {
if (im instanceof IntroductionRequest) {
return (IntroductionRequest) im;
......
......@@ -31,6 +31,7 @@ import static org.briarproject.briar.api.privategroup.Visibility.VISIBLE;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationFactory.SIGNING_LABEL_INVITE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class PrivateGroupManagerIntegrationTest
......@@ -102,7 +103,7 @@ public class PrivateGroupManagerIntegrationTest
header = h;
}
}
assertTrue(header != null);
assertNotNull(header);
assertFalse(header.isRead());
assertEquals(author0, header.getAuthor());
assertEquals(time, header.getTimestamp());
......@@ -244,7 +245,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager0.getPreviousMsgId(groupId0));
// share the group with 1
withinTransaction(db0, txn -> db0.setGroupVisibility(txn,
db0.transaction(false, txn -> db0.setGroupVisibility(txn,
contactId1From0, privateGroup0.getId(), SHARED));
// author1 joins privateGroup0 with wrong timestamp
......@@ -262,7 +263,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager1.getPreviousMsgId(groupId0));
// share the group with 0
withinTransaction(db1, txn -> db1.setGroupVisibility(txn,
db1.transaction(false, txn -> db1.setGroupVisibility(txn,
contactId0From1, privateGroup0.getId(), SHARED));
// sync join messages
......@@ -296,7 +297,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager0.getPreviousMsgId(groupId0));
// share the group with 1
withinTransaction(db0, txn -> db0.setGroupVisibility(txn,
db0.transaction(false, txn -> db0.setGroupVisibility(txn,
contactId1From0, privateGroup0.getId(), SHARED));
// author1 joins privateGroup0 with wrong signature in join message
......@@ -315,7 +316,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager1.getPreviousMsgId(groupId0));
// share the group with 0
withinTransaction(db1, txn -> db1.setGroupVisibility(txn,
db1.transaction(false, txn -> db1.setGroupVisibility(txn,
contactId0From1, privateGroup0.getId(), SHARED));
// sync join messages
......@@ -391,7 +392,7 @@ public class PrivateGroupManagerIntegrationTest
addGroup();
// share the group with 2
withinTransaction(db0, txn -> db0.setGroupVisibility(txn,
db0.transaction(false, txn -> db0.setGroupVisibility(txn,
contactId2From0, privateGroup0.getId(), SHARED));
// author2 joins privateGroup0
......@@ -404,7 +405,7 @@ public class PrivateGroupManagerIntegrationTest
GroupMessage joinMsg2 = groupMessageFactory
.createJoinMessage(privateGroup0.getId(), joinTime, author2,
inviteTime, creatorSignature);
withinTransaction(db2, txn -> {
db2.transaction(false, txn -> {
groupManager2.addPrivateGroup(txn, privateGroup0, joinMsg2, false);
// share the group with 0
db2.setGroupVisibility(txn,
......@@ -440,9 +441,9 @@ public class PrivateGroupManagerIntegrationTest
}
// reveal contact relationship
withinTransaction(db1, txn -> groupManager1.relationshipRevealed(txn,
db1.transaction(false, txn -> groupManager1.relationshipRevealed(txn,
groupId0, author2.getId(), false));
withinTransaction(db2, txn -> groupManager2.relationshipRevealed(txn,
db2.transaction(false, txn -> groupManager2.relationshipRevealed(txn,
groupId0, author1.getId(), true));
// assert that contact relationship is now revealed properly
......@@ -496,7 +497,7 @@ public class PrivateGroupManagerIntegrationTest
assertFalse(groupManager1.isDissolved(groupId0));
// creator dissolves group
withinTransaction(db1,
db1.transaction(false,
txn -> groupManager1.markGroupDissolved(txn, groupId0));
// group is dissolved now
......@@ -513,7 +514,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager0.getPreviousMsgId(groupId0));
// share the group with 1
withinTransaction(db0, txn -> db0.setGroupVisibility(txn,
db0.transaction(false, txn -> db0.setGroupVisibility(txn,
contactId1From0, privateGroup0.getId(), SHARED));
// author1 joins privateGroup0
......@@ -529,7 +530,7 @@ public class PrivateGroupManagerIntegrationTest
groupManager1.addPrivateGroup(privateGroup0, joinMsg1, false);
// share the group with 0
withinTransaction(db1, txn -> db1.setGroupVisibility(txn,
db1.transaction(false, txn -> db1.setGroupVisibility(txn,
contactId0From1, privateGroup0.getId(), SHARED));
assertEquals(joinMsg1.getMessage().getId(),
groupManager1.getPreviousMsgId(groupId0));
......
......@@ -92,7 +92,7 @@ public class GroupInvitationIntegrationTest
assertFalse(item.isSubscribed());
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db1, txn -> groupInvitationManager1
db1.transactionWithResult(true, txn -> groupInvitationManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(1, messages.size());
GroupInvitationRequest request =
......@@ -119,7 +119,7 @@ public class GroupInvitationIntegrationTest
.respondToInvitation(contactId0From1, privateGroup0, false);
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db1, txn -> groupInvitationManager1
db1.transactionWithResult(true, txn -> groupInvitationManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(2, messages.size());
boolean foundResponse = false;
......@@ -136,7 +136,7 @@ public class GroupInvitationIntegrationTest
sync1To0(1, true);
messages = withinTransactionReturns(db0, txn -> groupInvitationManager0
messages = db0.transactionWithResult(true, txn -> groupInvitationManager0
.getMessageHeaders(txn, contactId1From0));
assertEquals(2, messages.size());
foundResponse = false;
......@@ -169,7 +169,7 @@ public class GroupInvitationIntegrationTest
.respondToInvitation(contactId0From1, privateGroup0, true);
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db1, txn -> groupInvitationManager1
db1.transactionWithResult(true, txn -> groupInvitationManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(2, messages.size());
boolean foundResponse = false;
......@@ -190,7 +190,7 @@ public class GroupInvitationIntegrationTest
sync1To0(1, true);
messages = withinTransactionReturns(db1, txn -> groupInvitationManager0
messages = db1.transactionWithResult(true, txn -> groupInvitationManager0
.getMessageHeaders(txn, contactId1From0));
assertEquals(2, messages.size());
foundResponse = false;
......@@ -226,7 +226,7 @@ public class GroupInvitationIntegrationTest
// 1 has one unread message
Group g0 = groupInvitationManager1.getContactGroup(contact0From1);
assertGroupCount(messageTracker1, g0.getId(), 1, 1, timestamp);
PrivateMessageHeader m = withinTransactionReturns(db1,
PrivateMessageHeader m = db1.transactionWithResult(true,
txn -> groupInvitationManager1.getMessageHeaders(txn, contactId0From1)
.iterator().next());
......
......@@ -35,6 +35,7 @@ import static org.briarproject.briar.test.BriarTestUtils.assertGroupCount;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
......@@ -145,8 +146,9 @@ public class BlogSharingIntegrationTest
assertTrue(blogManager1.getBlogs().contains(blog2));
// invitee has one invitation message from sharer
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
txn -> blogSharingManager1.getMessageHeaders(txn, contactId0From1));
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> blogSharingManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the message
for (PrivateMessageHeader m : list) {
......@@ -166,8 +168,8 @@ public class BlogSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, withinTransactionReturns(db0,
txn -> blogSharingManager0.getMessageHeaders(txn, contactId1From0))
assertEquals(2, db0.transactionWithResult(true, txn ->
blogSharingManager0.getMessageHeaders(txn, contactId1From0))
.size());
// blog can not be shared again
assertFalse(blogSharingManager0.canBeShared(blog2.getId(),
......@@ -218,7 +220,7 @@ public class BlogSharingIntegrationTest
assertTrue(blogManager1.getBlogs().contains(rssBlog));
// invitee has one invitation message from sharer
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> blogSharingManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the message
......@@ -239,7 +241,7 @@ public class BlogSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, withinTransactionReturns(db0,
assertEquals(2, db0.transactionWithResult(true,
txn -> blogSharingManager0.getMessageHeaders(txn, contactId1From0))
.size());
// blog can not be shared again
......@@ -280,7 +282,7 @@ public class BlogSharingIntegrationTest
assertEquals(0, blogSharingManager1.getInvitations().size());
// invitee has one invitation message from sharer and one response
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> blogSharingManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, list.size());
// check things are alright with the message
......@@ -291,7 +293,7 @@ public class BlogSharingIntegrationTest
assertTrue(invitation.wasAnswered());
assertEquals(blog2.getAuthor().getName(),
invitation.getName());
assertEquals(null, invitation.getMessage());
assertNull(invitation.getMessage());
} else {
BlogInvitationResponse response = (BlogInvitationResponse) m;
assertEquals(blog2.getId(), response.getShareableId());
......@@ -300,7 +302,7 @@ public class BlogSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, withinTransactionReturns(db0,
assertEquals(2, db0.transactionWithResult(true,
txn -> blogSharingManager0.getMessageHeaders(txn, contactId1From0))
.size());
// blog can be shared again
......@@ -388,7 +390,7 @@ public class BlogSharingIntegrationTest
// make sure 1 knows that they have blog2 already
Collection<PrivateMessageHeader> messages =
withinTransactionReturns(db1, txn -> blogSharingManager1
db1.transactionWithResult(true, txn -> blogSharingManager1
.getMessageHeaders(txn, contactId0From1));
assertEquals(2, messages.size());
assertEquals(blog2, blogManager1.getBlog(blog2.getId()));
......
......@@ -39,6 +39,7 @@ import static org.briarproject.briar.api.forum.ForumSharingManager.CLIENT_ID;
import static org.briarproject.briar.api.forum.ForumSharingManager.MAJOR_VERSION;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ForumSharingIntegrationTest
......@@ -128,7 +129,7 @@ public class ForumSharingIntegrationTest
assertEquals(1, forumManager1.getForums().size());
// invitee has one invitation message from sharer
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> forumSharingManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the forum message
......@@ -148,7 +149,7 @@ public class ForumSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, withinTransactionReturns(db0, txn ->
assertEquals(2, db0.transactionWithResult(true, txn ->
forumSharingManager0.getMessageHeaders(txn, contactId1From0)).size());
// forum can not be shared again
Contact c1 = contactManager0.getContact(contactId1From0);
......@@ -184,7 +185,7 @@ public class ForumSharingIntegrationTest
assertEquals(0, forumSharingManager1.getInvitations().size());
// invitee has one invitation message from sharer and one response
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> forumSharingManager1.getMessageHeaders(txn, contactId0From1));
assertEquals(2, list.size());
// check things are alright with the forum message
......@@ -194,7 +195,7 @@ public class ForumSharingIntegrationTest
assertEquals(forum0, invitation.getNameable());
assertTrue(invitation.wasAnswered());
assertEquals(forum0.getName(), invitation.getName());
assertEquals(null, invitation.getMessage());
assertNull(invitation.getMessage());
assertFalse(invitation.canBeOpened());
} else {
ForumInvitationResponse response = (ForumInvitationResponse) m;
......@@ -204,7 +205,7 @@ public class ForumSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, withinTransactionReturns(db0, txn ->
assertEquals(2, db0.transactionWithResult(true, txn ->
forumSharingManager0.getMessageHeaders(txn, contactId1From0)).size());
// forum can be shared again
Contact c1 = contactManager0.getContact(contactId1From0);
......@@ -446,7 +447,7 @@ public class ForumSharingIntegrationTest
listenToEvents(true);
// invitee adds the same forum
withinTransaction(db1, txn -> forumManager1.addForum(txn, forum0));
db1.transaction(false, txn -> forumManager1.addForum(txn, forum0));
// send invitation
forumSharingManager0
......@@ -478,10 +479,10 @@ public class ForumSharingIntegrationTest
.contains(contact0From1));
// and both have each other's invitations (and no response)
assertEquals(2, withinTransactionReturns(db0, txn ->
assertEquals(2, db0.transactionWithResult(true, txn ->
forumSharingManager0.getMessageHeaders(txn, contactId1From0))
.size());
assertEquals(2, withinTransactionReturns(db1, txn ->
assertEquals(2, db1.transactionWithResult(true, txn ->
forumSharingManager1.getMessageHeaders(txn, contactId0From1))
.size());
......@@ -558,7 +559,7 @@ public class ForumSharingIntegrationTest
@Test
public void testTwoContactsShareSameForum() throws Exception {
// second sharer adds the same forum
withinTransaction(db2, txn -> db2.addGroup(txn, forum0.getGroup()));
db2.transaction(false, txn -> db2.addGroup(txn, forum0.getGroup()));
// add listeners
listener0 = new SharerListener(true);
......@@ -576,7 +577,7 @@ public class ForumSharingIntegrationTest
sync0To1(1, true);
// second sharer sends invitation for same forum
assertTrue(contactId1From2 != null);
assertNotNull(contactId1From2);
forumSharingManager2
.sendInvitation(forum0.getId(), contactId1From2, null,
clock.currentTimeMillis());
......@@ -735,7 +736,7 @@ public class ForumSharingIntegrationTest
// get invitation MessageId for later
MessageId invitationId = null;
Collection<PrivateMessageHeader> list = withinTransactionReturns(db1,
Collection<PrivateMessageHeader> list = db1.transactionWithResult(true,
txn -> forumSharingManager1.getMessageHeaders(txn, contactId0From1));
for (PrivateMessageHeader m : list) {
if (m instanceof ForumInvitationRequest) {
......
......@@ -2,7 +2,6 @@ package org.briarproject.briar.test;
import net.jodah.concurrentunit.Waiter;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.client.ContactGroupFactory;
import org.briarproject.bramble.api.contact.Contact;
......@@ -11,7 +10,6 @@ import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventListener;
import org.briarproject.bramble.api.identity.AuthorFactory;
......@@ -379,40 +377,4 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
contactManager2.removeContact(contactId1From2);
}
@FunctionalInterface
protected interface TransactionScope {
void execute(Transaction txn) throws DbException, FormatException;
}
protected void withinTransaction(DatabaseComponent db,
TransactionScope scope) throws DbException {
Transaction txn = db.startTransaction(false);
try {
scope.execute(txn);
db.commitTransaction(txn);
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
}
@FunctionalInterface
protected interface TransactionResultScope<R> {
R execute(Transaction txn) throws DbException;
}
protected <R> R withinTransactionReturns(DatabaseComponent db,
TransactionResultScope<R> scope) throws DbException {
Transaction txn = db.startTransaction(false);
R r;
try {
r = scope.execute(txn);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return r;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment