Ejemplo 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();
            }
        }
Ejemplo 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();
            }
        }
Ejemplo n.º 3
0
        private Assembly MockPlugin_MapToAssemblyCallback(MockPlugin mockPlugin)
        {
            // We'll just map the newly instantiated plugin to any of the assemblies
            // we're mock-providing, except the ones that are already mapped
            Assembly assembly;

            if (!this.pluginMap.TryGetValue(mockPlugin, out assembly))
            {
                assembly = this.assemblyMap
                           .Where(pair => !this.pluginMap.ContainsValue(pair.Value))
                           .Select(pair => pair.Value)
                           .FirstOrDefault();
                this.pluginMap.Add(mockPlugin, assembly);
            }
            return(assembly);
        }
Ejemplo 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();
            }
        }
Ejemplo n.º 5
0
 private Assembly MockPlugin_MapToAssemblyCallback(MockPlugin mockPlugin)
 {
     // We'll just map the newly instantiated plugin to any of the assemblies
     // we're mock-providing, except the ones that are already mapped
     Assembly assembly;
     if (!this.pluginMap.TryGetValue(mockPlugin, out assembly))
     {
         assembly = this.assemblyMap
             .Where(pair => !this.pluginMap.ContainsValue(pair.Value))
             .Select(pair => pair.Value)
             .FirstOrDefault();
         this.pluginMap.Add(mockPlugin, assembly);
     }
     return assembly;
 }