public void ImplementFinalMethodThatImplementsInterface()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(TargetObjectTest);
            Type proxy = builder.BuildProxyType();

            MethodInfo method = ReflectionUtils.GetMethod(proxy, "Spring.Proxy.ITargetObjectTest.InterfaceMethodWithArguments", new Type[] { typeof(string) });

            Assert.IsNotNull(method, "Should define this method.");
            Assert.AreEqual(proxy, method.DeclaringType, "Method should be defined in the proxy class.");

            // call final interfaced method
            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is ITargetObjectTest);
            ((ITargetObjectTest)foo).InterfaceMethodWithArguments("code");
        }
        public void OverrideVirtualMethod()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(TargetObjectTest);
            Type proxy = builder.BuildProxyType();

            MethodInfo method = ReflectionUtils.GetMethod(proxy, "InterfaceVirtualMethodWithNoArguments", Type.EmptyTypes);

            Assert.IsNotNull(method, "Should define this method.");
            Assert.AreEqual(proxy, method.DeclaringType, "Method should be overrided in the proxy class.");

            // call overrided method
            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is TargetObjectTest);
            ((TargetObjectTest)foo).InterfaceVirtualMethodWithNoArguments();
        }
        public void DoesNotImplementFinalMethodThatDoesNotImplementInterface()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(TargetObjectTest);
            Type proxy = builder.BuildProxyType();

            MethodInfo method = ReflectionUtils.GetMethod(proxy, "MethodWithArguments", new Type[] { typeof(string) });

            Assert.IsNotNull(method, "Should define this method.");
            Assert.AreNotEqual(proxy, method.DeclaringType, "Method can't be proxied.");

            // call base method
            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is TargetObjectTest);
            ((TargetObjectTest)foo).MethodWithArguments("code");
        }
        public void DoesNotProxyNonProxiableInterface()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ApplicationClass);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is IApplicationInterface);
            Assert.IsFalse(foo is IFrameworkInterface);

            // try to call proxied interface methods
            ((IApplicationInterface)foo).ApplicationMethod();
        }
Exemple #5
0
        public void ProxySpecificTargetTypeAttributeNotInstantiableWithDefaultValues()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithFakeGenerateScriptTypeAttribute);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object[] attrs = proxy.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(typeof(FakeGenerateScriptTypeAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type.");

            FakeGenerateScriptTypeAttribute fgsta = attrs[0] as FakeGenerateScriptTypeAttribute;

            Assert.AreEqual("ScriptName", fgsta.Name);
        }
Exemple #6
0
        public void ProxySpecificTargetTypeAttributeWithReadOnlyProperty()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithGuidAttribute);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object[] attrs = proxy.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(typeof(GuidAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type.");

            GuidAttribute ga = attrs[0] as GuidAttribute;

            Assert.AreEqual("7cfc9607-e81a-48ac-a080-bda88193607b", ga.Value);
        }
Exemple #7
0
        public void SetsInterfacesToProxy()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(MultipleInterfaces);
            builder.Interfaces = new Type[] { typeof(IBase) };

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is IBase);
            Assert.IsFalse(foo is IInherited);

            // try to call proxied interface methods
            ((IBase)foo).Base();
        }
