mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Rename bramble-j2se to bramble-java.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package org.briarproject.bramble.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class WindowsShutdownManagerImplTest extends ShutdownManagerImplTest {
|
||||
|
||||
@Override
|
||||
protected ShutdownManager createShutdownManager() {
|
||||
return new WindowsShutdownManagerImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManagerWaitsForHooksToRun() {
|
||||
WindowsShutdownManagerImpl s = new WindowsShutdownManagerImpl();
|
||||
SlowHook[] hooks = new SlowHook[10];
|
||||
for (int i = 0; i < hooks.length; i++) {
|
||||
hooks[i] = new SlowHook();
|
||||
s.addShutdownHook(hooks[i]);
|
||||
}
|
||||
s.runShutdownHooks();
|
||||
for (SlowHook hook : hooks) assertTrue(hook.finished);
|
||||
}
|
||||
|
||||
private static class SlowHook implements Runnable {
|
||||
|
||||
private volatile boolean finished = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
finished = true;
|
||||
} catch (InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.briarproject.bramble.plugin.modem;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class CountryCodesTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testTranslation() {
|
||||
// Unrecognised country for caller
|
||||
assertNull(CountryCodes.translate("02012345678", "ZZ", "GB"));
|
||||
// Unrecognised country for callee
|
||||
assertNull(CountryCodes.translate("02012345678", "GB", "ZZ"));
|
||||
|
||||
// GB to GB, callee has not included a prefix
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("2012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included NDD prefix
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("02012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included plus sign and country code
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("+442012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included IDD prefix and country code
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("00442012345678", "GB", "GB"));
|
||||
|
||||
// Russia to GB, callee has not included a prefix
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("2012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included NDD prefix
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("02012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included plus sign and country code
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("+442012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included IDD prefix and country code
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("00442012345678", "RU", "GB"));
|
||||
|
||||
// Andorra to Andorra (no NDD), callee has not included a prefix
|
||||
assertEquals("765432", CountryCodes.translate("765432", "AD", "AD"));
|
||||
// Andorra to Andorra, callee has included plus sign and country code
|
||||
assertEquals("765432",
|
||||
CountryCodes.translate("+376765432", "AD", "AD"));
|
||||
// Andorra to Andorra, callee has included IDD and country code
|
||||
assertEquals("765432",
|
||||
CountryCodes.translate("00376765432", "AD", "AD"));
|
||||
|
||||
// GB to Andorra (no NDD), callee has not included a prefix
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("765432", "GB", "AD"));
|
||||
// GB to Andorra, callee has included plus sign and country code
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("+376765432", "GB", "AD"));
|
||||
// GB to Andorra, callee has included IDD and country code
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("00376765432", "GB", "AD"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package org.briarproject.bramble.plugin.modem;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class ModemPluginTest extends BrambleTestCase {
|
||||
|
||||
private static final String ISO_1336 = "GB";
|
||||
private static final String NUMBER = "0123456789";
|
||||
|
||||
@Test
|
||||
public void testModemCreation() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, null, 0);
|
||||
Modem modem = context.mock(Modem.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo", "bar", "baz" }));
|
||||
// First call to createModem() returns false
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(false));
|
||||
// Second call to createModem() throws an exception
|
||||
oneOf(modemFactory).createModem(plugin, "bar");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(throwException(new IOException()));
|
||||
// Third call to createModem() returns true
|
||||
oneOf(modemFactory).createModem(plugin, "baz");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
}});
|
||||
plugin.start();
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnection() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
Modem modem = context.mock(Modem.class);
|
||||
TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
plugin.start();
|
||||
// A connection should be returned
|
||||
assertNotNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnectionWhenDialReturnsFalse() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
Modem modem = context.mock(Modem.class);
|
||||
TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(false));
|
||||
}});
|
||||
plugin.start();
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnectionWhenDialThrowsException() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
Modem modem = context.mock(Modem.class);
|
||||
TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(throwException(new IOException()));
|
||||
// resetModem()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
}});
|
||||
plugin.start();
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user