DefaultFactoryBaseTest.cs :  » Web-Frameworks » Ingenious-MVC » Ingenious » Mvc » UnitTest » Factories » 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 » Web Frameworks » Ingenious MVC 
Ingenious MVC » Ingenious » Mvc » UnitTest » Factories » DefaultFactoryBaseTest.cs
#region License

/**
 * Ingenious MVC : An MVC framework for .NET 2.0
 * Copyright (C) 2006, JDP Group
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Authors: - Kent Boogaart (kentcb@internode.on.net)
 */

#endregion

using System;
using NUnit.Framework;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.Factories;

namespace Ingenious.Mvc.UnitTest.Factories{
  /// <summary>
  /// Unit tests the <see cref="DefaultFactoryBase"/> class.
  /// </summary>
  public sealed class DefaultFactoryBaseTest : UnitTestBase
  {
    private ConcreteDefaultFactory _factory;

    protected override void SetUp()
    {
      _factory = new ConcreteDefaultFactory();
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestNullType()
    {
      _factory.CreateInstance(null, null, null, null, null, Type.EmptyTypes);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void TestNullArgType()
    {
      _factory.CreateInstance(typeof(NoArgsClass), null, new Type[] {typeof(string), typeof(int), null, typeof(string)}, new object[] {"s", 12, null, "hello"}, null, Type.EmptyTypes);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void TestNullRequiredInterface()
    {
      _factory.CreateInstance(typeof(NoArgsClass), null, new Type[] {typeof(string), typeof(int), typeof(string)}, new object[] {"s", 12, "hello"}, new string[] {"String", "Int", "String2"}, new Type[] {typeof(ICloneable), null});
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "The number of argument types is 2 but the number of supplied arguments is 3. They must be equal.")]
    public void TestTypesAndArgsMismatch()
    {
      _factory.CreateInstance(typeof(NoArgsClass), null, new Type[] {typeof(string), typeof(int)}, new object[] {"s", 12, "hello"}, new string[] {"String", "Int", "String2"}, null);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "The number of arguments is 2 but the number of supplied argument names is 3. They must be equal.")]
    public void TestArgsAndArgNamesMismatch()
    {
      _factory.CreateInstance(typeof(NoArgsClass), null, new Type[] {typeof(string), typeof(int)}, new object[] {"s", 12}, new string[] {"String", "Int", "String2"}, null);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "Type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+NoArgsClass' does not implement the interface 'System.IComparable' as required by the 'Test' factory.")]
    public void TestRequiredInterfaceNotImplemented()
    {
      _factory.CreateInstance(typeof(NoArgsClass), null, new Type[] {typeof(string), typeof(int), typeof(string)}, new object[] {"s", 12, "hello"}, new string[] {"String", "Int", "String2"}, new Type[] {typeof(IComparable)});
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "A constructor on type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+NoArgsClass' failed when the 'Test' factory attempted to invoke it.")]
    public void TestNoArgsThrow()
    {
      NoArgsClass.ThrowOnConstruct = true;
      object o = _factory.CreateInstance(typeof(NoArgsClass), null, null, null, null, Type.EmptyTypes);
    }

