Exemplo n.º 1
0
        [Test] public void PluginLifecycle()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                // Set up some mock data for available assemblies
                pluginLoader.AddBaseDir("MockDir");
                pluginLoader.AddPlugin(new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin)));
                pluginLoader.AddPlugin(new MockAssembly("MockDir/MockPluginB.dll", typeof(MockPlugin)));
                pluginLoader.AddPlugin(new MockAssembly("MockDir/MockPluginC.dll", typeof(MockPlugin)));

                // Set up a plugin manager using the mock loader
                MockPluginManager pluginManager = new MockPluginManager();
                pluginManager.Init(pluginLoader);

                // Register event handler to check if all events are fired as expected
                HashSet <MockPlugin> firedPluginReady    = new HashSet <MockPlugin>();
                HashSet <MockPlugin> firedPluginRemoving = new HashSet <MockPlugin>();
                HashSet <MockPlugin> firedPluginRemoved  = new HashSet <MockPlugin>();
                pluginManager.PluginsReady += (sender, args) =>
                {
                    foreach (MockPlugin plugin in args.Plugins)
                    {
                        firedPluginReady.Add(plugin);
                    }
                };
                pluginManager.PluginsRemoving += (sender, args) =>
                {
                    foreach (MockPlugin plugin in args.Plugins)
                    {
                        firedPluginRemoving.Add(plugin);
                    }
                };
                pluginManager.PluginsRemoved += (sender, args) =>
                {
                    foreach (MockPlugin plugin in args.Plugins)
                    {
                        firedPluginRemoved.Add(plugin);
                    }
                };

                // Load all plugins and expect them to be not initialized yet
                pluginManager.LoadPlugins();
                MockPlugin[] loadedPlugins = pluginManager.LoadedPlugins
                                             .Cast <MockPlugin>()
                                             .ToArray();
                Assert.IsTrue(loadedPlugins.All(plugin => !plugin.Initialized));
                Assert.IsTrue(loadedPlugins.All(plugin => !plugin.Disposed));

                // Initialize plugins and expect them all to be initialized
                pluginManager.InitPlugins();
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.Initialized));
                Assert.IsTrue(loadedPlugins.All(plugin => !plugin.Disposed));
                CollectionAssert.AreEquivalent(loadedPlugins, firedPluginReady);

                // Discard all plugins and expect them to be disposed
                pluginManager.ClearPlugins();
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.Initialized));
                Assert.IsTrue(loadedPlugins.All(plugin => plugin.Disposed));
                CollectionAssert.AreEquivalent(loadedPlugins, firedPluginRemoving);
                CollectionAssert.AreEquivalent(loadedPlugins, firedPluginRemoved);

                pluginManager.Terminate();
            }
        }