TaskManagerTest.cs :  » Web-Frameworks » Ingenious-MVC » Ingenious » Mvc » UnitTest » 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 » TaskManagerTest.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;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.Factories;
using Ingenious.Mvc.Navigators;
using Ingenious.Mvc.UnitTest.MockTypes;

namespace Ingenious.Mvc.UnitTest{
  /// <summary>
  /// Unit tests the <see cref="TaskManager"/> class.
  /// </summary>
  public sealed class TaskManagerTest : UnitTestBase
  {
    [Test]
    public void TestInstance()
    {
      Assert.IsNotNull(Ingenious.Mvc.TaskManager.Instance);
    }

    [Test]
    [ExpectedException(typeof(ConfigurationException), "Before using the TaskManager class, you must supply configuration information via the Configuration property (or by using a configurator).")]
    public void TestStartTaskNoConfig()
    {
      TaskManager.StartTask("nav");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestStartTaskNull()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      TaskManager.StartTask((string) null);
    }

    [Test]
    [ExpectedException(typeof(ConfigurationException), "A navigator with the specified ID ('nav') could not be found in the current configuration.")]
    public void TestStartTaskNavigatorNotFound()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      TaskManager.StartTask("nav");
    }

    [Test]
    [ExpectedException(typeof(ConfigurationException), "The view manager for the navigator with ID 'nav' was not specified.")]
    public void TestStartTaskViewManagerNotSpecified()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      NavigatorInfo navigatorInfo = new NavigatorInfo("nav");
      navigatorInfo.Type = typeof(MockNavigator);
      TaskManager.ConfigurationInfo.NavigatorInfos.Add(navigatorInfo);
      TaskManager.StartTask(navigatorInfo.Id);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "The configured view manager factory for navigator 'nav' failed to create a view manager instance.")]
    public void TestStartTaskViewManagerFactoryFailed()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      TaskManager.ConfigurationInfo.ViewManagerFactory = new MockViewManagerFactory();
      NavigatorInfo navigatorInfo = new NavigatorInfo("nav");
      navigatorInfo.Type = typeof(MockNavigator);
      ViewManagerInfo viewManagerInfo = new ViewManagerInfo("vm");
      navigatorInfo.ViewManagerInfo = viewManagerInfo;
      TaskManager.ConfigurationInfo.NavigatorInfos.Add(navigatorInfo);
      TaskManager.StartTask(navigatorInfo.Id);
    }

    [Test]
    [ExpectedException(typeof(FactoryException), "The configured navigator factory failed to create the navigator with ID 'nav'.")]
    public void TestStartTaskNavigatorFactoryFailed()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      MockViewManagerFactory viewManagerFactory = new MockViewManagerFactory();
      viewManagerFactory.ViewManager = new MockViewManager(TaskManager);
      TaskManager.ConfigurationInfo.ViewManagerFactory = viewManagerFactory;
      NavigatorInfo navigatorInfo = new NavigatorInfo("nav");
      navigatorInfo.Type = typeof(MockNavigator);
      ViewManagerInfo viewManagerInfo = new ViewManagerInfo("vm");
      navigatorInfo.ViewManagerInfo = viewManagerInfo;
      TaskManager.ConfigurationInfo.NavigatorInfos.Add(navigatorInfo);
      TaskManager.ConfigurationInfo.NavigatorFactory = new MockNavigatorFactory();
      TaskManager.StartTask(navigatorInfo.Id);
    }

    [Test]
    [ExpectedException(typeof(ConfigurationException), "The navigator with ID 'nav' does not have a starting view specified.")]
    public void TestStartTaskStartingViewNotSpecified()
    {
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      MockViewManagerFactory viewManagerFactory = new MockViewManagerFactory();
      viewManagerFactory.ViewManager = new MockViewManager(TaskManager);
      TaskManager.ConfigurationInfo.ViewManagerFactory = viewManagerFactory;
      NavigatorInfo navigatorInfo = new NavigatorInfo("nav");
      navigatorInfo.Type = typeof(MockNavigator);
      ViewManagerInfo viewManagerInfo = new ViewManagerInfo("vm");
      navigatorInfo.ViewManagerInfo = viewManagerInfo;
      TaskManager.ConfigurationInfo.NavigatorInfos.Add(navigatorInfo);
      MockNavigatorFactory navigatorFactory = new MockNavigatorFactory();
      navigatorFactory.Navigator = new MockNavigator();
      TaskManager.ConfigurationInfo.NavigatorFactory = navigatorFactory;
      TaskManager.StartTask(navigatorInfo.Id);
    }

    [Test]
    public void TestValidSimple()
    {
      Configure("Simple");

      object taskData = new object();
      object viewData = new object();
      Task task = TaskManager.StartTask("navigator", taskData, viewData);
      Assert.IsNotNull(task);
      Assert.AreEqual(TaskState.Started, task.State);
      Assert.AreEqual(task, TaskManager.ActiveTask);

      OpenNavigator navigator = task.Manager.GetNavigatorForTask(task) as OpenNavigator;
      Assert.IsNotNull(navigator);
      Assert.IsNotNull(navigator.LastNavigateTo);
      MockViewManager viewManager = task.Manager.GetViewManagerForTask(task) as MockViewManager;
      Assert.IsNotNull(viewManager);
      Assert.IsNotNull(viewManager.LastActivatedViewData);
      Assert.AreSame(viewData, viewManager.LastActivatedViewData);

      Assert.AreEqual("view1", navigator.LastNavigateTo.ToString());
      Assert.AreEqual(1, TaskManager.Tasks.Count);
      Assert.IsTrue(TaskManager.Tasks.Contains(task));
      Assert.AreSame(taskData, task.Data);

      task.Ending  += new EventHandler<TaskEndingEventArgs>(task_Ending);
      //this will be cancelled by handler
      task.End();
      Assert.IsFalse(task.State == TaskState.Ended);
      Assert.IsTrue(TaskManager.Tasks.Contains(task));

      //remove handle and then cancel
      task.Ending  -= new EventHandler<TaskEndingEventArgs>(task_Ending);
      task.End();
      Assert.IsTrue(task.State == TaskState.Ended);
      Assert.IsFalse(TaskManager.Tasks.Contains(task));
    }

    [Test]
    public void TestValidComplex()
    {
      Configure("Complex");

      //start task1
      Task task1 = TaskManager.StartTask("navigator1");
      Assert.IsNotNull(task1);
      Assert.AreEqual(task1, TaskManager.ActiveTask);
      Assert.IsNull(task1.Owner);

      OpenNavigator navigator1 = task1.Manager.GetNavigatorForTask(task1) as OpenNavigator;
      Assert.IsNotNull(navigator1);
      Assert.IsNotNull(navigator1.LastNavigateTo);
      IViewManager viewManager1 = TaskManager.GetViewManagerForTask(task1);
      Assert.IsNotNull(viewManager1);

      Assert.AreEqual("view1", navigator1.LastNavigateTo.ToString());
      Assert.AreEqual(1, TaskManager.Tasks.Count);
      Assert.IsTrue(TaskManager.Tasks.Contains(task1));

      //now go to view2 in task1
      navigator1.NavigateTo("view2");
      Assert.AreEqual(task1, TaskManager.ActiveTask);
      Assert.AreEqual("view2", navigator1.LastNavigateTo.ToString());

      //now start task2
      Task task2 = TaskManager.StartTask("navigator2");
      Assert.IsNotNull(task2);
      Assert.AreEqual(task2, TaskManager.ActiveTask);
      Assert.AreEqual(task1, task2.Owner);

      OpenNavigator navigator2 = TaskManager.GetNavigatorForTask(task2) as OpenNavigator;
      Assert.IsNotNull(navigator2);
      Assert.IsNotNull(navigator2.LastNavigateTo);
      IViewManager viewManager2 = TaskManager.GetViewManagerForTask(task2);
      Assert.IsNotNull(viewManager2);
      Assert.IsFalse(viewManager1 == viewManager2);

      Assert.AreEqual("view5", navigator2.LastNavigateTo.ToString());
      Assert.AreEqual(2, TaskManager.Tasks.Count);
      Assert.IsTrue(TaskManager.Tasks.Contains(task1));
      Assert.IsTrue(TaskManager.Tasks.Contains(task2));

      //now go to view6 in task2
      navigator2.NavigateTo("view6");
      Assert.AreEqual(task2, TaskManager.ActiveTask);
      Assert.AreEqual("view6", navigator2.LastNavigateTo.ToString());

      //now end task2
      task2.End();
      Assert.IsTrue(task2.State == TaskState.Ended);
      Assert.AreEqual(1, TaskManager.Tasks.Count);
    }

    private void task_Ending(object sender, TaskEndingEventArgs e)
    {
      e.Cancel = true;
    }

    [Test]
    [ExpectedException(typeof(MvcException), "A task with ID 't' could not be found.")]
    public void TestGetNavigatorTaskNotFound()
    {
      TaskManager.GetNavigatorForTask(CreateTask("t"));
    }

    [Test]
    [ExpectedException(typeof(MvcException), "A task with ID 't' could not be found.")]
    public void TestGetViewManagerTaskNotFound()
    {
      TaskManager.GetViewManagerForTask(CreateTask("t"));
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestGetControllerTaskIdNull()
    {
      TaskManager.GetControllerForTask(null, null, null);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestGetControllerControllerConfigNull()
    {
      TaskManager.GetControllerForTask(CreateTask(), null, null);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestGetControllerControllerFactoryNull()
    {
      TaskManager.GetControllerForTask(CreateTask(), new ControllerInfo("c"), null);
    }

    [Test]
    [ExpectedException(typeof(MvcException), "A task with ID 't' could not be found.")]
    public void TestGetControllerTaskNotFound()
    {
      TaskManager.GetControllerForTask(CreateTask("t"), new ControllerInfo("c"), new MockControllerFactory());
    }

    [Test]
    public void TestGetControllerValid()
    {
      IControllerFactory controllerFactory = DefaultControllerFactory.Instance;
      Id<Task> taskId = "t";
      Id<INavigator> navigatorId = "n";
      NavigatorInfo navigatorInfo = new NavigatorInfo(navigatorId);
      navigatorInfo.StartViewInfo = new ViewInfo("v");
      ViewManagerInfo viewManagerInfo = new ViewManagerInfo("vm");
      viewManagerInfo.Type = typeof(MockViewManager);
      navigatorInfo.ViewManagerInfo = viewManagerInfo;
      navigatorInfo.Type = typeof(MockNavigator);
      TaskManager.ConfigurationInfo = new ConfigurationInfo();
      TaskManager.ConfigurationInfo.NavigatorInfos.Add(navigatorInfo);

      Task task = TaskManager.StartTask(navigatorId);

      ControllerInfo controllerInfo = new ControllerInfo("c");
      controllerInfo.Type = typeof(MockController);
      IController controller1 = TaskManager.GetControllerForTask(task, controllerInfo, controllerFactory);
      Assert.IsNotNull(controller1);
      IController controller2 = TaskManager.GetControllerForTask(task, controllerInfo, controllerFactory);
      Assert.IsNotNull(controller2);
      Assert.AreSame(controller1, controller2);
    }

    [Test]
    public void TestUsesTaskFactory()
    {
      Configure("Simple");

      Task task = new Task(null, "id", TaskManager, null);
      MockTaskFactory taskFactory = new MockTaskFactory();
      taskFactory.Task = task;
      TaskManager.ConfigurationInfo.TaskFactory = taskFactory;

      Task startedTask = TaskManager.StartTask("navigator");
      Assert.AreSame(task, startedTask);
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.