public void FileModuleTypeLoaderCanBeDisposed()
        {
            var typeLoader = new FileModuleTypeLoader();
            var disposable = typeLoader as IDisposable;

            Assert.IsNotNull(disposable);
        }
        /// <summary>
        /// Load the module type.
        /// </summary>
        /// <param name="moduleInfo">The module Info.</param>
        /// <exception cref="System.ArgumentNullException">The <paramref name="moduleInfo" /> is <c>null</c>.</exception>
        public void LoadModuleType(ModuleInfo moduleInfo)
        {
            // ReSharper disable once ImplicitlyCapturedClosure
            Argument.IsNotNull(() => moduleInfo);

            Dispatcher currentDispatcher = DispatcherHelper.CurrentDispatcher;

            Log.Debug("Loading module type '{0}' from package '{1}'", moduleInfo.ModuleType, moduleInfo.Ref);

            new Thread(() =>
            {
                InstallPackageRequest installPackageRequest;
                var packageName = moduleInfo.GetPackageName();
                if (packageName != null && this.TryCreateInstallPackageRequest(moduleInfo, out installPackageRequest))
                {
                    currentDispatcher.BeginInvoke(() => OnModuleDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(moduleInfo, 0, 1)));

                    bool installed = false;
                    try
                    {
                        installPackageRequest.Execute();
                        installed = true;
                    }
                    catch (Exception e)
                    {
                        currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, e)));
                    }

                    if (installed)
                    {
                        var fileModuleTypeLoader = new FileModuleTypeLoader();
                        var fileModuleInfo       = new ModuleInfo(moduleInfo.ModuleName, moduleInfo.ModuleType)
                        {
                            Ref = installPackageRequest.AssemblyFileRef,
                            InitializationMode = moduleInfo.InitializationMode,
                            DependsOn          = moduleInfo.DependsOn
                        };

                        fileModuleTypeLoader.ModuleDownloadProgressChanged += (sender, args) =>
                        {
                            moduleInfo.State = args.ModuleInfo.State;
                            currentDispatcher.BeginInvoke(() => OnModuleDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(moduleInfo, args.BytesReceived, args.TotalBytesToReceive)));
                        };

                        fileModuleTypeLoader.LoadModuleCompleted += (sender, args) =>
                        {
                            moduleInfo.State = args.ModuleInfo.State;
                            currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, args.Error)));
                        };

                        fileModuleTypeLoader.LoadModuleType(fileModuleInfo);
                    }
                }
                else
                {
                    currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, new ModuleNotFoundException(moduleInfo.ModuleName, string.Format(CultureInfo.InvariantCulture, "The package '{0}' for module '{1}' was not found", moduleInfo.Ref, moduleInfo.ModuleName)))));
                }
            }).Start();
        }
        public void CannotRetrieveWithIncorrectRef()
        {
            var retriever  = new FileModuleTypeLoader();
            var moduleInfo = new ModuleInfo()
            {
                Ref = "NotForLocalRetrieval"
            };

            Assert.IsFalse(retriever.CanLoadModuleType(moduleInfo));
        }
        public void CanRetrieveWithCorrectRef()
        {
            var retriever  = new FileModuleTypeLoader();
            var moduleInfo = new ModuleInfo()
            {
                Ref = "file://somefile"
            };

            Assert.IsTrue(retriever.CanLoadModuleType(moduleInfo));
        }
        public void FileModuleTypeLoaderDisposeNukesAssemblyResolver()
        {
            Mock <IAssemblyResolver> mockResolver = new Mock <IAssemblyResolver>();
            var disposableMockResolver            = mockResolver.As <IDisposable>();

            disposableMockResolver.Setup(resolver => resolver.Dispose());

            var typeLoader = new FileModuleTypeLoader(mockResolver.Object);

            typeLoader.Dispose();

            disposableMockResolver.Verify(resolver => resolver.Dispose(), Times.Once());
        }
        public void FileModuleTypeLoaderDisposeDoesNotThrowWithNonDisposableAssemblyResolver()
        {
            Mock <IAssemblyResolver> mockResolver = new Mock <IAssemblyResolver>();
            var typeLoader = new FileModuleTypeLoader(mockResolver.Object);

            try
            {
                typeLoader.Dispose();
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
        public void CanRetrieveModule()
        {
            var    assemblyResolver = new MockAssemblyResolver();
            var    retriever        = new FileModuleTypeLoader(assemblyResolver);
            string assembly         = CompilerHelper.GenerateDynamicModule("FileModuleA", null);
            var    fileModuleInfo   = CreateModuleInfo(assembly, "TestModules.FileModuleAClass", "ModuleA", true, null);

            ManualResetEvent callbackEvent = new ManualResetEvent(false);

            retriever.BeginLoadModuleType(fileModuleInfo, delegate
            {
                callbackEvent.Set();
            });

            callbackEvent.WaitOne(500);

            Assert.AreEqual(assembly, assemblyResolver.LoadAssemblyFromArgument);
        }
        public void CanRetrieveModule()
        {
            var    assemblyResolver = new MockAssemblyResolver();
            var    retriever        = new FileModuleTypeLoader(assemblyResolver);
            string assembly         = CompilerHelper.GenerateDynamicModule("FileModuleA", null);
            string assemblyRef      = "file://" + assembly;
            var    fileModuleInfo   = CreateModuleInfo(assemblyRef, "TestModules.FileModuleAClass", "ModuleA", true, null);

            bool loadCompleted = false;

            retriever.LoadModuleCompleted += delegate(object sender, LoadModuleCompletedEventArgs e)
            {
                loadCompleted = true;
            };

            retriever.LoadModuleType(fileModuleInfo);

            Assert.IsTrue(loadCompleted);
            Assert.AreEqual(assemblyRef, assemblyResolver.LoadAssemblyFromArgument);
        }
        public void ShouldReturnErrorToCallback()
        {
            var assemblyResolver = new MockAssemblyResolver();
            var retriever        = new FileModuleTypeLoader(assemblyResolver);
            var fileModuleInfo   = CreateModuleInfo("NonExistentFile.dll", "NonExistentModule", "NonExistent", true, null);

            ManualResetEvent callbackEvent = new ManualResetEvent(false);

            assemblyResolver.ThrowOnLoadAssemblyFrom = true;
            Exception resultException = null;

            retriever.BeginLoadModuleType(fileModuleInfo, delegate(ModuleInfo moduleInfo, Exception error)
            {
                resultException = error;
                callbackEvent.Set();
            });

            callbackEvent.WaitOne(500);

            Assert.IsNotNull(resultException);
        }
        public void ShouldReturnErrorToCallback()
        {
            var assemblyResolver = new MockAssemblyResolver();
            var retriever        = new FileModuleTypeLoader(assemblyResolver);
            var fileModuleInfo   = CreateModuleInfo("NonExistentFile.dll", "NonExistentModule", "NonExistent", true, null);

            assemblyResolver.ThrowOnLoadAssemblyFrom = true;
            Exception resultException = null;

            bool loadCompleted = false;

            retriever.LoadModuleCompleted += delegate(object sender, LoadModuleCompletedEventArgs e)
            {
                loadCompleted   = true;
                resultException = e.Error;
            };

            retriever.LoadModuleType(fileModuleInfo);

            Assert.IsTrue(loadCompleted);
            Assert.IsNotNull(resultException);
        }
        /// <summary>
        /// Load the module type.
        /// </summary>
        /// <param name="moduleInfo">The module Info.</param>
        /// <exception cref="System.ArgumentNullException">The <paramref name="moduleInfo" /> is <c>null</c>.</exception>
        public void LoadModuleType(ModuleInfo moduleInfo)
        {
            // ReSharper disable once ImplicitlyCapturedClosure
            Argument.IsNotNull(() => moduleInfo);

            var currentDispatcher = DispatcherHelper.CurrentDispatcher;

            Log.Debug("Loading module type '{0}' from package '{1}'", moduleInfo.ModuleType, moduleInfo.Ref);

            var thread = new Thread(() =>
            {
                InstallPackageRequest installPackageRequest;
                var packageName = moduleInfo.GetPackageName();
                if (packageName != null && TryCreateInstallPackageRequest(moduleInfo, out installPackageRequest))
                {
                    currentDispatcher.BeginInvoke(() => OnModuleDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(moduleInfo, 0, 1)));

                    var installed = false;

                    try
                    {
                        installPackageRequest.Execute();
                        installed = true;
                    }
                    catch (Exception ex)
                    {
                        currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, ex)));
                    }

                    if (installed)
                    {
                        var fileModuleTypeLoader = new FileModuleTypeLoader();
                        var fileModuleInfo = new ModuleInfo(moduleInfo.ModuleName, moduleInfo.ModuleType)
                        {
                            Ref = installPackageRequest.ModuleAssemblyRef.Ref,
                            InitializationMode = moduleInfo.InitializationMode,
                            DependsOn = moduleInfo.DependsOn
                        };

                        fileModuleTypeLoader.ModuleDownloadProgressChanged += (sender, args) =>
                        {
                            moduleInfo.State = args.ModuleInfo.State;
                            currentDispatcher.BeginInvoke(() => OnModuleDownloadProgressChanged(new ModuleDownloadProgressChangedEventArgs(moduleInfo, args.BytesReceived, args.TotalBytesToReceive)));
                        };

                        fileModuleTypeLoader.LoadModuleCompleted += (sender, args) =>
                        {
                            moduleInfo.State = args.ModuleInfo.State;
                            currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, args.Error)));
                        };

                        fileModuleTypeLoader.LoadModuleType(fileModuleInfo);

                        Log.Info("Module '{0}' is installed and loaded", moduleInfo.ModuleName);
                    }
                }
                else
                {
                    currentDispatcher.BeginInvoke(() => OnLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, new ModuleNotFoundException(moduleInfo.ModuleName, string.Format(CultureInfo.InvariantCulture, "The package '{0}' for module '{1}' was not found", moduleInfo.Ref, moduleInfo.ModuleName)))));
                }
            });

            thread.Start();
        }