#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.Navigators;
using Ingenious.Mvc.UnitTest.MockTypes;
namespace Ingenious.Mvc.UnitTest.Navigators{
/// <summary>
/// Unit tests the <see cref="GraphNavigator"/> class.
/// </summary>
public sealed class GraphNavigatorTest : UnitTestBase
{
private GraphNavigator _navigator;
private Id<INavigator> _id;
private Task _task;
protected override void SetUp()
{
_id = "n";
_task = CreateTask("t");
_navigator = new GraphNavigator(_id, _task, "sv", new MockViewManager(TaskManager));
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'fromView'.")]
public void TestAddAliasFromViewEmpty()
{
_navigator.AddAlias(Id<IView>.Empty, "toView", "alias");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'toView'.")]
public void TestAddAliasToViewEmpty()
{
_navigator.AddAlias("fromView", Id<IView>.Empty, "alias");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewAliasId'.")]
public void TestAddAliasViewAliasIdEmpty()
{
_navigator.AddAlias("fromView", "toView", Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'fromView'.")]
public void TestRemoveAliasFromViewEmpty()
{
_navigator.RemoveAlias(Id<IView>.Empty, "alias");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewAliasId'.")]
public void TestRemoveAliasViewAliasIdEmpty()
{
_navigator.RemoveAlias("fromView", Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewAliasId'.")]
public void TestGetViewForAliasViewAliasEmpty()
{
_navigator.GetViewForAlias(Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(NavigatorException), "An attempt was made to obtain a mapping for view alias ID 'alias' from the active view but there is no active view.")]
public void TestGetViewForAliasNoActiveView()
{
_navigator.GetViewForAlias("alias");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'fromView'.")]
public void TestGetViewForAlias2FromViewEmpty()
{
_navigator.GetViewForAlias(Id<IView>.Empty, "alias");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewAliasId'.")]
public void TestGetViewForAlias2ViewAliasEmpty()
{
_navigator.GetViewForAlias("fromView", Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'toView'.")]
public void TestGetAliasForViewViewEmpty()
{
_navigator.GetAliasForView(Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(NavigatorException), "An attempt was made to obtain a mapping for view ID 'view' from the active view but there is no active view.")]
public void TestGetAliasForViewNoActiveView()
{
_navigator.GetAliasForView("view");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'fromView'.")]
public void TestGetAliasForView2FromViewEmpty()
{
_navigator.GetAliasForView(Id<IView>.Empty, "toView");
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'toView'.")]
public void TestGetAliasForView2ViewAliasEmpty()
{
_navigator.GetAliasForView("fromView", Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(NavigatorException), "An alias with ID 'Alias1' from view 'View1' has already been added to the graph navigator. The destination view has ID 'View2'.")]
public void TestAddAliasAlreadyExists()
{
_navigator.AddAlias(Views.View1, Views.View2, ViewAliases.Alias1);
_navigator.AddAlias(Views.View1, Views.View2, ViewAliases.Alias1);
}
[Test]
public void TestGraphManipulation()
{
Assert.AreEqual(Id<IView>.Empty, _navigator.GetViewForAlias(Views.View1, ViewAliases.Alias1));
_navigator.AddAlias(Views.View1, Views.View2, ViewAliases.Alias1);
Assert.IsFalse(_navigator.GetViewForAlias(Views.View1, ViewAliases.Alias1) == Id<IView>.Empty);
Id<IView> viewId = Views.View2;
Id<IView> aliasId = ViewAliases.Alias1;
Assert.AreEqual(viewId, _navigator.GetViewForAlias(Views.View1, ViewAliases.Alias1));
Assert.AreEqual(aliasId, _navigator.GetAliasForView(Views.View1, Views.View2));
_navigator.AddAlias(Views.View1, Views.View3, ViewAliases.Alias2);
Assert.IsFalse(_navigator.GetViewForAlias(Views.View1, ViewAliases.Alias2) == Id<IView>.Empty);
viewId = Views.View3;
aliasId = ViewAliases.Alias2;
Assert.AreEqual(viewId, _navigator.GetViewForAlias(Views.View1, ViewAliases.Alias2));
Assert.AreEqual(aliasId, _navigator.GetAliasForView(Views.View1, Views.View3));
_navigator.AddAlias(Views.View2, Views.View3, ViewAliases.Alias3);
aliasId = ViewAliases.Alias3;
Assert.IsFalse(_navigator.GetViewForAlias(Views.View2, ViewAliases.Alias3) == Id<IView>.Empty);
Assert.AreEqual(viewId, _navigator.GetViewForAlias(Views.View2, ViewAliases.Alias3));
Assert.AreEqual(aliasId, _navigator.GetAliasForView(Views.View2, Views.View3));
_navigator.RemoveAlias(Views.View2, ViewAliases.Alias3);
Assert.AreEqual(Id<IView>.Empty, _navigator.GetViewForAlias(Views.View2, ViewAliases.Alias3));
_navigator.RemoveAlias(Views.View1, ViewAliases.Alias2);
Assert.AreEqual(Id<IView>.Empty, _navigator.GetViewForAlias(Views.View1, ViewAliases.Alias2));
Assert.IsFalse(_navigator.GetViewForAlias(Views.View1, ViewAliases.Alias1) == Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewId'.")]
public void TestNavigateToViewOrAliasIdEmpty()
{
TaskManager.ConfigurationInfo = new ConfigurationInfo();
_navigator.NavigateTo(Id<IView>.Empty);
}
[Test]
[ExpectedException(typeof(NavigationException), "The starting view ('sv') must be the first view navigated to by the graph navigator. However, the first navigation request was to view / view alias 'v'.")]
public void TestNavigateToNotStartingView()
{
TaskManager.ConfigurationInfo = new ConfigurationInfo();
_navigator.NavigateTo("v");
}
[Test]
public void TestNavigateToInitialViewValid()
{
Configure("SimpleConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
}
[Test]
[ExpectedException(typeof(NavigationException), "No destination view could be determined for the specified alias or view ID ('myAliasNotFound') for the active view ('view1').")]
public void TestNavigateToSubsequentViewByAliasNotFound()
{
Configure("SimpleConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//try and navigate to an unknown alias
_navigator.NavigateTo((Id<IView>) "myAliasNotFound");
}
[Test]
public void TestNavigateToSubsequentViewByAliasValid()
{
Configure("SimpleConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//try and navigate to a valid alias
_navigator.NavigateTo((Id<IView>) "myAlias");
Assert.AreEqual("view2", _navigator.LastNavigateTo.ToString());
}
[Test]
[ExpectedException(typeof(NavigationException), "No destination view could be determined for the specified alias or view ID ('invalidView') for the active view ('view1').")]
public void TestNavigateToSubsequentViewByViewNotFound()
{
Configure("SimpleConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//try and navigate to an invalid view
_navigator.NavigateTo("invalidView");
}
[Test]
public void TestNavigateToSubsequentViewByViewValid()
{
Configure("SimpleConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//try and navigate to a valid view
_navigator.NavigateTo("view2");
Assert.AreEqual("view2", _navigator.LastNavigateTo.ToString());
}
[Test]
public void TestComplexNavigation()
{
Configure("ComplexConfiguration");
Task task = TaskManager.StartTask("navigator");
_navigator = (GraphNavigator) task.Manager.GetNavigatorForTask(task);
//the framework should have navigated to the starting view
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//view1 [failure] -> view3
_navigator.NavigateTo((Id<IView>) "failure");
Assert.AreEqual("view3", _navigator.LastNavigateTo.ToString());
//view3 [tryAgain] -> view1
_navigator.NavigateTo((Id<IView>) "tryAgain");
Assert.AreEqual("view1", _navigator.LastNavigateTo.ToString());
//view1 [success] -> view2
_navigator.NavigateTo((Id<IView>) "success");
Assert.AreEqual("view2", _navigator.LastNavigateTo.ToString());
//view2 [about] -> view4
_navigator.NavigateTo((Id<IView>) "about");
Assert.AreEqual("view4", _navigator.LastNavigateTo.ToString());
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetCustomConfigurationNull()
{
((ICustomConfigurable) _navigator).SetCustomConfiguration(null);
}
[Test]
[ExpectedException(typeof(NavigatorException), "The GraphNavigator requires an instance of 'Ingenious.Mvc.Navigators.GraphNavigator+CustomConfiguration' for its custom configuration but was given an instance of 'System.String'.")]
public void TestSetCustomConfigurationWrongType()
{
((ICustomConfigurable) _navigator).SetCustomConfiguration("test");
}
[Test]
public void TestSetCustomConfigurationValid()
{
Id<IView> v1 = "V1";
Id<IView> v2 = "V2";
Id<IView> v3 = "V3";
Id<IView> v4 = "V4";
Id<IView> va1 = "VA1";
Id<IView> va2 = "VA2";
Id<IView> va3 = "VA3";
GraphNavigator.CustomConfiguration aliases = new Ingenious.Mvc.Navigators.GraphNavigator.CustomConfiguration();
aliases.Add(new GraphNavigator.AliasInfo(v1, v3, va1));
aliases.Add(new GraphNavigator.AliasInfo(v1, v4, va2));
aliases.Add(new GraphNavigator.AliasInfo(v2, v1, va3));
Assert.AreEqual(Id<IView>.Empty, _navigator.GetViewForAlias("V1", "Success"));
((ICustomConfigurable) _navigator).SetCustomConfiguration(aliases);
Assert.AreEqual(v3, _navigator.GetViewForAlias(v1, va1));
Assert.AreEqual(v4, _navigator.GetViewForAlias(v1, va2));
Assert.AreEqual(v1, _navigator.GetViewForAlias(v2, va3));
}
#region Supporting Types
private enum Views
{
View1,
View2,
View3
}
private enum ViewAliases
{
Alias1,
Alias2,
Alias3
}
#endregion
}
}
|