No need to use @Before to create stateful test objects.

This commit is contained in:
akwizgran
2022-06-20 16:24:55 +01:00
parent 0b93af5d71
commit 05bf3833cf
13 changed files with 34 additions and 97 deletions

View File

@@ -17,7 +17,6 @@ import org.briarproject.bramble.api.identity.LocalAuthor;
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.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Collection; import java.util.Collection;
@@ -63,13 +62,9 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
private final long timestamp = System.currentTimeMillis(); private final long timestamp = System.currentTimeMillis();
private final boolean alice = new Random().nextBoolean(); private final boolean alice = new Random().nextBoolean();
private ContactManagerImpl contactManager; private final ContactManagerImpl contactManager =
new ContactManagerImpl(db, keyManager, identityManager,
@Before pendingContactFactory);
public void setUp() {
contactManager = new ContactManagerImpl(db, keyManager,
identityManager, pendingContactFactory);
}
@Test @Test
public void testAddContact() throws Exception { public void testAddContact() throws Exception {

View File

@@ -2,7 +2,6 @@ package org.briarproject.bramble.data;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleTestCase;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -17,14 +16,8 @@ import static org.junit.Assert.assertArrayEquals;
public class BdfWriterImplTest extends BrambleTestCase { public class BdfWriterImplTest extends BrambleTestCase {
private ByteArrayOutputStream out = null; private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private BdfWriterImpl w = null; private final BdfWriterImpl w = new BdfWriterImpl(out);
@Before
public void setUp() {
out = new ByteArrayOutputStream();
w = new BdfWriterImpl(out);
}
@Test @Test
public void testWriteNull() throws IOException { public void testWriteNull() throws IOException {

View File

@@ -14,7 +14,6 @@ import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
@@ -41,13 +40,8 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
private final KeyPair handshakeKeyPair = private final KeyPair handshakeKeyPair =
new KeyPair(handshakePublicKey, handshakePrivateKey); new KeyPair(handshakePublicKey, handshakePrivateKey);
private IdentityManagerImpl identityManager; private final IdentityManagerImpl identityManager =
new IdentityManagerImpl(db, crypto, authorFactory, clock);
@Before
public void setUp() {
identityManager =
new IdentityManagerImpl(db, crypto, authorFactory, clock);
}
@Test @Test
public void testOpenDatabaseIdentityRegistered() throws Exception { public void testOpenDatabaseIdentityRegistered() throws Exception {

View File

@@ -10,7 +10,6 @@ import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@@ -34,12 +33,8 @@ public class LifecycleManagerImplTest extends BrambleMockTestCase {
private final SecretKey dbKey = getSecretKey(); private final SecretKey dbKey = getSecretKey();
private LifecycleManagerImpl lifecycleManager; private final LifecycleManagerImpl lifecycleManager =
new LifecycleManagerImpl(db, eventBus, clock);
@Before
public void setUp() {
lifecycleManager = new LifecycleManagerImpl(db, eventBus, clock);
}
@Test @Test
public void testOpenDatabaseHooksAreCalledAtStartup() throws Exception { public void testOpenDatabaseHooksAreCalledAtStartup() throws Exception {

View File

@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.mailbox.MailboxFolderId;
import org.briarproject.bramble.api.mailbox.MailboxProperties; import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.briarproject.bramble.api.mailbox.MailboxConstants.CLIENT_SUPPORTS; import static org.briarproject.bramble.api.mailbox.MailboxConstants.CLIENT_SUPPORTS;
@@ -34,13 +33,9 @@ public class ContactMailboxClientTest extends BrambleMockTestCase {
requireNonNull(properties.getOutboxId()); requireNonNull(properties.getOutboxId());
private final ContactId contactId = getContactId(); private final ContactId contactId = getContactId();
private ContactMailboxClient client; private final ContactMailboxClient client =
new ContactMailboxClient(workerFactory, connectivityChecker,
@Before reachabilityMonitor);
public void setUp() {
client = new ContactMailboxClient(workerFactory, connectivityChecker,
reachabilityMonitor);
}
@Test @Test
public void testStartAndDestroyWithNoContactsAssigned() { public void testStartAndDestroyWithNoContactsAssigned() {

View File

@@ -12,7 +12,6 @@ import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.CaptureArgumentAction; import org.briarproject.bramble.test.CaptureArgumentAction;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.lib.action.DoAllAction; import org.jmock.lib.action.DoAllAction;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@@ -38,13 +37,9 @@ public class TorReachabilityMonitorImplTest extends BrambleMockTestCase {
private final TorReachabilityObserver observer = private final TorReachabilityObserver observer =
context.mock(TorReachabilityObserver.class); context.mock(TorReachabilityObserver.class);
private TorReachabilityMonitorImpl monitor; private final TorReachabilityMonitorImpl monitor =
new TorReachabilityMonitorImpl(ioExecutor, taskScheduler,
@Before pluginManager, eventBus);
public void setUp() {
monitor = new TorReachabilityMonitorImpl(ioExecutor, taskScheduler,
pluginManager, eventBus);
}
@Test @Test
public void testSchedulesTaskWhenStartedIfTorIsActive() { public void testSchedulesTaskWhenStartedIfTorIsActive() {

View File

@@ -26,7 +26,6 @@ import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.bramble.test.RunAction; import org.briarproject.bramble.test.RunAction;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.imposters.ByteBuddyClassImposteriser; import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.security.SecureRandom; import java.security.SecureRandom;
@@ -59,22 +58,18 @@ public class PollerImplTest extends BrambleMockTestCase {
private final SecureRandom random; private final SecureRandom random;
private final Executor ioExecutor = new ImmediateExecutor(); private final Executor ioExecutor = new ImmediateExecutor();
private final Executor wakefulIoExecutor = new ImmediateExecutor();
private final TransportId transportId = getTransportId(); private final TransportId transportId = getTransportId();
private final ContactId contactId = getContactId(); private final ContactId contactId = getContactId();
private final TransportProperties properties = new TransportProperties(); private final TransportProperties properties = new TransportProperties();
private final int pollingInterval = 60 * 1000; private final int pollingInterval = 60 * 1000;
private final long now = System.currentTimeMillis(); private final long now = System.currentTimeMillis();
private PollerImpl poller; private final PollerImpl poller;
public PollerImplTest() { public PollerImplTest() {
context.setImposteriser(ByteBuddyClassImposteriser.INSTANCE); context.setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
random = context.mock(SecureRandom.class); random = context.mock(SecureRandom.class);
} Executor wakefulIoExecutor = new ImmediateExecutor();
@Before
public void setUp() {
poller = new PollerImpl(ioExecutor, wakefulIoExecutor, scheduler, poller = new PollerImpl(ioExecutor, wakefulIoExecutor, scheduler,
connectionManager, connectionRegistry, pluginManager, connectionManager, connectionRegistry, pluginManager,
transportPropertyManager, random, clock); transportPropertyManager, random, clock);

View File

@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.settings.Settings; import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleTestCase;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@@ -46,13 +45,11 @@ public class LanTcpPluginTest extends BrambleTestCase {
private final Backoff backoff = new TestBackoff(); private final Backoff backoff = new TestBackoff();
private final ExecutorService ioExecutor = newCachedThreadPool(); private final ExecutorService ioExecutor = newCachedThreadPool();
private final Callback callback = new Callback();
private Callback callback = null; private final LanTcpPlugin plugin;
private LanTcpPlugin plugin = null;
@Before public LanTcpPluginTest() {
public void setUp() {
callback = new Callback();
plugin = new LanTcpPlugin(ioExecutor, ioExecutor, backoff, callback, plugin = new LanTcpPlugin(ioExecutor, ioExecutor, backoff, callback,
0, 0, 1000) { 0, 0, 1000) {
@Override @Override

View File

@@ -34,7 +34,6 @@ import org.briarproject.bramble.test.DbExpectations;
import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.bramble.test.PredicateMatcher; import org.briarproject.bramble.test.PredicateMatcher;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Random; import java.util.Random;
@@ -93,14 +92,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
getTransportProperties(3); getTransportProperties(3);
private final boolean alice = new Random().nextBoolean(); private final boolean alice = new Random().nextBoolean();
private RendezvousPollerImpl rendezvousPoller; private final RendezvousPollerImpl rendezvousPoller =
new RendezvousPollerImpl(ioExecutor, scheduler, db,
@Before identityManager, transportCrypto, rendezvousCrypto,
public void setUp() { pluginManager, connectionManager, eventBus, clock);
rendezvousPoller = new RendezvousPollerImpl(ioExecutor, scheduler, db,
identityManager, transportCrypto, rendezvousCrypto,
pluginManager, connectionManager, eventBus, clock);
}
@Test @Test
public void testAddsPendingContactsAndSchedulesPollingAtStartup() public void testAddsPendingContactsAndSchedulesPollingAtStartup()

View File

@@ -14,7 +14,6 @@ import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.api.sync.Versions; import org.briarproject.bramble.api.sync.Versions;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -44,12 +43,8 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
context.mock(MessageFactory.class); context.mock(MessageFactory.class);
private final RecordReader recordReader = context.mock(RecordReader.class); private final RecordReader recordReader = context.mock(RecordReader.class);
private SyncRecordReader reader; private final SyncRecordReader reader =
new SyncRecordReaderImpl(messageFactory, recordReader);
@Before
public void setUp() {
reader = new SyncRecordReaderImpl(messageFactory, recordReader);
}
@Test @Test
public void testNoFormatExceptionIfAckIsMaximumSize() throws Exception { public void testNoFormatExceptionIfAckIsMaximumSize() throws Exception {

View File

@@ -20,7 +20,6 @@ import org.briarproject.bramble.api.sync.validation.MessageValidator;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.bramble.test.ImmediateExecutor;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@@ -70,11 +69,10 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
private final MessageContext validResultWithDependencies = private final MessageContext validResultWithDependencies =
new MessageContext(metadata, singletonList(messageId1)); new MessageContext(metadata, singletonList(messageId1));
private ValidationManagerImpl vm; private final ValidationManagerImpl vm =
new ValidationManagerImpl(db, dbExecutor, validationExecutor);
@Before public ValidationManagerImplTest() {
public void setUp() {
vm = new ValidationManagerImpl(db, dbExecutor, validationExecutor);
vm.registerMessageValidator(clientId, majorVersion, validator); vm.registerMessageValidator(clientId, majorVersion, validator);
vm.registerIncomingMessageHook(clientId, majorVersion, hook); vm.registerIncomingMessageHook(clientId, majorVersion, hook);
} }

View File

@@ -22,7 +22,6 @@ import org.hamcrest.Description;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.api.Action; import org.jmock.api.Action;
import org.jmock.api.Invocation; import org.jmock.api.Invocation;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
@@ -72,13 +71,9 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
private final SecretKey rootKey = getSecretKey(); private final SecretKey rootKey = getSecretKey();
private final Random random = new Random(); private final Random random = new Random();
private TransportKeyManager transportKeyManager; private final TransportKeyManager transportKeyManager =
new TransportKeyManagerImpl(db, transportCrypto, dbExecutor,
@Before scheduler, clock, transportId, maxLatency);
public void setUp() {
transportKeyManager = new TransportKeyManagerImpl(db, transportCrypto,
dbExecutor, scheduler, clock, transportId, maxLatency);
}
@Test @Test
public void testKeysAreUpdatedAtStartup() throws Exception { public void testKeysAreUpdatedAtStartup() throws Exception {

View File

@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@@ -25,12 +24,8 @@ public class ModemPluginTest extends BrambleMockTestCase {
private final PluginCallback callback = context.mock(PluginCallback.class); private final PluginCallback callback = context.mock(PluginCallback.class);
private final Modem modem = context.mock(Modem.class); private final Modem modem = context.mock(Modem.class);
private ModemPlugin plugin; private final ModemPlugin plugin =
new ModemPlugin(modemFactory, serialPortList, callback, 0);
@Before
public void setUp() {
plugin = new ModemPlugin(modemFactory, serialPortList, callback, 0);
}
@Test @Test
public void testModemCreation() throws Exception { public void testModemCreation() throws Exception {