[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(); } }
[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(); } }