internal static void ExecuteTestInsideTestAppDomain(Action <TestSetup> action) { IsolatedStorageWrapper isolatedStorageWrapper = null; TestAppDomain testAppDomain = null; try { isolatedStorageWrapper = new IsolatedStorageWrapper(IsolatedStorageFile.GetUserStoreForAssembly()); GivenAnEmbeddedAssemblyLoader.CopyExecutingAssemblyTo(isolatedStorageWrapper.StorageDirectory); testAppDomain = new TestAppDomain(isolatedStorageWrapper.StorageDirectory); action(new TestSetup(AppDomain.CurrentDomain, testAppDomain.AppDomain)); } finally { ActOnObject.IfNotNull(testAppDomain, (tad) => tad.Unload()); ActOnObject.IfNotNull(isolatedStorageWrapper, (sw) => sw.Remove()); } }
public void AndRefersToATypeThatIsEmbeddedAsAResourceShouldThrow() { var testNamespace = MethodBase.GetCurrentMethod().DeclaringType.Namespace; var testClassName = MethodBase.GetCurrentMethod().Name; Action <TestSetup> testSetup = (ts) => { FileInfo libraryBinary = null; try { // arrange var librarySource = @" namespace " + testNamespace + @" { public class LibraryType { } }"; var libraryEmbeddedResources = new string[] { }; var libraryReferencedAssemblies = new string[] { }; libraryBinary = GivenAnEmbeddedAssemblyLoader.CompileCodeIntoLocation(librarySource, libraryEmbeddedResources, libraryReferencedAssemblies, new DirectoryInfo(Path.GetTempPath())); var testSource = @" namespace " + testNamespace + @" { using System; public class " + testClassName + @" : MarshalByRefObject { public " + testClassName + @"() { new LibraryType(); } } }"; var testEmbeddedResources = new string[] { libraryBinary.FullName }; var testReferencedAssemblies = new string[] { "System.dll", libraryBinary.FullName }; var testBinary = GivenAnEmbeddedAssemblyLoader.CompileCodeIntoLocation(testSource, testEmbeddedResources, testReferencedAssemblies, new DirectoryInfo(ts.TestAppDomain.BaseDirectory)); Exception thrownException = null; // act try { var proxy = ts.TestAppDomain.CreateInstanceFromAndUnwrap(testBinary.FullName, string.Format("{0}.{1}", testNamespace, testClassName)); } catch (TargetInvocationException tie) { thrownException = tie; } catch (Exception e) { Assert.True(false, string.Format("An unexpected exception occurred: {0}", e.Message)); } // assert Assert.NotNull(thrownException); Assert.NotNull(thrownException.InnerException); Assert.Equal(typeof(FileNotFoundException), thrownException.InnerException.GetType()); Assert.Equal(string.Format("Could not load file or assembly '{0}' or one of its dependencies. The system cannot find the file specified.", AssemblyName.GetAssemblyName(libraryBinary.FullName)), thrownException.InnerException.Message); } finally { ActOnObject.IfNotNull(libraryBinary, (fi) => fi.Delete()); } }; GivenAnEmbeddedAssemblyLoader.ExecuteTestInsideTestAppDomain(testSetup); }
public void AndRefersToATypeThatIsEmbeddedAsANestedEmbeddedResourceThenEmbeddedResourceAssemblyShouldBeLoadedIntoAppDomain() { var testNamespace = MethodBase.GetCurrentMethod().DeclaringType.Namespace; var testClassName = MethodBase.GetCurrentMethod().Name; Action <TestSetup> testSetup = (ts) => { FileInfo nestedLibraryBinary = null; FileInfo libraryBinary = null; try { // arrange var nestedLibrarySource = @" namespace " + testNamespace + @" { public class NestedLibraryType { } }"; var nestedLibraryEmbeddedResources = new string[] { }; var nestedLibraryReferencedAssemblies = new string[] { }; nestedLibraryBinary = GivenAnEmbeddedAssemblyLoader.CompileCodeIntoLocation(nestedLibrarySource, nestedLibraryEmbeddedResources, nestedLibraryReferencedAssemblies, new DirectoryInfo(Path.GetTempPath())); var librarySource = @" namespace " + testNamespace + @" { public class LibraryType { } }"; var libraryEmbeddedResources = new string[] { nestedLibraryBinary.FullName }; var libraryReferencedAssemblies = new string[] { }; libraryBinary = GivenAnEmbeddedAssemblyLoader.CompileCodeIntoLocation(librarySource, libraryEmbeddedResources, libraryReferencedAssemblies, new DirectoryInfo(Path.GetTempPath())); var testSource = @" namespace " + testNamespace + @" { using System; using EmbeddedFx; public class " + testClassName + @" : MarshalByRefObject { static " + testClassName + @"() { EmbeddedAssemblyLoader.Register(); } public " + testClassName + @"() { new NestedLibraryType(); } } }"; var testEmbeddedResources = new string[] { libraryBinary.FullName }; var testReferencedAssemblies = new string[] { "System.dll", "EmbeddedFx.dll", libraryBinary.FullName, nestedLibraryBinary.FullName }; var testBinary = GivenAnEmbeddedAssemblyLoader.CompileCodeIntoLocation(testSource, testEmbeddedResources, testReferencedAssemblies, new DirectoryInfo(ts.TestAppDomain.BaseDirectory)); // act var proxy = ts.TestAppDomain.CreateInstanceFromAndUnwrap(testBinary.FullName, string.Format("{0}.{1}", testNamespace, testClassName)); // assert var libraryBinaryAssemblyName = AssemblyName.GetAssemblyName(libraryBinary.FullName); GivenAnEmbeddedAssemblyLoader.AssertAppDomainHasLoadedAssemblyName(false, ts.ParentAppDomain, libraryBinaryAssemblyName); GivenAnEmbeddedAssemblyLoader.AssertAppDomainHasLoadedAssemblyName(false, ts.TestAppDomain, libraryBinaryAssemblyName); var nestedLibraryBinaryAssemblyName = AssemblyName.GetAssemblyName(nestedLibraryBinary.FullName); GivenAnEmbeddedAssemblyLoader.AssertAppDomainHasLoadedAssemblyName(false, ts.ParentAppDomain, nestedLibraryBinaryAssemblyName); GivenAnEmbeddedAssemblyLoader.AssertAppDomainHasLoadedAssemblyName(true, ts.TestAppDomain, nestedLibraryBinaryAssemblyName); } finally { ActOnObject.IfNotNull(libraryBinary, (fi) => fi.Delete()); ActOnObject.IfNotNull(nestedLibraryBinary, (fi) => fi.Delete()); } }; GivenAnEmbeddedAssemblyLoader.ExecuteTestInsideTestAppDomain(testSetup); }