Unit test refactoring.

This commit is contained in:
akwizgran
2011-10-07 00:08:32 +01:00
parent 8fa77b57da
commit 16c9bf7079
5 changed files with 59 additions and 61 deletions

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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) {
}
}

View File

@@ -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();
}
}
}

View File

@@ -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) {
}
}
}