PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/Modularity/FileModuleRetrieverFixture.Desktop.cs

#
C# | 115 lines | 81 code | 18 blank | 16 comment | 3 complexity | 7b57a81fe30aa2eb3793b53521593fc2 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Threading;
 19using Microsoft.Practices.Composite.Modularity;
 20using Microsoft.VisualStudio.TestTools.UnitTesting;
 21
 22namespace Microsoft.Practices.Composite.Tests.Modularity
 23{
 24    [TestClass]
 25    public class FileModuleRetrieverFixture
 26    {
 27        [TestMethod]
 28        public void CanRetrieveModule()
 29        {
 30            var assemblyResolver = new MockAssemblyResolver();
 31            var retriever = new FileModuleTypeLoader(assemblyResolver);
 32            string assembly = CompilerHelper.GenerateDynamicModule("FileModuleA", null);
 33            var fileModuleInfo = CreateModuleInfo(assembly, "TestModules.FileModuleAClass", "ModuleA", true, null);
 34
 35            ManualResetEvent callbackEvent = new ManualResetEvent(false);
 36
 37            retriever.BeginLoadModuleType(fileModuleInfo, delegate
 38            {
 39                callbackEvent.Set();
 40            });
 41
 42            callbackEvent.WaitOne(500);
 43
 44            Assert.AreEqual(assembly, assemblyResolver.LoadAssemblyFromArgument);
 45        }
 46
 47        [TestMethod]
 48        public void ShouldReturnErrorToCallback()
 49        {
 50            var assemblyResolver = new MockAssemblyResolver();
 51            var retriever = new FileModuleTypeLoader(assemblyResolver);
 52            var fileModuleInfo = CreateModuleInfo("NonExistentFile.dll", "NonExistentModule", "NonExistent", true, null);
 53
 54            ManualResetEvent callbackEvent = new ManualResetEvent(false);
 55
 56            assemblyResolver.ThrowOnLoadAssemblyFrom = true;
 57            Exception resultException = null;
 58            retriever.BeginLoadModuleType(fileModuleInfo, delegate(ModuleInfo moduleInfo, Exception error)
 59                                                        {
 60                                                            resultException = error;
 61                                                            callbackEvent.Set();
 62                                                        });
 63
 64            callbackEvent.WaitOne(500);
 65
 66            Assert.IsNotNull(resultException);
 67        }
 68
 69        [TestMethod]
 70        public void CanRetrieveWithCorrectRef()
 71        {
 72            var retriever = new FileModuleTypeLoader();
 73            var moduleInfo = new ModuleInfo() { Ref = "file://somefile" };
 74
 75            Assert.IsTrue(retriever.CanLoadModuleType(moduleInfo));
 76        }
 77
 78        [TestMethod]
 79        public void CannotRetrieveWithIncorrectRef()
 80        {
 81            var retriever = new FileModuleTypeLoader();
 82            var moduleInfo = new ModuleInfo() { Ref = "NotForLocalRetrieval" };
 83
 84            Assert.IsFalse(retriever.CanLoadModuleType(moduleInfo));
 85        }
 86
 87        private static ModuleInfo CreateModuleInfo(string assemblyFile, string moduleType, string moduleName, bool startupLoaded, params string[] dependsOn)
 88        {
 89            ModuleInfo moduleInfo = new ModuleInfo(moduleName, moduleType)
 90            {
 91                InitializationMode = startupLoaded ? InitializationMode.WhenAvailable : InitializationMode.OnDemand,
 92                Ref = assemblyFile,
 93            };
 94            if (dependsOn != null)
 95            {
 96                moduleInfo.DependsOn.AddRange(dependsOn);
 97            }
 98
 99            return moduleInfo;
100        }
101    }
102
103    internal class MockAssemblyResolver : IAssemblyResolver
104    {
105        public string LoadAssemblyFromArgument;
106        public bool ThrowOnLoadAssemblyFrom;
107
108        public void LoadAssemblyFrom(string assemblyFilePath)
109        {
110            LoadAssemblyFromArgument = assemblyFilePath;
111            if (ThrowOnLoadAssemblyFrom)
112                throw new Exception();
113        }
114    }
115}