Plugins don't need their own copies of configs and properties.

This commit is contained in:
akwizgran
2011-10-12 16:52:39 +01:00
parent 631f4e74b5
commit 3a07d1b882
14 changed files with 226 additions and 311 deletions

View File

@@ -24,22 +24,18 @@ public class BluetoothClientTest {
System.exit(1);
}
ContactId contactId = new ContactId(0);
TransportProperties localProperties = new TransportProperties();
Map<ContactId, TransportProperties> remoteProperties =
new HashMap<ContactId, TransportProperties>();
TransportConfig config = new TransportConfig();
StreamTransportCallback callback = new ClientCallback();
ClientCallback callback = new ClientCallback();
// Store the server's Bluetooth address and UUID
TransportProperties p = new TransportProperties();
p.put("address", args[0]);
p.put("uuid", BluetoothServerTest.UUID);
remoteProperties.put(contactId, p);
callback.remote.put(contactId, p);
// Create the plugin
BluetoothPlugin plugin =
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
// Start the plugin
System.out.println("Starting plugin");
plugin.start(localProperties, remoteProperties, config);
plugin.start();
// Try to connect to the server
System.out.println("Creating connection");
StreamTransportConnection conn = plugin.createConnection(contactId);
@@ -66,19 +62,40 @@ public class BluetoothClientTest {
private static class ClientCallback implements StreamTransportCallback {
public void setLocalProperties(TransportProperties p) {}
private TransportConfig config = new TransportConfig();
private TransportProperties local = new TransportProperties();
private Map<ContactId, TransportProperties> remote =
new HashMap<ContactId, TransportProperties>();
public void setConfig(TransportConfig c) {}
public TransportConfig getConfig() {
return config;
}
public void showMessage(String... message) {}
public TransportProperties getLocalProperties() {
return local;
}
public Map<ContactId, TransportProperties> getRemoteProperties() {
return remote;
}
public void setConfig(TransportConfig c) {
config = c;
}
public void setLocalProperties(TransportProperties p) {
local = p;
}
public int showChoice(String[] options, String... message) {
return -1;
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public int showChoice(String[] choices, String... message) {
return -1;
}
public void showMessage(String... message) {}
public void incomingConnectionCreated(StreamTransportConnection c) {}

View File

@@ -2,7 +2,7 @@ package net.sf.briar.plugins.bluetooth;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
@@ -21,19 +21,15 @@ public class BluetoothServerTest {
public static final String CHALLENGE = "Potatoes!";
public static void main(String[] args) throws Exception {
TransportProperties localProperties = new TransportProperties();
Map<ContactId, TransportProperties> remoteProperties =
Collections.emptyMap();
TransportConfig config = new TransportConfig();
StreamTransportCallback callback = new ServerCallback();
ServerCallback callback = new ServerCallback();
// Store the UUID
config.put("uuid", UUID);
callback.config.put("uuid", UUID);
// Create the plugin
BluetoothPlugin plugin =
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
// Start the plugin
System.out.println("Starting plugin");
plugin.start(localProperties, remoteProperties, config);
plugin.start();
// Wait for a connection
System.out.println("Waiting for connection");
synchronized(callback) {
@@ -46,19 +42,40 @@ public class BluetoothServerTest {
private static class ServerCallback implements StreamTransportCallback {
public void setLocalProperties(TransportProperties p) {}
private TransportConfig config = new TransportConfig();
private TransportProperties local = new TransportProperties();
private Map<ContactId, TransportProperties> remote =
new HashMap<ContactId, TransportProperties>();
public void setConfig(TransportConfig c) {}
public TransportConfig getConfig() {
return config;
}
public void showMessage(String... message) {}
public TransportProperties getLocalProperties() {
return local;
}
public Map<ContactId, TransportProperties> getRemoteProperties() {
return remote;
}
public void setConfig(TransportConfig c) {
config = c;
}
public void setLocalProperties(TransportProperties p) {
local = p;
}
public int showChoice(String[] options, String... message) {
return -1;
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public int showChoice(String[] choices, String... message) {
return -1;
}
public void showMessage(String... message) {}
public void incomingConnectionCreated(StreamTransportConnection conn) {
System.out.println("Connection received");

View File

@@ -5,16 +5,12 @@ import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.BatchTransportCallback;
import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.TransportConstants;
@@ -32,16 +28,8 @@ public class RemovableDrivePluginTest extends TestCase {
private final File testDir = TestUtils.getTestDirectory();
private final ContactId contactId = new ContactId(0);
private TransportProperties localProperties = null;
private Map<ContactId, TransportProperties> remoteProperties = null;
private TransportConfig config = null;
@Before
public void setUp() {
localProperties = new TransportProperties();
remoteProperties = new HashMap<ContactId, TransportProperties>();
remoteProperties.put(contactId, new TransportProperties());
config = new TransportConfig();
testDir.mkdirs();
}
@@ -86,7 +74,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -121,7 +109,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
assertNull(plugin.createWriter(contactId));
File[] files = drive1.listFiles();
@@ -158,7 +146,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
assertNull(plugin.createWriter(contactId));
File[] files = drive1.listFiles();
@@ -197,7 +185,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
assertNull(plugin.createWriter(contactId));
File[] files = drive1.listFiles();
@@ -236,7 +224,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
assertNotNull(plugin.createWriter(contactId));
// The output file should exist and should be empty
@@ -279,7 +267,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
BatchTransportWriter writer = plugin.createWriter(contactId);
assertNotNull(writer);
@@ -319,7 +307,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
plugin.driveInserted(testDir);
@@ -366,7 +354,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
@@ -396,7 +384,7 @@ public class RemovableDrivePluginTest extends TestCase {
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), callback, finder, monitor);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);

View File

@@ -18,39 +18,25 @@ import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.plugins.ImmediateExecutor;
import org.junit.Before;
import org.junit.Test;
public class SimpleSocketPluginTest extends TestCase {
private final ContactId contactId = new ContactId(0);
private TransportProperties localProperties = null;
private Map<ContactId, TransportProperties> remoteProperties = null;
private TransportConfig config = null;
@Before
public void setUp() {
localProperties = new TransportProperties();
remoteProperties = new HashMap<ContactId, TransportProperties>();
remoteProperties.put(contactId, new TransportProperties());
config = new TransportConfig();
}
@Test
public void testIncomingConnection() throws Exception {
StubCallback callback = new StubCallback();
localProperties.put("host", "127.0.0.1");
localProperties.put("port", "0");
StreamCallback callback = new StreamCallback();
callback.local.put("host", "127.0.0.1");
callback.local.put("port", "0");
SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertNotNull(callback.localProperties);
String host = callback.localProperties.get("host");
String host = callback.local.get("host");
assertNotNull(host);
assertEquals("127.0.0.1", host);
String portString = callback.localProperties.get("port");
String portString = callback.local.get("port");
assertNotNull(portString);
int port = Integer.valueOf(portString);
assertTrue(port > 0 && port < 65536);
@@ -75,10 +61,10 @@ public class SimpleSocketPluginTest extends TestCase {
@Test
public void testOutgoingConnection() throws Exception {
StubCallback callback = new StubCallback();
StreamCallback callback = new StreamCallback();
SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
plugin.start(localProperties, remoteProperties, config);
plugin.start();
// Listen on a local port
final ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", 0), 10);
@@ -97,10 +83,10 @@ public class SimpleSocketPluginTest extends TestCase {
}
}.start();
// Tell the plugin about the port
TransportProperties properties = new TransportProperties();
properties.put("host", "127.0.0.1");
properties.put("port", String.valueOf(port));
plugin.setRemoteProperties(contactId, properties);
TransportProperties p = new TransportProperties();
p.put("host", "127.0.0.1");
p.put("port", String.valueOf(port));
callback.remote.put(contactId, p);
// Connect to the port
StreamTransportConnection conn = plugin.createConnection(contactId);
assertNotNull(conn);
@@ -113,86 +99,44 @@ public class SimpleSocketPluginTest extends TestCase {
plugin.stop();
}
@Test
public void testUpdatingPropertiesReopensSocket() throws Exception {
StubCallback callback = new StubCallback();
localProperties.put("host", "127.0.0.1");
localProperties.put("port", "0");
SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
plugin.start(localProperties, remoteProperties, config);
// The plugin should have bound a socket and stored the port number
assertNotNull(callback.localProperties);
String host = callback.localProperties.get("host");
assertNotNull(host);
assertEquals("127.0.0.1", host);
String portString = callback.localProperties.get("port");
assertNotNull(portString);
int port = Integer.valueOf(portString);
assertTrue(port > 0 && port < 65536);
// The plugin should be listening on the port
InetSocketAddress addr = new InetSocketAddress(host, port);
Socket s = new Socket();
assertEquals(0, callback.incomingConnections);
s.connect(addr, 100);
Thread.sleep(10);
assertEquals(1, callback.incomingConnections);
s.close();
// Update the local properties with a new port number
localProperties.put("port", "0");
plugin.setLocalProperties(localProperties);
// The plugin should no longer be listening on the old port
try {
s = new Socket();
s.connect(addr, 100);
fail();
} catch(IOException expected) {}
// Find out what the new port is
portString = callback.localProperties.get("port");
assertNotNull(portString);
int newPort = Integer.valueOf(portString);
assertFalse(newPort == port);
// The plugin should be listening on the new port
addr = new InetSocketAddress(host, newPort);
assertEquals(1, callback.incomingConnections);
s = new Socket();
s.connect(addr, 100);
Thread.sleep(10);
assertEquals(2, callback.incomingConnections);
s.close();
// Stop the plugin
plugin.stop();
Thread.sleep(10);
// The plugin should no longer be listening
try {
s = new Socket();
s.connect(addr, 100);
fail();
} catch(IOException expected) {}
}
private static class StreamCallback implements StreamTransportCallback {
private static class StubCallback implements StreamTransportCallback {
private TransportConfig config = new TransportConfig();
private TransportProperties local = new TransportProperties();
private Map<ContactId, TransportProperties> remote =
new HashMap<ContactId, TransportProperties>();
public TransportProperties localProperties = null;
public volatile int incomingConnections = 0;
private int incomingConnections = 0;
public void setLocalProperties(TransportProperties properties) {
localProperties = properties;
public TransportConfig getConfig() {
return config;
}
public void setConfig(TransportConfig config) {
public TransportProperties getLocalProperties() {
return local;
}
public void showMessage(String... message) {
public Map<ContactId, TransportProperties> getRemoteProperties() {
return remote;
}
public void setConfig(TransportConfig c) {
config = c;
}
public void setLocalProperties(TransportProperties p) {
local = p;
}
public int showChoice(String[] options, String... message) {
return -1;
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public int showChoice(String[] choices, String... message) {
return -1;
}
public void showMessage(String... message) {}
public void incomingConnectionCreated(StreamTransportConnection c) {
incomingConnections++;