    [Test]
    public void TestNoArgsValid()
    {
      NoArgsClass.ThrowOnConstruct = false;
      object o = _factory.CreateInstance(typeof(NoArgsClass), null, null, null, null, new Type[] {typeof(ICloneable)});
      Assert.IsNotNull(o);
      Assert.IsTrue(o is NoArgsClass);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "A constructor on type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+ArgsClass' failed when the 'Test' factory attempted to invoke it.")]
    public void TestArgsThrow()
    {
      ArgsClass.ThrowOnConstruct = true;
      object o = _factory.CreateInstance(typeof(ArgsClass), null, new Type[] {typeof(string), typeof(int)}, new object[] {"s", 1}, new string[] {"String", "Int"}, Type.EmptyTypes);
    }

    [Test]
    public void TestArgsConstructorFound()
    {
      ArgsClass.ThrowOnConstruct = false;
      object o = _factory.CreateInstance(typeof(ArgsClass), null, new Type[] {typeof(string), typeof(int)}, new object[] {"s", 1}, new string[] {"String", "Int"}, Type.EmptyTypes);
      Assert.IsNotNull(o);
      Assert.IsTrue(o is ArgsClass);
      ArgsClass argsClass = (ArgsClass) o;
      Assert.AreEqual("s", argsClass.String);
      Assert.AreEqual(1, argsClass.Int);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "No appropriate constructor on type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+ArgsClassNoDefaultConstructor' was found as required by the 'Test' factory. You need either a default constructor or one accepting arguments (System.String, System.Int32, System.Double).")]
    public void TestArgsConstructorNotFoundNoDefaultConstructor()
    {
      ArgsClassNoDefaultConstructor.ThrowOnConstruct = false;
      object o = _factory.CreateInstance(typeof(ArgsClassNoDefaultConstructor), null, new Type[] {typeof(string), typeof(int), typeof(double)}, new object[] {"s", 1, 12.7d}, new string[] {"String", "Int", "Double"}, Type.EmptyTypes);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "The type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+ArgsClass' does not declare a constructor taking arguments '(System.String, System.Int32, System.DateTime)', nor does it declare a property with signature 'public DateTime PropertyName { get; set; }'. The 'Test' factory relies on one of these mechanisms.")]
    public void TestArgsConstructorNotFoundPropertiesNotFound()
    {
      ArgsClass.ThrowOnConstruct = false;
      object o = _factory.CreateInstance(typeof(ArgsClass), null, new Type[] {typeof(string), typeof(int), typeof(DateTime)}, new object[] {"s", 1, DateTime.Now}, new string[] {"String", "Int", "DateTime"}, Type.EmptyTypes);
    }

    [Test]
    public void TestArgsConstructorNotFoundPropertiesFound()
    {
      ArgsClass.ThrowOnConstruct = false;
      object o = _factory.CreateInstance(typeof(ArgsClass), null, new Type[] {typeof(string), typeof(int), typeof(double)}, new object[] {"s", 1, 61.32d}, new string[] {"String", "Int", "Double"}, Type.EmptyTypes);
      Assert.IsNotNull(o);
      Assert.IsTrue(o is ArgsClass);
      ArgsClass argsClass = (ArgsClass) o;
      Assert.AreEqual("s", argsClass.String);
      Assert.AreEqual(1, argsClass.Int);
      Assert.AreEqual(61.32d, argsClass.Double);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "Type 'Ingenious.Mvc.UnitTest.Factories.DefaultFactoryBaseTest+NotConfigurable' does not implement the interface 'Ingenious.Mvc.Configuration.ICustomConfigurable' as required by the 'Test' factory in order to pass on custom configuration.")]
    public void TestNotConfigurable()
    {
      object customConfig = new object();
      object o = _factory.CreateInstance(typeof(NotConfigurable), customConfig, null, null, null, Type.EmptyTypes);
    }

    [Test]
    public void TestConfigurable()
    {
      object customConfig = new object();
      object o = _factory.CreateInstance(typeof(Configurable), customConfig, null, null, null, Type.EmptyTypes);
      Assert.IsNotNull(o);
      Assert.IsTrue(o is Configurable);
      Configurable configurable = (Configurable) o;
      Assert.AreEqual(customConfig, configurable.CustomConfiguration);
    }

    #region Supporting Types

    private sealed class ConcreteDefaultFactory : DefaultFactoryBase
    {
      public const string FactoryName = "Test";

      public override string Name
      {
        get
        {
          return FactoryName;
        }
      }

      public new object CreateInstance(Type type, object customConfiguration, Type[] argTypes, object[] args, string[] argNames, Type[] requiredInterfaces)
      {
        return base.CreateInstance(type, customConfiguration, argTypes, args, argNames, requiredInterfaces);
      }
    }

    private sealed class NoArgsClass : ICloneable
    {
      public static bool ThrowOnConstruct = false;

      public NoArgsClass()
      {
        if (ThrowOnConstruct)
        {
          throw new InvalidOperationException();
        }
      }

      public object Clone()
      {
        return null;
      }
    }

    private sealed class ArgsClassNoDefaultConstructor
    {
      private string _s;
      private int _i;
      private double _d;

      public string String
      {
        get
        {
          return _s;
        }
        set
        {
          _s = value;
        }
      }

      public int Int
      {
        get
        {
          return _i;
        }
        set
        {
          _i = value;
        }
      }

      public double Double
      {
        get
        {
          return _d;
        }
        set
        {
          _d = value;
        }
      }

      public static bool ThrowOnConstruct = false;

      public ArgsClassNoDefaultConstructor(string s, int i)
      {
        if (ThrowOnConstruct)
        {
          throw new InvalidOperationException();
        }

        _s = s;
        _i = i;
      }
    }

    private sealed class ArgsClass
    {
      private string _s;
      private int _i;
      private double _d;

      public string String
      {
        get
        {
          return _s;
        }
        set
        {
          _s = value;
        }
      }

      public int Int
      {
        get
        {
          return _i;
        }
        set
        {
          _i = value;
        }
      }

      public double Double
      {
        get
        {
          return _d;
        }
        set
        {
          _d = value;
        }
      }

      public static bool ThrowOnConstruct = false;

      public ArgsClass()
      {
      }

      public ArgsClass(string s, int i)
      {
        if (ThrowOnConstruct)
        {
          throw new InvalidOperationException();
        }

        _s = s;
        _i = i;
      }
    }

    private sealed class NotConfigurable
    {
    }

    private sealed class Configurable : ICustomConfigurable
    {
      private object _customConfiguration;

      public object CustomConfiguration
      {
        get
        {
          return _customConfiguration;
        }
      }

      public void SetCustomConfiguration(object customConfiguration)
      {
        _customConfiguration = customConfiguration;
      }
    }

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