Exemple #8
0
        public void ProxyWebServiceAttribute()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithWebServiceAttribute);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object[] attrs = proxy.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(typeof(WebServiceAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type.");

            WebServiceAttribute wsa = attrs[0] as WebServiceAttribute;

            Assert.AreEqual("blah", wsa.Name);
            Assert.AreEqual("http://mynamespace.com", wsa.Namespace);
        }
Exemple #9
0
        public void ProxyTargetMethodParameterAttributes()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(SomeMarkerClass);
            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");
            MethodInfo method = proxy.GetMethod("Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance);

            if (method == null)
            {
                method = proxy.GetMethod("MarkerMethod");
            }
            Assert.IsNotNull(method);
            object[] attrs = method.GetParameters()[1].GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the method's parameter.");
        }
Exemple #10
0
        public void DoesNotProxyTargetMethodReturnValueAttributesWithProxyTargetAttributesEqualsFalse()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType            = typeof(SomeMarkerClass);
            builder.ProxyTargetAttributes = false;
            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");
            MethodInfo method = proxy.GetMethod("Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance);

            if (method == null)
            {
                method = proxy.GetMethod("MarkerMethod");
            }
            Assert.IsNotNull(method);
            object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false);
            Assert.IsNotNull(attrs);
            Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the method's return value.");
        }
        public void DoesNotProxyNonProxiableInterface()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ApplicationClass);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is IApplicationInterface);
            Assert.IsTrue(foo is IFrameworkInterface);

            // proxy implements IApplicationInterface and proxy methods
            Assert.IsNotNull(foo.GetType().GetMethod("Spring.Proxy.IApplicationInterface.ApplicationMethod", BindingFlags.Instance | BindingFlags.NonPublic));

            // proxy implements IFrameworkInterface but should not proxy methods
            Assert.IsNull(foo.GetType().GetMethod("Spring.Proxy.IFrameworkInterface.FrameworkMethod", BindingFlags.Instance | BindingFlags.NonPublic));
        }
        [Test] // SPRNET-1424
        public void DoesNotProxyInterfaceMethodAttributes()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(object);
            builder.Interfaces = new Type[] { typeof(IAnotherMarkerInterface) };

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");
            MethodInfo method = proxy.GetMethod("Oragon.Spring.Proxy.IAnotherMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance);

            if (method == null)
            {
                method = proxy.GetMethod("MarkerMethod");
            }
            Assert.IsNotNull(method);
            object[] attrs = method.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have 0 attribute applied to the target method.");
            Assert.AreEqual(0, attrs.Length, "Should have 0 attribute applied to the target method.");
        }
Exemple #13
0
        // SPRNET-1262
        public void AppliesSpecificTypeAttributeWithNoPropertySettersToTargetTypeUsingCustomAttributeBuilder()
        {
            ConstructorInfo        ci  = typeof(FakeServiceKnownTypeAttribute).GetConstructor(new Type[] { typeof(Type) });
            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new object[] { typeof(TestObject) });

            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithFakeServiceKnownTypeAttribute);
            builder.TypeAttributes.Add(cab);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            object[] attrs = proxy.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type.");
            Assert.AreEqual(typeof(FakeServiceKnownTypeAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type.");

            FakeServiceKnownTypeAttribute fgsta = attrs[0] as FakeServiceKnownTypeAttribute;

            Assert.AreEqual(typeof(TestObject), fgsta.Type, "Property 'Type' on Target Attribute not set!");
        }
Exemple #14
0
        public void ExplicitInterfaceImplementation()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(MultipleInterfaces);
            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");
            object foo = Activator.CreateInstance(proxy);

            Assert.IsTrue(foo is IBase);
            Assert.IsTrue(foo is IInherited);

            Type fooType = foo.GetType();

            MethodInfo[] methods = fooType.GetMethods(BindingFlags.DeclaredOnly);
            Assert.AreEqual(0, methods.Length, "Methods array should be empty.");

            methods = fooType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);
            Assert.AreEqual(2, methods.Length, "Methods array should have two elements.");
            Assert.IsTrue(methods[0].Name.IndexOf('.') > 0, "Method should have '.' in the name");
            Assert.IsTrue(methods[1].Name.IndexOf('.') > 0, "Method should have '.' in the name");
        }
Exemple #15
0
        public void ProxyGenericInterface()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassThatImplementsGenericInterface <TestObject>);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            GenericInterface <TestObject> foo = Activator.CreateInstance(proxy) as GenericInterface <TestObject>;

            Assert.IsNotNull(foo);

            TestObject to1 = foo.Create();

            Assert.IsNotNull(to1);

            TestObject to2 = foo.Populate(new TestObject());

            Assert.IsNotNull(to2);
            Assert.AreEqual("Populated", to2.Name);
        }
