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