Use mocks rather than real plugins for better coverage of error cases.

This commit is contained in:
akwizgran
2013-02-12 16:25:08 +00:00
parent d18fc1330b
commit 79fc630ab7

View File

@@ -1,24 +1,24 @@
package net.sf.briar.plugins; package net.sf.briar.plugins;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.TransportConfig; import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.android.AndroidExecutor; import net.sf.briar.api.android.AndroidExecutor;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.messaging.TransportId; import net.sf.briar.api.messaging.TransportId;
import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginConfig; import net.sf.briar.api.plugins.duplex.DuplexPluginConfig;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.api.plugins.simplex.SimplexPlugin;
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexPluginConfig; import net.sf.briar.api.plugins.simplex.SimplexPluginConfig;
import net.sf.briar.api.plugins.simplex.SimplexPluginFactory; import net.sf.briar.api.plugins.simplex.SimplexPluginFactory;
import net.sf.briar.api.transport.ConnectionDispatcher; import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.ui.UiCallback; import net.sf.briar.api.ui.UiCallback;
import net.sf.briar.plugins.file.RemovableDrivePluginFactory;
import net.sf.briar.plugins.tcp.LanTcpPluginFactory;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
@@ -26,7 +26,6 @@ import org.junit.Test;
public class PluginManagerImplTest extends BriarTestCase { public class PluginManagerImplTest extends BriarTestCase {
@SuppressWarnings("unchecked")
@Test @Test
public void testStartAndStop() throws Exception { public void testStartAndStop() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
@@ -42,31 +41,76 @@ public class PluginManagerImplTest extends BriarTestCase {
final ConnectionDispatcher dispatcher = final ConnectionDispatcher dispatcher =
context.mock(ConnectionDispatcher.class); context.mock(ConnectionDispatcher.class);
final UiCallback uiCallback = context.mock(UiCallback.class); final UiCallback uiCallback = context.mock(UiCallback.class);
// One simplex plugin // Two simplex plugin factories: both create plugins, one fails to start
final SimplexPluginFactory removableDrive = final SimplexPluginFactory simplexFactory =
new RemovableDrivePluginFactory(pluginExecutor); context.mock(SimplexPluginFactory.class);
// One duplex plugin final SimplexPlugin simplexPlugin = context.mock(SimplexPlugin.class);
final DuplexPluginFactory lanTcp = final TransportId simplexId = new TransportId(TestUtils.getRandomId());
new LanTcpPluginFactory(pluginExecutor); final SimplexPluginFactory simplexFailFactory =
context.mock(SimplexPluginFactory.class, "simplexFailFactory");
final SimplexPlugin simplexFailPlugin =
context.mock(SimplexPlugin.class, "simplexFailPlugin");
final TransportId simplexFailId =
new TransportId(TestUtils.getRandomId());
// Two duplex plugin factories: one creates a plugin, the other fails
final DuplexPluginFactory duplexFactory =
context.mock(DuplexPluginFactory.class);
final DuplexPlugin duplexPlugin = context.mock(DuplexPlugin.class);
final TransportId duplexId = new TransportId(TestUtils.getRandomId());
final DuplexPluginFactory duplexFailFactory =
context.mock(DuplexPluginFactory.class, "duplexFailFactory");
final TransportId duplexFailId =
new TransportId(TestUtils.getRandomId());
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Start // Start the simplex plugins
oneOf(simplexPluginConfig).getFactories(); oneOf(simplexPluginConfig).getFactories();
will(returnValue(Arrays.asList(removableDrive))); will(returnValue(Arrays.asList(simplexFactory,
oneOf(duplexPluginConfig).getFactories(); simplexFailFactory)));
will(returnValue(Arrays.asList(lanTcp))); oneOf(simplexFactory).getId();
exactly(2).of(db).addTransport(with(any(TransportId.class))); will(returnValue(simplexId));
oneOf(simplexFactory).createPlugin(with(any(
SimplexPluginCallback.class)));
will(returnValue(simplexPlugin)); // Created
oneOf(db).addTransport(simplexId);
will(returnValue(true)); will(returnValue(true));
oneOf(poller).start(with(any(Collection.class))); oneOf(simplexPlugin).start();
allowing(db).getConfig(with(any(TransportId.class))); will(returnValue(true)); // Started
will(returnValue(new TransportConfig())); oneOf(simplexFailFactory).getId();
allowing(db).getLocalProperties(with(any(TransportId.class))); will(returnValue(simplexFailId));
will(returnValue(new TransportProperties())); oneOf(simplexFailFactory).createPlugin(with(any(
allowing(db).getRemoteProperties(with(any(TransportId.class))); SimplexPluginCallback.class)));
will(returnValue(new TransportProperties())); will(returnValue(simplexFailPlugin)); // Created
allowing(db).mergeLocalProperties(with(any(TransportId.class)), oneOf(db).addTransport(simplexFailId);
with(any(TransportProperties.class))); will(returnValue(true));
// Stop oneOf(simplexFailPlugin).start();
will(returnValue(false)); // Failed to start
// Start the duplex plugins
oneOf(duplexPluginConfig).getFactories();
will(returnValue(Arrays.asList(duplexFactory, duplexFailFactory)));
oneOf(duplexFactory).getId();
will(returnValue(duplexId));
oneOf(duplexFactory).createPlugin(with(any(
DuplexPluginCallback.class)));
will(returnValue(duplexPlugin)); // Created
oneOf(db).addTransport(duplexId);
will(returnValue(true));
oneOf(duplexPlugin).start();
will(returnValue(true)); // Started
oneOf(duplexFailFactory).getId();
will(returnValue(duplexFailId));
oneOf(db).addTransport(duplexFailId);
will(returnValue(true));
oneOf(duplexFailFactory).createPlugin(with(any(
DuplexPluginCallback.class)));
will(returnValue(null)); // Failed to create a plugin
// Start the poller
oneOf(poller).start(Arrays.asList(simplexPlugin, duplexPlugin));
// Stop the poller
oneOf(poller).stop(); oneOf(poller).stop();
// Stop the plugins
oneOf(simplexPlugin).stop();
oneOf(duplexPlugin).stop();
// Shut down the executor
oneOf(androidExecutor).shutdown(); oneOf(androidExecutor).shutdown();
}}); }});
PluginManagerImpl p = new PluginManagerImpl(pluginExecutor, PluginManagerImpl p = new PluginManagerImpl(pluginExecutor,