PropertyComparatorTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Objects » Support » 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 » Objects » Support » PropertyComparatorTests.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 NUnit.Framework;
using Spring.Core;

#endregion

namespace Spring.Objects.Support{
    /// <summary>
    /// Unit tests for the PropertyComparator class.
    /// </summary>
    /// <remarks>
    /// <p>
    /// The sort algorithm for arrays changed between versions 1.1 and 2.0 of the .NET
    /// Framework. Hence there are two batches of tests in this suite, one for versions
    /// 1.1 and 2.0 of the .NET Framework respectively.
    /// </p>
    /// </remarks>
    /// <author>Rick Evans</author>
    /// <author>Michael Loll</author>
    [TestFixture]
    public sealed class PropertyComparatorTests
    {
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void InstantiationWithNullArgument()
        {
            new PropertyComparator(null);
        }

        [Test]
        public void Compare()
        {
            ISortDefinition definition = new MutableSortDefinition("Age", false, true);

            PropertyComparator cmp = new PropertyComparator(definition);
            TestObject one = new TestObject("Rick", 19);
            TestObject two = new TestObject("Balzac", 205);
            int actual = cmp.Compare(one, two);
            Assert.IsTrue(actual < 0); // 19 is less than 205
        }

        [Test]
        public void CompareNestedProperty()
        {
            ISortDefinition definition = new MutableSortDefinition("Lawyer.Company", true, true);

            PropertyComparator cmp = new PropertyComparator(definition);
            TestObject one = new TestObject("Rick", 19);
            one.Lawyer = new NestedTestObject("Gizajoab");
            TestObject two = new TestObject("Balzac", 205);
            two.Lawyer = new NestedTestObject("Wallpaperer");
            int actual = cmp.Compare(one, two);
            Assert.IsTrue(actual < 0); // Gizajoab is less than Wallpaperer
        }

        [Test]
        public void CompareWithNullProperty()
        {
            ISortDefinition definition = new MutableSortDefinition("Lawyer.Company", true, true);

            PropertyComparator cmp = new PropertyComparator(definition);
            TestObject one = new TestObject("Rick", 19);
            one.Lawyer = new NestedTestObject("Gizajoab");
            TestObject two = new TestObject("Balzac", 205);
            // no Lawyer.Company property set on object two...
            int actual = cmp.Compare(one, two);
            Assert.IsTrue(actual > 0); // Gizajoab is greater than null
        }

        [Test]
        [ExpectedException(typeof(InvalidPropertyException))]
        public void CompareWithNonExistantProperty()
        {
            ISortDefinition definition = new MutableSortDefinition("Deborahs.Banjo", true, true);

            PropertyComparator cmp = new PropertyComparator(definition);
            TestObject one = new TestObject("Rick", 19);
            TestObject two = new TestObject("Balzac", 205);
            int actual = cmp.Compare(one, two);
            Assert.IsTrue(actual == 0);
        }

        [Test]
        public void Sort()
        {
            ISortDefinition definition = new MutableSortDefinition("NAmE", true, true);

            TestObject one = new TestObject("Rick", 19);
            TestObject two = new TestObject("Balzac", 205);
            TestObject three = new TestObject("Jenny", 89);
            TestObject[] actual = new TestObject[] {one, two, three};
            TestObject[] expected = new TestObject[] {two /*Balzac*/, three /*Jenny*/, one /*Rick*/};
            PropertyComparator.Sort(actual, definition);
            for (int i = 0; i < actual.Length; ++i)
            {
                Assert.AreEqual(expected[i].Name, actual[i].Name);
            }
        }

        [Test]
        public void SortInDescendingOrder()
        {
            ISortDefinition definition = new MutableSortDefinition("NAmE", true, false);

            TestObject one = new TestObject("Rick", 19);
            TestObject two = new TestObject("Balzac", 205);
            TestObject three = new TestObject("Jenny", 89);
            TestObject[] actual = new TestObject[] {one, two, three};
            // Rick comes after Jenny comes after Balzac (descending sort order)...
            TestObject[] expected = new TestObject[] {one /*Rick*/, three /*Jenny*/, two /*Balzac*/};
            PropertyComparator.Sort(actual, definition);
            for (int i = 0; i < actual.Length; ++i)
            {
                Assert.AreEqual(expected[i].Name, actual[i].Name);
            }
        }

        [Test]
//        [Ignore("Sort ordering is not preserved (unstable) with equal elements (c.f. System.Array.Sort (Array, IComparer)))")]
        public void OrderingIsUnperturbedWithEqualProps()
        {
            ISortDefinition definition = new MutableSortDefinition("Age", false, true);

            TestObject one = new TestObject("Rick", 19);
            TestObject two = new TestObject("Balzac", 19);
            TestObject three = new TestObject("Jenny", 19);
            TestObject[] actual = new TestObject[] {one, two, three};
            TestObject[] expected = new TestObject[] {one /*Rick*/, two /*Balzac*/, three /*Jenny*/};
            PropertyComparator.Sort(actual, definition);
            for (int i = 0; i < actual.Length; ++i)
            {
                Assert.AreEqual(expected[i].Name, actual[i].Name);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SortWithNullDefinition()
        {
            PropertyComparator.Sort(Type.EmptyTypes, null);
        }

        [Test]
        public void CompareWithNullArguments()
        {
            ISortDefinition definition = new MutableSortDefinition("Nails", false, true);

            Funk one = new Funk(1, "long");
            PropertyComparator cmp = new PropertyComparator(definition);
            Assert.AreEqual(0, cmp.Compare(null, null));
            // nulls are always last (i.e. greater than)
            Assert.AreEqual(1, cmp.Compare(null, one));
            // any non-null instance comes before null (i.e. less than).
            Assert.AreEqual(-1, cmp.Compare(one, null));
        }

        /// <summary>
        /// Validates that a PropertyComparator can return its internal ISortDefinition
        /// to a consumer for use.
        /// </summary>
        [Test(Description = "SPRNET-171")]
        public void CanGetSortDefinition()
        {
            ISortDefinition definition = new MutableSortDefinition("Age", false, true);

            PropertyComparator cmp = new PropertyComparator(definition);
            ISortDefinition sortDefinition = cmp.SortDefinition;

            Assert.AreSame(definition, sortDefinition);
            Assert.AreEqual(definition.Property, sortDefinition.Property);
            Assert.AreEqual(definition.Ascending, sortDefinition.Ascending);
            Assert.AreEqual(definition.IgnoreCase, sortDefinition.IgnoreCase);
        }

        internal sealed class Funk
        {
            public Funk(int id, string nails)
            {
                _id = id;
                _nails = nails;
            }

            private string _nails;
            private int _id;

            public string Nails
            {
                get { return _nails; }
            }

            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
        }

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