ReflectionUtilsMemberwiseCopyTests.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 » ReflectionUtilsMemberwiseCopyTests.cs
using System;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Web;
using NUnit.Framework;

namespace Spring.Util{
  /// <summary>
  /// Summary description for TestMemberwiseCopy.
  /// </summary>
  [TestFixture]
  public class ReflectionUtilsMemberwiseCopyTests
  {
    [Test]
    public void TestSameType()
    {
      SampleBaseClass i1 = new SampleDerivedClass("1st config val");
      SampleBaseClass i2 = new SampleDerivedClass("2nd config val");

      ReflectionUtils.MemberwiseCopy(i1, i2);

      Assert.AreEqual(i1, i2);
    }

    [Test]
    [ExpectedException(typeof (ArgumentException), ExpectedMessage="object types are not related")]
    public void DifferentTypesForbidden()
    {
      ReflectionUtils.MemberwiseCopy("test", 2);
    }

    [Test]
    public void TestDerivedTypeAllowed()
    {
      SampleBaseClass i1 = new SampleDerivedClass("1st config val");
      SampleBaseClass i2 = new SampleFurtherDerivedClass("2nd config val");

      ReflectionUtils.MemberwiseCopy(i1, i2);

      Assert.AreEqual(i1, i2);
    }

    [Test]
    public void TestBaseTypeAllowed()
    {
      SampleBaseClass i1 = new SampleDerivedClass("1st config val");
      SampleBaseClass i2 = new SampleFurtherDerivedClass("2nd config val");

      ReflectionUtils.MemberwiseCopy(i2, i1);

      Assert.AreEqual(i1, i2);
    }

#if NET_2_0
    [Test]
    public void MediumTrustAllowsCopyingBetweenTypesFromSameModule()
    {
      SampleBaseClass i1 = new SampleDerivedClass("1st config val");
      SampleBaseClass i2 = new SampleFurtherDerivedClass("2nd config val");

            SecurityTemplate.MediumTrustInvoke(new ThreadStart(new CopyCommand(i2, i1).Execute));
      Assert.AreEqual(i1, i2);
    }

    [Test]
    public void MediumTrustThrowsSecurityExceptionWhenCopyingBetweenTypesFromDifferentModules()
    {
      Exception e1 = new Exception("my name is e1");
            HttpException e2 = new HttpException("my name is e2");
            // I know, I am a bit paranoid about that basic assumption
            Assert.AreNotEqual( e1.GetType().Assembly, e2.GetType().Assembly );

            SecurityTemplate.MediumTrustInvoke(new ThreadStart(new CopyCommand(e2, e1).Execute));
      Assert.AreEqual(e1.Message, e2.Message);
    }

        class CopyCommand
        {
            private object a;
            private object b;

            public CopyCommand(object a, object b)
            {
                this.a = a;
                this.b = b;
            }

            public void Execute()
            {
                ReflectionUtils.MemberwiseCopy(a, b);
            }
        }
#endif
    }
  
  #region Test Support Classes
  
  public class SampleBaseClass
  {
    private const string MyConstant = "SampleBaseClass.MyConstant";
    private readonly string _someReadOnlyVal = "SampleBaseClass.SomeReadOnlyVal";
    protected readonly string _someProtectedReadOnlyVal = "SampleBaseClass.SomeProtectedReadOnlyVal";
    private string _someConfigVal;

    public SampleBaseClass(string someConfigVal)
    {
      if (someConfigVal == null) throw new ArgumentNullException("someConfigVal must not be null");
      _someConfigVal = someConfigVal;
      _someProtectedReadOnlyVal = "Protected" + someConfigVal;
    }

    public override int GetHashCode()
    {
      int result = _someReadOnlyVal != null ? _someReadOnlyVal.GetHashCode() : 0;
      result = 29*result + (_someProtectedReadOnlyVal != null ? _someProtectedReadOnlyVal.GetHashCode() : 0);
      result = 29*result + (_someConfigVal != null ? _someConfigVal.GetHashCode() : 0);
      return result;
    }

    public override bool Equals(object obj)
    {
            if (ReferenceEquals(obj, null) || (!this.GetType().IsAssignableFrom(obj.GetType())))
                return false;
            if (ReferenceEquals(this , obj))
                return true;
      
            SampleBaseClass sampleBaseClass = (SampleBaseClass) obj;
      if (!Equals(_someReadOnlyVal, sampleBaseClass._someReadOnlyVal)) return false;
      if (!Equals(_someProtectedReadOnlyVal, sampleBaseClass._someProtectedReadOnlyVal)) return false;
      if (!Equals(_someConfigVal, sampleBaseClass._someConfigVal)) return false;
      return true;
    }
  }
  
  public class SampleDerivedClass : SampleBaseClass
  {
    private string _someConfigVal = "SampleDerivedClass.SomeConfigVal";

    public SampleDerivedClass(string someConfigVal) : base(someConfigVal)
    {
      _someConfigVal = "SampleDerivedClass." + someConfigVal;
    }

    public override int GetHashCode()
    {
      return base.GetHashCode() + 29*(_someConfigVal != null ? _someConfigVal.GetHashCode() : 0);
    }

    public override bool Equals(object obj)
    {
      if (!base.Equals(obj)) 
                return false;
            
            SampleDerivedClass sampleDerivedClass = (SampleDerivedClass)obj;
            if (!Equals(_someConfigVal, sampleDerivedClass._someConfigVal))
                return false;
      return true;
    }
  }
  
  public class SampleFurtherDerivedClass : SampleDerivedClass
  {
    public SampleFurtherDerivedClass(string someConfigVal) : base(someConfigVal)
    {
    }
  }
  
  #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.