PageRenderTime 204ms CodeModel.GetById 161ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/src/UnitTests/Reflection/ReflectionEmitTests.cs

http://github.com/philiplaureano/LinFu
C# | 131 lines | 97 code | 28 blank | 6 comment | 4 complexity | ea845dfe595199c123443c4288e82277 MD5 | raw file
  1using System;
  2using System.IO;
  3using System.Linq;
  4using System.Reflection;
  5using LinFu.AOP.Cecil;
  6using LinFu.IoC;
  7using LinFu.IoC.Configuration;
  8using LinFu.IoC.Configuration.Interfaces;
  9using LinFu.Reflection.Emit;
 10using Mono.Cecil;
 11using Xunit;
 12using SampleStronglyNamedLibrary;
 13
 14namespace LinFu.UnitTests.Reflection
 15{
 16    public class ReflectionEmitTests : BasePEVerifyTestCase
 17    {
 18        private Loader loader;
 19        private ServiceContainer container;
 20        private string filename;
 21
 22        protected override void OnInit()
 23        {
 24            if (File.Exists(filename))
 25                File.Delete(filename);
 26
 27            // Initialize the loader
 28            // with all of the default LinFu plugins
 29            loader = new Loader();
 30            container = new ServiceContainer();
 31            filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.dll", Guid.NewGuid()));
 32            AutoDelete(filename);
 33
 34            loader.LoadDirectory(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
 35            loader.LoadInto(container);
 36        }
 37
 38        protected override void OnTerm()
 39        {
 40            if (!File.Exists(filename))
 41                return;
 42
 43            PEVerify(filename);
 44        }
 45
 46
 47        [Fact]
 48        public void AssemblyDefinitionMustBeConvertibleToActualAssembly()
 49        {
 50            var name = new AssemblyNameDefinition("testAssembly", new Version(1, 0));
 51            var definition = AssemblyDefinition.CreateAssembly(name, "testModule", ModuleKind.Dll);
 52
 53            var assembly = definition.ToAssembly();
 54            Assert.True(assembly != null);
 55        }
 56
 57        [Fact]
 58        public void CecilShouldExtractSampleClassFromSignedAssembly()
 59        {
 60            var location = typeof(SampleHelloClass).Assembly.Location;
 61
 62            var sourceAssembly = AssemblyDefinition.ReadAssembly(location);
 63            Assert.NotNull(sourceAssembly);
 64
 65            var name = new AssemblyNameDefinition("testAssembly", new Version(1, 0));
 66            var definition = AssemblyDefinition.CreateAssembly(name, "testModule", ModuleKind.Dll);
 67            var targetModule = definition.MainModule;
 68            foreach (TypeDefinition typeDef in sourceAssembly.MainModule.Types)
 69            {
 70                // Copy the source type to the target assembly
 71                targetModule.Types.Add(typeDef);   
 72            }        
 73
 74            // Convert the new assemblyDef into an actual assembly
 75            var assembly = definition.ToAssembly();
 76            Assert.NotNull(assembly);
 77
 78            var types = assembly.GetTypes();
 79            Assert.True(types.Length > 0);
 80
 81            // The imported type must match the original type
 82            var firstType = types.FirstOrDefault();
 83            Assert.NotNull(firstType);
 84            Assert.Equal(firstType.Name, typeof(SampleHelloClass).Name);
 85
 86            var instance = Activator.CreateInstance(firstType);
 87            Assert.NotNull(instance);
 88
 89            var speakMethod = firstType.GetMethod("Speak");
 90            Assert.NotNull(speakMethod);
 91
 92            speakMethod.Invoke(instance, new object[] { });
 93        }
 94
 95        [Fact]
 96        public void CecilShouldRemoveStrongNameFromAssembly()
 97        {
 98            var location = typeof(SampleHelloClass).Assembly.Location;
 99
100            var sourceAssembly = AssemblyDefinition.ReadAssembly(location);
101
102            Assert.NotNull(sourceAssembly);
103            sourceAssembly.RemoveStrongName();
104
105            var assembly = sourceAssembly.ToAssembly();
106            Assert.NotNull(assembly);
107
108            var assemblyName = assembly.GetName();
109
110            // The public key should be empty
111            var bytes = assemblyName.GetPublicKey();
112            Assert.True(bytes.Length == 0);
113        }
114
115        [Fact]
116        public void MethodInvokerShouldProperlyHandleReturnValues()
117        {
118            var targetMethod = typeof(object).GetMethod("GetHashCode");
119            var instance = new object();
120
121            var hash = instance.GetHashCode();
122            container.AddDefaultServices();
123
124            var invoker = container.GetService<IMethodInvoke<MethodInfo>>();
125            Assert.NotNull(invoker);
126
127            var result = invoker.Invoke(instance, targetMethod);
128            Assert.Equal(result, hash);
129        }
130    }
131}