Esempio n. 1
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();
            }
        }