Example #1
0
        public void GetAssemblyPath_WithHashInDirectoryName()
        {
            string directoryPath        = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "#HashTestPath");
            string originalAssemblyPath = typeof(ReflectionUtilityTest).Assembly.Location;
            string newAssemblyPath      = Path.Combine(directoryPath, Path.GetFileName(originalAssemblyPath));

            if (Directory.Exists(directoryPath))
            {
                Directory.Delete(directoryPath, true);
            }

            Directory.CreateDirectory(directoryPath);
            try
            {
                File.Copy(originalAssemblyPath, newAssemblyPath);
                AppDomainRunner.Run(
                    delegate(object[] args)
                {
                    string directory    = (string)args[0];
                    string assemblyPath = (string)args[1];

                    Assembly assembly = Assembly.LoadFile(assemblyPath);
                    Assert.That(Path.GetDirectoryName(assembly.Location), Is.EqualTo(directory));
                    Assert.That(ReflectionUtility.GetAssemblyDirectory(assembly), Is.EqualTo(directory));
                },
                    directoryPath,
                    newAssemblyPath);
            }
            finally
            {
                Directory.Delete(directoryPath, true);
            }
        }
 public void SpecificAppBase()
 {
     AppDomainRunner.Run(@"C:\", delegate(object[] args)
     {
         Assert.That(AppDomain.CurrentDomain.BaseDirectory, Is.EqualTo(@"C:\"));
     });
 }
        public void DoesntChangeCurrentSetup()
        {
            string dynamicBaseBefore = AppDomain.CurrentDomain.SetupInformation.DynamicBase;

            AppDomainRunner.Run(delegate { new AppDomainRunnerTest(); });
            Assert.That(AppDomain.CurrentDomain.SetupInformation.DynamicBase, Is.EqualTo(dynamicBaseBefore));
        }
        public void ApplicationAssemblyInclusion_DependsOnAttribute()
        {
            string compiledAssemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NonApplicationMarkedAssembly.dll");

            try
            {
                AppDomainRunner.Run(
                    delegate(object[] args)
                {
                    var path = (string)args[0];

                    ApplicationAssemblyLoaderFilter filter = ApplicationAssemblyLoaderFilter.Instance;
                    Assert.That(filter.ShouldIncludeAssembly(typeof(AttributeAssemblyLoaderFilterTest).Assembly), Is.True);
                    Assert.That(filter.ShouldIncludeAssembly(typeof(TestFixtureAttribute).Assembly), Is.True);
                    Assert.That(filter.ShouldIncludeAssembly(typeof(ApplicationAssemblyLoaderFilter).Assembly), Is.True);
                    Assert.That(filter.ShouldIncludeAssembly(typeof(object).Assembly), Is.True);
                    Assert.That(filter.ShouldIncludeAssembly(typeof(Uri).Assembly), Is.True);

                    var assemblyCompiler = new AssemblyCompiler(@"Reflection\TypeDiscovery\TestAssemblies\NonApplicationMarkedAssembly", path,
                                                                typeof(NonApplicationAssemblyAttribute).Assembly.Location);
                    assemblyCompiler.Compile();
                    Assert.That(filter.ShouldIncludeAssembly(assemblyCompiler.CompiledAssembly), Is.False);
                }, compiledAssemblyPath);
            }
            finally
            {
                if (File.Exists(compiledAssemblyPath))
                {
                    FileUtility.DeleteAndWaitForCompletion(compiledAssemblyPath);
                }
            }
        }
 public void GetDefaultQueryFilePath_ThrowsIfNoQueryFileExists()
 {
     AppDomainRunner.Run(@"C:\", delegate
     {
         QueryConfiguration configuration = new QueryConfiguration();
         configuration.GetDefaultQueryFilePath();
     });
 }
        public void AppDomainIsCreated()
        {
            AppDomain current = AppDomain.CurrentDomain;

            AppDomainRunner.Run(
                delegate(object[] args)
            {
                Assert.That(AppDomain.CurrentDomain.FriendlyName, Is.EqualTo("AppDomainRunner - AppDomain"));
                Assert.That(AppDomain.CurrentDomain, Is.Not.SameAs(args[0]));
            },
                current);
        }
        public void ArgumentsArePassedInCorrectly()
        {
            AppDomainRunner.Run(delegate(object[] args)
            {
                Assert.That(args.Length, Is.EqualTo(2));
                Assert.That("Foo", Is.EqualTo(args[0]));
                Assert.That(4, Is.EqualTo(args[1]));
            }, "Foo", 4);

            AppDomainRunner.Run(delegate(object[] args)
            {
                Assert.That(args.Length, Is.EqualTo(0));
            });
        }
        public void FindAssemblies_FindsReferencedAssemblies_Transitive()
        {
            const string buildOutputDirectory = "Reflection.AssemblyFinderTest.FindAssemblies_FindsReferencedAssemblies_Transitive";
            const string sourceDirectoryRoot  = @"Reflection\TypeDiscovery\AssemblyFinding\TestAssemblies\AssemblyFinderTest";

            Action <object[]> testAction = delegate(object[] args)
            {
                var outputManagerWithoutClosure = (AssemblyCompilerBuildOutputManager)args[0];

                // dependency chain: mixinSamples -> remotion -> log4net
                var log4NetAssembly     = typeof(log4net.LogManager).Assembly;
                var remotionAssembly    = typeof(AssemblyFinder).Assembly;
                var referencingAssembly = CompileReferencingAssembly(outputManagerWithoutClosure, remotionAssembly);

                var loaderMock = MockRepository.GenerateMock <IAssemblyLoader>();
                loaderMock
                .Expect(mock => mock.TryLoadAssembly(ArgReferenceMatchesDefinition(remotionAssembly), Arg.Is(referencingAssembly.FullName)))
                // load re-motion via samples
                .Return(remotionAssembly);
                loaderMock
                .Expect(mock => mock.TryLoadAssembly(ArgReferenceMatchesDefinition(log4NetAssembly), Arg.Is(remotionAssembly.FullName)))
                // load log4net via re-motion
                .Return(log4NetAssembly);
                loaderMock.Replay();

                var rootAssemblyFinderStub = MockRepository.GenerateMock <IRootAssemblyFinder>();
                rootAssemblyFinderStub.Stub(stub => stub.FindRootAssemblies()).Return(
                    new[] { new RootAssembly(referencingAssembly, true) });
                rootAssemblyFinderStub.Replay();

                var finder = new AssemblyFinder(rootAssemblyFinderStub, loaderMock);
                var result = finder.FindAssemblies();

                loaderMock.VerifyAllExpectations();
                Assert.That(result, Is.EquivalentTo(new[] { referencingAssembly, remotionAssembly, log4NetAssembly }));
            };

            // Run test action in separate AppDomain
            using (var outputManager = new AssemblyCompilerBuildOutputManager(buildOutputDirectory, true, sourceDirectoryRoot))
            {
                AppDomainRunner.Run(testAction, outputManager);
            }
        }
        public void SavesMixedTypes()
        {
            AppDomainRunner.Run(
                delegate
            {
                using (MixinConfiguration.BuildNew()
                       .ForClass <BaseType1>().AddMixins(typeof(BT1Mixin1))
                       .ForClass <Page> ().AddMixin(typeof(NullMixin))
                       .EnterScope())
                {
                    Mixer mixer = Mixer.Create("Assembly", _assemblyOutputDirectory, 1);
                    mixer.PrepareOutputDirectory();
                    mixer.Execute(MixinConfiguration.ActiveConfiguration);

                    Assembly theAssembly = Assembly.LoadFile(mixer.MixerPipelineFactory.GetModulePaths(_assemblyOutputDirectory).Single());
                    var types            = theAssembly.GetTypes();

                    var concreteType = types.SingleOrDefault(t => t.BaseType == typeof(BaseType1));
                    Assert.NotNull(concreteType);
                    Assert.That(
                        MixinTypeUtility.GetClassContextForConcreteType(concreteType),
                        Is.EqualTo(MixinConfiguration.ActiveConfiguration.GetContext(typeof(BaseType1))));

                    object instance = Activator.CreateInstance(concreteType);
                    Assert.That(Mixin.Get <BT1Mixin1> (instance), Is.Not.Null);

                    var concreteTypeFromSystemAssembly = types.SingleOrDefault(t => t.BaseType == typeof(Page));
                    Assert.That(concreteTypeFromSystemAssembly, Is.Not.Null);

                    SafeServiceLocator.Current.GetInstance <IPipelineRegistry>().DefaultPipeline.CodeManager.LoadFlushedCode(theAssembly);

                    Type concreteTypeFromFactory = TypeFactory.GetConcreteType(typeof(BaseType1));
                    Assert.That(concreteTypeFromFactory, Is.SameAs(concreteType));

                    Assert.That(theAssembly.IsDefined(typeof(NonApplicationAssemblyAttribute), false), Is.True);
                }
            });
        }
 public void TypesFromRunnerBaseAssemblyCanBeAccessed_EvenWithDifferentBaseDirectory()
 {
     AppDomainRunner.Run(@"c:\", delegate { Dev.Null = typeof(AppDomainRunnerBase); });
 }
 public void TypesFromCurrentAssemblyCanBeAccessed_EvenWithDifferentBaseDirectory()
 {
     AppDomainRunner.Run(@"c:\", delegate { new AppDomainRunnerTest(); });
 }
 public void TypesFromCurrentAssemblyCanBeAccessed()
 {
     AppDomainRunner.Run(delegate { new AppDomainRunnerTest(); });
 }