AopUtilsTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Aop » Framework » DynamicProxy » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Aop » Framework » DynamicProxy » AopUtilsTests.cs
#region License

/*
 * Copyright  2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Collections;
using System.Reflection;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Aop.Interceptor;
using Spring.Aop.Support;
using Spring.Collections;
using Spring.Objects;

#endregion

namespace Spring.Aop.Framework.DynamicProxy{
    /// <summary>
    /// Unit tests for the AopUtils class.
    /// </summary>
    /// <author>Rod Johnson</author>
    /// <author>Choy Rim (.NET)</author>
    [TestFixture]
    public sealed class AopUtilsTests
    {
        private MockRepository mocks;

        [SetUp]
        public void SetUp()
        {
            mocks = new MockRepository();
        }

        [Test]
        public void PointcutCanNeverApply()
        {
            IPointcut pointcut = new NeverMatchesPointcut();
            Assert.IsFalse(AopUtils.CanApply(pointcut, typeof (Object), null));
        }

        [Test]
        public void PointcutAlwaysApplies()
        {
            Assert.IsTrue(AopUtils.CanApply(new DefaultPointcutAdvisor(new NopInterceptor()), typeof (Object), null));
            Assert.IsTrue(AopUtils.CanApply(new DefaultPointcutAdvisor(new NopInterceptor()), typeof (TestObject), new Type[] {typeof (ITestObject)}));
        }

        [Test]
        public void PointcutAppliesToOneMethodOnObject()
        {
            IPointcut pointcut = new OneMethodTestPointcut();
            Assert.IsTrue(AopUtils.CanApply(pointcut, typeof (object), null),
                          "Must return true if we're not proxying interfaces.");
            Assert.IsFalse(AopUtils.CanApply(pointcut, typeof (object),
                                             new Type[] {typeof (ITestObject)}),
                           "Must return false if we're proxying interfaces.");
        }

        [Test]
        public void PointcutAppliesToOneInterfaceOfSeveral()
        {
            IPointcut pointcut = new OneInterfaceTestPointcut();

            // Will return true if we're proxying interfaces including ITestObject 
            Assert.IsTrue(AopUtils.CanApply(pointcut, typeof (TestObject), new Type[] {typeof (ITestObject), typeof (IComparable)}));

            // Will return true if we're proxying interfaces including ITestObject 
            Assert.IsFalse(AopUtils.CanApply(pointcut, typeof (TestObject), new Type[] {typeof (IComparable)}));
        }

        [Test]
        public void CanApplyWithAdvisorYieldsTrueIfAdvisorIsNotKnownAdvisorType()
        {
            IAdvisor advisor = (IAdvisor) mocks.CreateMock(typeof (IAdvisor));
            Assert.IsTrue(AopUtils.CanApply(advisor, typeof (TestObject), null));
        }

        [Test]
        public void CanApplyWithAdvisorYieldsTrueIfAdvisorIsNull()
        {
            Assert.IsTrue(AopUtils.CanApply((IAdvisor) null, typeof (TestObject), null));
        }

        /// <summary>
        /// Test preconditions for all tests related to GetAllInterfaces
        /// </summary>
        [Test]
        public void GetAllInterfacesTestsPreconditions()
        {
            DerivedTestObject testObject = new DerivedTestObject();
            IList interfaces = new ArrayList(testObject.GetType().GetInterfaces());
            Assert.AreEqual(9, interfaces.Count, "Incorrect number of interfaces");
            Assert.IsTrue(interfaces.Contains(typeof(ITestObject)), "Does not contain ITestObject");
            Assert.IsTrue(interfaces.Contains(typeof(IOther)), "Does not contain IOther");
        }

        [Test]
        public void GetAllInterfacesWithNull()
        {
            Type[] interfaces = AopUtils.GetAllInterfaces(null);
            Assert.IsNotNull(interfaces,
                             "Must never return null, even if the argument is null.");
            Assert.AreEqual(0, interfaces.Length,
                            "Must return an empty array is the argument is null.");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetAllInterfacesFromTypeWithNull()
        {
            AopUtils.GetAllInterfacesFromType(null);
        }

        [Test]
        public void GetAllInterfacesWithObjectThatDoesntImpementAnything()
        {
            ImplementsNothing instance = new ImplementsNothing();
            Type[] interfaces = AopUtils.GetAllInterfaces(instance);
            Assert.IsNotNull(interfaces,
                             "Must never return null, even if the argument doesn't implement any interfaces.");
            Assert.AreEqual(0, interfaces.Length,
                            "Must return an empty array is the argument doesn't implement any interfaces.");
        }

        [Test]
        public void GetAllInterfacesSunnyDay()
        {
            ImplementsTwoInterfaces instance = new ImplementsTwoInterfaces();
            Type[] interfaces = AopUtils.GetAllInterfaces(instance);
            Assert.IsNotNull(interfaces, "Must never return null.");
            Assert.AreEqual(2, interfaces.Length,
                            "Implements two interfaces.");
            ISet ifaces = new ListSet(interfaces);
            Assert.IsTrue(
                ifaces.ContainsAll(
                    new Type [] {typeof(IDisposable), typeof(ICloneable)}),
                "Did not find the correct interfaces.");
        }

        [Test]
        public void GetAllInterfacesWithObjectThatInheritsInterfaces()
        {
            InheritsOneInterface instance = new InheritsOneInterface();
            Type[] interfaces = AopUtils.GetAllInterfaces(instance);
            Assert.IsNotNull(interfaces, "Must never return null.");
            Assert.AreEqual(1, interfaces.Length,
                            "Inherited one interface from superclass.");
            Type iface = interfaces[0];
            Assert.IsNotNull(iface, "Returned interface cannot be null.");
            Assert.AreEqual(typeof(IDisposable), iface, "Wrong interface returned.");
        }

        [Test]
        public void CanApplyWithTrueIntroductionAdvisor()
        {
            IIntroductionAdvisor mockIntroAdvisor = (IIntroductionAdvisor) mocks.CreateMock(typeof(IIntroductionAdvisor));
            Expect.Call(mockIntroAdvisor.TypeFilter).Return(TrueTypeFilter.True);
            mocks.ReplayAll();

            Assert.IsTrue(AopUtils.CanApply(mockIntroAdvisor, typeof (TestObject), null));
            mocks.VerifyAll();
        }

        #region Helper Classes

        private sealed class OneMethodTestPointcut : StaticMethodMatcherPointcut
        {
            public override bool Matches(MethodInfo m, Type targetClass)
            {
                return m.Name.Equals("GetHashCode");
            }
        }

        private sealed class OneInterfaceTestPointcut : StaticMethodMatcherPointcut
        {
            public override bool Matches(MethodInfo m, Type targetClass)
            {
                return m.Name.Equals("ReturnsThis");
            }
        }

        private sealed class NeverMatchesPointcut : StaticMethodMatcherPointcut
        {
            public override bool Matches(MethodInfo m, Type targetClass)
            {
                return false;
            }
        }

        private sealed class ImplementsNothing
        {
        }

        private abstract class ImplementsOneInterface : IDisposable
        {
            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }

        private sealed class InheritsOneInterface : ImplementsOneInterface
        {
        }

        private sealed class ImplementsTwoInterfaces : IDisposable, ICloneable
        {
            public void Dispose()
            {
                throw new NotImplementedException();
            }

            public object Clone()
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.