public void ShouldSetALogger()
        {
            IPluginCreator tested    = PluginCreator.GetCreator();
            ILogWriter     logwriter = tested as ILogWriter;

            Assert.IsNotNull(logwriter.Log);
        }
Ejemplo n.º 2
0
        private static string[] TryUsePlugin(IPluginCreator Plugin)
        {
            try
            {
                if (Plugin.InitializeWithScript)
                {
                    CurrentPlugin = Plugin.Create(CurrentScript);
                }
                else
                {
                    CurrentPlugin = Plugin.Create();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Plugin \"{Plugin.Name}\" Creation Error:\n{ex}");
                return(null);
            }

            try
            {
                return(Plugin.InitializeWithScript ? CurrentPlugin.Import() : CurrentPlugin.Import(CurrentScript));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Plugin \"{Plugin.Name}\" Load Error:\n{ex}");
                return(null);
            }
        }
Ejemplo n.º 3
0
        private void QueryAndRunPlugins()
        {
            Console.WriteLine("Creating AppDomain PluginDomain");
            AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain");

            pluginDomain.AssemblyLoad += (s, e) => Console.WriteLine("{0} loaded {1}", AppDomain.CurrentDomain.FriendlyName, e.LoadedAssembly.FullName);
            IPluginCreator pluginCreator = PluginCreator.GetCreator(pluginDomain);

            try
            {
                ITestPlugin plugin;
                Dictionary <string, object> settings = new Dictionary <string, object>();
                settings.Add("Name", "SettingName");

                PluginFilter filter = PluginFilter.Create.Implements(typeof(ITestPlugin)).HasMinVersion("1.0");
                Console.WriteLine("Querying for plugins satisfying {0}", filter);
                PluginDescriptor[] foundPlugins = this.pluginRepository.Plugins(filter).ToArray();
                Console.WriteLine(string.Format("{0} plugins found", foundPlugins.Length));

                foreach (var pluginDescriptor in foundPlugins)
                {
                    Console.WriteLine(string.Format("Creating plugin {0} inside {1}", pluginDescriptor.QualifiedName.TypeFullName, pluginDomain.FriendlyName));
                    plugin = pluginCreator.Create(pluginDescriptor, this.assemblyRepository, settings) as ITestPlugin;
                    Console.WriteLine("Say hello plugin...");
                    plugin.SayHello();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Unloading {0}", pluginDomain.FriendlyName);
            AppDomain.Unload(pluginDomain);
        }
Ejemplo n.º 4
0
        internal static IPluginCreator GetCreator(AppDomain domain, ILoggerFactory logfactory)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }

            if (logfactory == null)
            {
                throw new ArgumentNullException("logfactory");
            }

            IPluginCreator creator = domain.GetData(PLUGINCREATORKEY) as IPluginCreator;

            if (creator == null)
            {
                domain.SetData(LOGGERFACTORYKEY, new ProxyLoggerFactory(logfactory));
                domain.DoCallBack(() =>
                {
                    Logger.Singleton.LoggerFactory = AppDomain.CurrentDomain.GetData(LOGGERFACTORYKEY) as ILoggerFactory;
                    AppDomain.CurrentDomain.SetData(PLUGINCREATORKEY, new PluginCreator());
                });
                domain.SetData(LOGGERFACTORYKEY, null);
                creator = domain.GetData(PLUGINCREATORKEY) as IPluginCreator;
            }
            return(creator);
        }
 public void GetCreatorCanReturnCreatorInsideAnotherAppDomain()
 {
     using (MockDomain otherDomain = new MockDomain())
     {
         IPluginCreator tested = PluginCreator.GetCreator(otherDomain);
         Assert.IsNotNull(tested);
     }
 }
        public void CreateShouldThrowIfMissingRequiredSettings()
        {
            MockAssemblyRepository repository = new MockAssemblyRepository();
            IPluginCreator         tested     = PluginCreator.GetCreator();
            PluginDescriptor       descriptor = MockPluginDescriptor.For <MockPlugin2>();

            DoAssert.Throws <PluginSettingException>(() => tested.Create(descriptor, repository, null));
        }
        public void CreateRequiresAssemblyRepository()
        {
            PluginDescriptor            descriptor = MockPluginDescriptor.For <MockPlugin1>();
            Dictionary <string, object> settings   = new Dictionary <string, object>();
            IPluginCreator tested = PluginCreator.GetCreator();

            DoAssert.Throws <ArgumentNullException>(() => tested.Create(descriptor, null, settings));
        }
        public void CreateRequiresPluginDescriptor()
        {
            IAssemblyRepository         repository = new MockAssemblyRepository();
            Dictionary <string, object> settings   = new Dictionary <string, object>();
            IPluginCreator tested = PluginCreator.GetCreator();

            DoAssert.Throws <ArgumentNullException>(() => tested.Create(null, repository, settings));
        }
 public void GetCreatorShouldReturnSameInstanceForSameDomain()
 {
     using (MockDomain otherDomain = new MockDomain())
     {
         IPluginCreator first  = PluginCreator.GetCreator(otherDomain);
         IPluginCreator tested = PluginCreator.GetCreator(otherDomain);
         Assert.AreSame(first, tested);
     }
 }
        public void CreateShouldCreatePluginInstance()
        {
            IAssemblyRepository repository = new MockAssemblyRepository();
            IPluginCreator      tested     = PluginCreator.GetCreator();
            PluginDescriptor    descriptor = MockPluginDescriptor.For <MockPlugin1>();
            object plugin = tested.Create(descriptor, repository, null);

            Assert.IsNotNull(plugin);
        }
 public void GetCreatorShouldReturnDifferentInstanceForDifferentDomain()
 {
     using (MockDomain domain1 = new MockDomain())
         using (MockDomain domain2 = new MockDomain())
         {
             IPluginCreator tested1 = PluginCreator.GetCreator(domain1);
             IPluginCreator tested2 = PluginCreator.GetCreator(domain2);
             Assert.AreNotSame(tested1, tested2);
         }
 }
        public void CreateShouldLogCatchedPluginExceptionAsError()
        {
            MockAssemblyRepository repository = new MockAssemblyRepository();
            IPluginCreator         tested     = PluginCreator.GetCreator();
            MockLog          mocklog          = new MockLog((ILogWriter)tested);
            PluginDescriptor descriptor       = MockPluginDescriptor.For <MockPlugin2>();
            Exception        ex = DoAssert.Throws <PluginSettingException>(() => tested.Create(descriptor, repository, null));

            Assert.IsTrue(mocklog.Any(x => x.Level == MockLog.Level.Error && x.Message.Contains(ex.Message)));
        }
 public void CreateShouldResolveMissingAssemblies()
 {
     using (MockDomain domain = new MockDomain())
     {
         MockAssemblyRepository repository = new MockAssemblyRepository();
         IPluginCreator         tested     = PluginCreator.GetCreator(domain);
         PluginDescriptor       descriptor = MockPluginDescriptor.For <MockPlugin1>();
         object instance = tested.Create(descriptor, repository, null);
         Assert.IsNotNull(instance);
         Assert.IsTrue(repository.Fetched.Keys.Contains(typeof(MockPlugin1).Assembly.FullName));
     }
 }
        public void CreateShouldThrowIfSettingIsWrongType()
        {
            MockAssemblyRepository repository = new MockAssemblyRepository();
            IPluginCreator         tested     = PluginCreator.GetCreator();
            PluginDescriptor       descriptor = MockPluginDescriptor.For <MockPlugin2>();

            Dictionary <string, object> settings = new Dictionary <string, object>()
            {
                { "NamedSetting", "not int" }
            };

            DoAssert.Throws <PluginSettingException>(() => tested.Create(descriptor, repository, settings));
        }
        public void CreateShouldApplySettings()
        {
            MockAssemblyRepository repository = new MockAssemblyRepository();
            IPluginCreator         tested     = PluginCreator.GetCreator();
            PluginDescriptor       descriptor = MockPluginDescriptor.For <MockPlugin2>();

            Dictionary <string, object> settings = new Dictionary <string, object>()
            {
                { "NamedSetting", 42 }
            };

            MockPlugin2 plugin = tested.Create(descriptor, repository, settings) as MockPlugin2;

            Assert.AreEqual(42, plugin.Setting);
        }
        public void CreateShouldLogCatchedExceptionAsError()
        {
            using (MockDomain domain = new MockDomain())
            {
                MockAssemblyRepository repository = new MockAssemblyRepository();
                QualifiedName          fakeName   = new QualifiedName(
                    typeof(string).FullName.Replace("mscorlib", "NonExistingAssemblyName"),
                    typeof(string).Assembly.FullName.Replace("mscorlib", "NonExistingAssemblyName"));

                IPluginCreator   tested     = PluginCreator.GetCreator(domain);
                MockLog          mocklog    = new MockLog((ILogWriter)tested);
                PluginDescriptor descriptor = MockPluginDescriptor.For(fakeName);
                Exception        ex         = DoAssert.Throws <PluginException>(() => tested.Create(descriptor, repository, null));
                Assert.IsTrue(mocklog.Any(x => x.Level == MockLog.Level.Error && x.Message.Contains(ex.Message)));
            }
        }
        public void CreateShouldThrowOnUnresolvedAssembly()
        {
            using (MockDomain domain = new MockDomain())
            {
                MockAssemblyRepository repository = new MockAssemblyRepository();
                IPluginCreator         tested     = PluginCreator.GetCreator(domain);
                QualifiedName          fakeName   = new QualifiedName(
                    typeof(string).FullName.Replace("mscorlib", "NonExistingAssemblyName"),
                    typeof(string).Assembly.FullName.Replace("mscorlib", "NonExistingAssemblyName"));

                PluginDescriptor descriptor = MockPluginDescriptor.For(fakeName);

                PluginException ex = DoAssert.Throws <PluginException>(() => tested.Create(descriptor, repository, null));
                Assert.IsNotNull(ex.InnerException);
                Assert.IsInstanceOfType(ex.InnerException, typeof(FileNotFoundException));
            }
        }
Ejemplo n.º 18
0
 public void RegisterPlugin(string p, IPluginCreator plugin)
 {
     pluginCreators.Add(p, plugin);
 }
        public void GetCreatorCanReturnCreatorInsideCurrentAppDomain()
        {
            IPluginCreator tested = PluginCreator.GetCreator();

            Assert.IsNotNull(tested);
        }