From 16c9bf70796ce79f5e233116ca83abe7a065e45a Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 7 Oct 2011 00:08:32 +0100 Subject: [PATCH] Unit test refactoring. --- .../plugins/bluetooth/BluetoothPlugin.java | 14 ++---- .../sf/briar/plugins/ImmediateExecutor.java | 10 ++++ .../sf/briar/plugins/StubStreamCallback.java | 39 +++++++++++++++ .../file/RemovableDrivePluginTest.java | 8 +-- .../socket/SimpleSocketPluginTest.java | 49 ++----------------- 5 files changed, 59 insertions(+), 61 deletions(-) create mode 100644 test/net/sf/briar/plugins/ImmediateExecutor.java create mode 100644 test/net/sf/briar/plugins/StubStreamCallback.java diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java index a2a6e3083a..c79d2e5906 100644 --- a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java +++ b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java @@ -31,6 +31,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin { private final long pollingInterval; private StreamTransportCallback callback = null; + private LocalDevice localDevice = null; private StreamConnectionNotifier streamConnectionNotifier = null; BluetoothPlugin(Executor executor, String uuid, long pollingInterval) { @@ -46,10 +47,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin { public synchronized void start(Map<String, String> localProperties, Map<ContactId, Map<String, String>> remoteProperties, Map<String, String> config, StreamTransportCallback callback) - throws InvalidPropertiesException, InvalidConfigException, - IOException { + throws InvalidPropertiesException, InvalidConfigException, IOException { super.start(localProperties, remoteProperties, config); this.callback = callback; + // Initialise the Bluetooth stack + localDevice = LocalDevice.getLocalDevice(); executor.execute(createBinder()); } @@ -73,14 +75,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin { synchronized(this) { if(!started) return; } - // Initialise the Bluetooth stack - LocalDevice localDevice = null; - try { - localDevice = LocalDevice.getLocalDevice(); - } catch(BluetoothStateException e) { - // FIXME: Logging - return; - } // Try to make the device discoverable (requires root on Linux) try { localDevice.setDiscoverable(DiscoveryAgent.GIAC); diff --git a/test/net/sf/briar/plugins/ImmediateExecutor.java b/test/net/sf/briar/plugins/ImmediateExecutor.java new file mode 100644 index 0000000000..fe21666e06 --- /dev/null +++ b/test/net/sf/briar/plugins/ImmediateExecutor.java @@ -0,0 +1,10 @@ +package net.sf.briar.plugins; + +import java.util.concurrent.Executor; + +public class ImmediateExecutor implements Executor { + + public void execute(Runnable r) { + r.run(); + } +} diff --git a/test/net/sf/briar/plugins/StubStreamCallback.java b/test/net/sf/briar/plugins/StubStreamCallback.java new file mode 100644 index 0000000000..d39b32cd45 --- /dev/null +++ b/test/net/sf/briar/plugins/StubStreamCallback.java @@ -0,0 +1,39 @@ +package net.sf.briar.plugins; + +import java.util.Map; + +import net.sf.briar.api.ContactId; +import net.sf.briar.api.transport.stream.StreamTransportCallback; +import net.sf.briar.api.transport.stream.StreamTransportConnection; + +public class StubStreamCallback implements StreamTransportCallback { + + public Map<String, String> localProperties = null; + public volatile int incomingConnections = 0; + + public void setLocalProperties(Map<String, String> properties) { + localProperties = properties; + } + + public void setConfig(Map<String, String> config) { + } + + public void showMessage(String... message) { + } + + public boolean showConfirmationMessage(String... message) { + return false; + } + + public int showChoice(String[] choices, String... message) { + return -1; + } + + public void incomingConnectionCreated(StreamTransportConnection c) { + incomingConnections++; + } + + public void outgoingConnectionCreated(ContactId contactId, + StreamTransportConnection c) { + } +} diff --git a/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java b/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java index 2bede14174..02f42665cd 100644 --- a/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java +++ b/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java @@ -17,6 +17,7 @@ import net.sf.briar.api.ContactId; import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.batch.BatchTransportCallback; import net.sf.briar.api.transport.batch.BatchTransportWriter; +import net.sf.briar.plugins.ImmediateExecutor; import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback; import org.jmock.Expectations; @@ -407,11 +408,4 @@ public class RemovableDrivePluginTest extends TestCase { public void tearDown() { TestUtils.deleteTestDirectory(testDir); } - - private static class ImmediateExecutor implements Executor { - - public void execute(Runnable r) { - r.run(); - } - } } diff --git a/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java b/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java index 6093a7e360..e94b206091 100644 --- a/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java +++ b/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java @@ -8,14 +8,14 @@ import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import junit.framework.TestCase; import net.sf.briar.api.ContactId; -import net.sf.briar.api.transport.stream.StreamTransportCallback; import net.sf.briar.api.transport.stream.StreamTransportConnection; +import net.sf.briar.plugins.ImmediateExecutor; +import net.sf.briar.plugins.StubStreamCallback; import org.junit.Before; import org.junit.Test; @@ -38,7 +38,7 @@ public class SimpleSocketPluginTest extends TestCase { @Test public void testIncomingConnection() throws Exception { - StubCallback callback = new StubCallback(); + StubStreamCallback callback = new StubStreamCallback(); localProperties.put("host", "127.0.0.1"); localProperties.put("port", "0"); SimpleSocketPlugin plugin = @@ -74,7 +74,7 @@ public class SimpleSocketPluginTest extends TestCase { @Test public void testOutgoingConnection() throws Exception { - StubCallback callback = new StubCallback(); + StubStreamCallback callback = new StubStreamCallback(); SimpleSocketPlugin plugin = new SimpleSocketPlugin(new ImmediateExecutor(), 10); plugin.start(localProperties, remoteProperties, config, callback); @@ -114,7 +114,7 @@ public class SimpleSocketPluginTest extends TestCase { @Test public void testUpdatingPropertiesReopensSocket() throws Exception { - StubCallback callback = new StubCallback(); + StubStreamCallback callback = new StubStreamCallback(); localProperties.put("host", "127.0.0.1"); localProperties.put("port", "0"); SimpleSocketPlugin plugin = @@ -169,43 +169,4 @@ public class SimpleSocketPluginTest extends TestCase { fail(); } catch(IOException expected) {} } - - private static class ImmediateExecutor implements Executor { - - public void execute(Runnable r) { - r.run(); - } - } - - private static class StubCallback implements StreamTransportCallback { - - private Map<String, String> localProperties = null; - private volatile int incomingConnections = 0; - - public void setLocalProperties(Map<String, String> properties) { - localProperties = properties; - } - - public void setConfig(Map<String, String> config) { - } - - public void showMessage(String... message) { - } - - public boolean showConfirmationMessage(String... message) { - return false; - } - - public int showChoice(String[] choices, String... message) { - return -1; - } - - public void incomingConnectionCreated(StreamTransportConnection c) { - incomingConnections++; - } - - public void outgoingConnectionCreated(ContactId contactId, - StreamTransportConnection c) { - } - } } -- GitLab