Exemple #16
0
        public void ProxySpecificTargetTypeAttributeWithPublicEnumProperty()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithPublicEnumPropertyAttribute);
            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");
            MethodInfo method = proxy.GetMethod("Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance);

            if (method == null)
            {
                method = proxy.GetMethod("MarkerMethod");
            }
            Assert.IsNotNull(method);
            object[] attrs = method.GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have 1 attribute applied to the target method.");
            Assert.AreEqual(1, attrs.Length, "Should have 1 attribute applied to the target method.");
            Assert.AreEqual(typeof(PublicEnumPropertyAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target method.");

            PublicEnumPropertyAttribute pepa = attrs[0] as PublicEnumPropertyAttribute;

            Assert.AreEqual(AttributeTargets.All, pepa.Data);
        }
Exemple #17
0
        public void AppliesStarredMemberAttributesToAllMethods()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(TargetObjectTest);
            IDictionary atts         = new Hashtable();
            string      methodPrefix = typeof(ITargetObjectTest).FullName + ".";

            atts.Add("*", new Attribute[] { new MarkerAttribute() });
            builder.MemberAttributes = atts;
            Type   proxy = builder.BuildProxyType();
            object foo   = Activator.CreateInstance(proxy);

            Type       fooType = foo.GetType();
            MethodInfo method  = ReflectionUtils.GetMethod(fooType, methodPrefix + "InterfaceMethodWithArguments", new Type[] { typeof(string) });

            object[] appliedAttributes = method.GetCustomAttributes(false);
            Assert.AreEqual(1, appliedAttributes.Length,
                            "Custom attribute not applied to member method.");
            method            = ReflectionUtils.GetMethod(fooType, methodPrefix + "InterfaceVirtualMethodWithNoArguments", Type.EmptyTypes);
            appliedAttributes = method.GetCustomAttributes(false);
            Assert.AreEqual(1, appliedAttributes.Length,
                            "Custom attribute not applied to member method.");
        }
Exemple #18
0
        public void ProxyGenericMethod()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.TargetType = typeof(ClassWithGenericMethod);

            Type proxy = builder.BuildProxyType();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null.");

            InterfaceWithGenericMethod foo = Activator.CreateInstance(proxy) as InterfaceWithGenericMethod;

            Assert.IsNotNull(foo);

            foo.PolymorphicMethod <int>();

            string result1 = foo.PolymorphicMethod("coucou");

            Assert.AreEqual("coucou", result1);

            foo.WithGenericParameter <string>();

            foo.WithGenericParameterAndGenericArgument <string>("ola");

            string result2 = foo.WithGenericParameterAndGenericArgumentAndGenericReturnType <string>("ola");

            Assert.AreEqual("ola", result2);

            foo.WithInterfaceConstraint <bool>();

            foo.WithBaseTypeConstraint <DerivedTestObject>();

            foo.WithBaseTypeAndInterfaceConstraints <DerivedTestObject>();

            foo.WithMixedConstraint <bool, DerivedTestObject>();
        }
        /// <summary>
        /// Generates the proxy type and caches the <see cref="System.Type"/>
        /// instance against the base type and the interfaces to proxy.
        /// </summary>
        /// <param name="typeBuilder">
        /// The <see cref="Spring.Proxy.IProxyTypeBuilder"/> to use
        /// </param>
        /// <returns>The generated or cached proxy class.</returns>
        protected override Type BuildProxyType(IProxyTypeBuilder typeBuilder)
        {
            ProxyTypeCacheKey cacheKey = new ProxyTypeCacheKey(
                typeBuilder.BaseType, typeBuilder.TargetType, typeBuilder.Interfaces, typeBuilder.ProxyTargetAttributes);
            Type proxyType = null;
            lock (typeCache)
            {
                proxyType = typeCache[cacheKey] as Type;
                if (proxyType == null)
                {
                    proxyType = typeBuilder.BuildProxyType();
                    typeCache[cacheKey] = proxyType;
                }
                else
                {
                    #region Instrumentation

                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(String.Format(
                                        "AOP proxy type found in cache for '{0}'.", cacheKey));
                    }

                    #endregion
                }
            }
            return proxyType;
        }
        public void BuildProxyWithNothingSet()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            Assert.Throws <NullReferenceException>(() => builder.BuildProxyType());
        }
Exemple #21
0
        public void BuildProxyWithNothingSet()
        {
            IProxyTypeBuilder builder = GetProxyBuilder();

            builder.BuildProxyType();
        }
 /// <summary>
 /// Generates the proxy type.
 /// </summary>
 /// <param name="typeBuilder">
 /// The <see cref="Spring.Proxy.IProxyTypeBuilder"/> to use
 /// </param>
 /// <returns>The generated proxy class.</returns>
 protected virtual Type BuildProxyType(IProxyTypeBuilder typeBuilder)
 {
     return(typeBuilder.BuildProxyType());
 }
 /// <summary>
 /// Generates the proxy type.
 /// </summary>
 /// <param name="typeBuilder">
 /// The <see cref="Spring.Proxy.IProxyTypeBuilder"/> to use
 /// </param>
 /// <returns>The generated proxy class.</returns>
 protected virtual Type BuildProxyType(IProxyTypeBuilder typeBuilder)
 {
     return typeBuilder.BuildProxyType();
 }