#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;
using Ingenious.Mvc.UnitTest.MockTypes;
namespace Ingenious.Mvc.UnitTest.Factories{
/// <summary>
/// Unit tests the <see cref="DefaultNavigatorFactory"/> class.
/// </summary>
public sealed class DefaultNavigatorFactoryTest : UnitTestBase
{
private INavigatorFactory _factory;
protected override void SetUp()
{
_factory = DefaultNavigatorFactory.Instance;
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullNavigator()
{
_factory.Create(null, null, null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullTask()
{
_factory.Create(new NavigatorInfo("Test"), null, null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullViewManager()
{
_factory.Create(new NavigatorInfo("Test"), CreateTask(), null);
}
[Test]
[ExpectedException(typeof(FactoryException), "There is no type specified for the navigator with ID 'Test'.")]
public void TestNullType()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
_factory.Create(navigatorInfo, CreateTask(), new MockViewManager(TaskManager));
}
[Test]
[ExpectedException(typeof(FactoryException), "There is no starting view specified for the navigator with ID 'Test'.")]
public void TestNullStartingView()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(MockNavigator);
_factory.Create(navigatorInfo, CreateTask(), new MockViewManager(TaskManager));
}
[Test]
[ExpectedException(typeof(FactoryException), "The property 'Id' on type 'Ingenious.Mvc.UnitTest.Factories.DefaultNavigatorFactoryTest+NoConstructorNoProperty' is not writable.")]
public void TestNoConstructorNoProperty()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(NoConstructorNoProperty);
navigatorInfo.StartViewInfo = new ViewInfo("v");
_factory.Create(navigatorInfo, CreateTask(), new MockViewManager(TaskManager));
}
[Test]
public void TestImplementsConstructor()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(ImplementsConstructor);
navigatorInfo.StartViewInfo = new ViewInfo("v");
MockViewManager viewManager = new MockViewManager(TaskManager);
ImplementsConstructor navigator = (ImplementsConstructor) _factory.Create(navigatorInfo, CreateTask(), viewManager);
Assert.AreEqual("Test", navigator.Id.ToString());
Assert.AreEqual(TaskManager, navigator.Task.Manager);
Assert.AreEqual("v", navigator.StartingViewId.ToString());
Assert.AreEqual(viewManager, navigator.ViewManager);
}
[Test]
public void TestImplementsProperty()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(ImplementsProperty);
navigatorInfo.StartViewInfo = new ViewInfo("v");
MockViewManager viewManager = new MockViewManager(TaskManager);
ImplementsProperty navigator = (ImplementsProperty) _factory.Create(navigatorInfo, CreateTask(), viewManager);
Assert.AreEqual("Test", navigator.Id.ToString());
Assert.AreEqual(TaskManager, navigator.Task.Manager);
Assert.AreEqual("v", navigator.StartingViewId.ToString());
Assert.AreEqual(viewManager, navigator.ViewManager);
}
[Test]
public void TestCustomConfiguration()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(ConfigurableNavigator);
navigatorInfo.StartViewInfo = new ViewInfo("v");
navigatorInfo.CustomConfiguration = new object();
MockViewManager viewManager = new MockViewManager(TaskManager);
ConfigurableNavigator navigator = (ConfigurableNavigator) _factory.Create(navigatorInfo, CreateTask(), viewManager);
Assert.AreEqual("Test", navigator.Id.ToString());
Assert.AreEqual(TaskManager, navigator.Task.Manager);
Assert.AreEqual("v", navigator.StartingViewId.ToString());
Assert.AreEqual(viewManager, navigator.ViewManager);
Assert.AreEqual(navigatorInfo.CustomConfiguration, navigator.CustomConfiguration);
}
[Test]
public void TestInitialize()
{
NavigatorInfo navigatorInfo = new NavigatorInfo("Test");
navigatorInfo.Type = typeof(MockNavigator);
navigatorInfo.StartViewInfo = new ViewInfo("v");
MockViewManager viewManager = new MockViewManager(TaskManager);
MockNavigator navigator = (MockNavigator) _factory.Create(navigatorInfo, CreateTask(), viewManager);
Assert.AreEqual("Test", navigator.Id.ToString());
Assert.AreEqual(TaskManager, navigator.Task.Manager);
Assert.AreEqual("v", navigator.StartingViewId.ToString());
Assert.AreEqual(viewManager, navigator.ViewManager);
Assert.IsTrue(navigator.Initialized);
}
#region Supporting Types
private abstract class NavigatorBase : INavigator
{
public abstract Id<INavigator> Id
{
get;
}
public abstract Task Task
{
get;
}
public abstract Id<IView> StartingViewId
{
get;
}
public virtual bool CanNavigateTo(string viewId)
{
return CanNavigateTo(viewId);
}
public virtual bool CanNavigateTo(Enum viewId)
{
return CanNavigateTo(viewId);
}
public virtual bool CanNavigateTo(Id<IView> viewId)
{
return true;
}
public IView NavigateTo(Id<IView> viewId)
{
return NavigateTo(viewId, null);
}
public abstract IView NavigateTo(Id<IView> viewId, object viewData);
public virtual void Initialize()
{
}
}
private sealed class DoesNotImplementINavigator
{
}
private sealed class NoConstructorNoProperty : NavigatorBase
{
public IViewManager ViewManager
{
get
{
return null;
}
}
public override Id<INavigator> Id
{
get
{
return Id<INavigator>.Empty;
}
}
public override Task Task
{
get
{
return null;
}
}
public override Id<IView> StartingViewId
{
get
{
return Id<IView>.Empty;
}
}
public NoConstructorNoProperty()
{
}
public override IView NavigateTo(Id<IView> viewId, object viewData)
{
return null;
}
}
private sealed class ImplementsConstructor : NavigatorBase
{
private Id<INavigator> _id;
private Task _task;
private Id<IView> _startingViewId;
private IViewManager _viewManager;
public IViewManager ViewManager
{
get
{
return _viewManager;
}
}
public override Id<INavigator> Id
{
get
{
return _id;
}
}
public override Task Task
{
get
{
return _task;
}
}
public override Id<IView> StartingViewId
{
get
{
return _startingViewId;
}
}
public ImplementsConstructor(Id<INavigator> id, Task task, Id<IView> startingViewId, IViewManager viewManager)
{
_id = id;
_task = task;
_startingViewId = startingViewId;
_viewManager = viewManager;
}
public override IView NavigateTo(Id<IView> viewId, object viewData)
{
return null;
}
}
private class ImplementsProperty : INavigator
{
private Id<INavigator> _id;
private Task _task;
private Id<IView> _startingViewId;
private IViewManager _viewManager;
public Id<INavigator> Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public Task Task
{
get
{
return _task;
}
set
{
_task = value;
}
}
public Id<IView> StartingViewId
{
get
{
return _startingViewId;
}
set
{
_startingViewId = value;
}
}
public IViewManager ViewManager
{
get
{
return _viewManager;
}
set
{
_viewManager = value;
}
}
public virtual bool CanNavigateTo(string viewId)
{
return CanNavigateTo(viewId);
}
public virtual bool CanNavigateTo(Enum viewId)
{
return CanNavigateTo(viewId);
}
public virtual bool CanNavigateTo(Id<IView> viewId)
{
return true;
}
public IView NavigateTo(Id<IView> viewId)
{
return NavigateTo(viewId, null);
}
public IView NavigateTo(Id<IView> viewId, object viewData)
{
return null;
}
public void Initialize()
{
}
}
private sealed class ConfigurableNavigator : ImplementsProperty, ICustomConfigurable
{
private object _customConfiguration;
public object CustomConfiguration
{
get
{
return _customConfiguration;
}
}
public void SetCustomConfiguration(object customConfiguration)
{
_customConfiguration = customConfiguration;
}
}
#endregion
}
}
|