AssertUtilsTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Util » 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 » Util » AssertUtilsTests.cs
#region License

/*
 * Copyright 2004 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.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using NUnit.Framework;
using Spring.Objects;

#endregion

namespace Spring.Util{
    /// <summary>
    /// Unit tests for the AssertUtils class.
    /// </summary>
    /// <author>Rick Evans</author>
    [TestFixture]
    public sealed class AssertUtilsTests
    {
        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "foo")]
        public void IsTrueWithMesssage()
        {
            AssertUtils.IsTrue(false, "foo");
        }

        [Test]
        public void IsTrueWithMessageValidExpression()
        {
            AssertUtils.IsTrue(true, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException), ExpectedMessage = "[Assertion failed] - this expression must be true")]
        public void IsTrue()
        {
            AssertUtils.IsTrue(false);
        }

        [Test]
        public void IsTrueValidExpression()
        {
            AssertUtils.IsTrue(true);
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void StateTrue()
        {
            AssertUtils.State(false, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentNotNull()
        {
            AssertUtils.ArgumentNotNull(null, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentNotNullWithMessage()
        {
            AssertUtils.ArgumentNotNull(null, "foo", "Bang!");
        }

        [Test]
        public void ArgumentHasTextWithValidText()
        {
            AssertUtils.ArgumentHasText("... and no-one's getting fat 'cept Mama Cas!", "foo");
        }

        [Test]
        public void ArgumentHasTextWithValidTextAndMessage()
        {
            AssertUtils.ArgumentHasText("... and no-one's getting fat 'cept Mama Cas!", "foo", "Bang!");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasText()
        {
            AssertUtils.ArgumentHasText(null, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasTextWithMessage()
        {
            AssertUtils.ArgumentHasText(null, "foo", "Bang!");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasLengthArgumentIsNull()
        {
            AssertUtils.ArgumentHasLength(null, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasLengthArgumentIsNullWithMessage()
        {
            AssertUtils.ArgumentHasLength(null, "foo", "Bang!");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasLengthArgumentIsEmpty()
        {
            AssertUtils.ArgumentHasLength(new byte[0], "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentHasLengthArgumentIsEmptyWithMessage()
        {
            AssertUtils.ArgumentHasLength(new byte[0], "foo", "Bang!");
        }

        [Test]
        public void ArgumentHasLengthArgumentHasElements()
        {
            AssertUtils.ArgumentHasLength(new byte[1], "foo");
        }

        [Test]
        public void ArgumentHasLengthArgumentHasElementsWithMessage()
        {
            AssertUtils.ArgumentHasLength(new byte[1], "foo", "Bang!");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void ArgumentHasElementsArgumentIsNull()
        {
            AssertUtils.ArgumentHasElements(null, "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void ArgumentHasElementsArgumentIsEmpty()
        {
            AssertUtils.ArgumentHasElements(new object[0], "foo");
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void ArgumentHasElementsArgumentContainsNull()
        {
            AssertUtils.ArgumentHasElements(new object[] { new object(), null, new object() }, "foo");
        }

        [Test]
        public void ArgumentHasElementsArgumentContainsNonNullsOnly()
        {
            AssertUtils.ArgumentHasElements(new object[] { new object(), new object(), new object() }, "foo");
        }

        [Test]
        public void UnderstandsType()
        {
            MethodInfo getDescriptionMethod = typeof(ITestObject).GetMethod("GetDescription", new Type[0]);
            MethodInfo understandsMethod = typeof(AssertUtils).GetMethod("Understands", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof (object), typeof(string), typeof (MethodBase)}, null);

            // null target, any type
            AssertNotUnderstandsType(null, "target", typeof(object), typeof(NotSupportedException), "Target 'target' is null.");
            // any target, null type
            AssertNotUnderstandsType(new object(), "target", null, typeof(ArgumentNullException), "Argument 'requiredType' cannot be null.");
        }

        [Test]
        public void UnderstandsMethod()
        {
            MethodInfo getDescriptionMethod = typeof(ITestObject).GetMethod("GetDescription", new Type[0]);
            MethodInfo understandsMethod = typeof(AssertUtils).GetMethod("Understands", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof (object), typeof(string), typeof (MethodBase)}, null);

            // null target, static method
            AssertUtils.Understands(null, "target", understandsMethod);
            // null target, instance method
            AssertNotUnderstandsMethod(null, "target", getDescriptionMethod, typeof(NotSupportedException), "Target 'target' is null and target method 'Spring.Objects.ITestObject.GetDescription' is not static.");
            // compatible target, instance method
            AssertUtils.Understands(new TestObject(), "target", getDescriptionMethod);
            // incompatible target, instance method
                AssertNotUnderstandsMethod(new object(), "target", getDescriptionMethod, typeof(NotSupportedException), "Target 'target' of type 'System.Object' does not support methods of 'Spring.Objects.ITestObject'.");
            // compatible transparent proxy, instance method
            object compatibleProxy = new TestProxy(new TestObject()).GetTransparentProxy();
            AssertUtils.Understands(compatibleProxy, "compatibleProxy", getDescriptionMethod);
            // incompatible transparent proxy, instance method
            object incompatibleProxy = new TestProxy(new object()).GetTransparentProxy();
            AssertNotUnderstandsMethod(incompatibleProxy, "incompatibleProxy", getDescriptionMethod, typeof(NotSupportedException), "Target 'incompatibleProxy' is a transparent proxy that does not support methods of 'Spring.Objects.ITestObject'.");
        }

        private void AssertNotUnderstandsType(object target, string targetName, Type requiredType, Type exceptionType, string partialMessage)
        {
            try
            {
                AssertUtils.Understands(target, targetName, requiredType);
                Assert.Fail();
            }
            catch(Exception ex)
            {
                if (ex.GetType() != exceptionType)
                {
                    Assert.Fail("Expected Exception of type {0}, but was {1}", exceptionType, ex.GetType());
                }

                if (-1 == ex.Message.IndexOf(partialMessage))
                {
                    Assert.Fail("Expected Message '{0}', but got '{1}'", partialMessage, ex.Message);
                }
            }
        }

        private void AssertNotUnderstandsMethod(object target, string targetName, MethodBase method, Type exceptionType, string partialMessage)
        {
            try
            {
                AssertUtils.Understands(target, targetName, method);
                Assert.Fail();
            }
            catch(Exception ex)
            {
                if (ex.GetType() != exceptionType)
                {
                    Assert.Fail("Expected Exception of type {0}, but was {1}", exceptionType, ex.GetType());
                }

                if (-1 == ex.Message.IndexOf(partialMessage))
                {
                    Assert.Fail("Expected Message '{0}', but got '{1}'", partialMessage, ex.Message);
                }
            }
        }

        private class TestProxy : RealProxy, IRemotingTypeInfo
        {
            private readonly object targetInstance;

            public TestProxy(object targetInstance) 
                : base(typeof(MarshalByRefObject))
            {
                this.targetInstance = targetInstance;
            }

            public override IMessage Invoke(IMessage msg)
            {
                //                    return new ReturnMessage(result, null, 0, null, callMsg);
                throw new NotSupportedException();
            }

            public bool CanCastTo(Type fromType, object o)
            {
                bool res = fromType.IsAssignableFrom(targetInstance.GetType());
                return res;
            }

            public string TypeName
            {
                get { return targetInstance.GetType().AssemblyQualifiedName; }
                set { throw new System.NotSupportedException(); }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.