Esempio n. 1
0
        [Test] public void DuplicateLoad()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                MockAssembly[] mockPlugins = new MockAssembly[]
                {
                    new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir/MockPluginB.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir/MockPluginC.dll", typeof(MockPlugin))
                };

                // Set up some mock data for available assemblies
                pluginLoader.AddBaseDir("MockDir");
                for (int i = 0; i < mockPlugins.Length; i++)
                {
                    pluginLoader.AddPlugin(mockPlugins[i]);
                }

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

                // Load and init all plugins
                pluginManager.LoadPlugins();
                pluginManager.InitPlugins();

                // Now load them again
                pluginManager.LoadPlugins();

                // Assert that we do not have any duplicates and no disposed plugins
                Assert.AreEqual(3, pluginManager.LoadedPlugins.Count());
                Assert.IsEmpty(pluginManager.DisposedPlugins);

                // Assert that we did not load any assembly twice
                Assert.AreEqual(3, pluginLoader.LoadedAssemblies.Count());

                // Let's try loading assembly duplicates manually
                for (int i = 0; i < mockPlugins.Length; i++)
                {
                    MockPlugin plugin = pluginManager.LoadPlugin(mockPlugins[i], mockPlugins[i].Location);
                    Assert.IsNotNull(plugin);
                }

                // Assert that we do not have any duplicates and no disposed plugins
                Assert.AreEqual(3, pluginManager.LoadedPlugins.Count());
                Assert.IsEmpty(pluginManager.DisposedPlugins);

                // Assert that we did not load any assembly twice
                Assert.AreEqual(3, pluginLoader.LoadedAssemblies.Count());

                pluginManager.Terminate();
            }
        }
Esempio n. 2
0
        [Test] public void LockedPlugin()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                // Set up some mock data for available assemblies
                MockAssembly lockedAssembly = new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin));

                pluginLoader.AddBaseDir("MockDir");
                pluginLoader.AddPlugin(lockedAssembly);
                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);

                // Load and init all plugins
                pluginManager.LoadPlugins();
                pluginManager.InitPlugins();

                // Register event handler to check if all events are fired as expected
                HashSet <MockPlugin> firedPluginRemoving = new HashSet <MockPlugin>();
                HashSet <MockPlugin> firedPluginRemoved  = new HashSet <MockPlugin>();
                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);
                    }
                };

                // Lock the plugin we're about to reload
                pluginManager.LockPlugin(lockedAssembly);

                // Attempt to reload the plugin that was locked
                MockPlugin reloadedPlugin = pluginManager.ReloadPlugin(lockedAssembly.Location);

                // Assert that nothing has changed and the reload attempt was rejected
                Assert.IsNull(reloadedPlugin);
                CollectionAssert.Contains(pluginManager.LoadedPlugins.Select(p => p.PluginAssembly), lockedAssembly);
                CollectionAssert.DoesNotContain(firedPluginRemoving.Select(p => p.PluginAssembly), lockedAssembly);
                CollectionAssert.DoesNotContain(firedPluginRemoved.Select(p => p.PluginAssembly), lockedAssembly);

                pluginManager.Terminate();
            }
        }
Esempio n. 3
0
        [Test] public void ResolveAssembly()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                MockAssembly[] mockAssemblies = new MockAssembly[]
                {
                    new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir/MockPluginB.dll", typeof(MockPlugin)),
                    new MockAssembly("MockDir/MockPluginC.dll", typeof(MockPlugin))
                };

                // Set up some mock data for available assemblies
                pluginLoader.AddBaseDir("MockDir");
                for (int i = 0; i < mockAssemblies.Length; i++)
                {
                    pluginLoader.AddPlugin(mockAssemblies[i]);
                }

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

                // Load and init all plugins
                pluginManager.LoadPlugins();
                pluginManager.InitPlugins();

                // Assert that other resolve calls will map to existing assemblies,
                // both for plugins and auxilliary libraries
                for (int i = 0; i < mockAssemblies.Length; i++)
                {
                    Assert.AreSame(mockAssemblies[i], pluginLoader.InvokeResolveAssembly(mockAssemblies[i].FullName));
                }

                // Assert that we still have the expected amount of loaded and disposed plugins
                Assert.AreEqual(3, pluginManager.LoadedPlugins.Count());
                Assert.IsEmpty(pluginManager.DisposedPlugins);

                pluginManager.Terminate();
            }
        }
Esempio n. 4
0
        [Test] public void ReloadPlugin()
        {
            using (MockPluginLoader pluginLoader = new MockPluginLoader())
            {
                // Set up some mock data for available assemblies
                MockAssembly oldAssembly = new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin));
                MockAssembly newAssembly = new MockAssembly("MockDir/MockPluginA.dll", typeof(MockPlugin));

                pluginLoader.AddBaseDir("MockDir");
                pluginLoader.AddPlugin(oldAssembly);
                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);

                // Load and init all plugins
                pluginManager.LoadPlugins();
                pluginManager.InitPlugins();

                // 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);
                    }
                };

                // Silently replace one of the assemblies
                pluginLoader.ReplacePlugin(oldAssembly, newAssembly);

                // Reload the plugin that was replaced
                MockPlugin reloadedPlugin = pluginManager.ReloadPlugin(oldAssembly.Location) as MockPlugin;

                // Assert that we got back the new plugin, while the old one is now disposed
                Assert.IsNotNull(reloadedPlugin);
                Assert.AreSame(newAssembly, reloadedPlugin.PluginAssembly);
                CollectionAssert.Contains(pluginManager.LoadedPlugins.Select(p => p.PluginAssembly), newAssembly);
                CollectionAssert.Contains(pluginManager.DisposedPlugins, oldAssembly);
                CollectionAssert.DoesNotContain(pluginManager.LoadedPlugins.Select(p => p.PluginAssembly), oldAssembly);
                CollectionAssert.DoesNotContain(pluginManager.DisposedPlugins, newAssembly);
                CollectionAssert.Contains(firedPluginRemoving.Select(p => p.PluginAssembly), oldAssembly);
                CollectionAssert.Contains(firedPluginRemoved.Select(p => p.PluginAssembly), oldAssembly);

                // Assert that the reloaded plugin is not yet initialized.
                // That's not what the API is supposed to do.
                Assert.IsFalse(reloadedPlugin.Initialized);
                CollectionAssert.DoesNotContain(firedPluginReady, newAssembly);

                pluginManager.Terminate();
            }
        }
Esempio n. 5
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();
            }
        }