ObjectFactoryTest.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » Common » Test » NUnit » CommonTests » Utilities » 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 » Persistence Frameworks » iBATIS.NET 
iBATIS.NET » IBatisNet » Common » Test » NUnit » CommonTests » Utilities » ObjectFactoryTest.cs
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Test.Domain;
using IBatisNet.Common.Utilities.Objects;
using NUnit.Framework;
using System;

namespace IBatisNet.Common.Test.NUnit.CommonTests.Utilities{

    [TestFixture] 
  public class ObjectFactoryTest
  {
    [Test]
        [ExpectedException(typeof(ProbeException))]
    public void AbstractConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      IFactory factory = objectFactory.CreateFactory(typeof (Document), Type.EmptyTypes );

            object obj = factory.CreateInstance(null);
    }
      
    [Test]
    public void DevivedClassConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      IFactory factory = objectFactory.CreateFactory(typeof (Book), Type.EmptyTypes );

      Assert.IsNotNull(factory);
    }
      
    [Test]
    [ExpectedException(typeof(ProbeException))]
    public void PrivateConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      IFactory factory = objectFactory.CreateFactory(typeof (Order), Type.EmptyTypes );

      object obj = factory.CreateInstance(null);
    }

        [Test]
        [ExpectedException(typeof(ProbeException))]
        public void NoMatchConstructor()
        {
            IObjectFactory objectFactory = new ObjectFactory(true);

            IFactory factory = objectFactory.CreateFactory(typeof(ItemBis), Type.EmptyTypes);

            object obj = factory.CreateInstance(null);
        }

    [Test]
    [ExpectedException(typeof(ProbeException))]
    public void ProtectedConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      IFactory factory = objectFactory.CreateFactory(typeof (Item), Type.EmptyTypes );

      object obj = factory.CreateInstance(null);

      Assert.IsTrue(obj is Item);
    }

    [Test]
    public void ClassWithMultipleConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(string)};
      IFactory factory0 = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = {"gilles"};
      object obj0 = factory0.CreateInstance(parameters);

      Assert.IsTrue(obj0 is Account);
      Account account = (Account)obj0;
      Assert.AreEqual("gilles", account.Test);

      IFactory factory1 = objectFactory.CreateFactory(typeof (Account), Type.EmptyTypes );

      object obj1 = factory1.CreateInstance(parameters);

      Assert.IsTrue(obj1 is Account);
    }

    [Test]
    public void StringConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(string)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = {"gilles"};
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual("gilles", account.Test);
    }

    [Test]
    public void MultipleParamConstructor1()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(string)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[1];
      parameters[0] = null;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual(null, account.Test);
    }

    [Test]
    public void IntConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(int)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[1];
      parameters[0] = -55;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual( -55, account.Id);
    }

    [Test]
    public void EnumConstructorEnum()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(Days)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[1];
      parameters[0] = Days.Sun;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual( Days.Sun, account.Days);
    }

    [Test]
    public void ClassConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(Property)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[1];
      Property prop = new Property();
      prop.String = "Gilles";
      parameters[0] = prop;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.IsNotNull(account.Property);
      Assert.AreEqual( "Gilles", account.Property.String);
    }

    [Test]
    public void DateTimeConstructor()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(DateTime)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[1];
      DateTime date = DateTime.Now;
      parameters[0] = date;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual( date, account.Date);
    }

        [Test]
        public void ArrayParamConstructor()
        {
            IObjectFactory objectFactory = new ObjectFactory(true);

            Type[] types = { typeof(int[]) };
            IFactory factory = objectFactory.CreateFactory(typeof(Account), types);

            object[] parameters = new object[1];

            int[] ids = new int[2];
            ids[0] = 1;
            ids[1] = 2;

            parameters[0] = ids;
            object obj = factory.CreateInstance(parameters);

            Assert.IsTrue(obj is Account);
            Account account = (Account)obj;

            Assert.AreEqual(2, account.Ids.Length);
            Assert.AreEqual(1, account.Ids[0]);
            Assert.AreEqual(2, account.Ids[1]);
        }

    [Test]
    public void MultipleParamConstructor0()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      Type[] types = {typeof(string), typeof(Property)};
      IFactory factory = objectFactory.CreateFactory(typeof (Account), types );

      object[] parameters = new object[2];
      Property prop = new Property();
      prop.String = "Gilles";
      parameters[0] = "Hlose";
      parameters[1] = prop;
      object obj = factory.CreateInstance(parameters);

      Assert.IsTrue(obj is Account);
      Account account = (Account)obj;
      Assert.AreEqual("Hlose", account.FirstName);
      Assert.IsNotNull(account.Property);
      Assert.AreEqual( "Gilles", account.Property.String);
    }



    [Test]
    public void DynamicFactoryCreatesTypes()
    {
      IObjectFactory objectFactory = new ObjectFactory(true);

      IFactory factory = objectFactory.CreateFactory(typeof (Account), Type.EmptyTypes);
      object obj = factory.CreateInstance(null);
      Assert.IsTrue(obj is Account);

      factory = objectFactory.CreateFactory(typeof (Account), Type.EmptyTypes);
      obj = factory.CreateInstance(Type.EmptyTypes);
      Assert.IsTrue(obj is Account);

      factory = objectFactory.CreateFactory(typeof (Simple), Type.EmptyTypes);
      obj = factory.CreateInstance(Type.EmptyTypes);
      Assert.IsTrue(obj is Simple);
    }

    [Test]
    public void CreateInstanceWithDifferentFactories()
    {
      const int TEST_ITERATIONS = 1000000;
      IFactory factory = null;

      #region new
      factory = new NewAccountFactory();

      // create an instance so that Activators can
      // cache the type/constructor/whatever
      factory.CreateInstance(Type.EmptyTypes);

      GC.Collect();
      GC.WaitForPendingFinalizers();

      Timer timer = new Timer();
      timer.Start();
      for (int i = 0; i < TEST_ITERATIONS; i++)
      {
        factory.CreateInstance(Type.EmptyTypes);
      }
      timer.Stop();
      double newFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS);
      #endregion

      #region activator
      factory = new ActivatorObjectFactory().CreateFactory(typeof(Account), Type.EmptyTypes);

      // create an instance so that Activators can
      // cache the type/constructor/whatever
      factory.CreateInstance(Type.EmptyTypes);

      GC.Collect();
      GC.WaitForPendingFinalizers();

      timer.Start();
      for (int i = 0; i < TEST_ITERATIONS; i++)
      {
        factory.CreateInstance(Type.EmptyTypes);
      }
      timer.Stop();
      double activatorFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS);
      #endregion

      #region Emit
      factory = new EmitObjectFactory().CreateFactory(typeof(Account), Type.EmptyTypes);

      // create an instance so that Activators can
      // cache the type/constructor/whatever
      factory.CreateInstance(Type.EmptyTypes);

      GC.Collect();
      GC.WaitForPendingFinalizers();

      timer.Start();
      for (int i = 0; i < TEST_ITERATIONS; i++)
      {
        factory.CreateInstance(Type.EmptyTypes);
      }
      timer.Stop();
      double emitFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS);
      #endregion

      // Print results
      Console.WriteLine(
        "Create " + TEST_ITERATIONS.ToString() + " objects via factory :"
        + "\nNew : \t\t\t" + newFactoryResult.ToString("F3")
        + "\nActivator : \t\t" + activatorFactoryResult.ToString("F3")+ " Ratio : " + ((activatorFactoryResult / newFactoryResult)).ToString("F3")
        + "\nEmit IL : \t\t\t" + emitFactoryResult.ToString("F3") + " Ratio : " + ((emitFactoryResult / newFactoryResult)).ToString("F3"));
    }

    internal class NewAccountFactory : IFactory
    {
      public object CreateInstance(object[] parameters)
      {
        return new Account();
      }

    }

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