From effa5c9d8eb46aa924ed4185f40905fd6da0d2a0 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Tue, 15 Nov 2011 09:51:51 +0000
Subject: [PATCH] Throw checked exception if concurrent attempts are made to
 remove a contact.

---
 .../sf/briar/db/DatabaseComponentImpl.java    |  1 +
 .../sf/briar/db/DatabaseComponentTest.java    | 23 ++++++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/components/net/sf/briar/db/DatabaseComponentImpl.java b/components/net/sf/briar/db/DatabaseComponentImpl.java
index a437dde9a5..e9947db4bf 100644
--- a/components/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/components/net/sf/briar/db/DatabaseComponentImpl.java
@@ -1229,6 +1229,7 @@ DatabaseCleaner.Callback {
 		if(LOG.isLoggable(Level.FINE)) LOG.fine("Removing contact " + c);
 		contactLock.writeLock().lock();
 		try {
+			if(!containsContact(c)) throw new NoSuchContactException();
 			messageLock.writeLock().lock();
 			try {
 				messageFlagLock.writeLock().lock();
diff --git a/test/net/sf/briar/db/DatabaseComponentTest.java b/test/net/sf/briar/db/DatabaseComponentTest.java
index 59e0e9fe12..335d11d711 100644
--- a/test/net/sf/briar/db/DatabaseComponentTest.java
+++ b/test/net/sf/briar/db/DatabaseComponentTest.java
@@ -183,6 +183,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			oneOf(database).setConnectionWindow(txn, contactId, remoteIndex,
 					connectionWindow);
 			// removeContact(contactId)
+			oneOf(database).containsContact(txn, contactId);
+			will(returnValue(true));
 			oneOf(database).removeContact(txn, contactId);
 			oneOf(listener).eventOccurred(with(any(ContactRemovedEvent.class)));
 			// close()
@@ -502,11 +504,11 @@ public abstract class DatabaseComponentTest extends TestCase {
 			context.mock(TransportUpdate.class);
 		context.checking(new Expectations() {{
 			// Check whether the contact is still in the DB (which it's not)
-			exactly(17).of(database).startTransaction();
+			exactly(20).of(database).startTransaction();
 			will(returnValue(txn));
-			exactly(17).of(database).containsContact(txn, contactId);
+			exactly(20).of(database).containsContact(txn, contactId);
 			will(returnValue(false));
-			exactly(17).of(database).commitTransaction(txn);
+			exactly(20).of(database).commitTransaction(txn);
 		}});
 		DatabaseComponent db = createDatabaseComponent(database, cleaner);
 
@@ -546,11 +548,21 @@ public abstract class DatabaseComponentTest extends TestCase {
 			fail();
 		} catch(NoSuchContactException expected) {}
 
+		try {
+			db.getConnectionNumber(contactId, remoteIndex);
+			fail();
+		} catch(NoSuchContactException expected) {}
+
 		try {
 			db.getConnectionWindow(contactId, remoteIndex);
 			fail();
 		} catch(NoSuchContactException expected) {}
 
+		try {
+			db.getRemoteIndex(contactId, transportId);
+			fail();
+		} catch(NoSuchContactException expected) {}
+
 		try {
 			db.getSharedSecret(contactId);
 			fail();
@@ -586,6 +598,11 @@ public abstract class DatabaseComponentTest extends TestCase {
 			fail();
 		} catch(NoSuchContactException expected) {}
 
+		try {
+			db.removeContact(contactId);
+			fail();
+		} catch(NoSuchContactException expected) {}
+
 		try {
 			db.setConnectionWindow(contactId, remoteIndex, null);
 			fail();
-- 
GitLab