Use predicates to match events.

parent 1aa579a4
...@@ -23,15 +23,14 @@ import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedE ...@@ -23,15 +23,14 @@ import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedE
import org.briarproject.bramble.api.rendezvous.event.RendezvousFailedEvent; import org.briarproject.bramble.api.rendezvous.event.RendezvousFailedEvent;
import org.briarproject.bramble.api.transport.KeyManager; import org.briarproject.bramble.api.transport.KeyManager;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.CaptureArgumentAction;
import org.briarproject.bramble.test.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
import org.briarproject.bramble.test.PredicateMatcher;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Collection; import java.util.Collection;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
...@@ -332,20 +331,14 @@ public class ContactManagerImplTest extends BrambleMockTestCase { ...@@ -332,20 +331,14 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
@Test @Test
public void testFailedStateIsNotReplaced() throws Exception { public void testFailedStateIsNotReplaced() throws Exception {
Transaction txn = new Transaction(null, true); Transaction txn = new Transaction(null, true);
AtomicReference<PendingContactStateChangedEvent> captureFirstEvent =
new AtomicReference<>();
AtomicReference<PendingContactStateChangedEvent> captureSecondEvent =
new AtomicReference<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(eventBus).broadcast(with(any( oneOf(eventBus).broadcast(with(new PredicateMatcher<>(
PendingContactStateChangedEvent.class))); PendingContactStateChangedEvent.class, e ->
will(new CaptureArgumentAction<>(captureFirstEvent, e.getPendingContactState() == ADDING_CONTACT)));
PendingContactStateChangedEvent.class, 0)); oneOf(eventBus).broadcast(with(new PredicateMatcher<>(
oneOf(eventBus).broadcast(with(any( PendingContactStateChangedEvent.class, e ->
PendingContactStateChangedEvent.class))); e.getPendingContactState() == FAILED)));
will(new CaptureArgumentAction<>(captureSecondEvent,
PendingContactStateChangedEvent.class, 0));
}}); }});
// A rendezvous connection is opened, then the pending contact expires, // A rendezvous connection is opened, then the pending contact expires,
...@@ -356,10 +349,7 @@ public class ContactManagerImplTest extends BrambleMockTestCase { ...@@ -356,10 +349,7 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
pendingContact.getId())); pendingContact.getId()));
contactManager.eventOccurred(new RendezvousConnectionClosedEvent( contactManager.eventOccurred(new RendezvousConnectionClosedEvent(
pendingContact.getId(), false)); pendingContact.getId(), false));
context.assertIsSatisfied();
assertEquals(ADDING_CONTACT,
captureFirstEvent.get().getPendingContactState());
assertEquals(FAILED, captureSecondEvent.get().getPendingContactState());
context.checking(new DbExpectations() {{ context.checking(new DbExpectations() {{
oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
......
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.Predicate;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
public class PredicateMatcher<T> extends BaseMatcher<T> {
private final Class<T> matchedClass;
private final Predicate<T> predicate;
public PredicateMatcher(Class<T> matchedClass, Predicate<T> predicate) {
this.matchedClass = matchedClass;
this.predicate = predicate;
}
@Override
public boolean matches(Object item) {
if (matchedClass.isInstance(item))
return predicate.test(matchedClass.cast(item));
return false;
}
@Override
public void describeTo(Description description) {
description.appendText("matches an item against a predicate");
}
}
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