#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 System.IO;
using System.Reflection;
using System.Text;
using System.Resources;
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.Configuration.Configurators;
using Ingenious.Mvc.Factories;
using Ingenious.Mvc.Navigators;
using Ingenious.Mvc.UnitTest.MockTypes;
namespace Ingenious.Mvc.UnitTest.Configuration.Configurators{
/// <summary>
/// Unit tests the <see cref="XmlParser"/> class via the <see cref="XmlConfigurator"/> class.
/// </summary>
public sealed class XmlConfiguratorTest : UnitTestBase
{
[Test]
[ExpectedException(typeof(ConfigurationException), "The following XML errors were detected during validation:\r\n - The element 'ingeniousMvc' has incomplete content. List of possible elements expected: 'factories, controllers, views'.\r\n")]
public void TestInvalid()
{
//just make sure it is validating against XSD
Configure("Invalid");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "Type 'FooBar' for element 'controllerFactory' could not be loaded.")]
public void TestFactoryNotFound()
{
Configure("InvalidFactoryNotFound");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The specified factory type 'Ingenious.Mvc.UnitTest.MockTypes.MockViewFactory' does not implement the required interface 'Ingenious.Mvc.IControllerFactory'.")]
public void TestFactoryDoesNotImplementInterface()
{
Configure("InvalidFactoryDoesNotImplementInterface");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The specified factory type 'Ingenious.Mvc.UnitTest.Configuration.Configurators.XmlConfiguratorTest+ControllerFactory' does not implement a default constructor.")]
public void TestFactoryDoesNotHaveDefaultConstructor()
{
Configure("InvalidFactoryDoesNotHaveDefaultConstructor");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The enumeration member 'C' could not be found in enumeration type 'Ingenious.Mvc.UnitTest.Configuration.Configurators.XmlConfiguratorTest+Controllers'.")]
public void TestControllerIdInvalid()
{
Configure("InvalidControllerIdInvalid");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "Type 'FooBar' for element 'controller' with ID 'C' could not be loaded.")]
public void TestControllerNotFound()
{
Configure("InvalidControllerNotFound");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The enumeration member 'N' could not be found in enumeration type 'Ingenious.Mvc.UnitTest.Configuration.Configurators.XmlConfiguratorTest+Navigators'.")]
public void TestNavigatorIdInvalid()
{
Configure("InvalidNavigatorIdInvalid");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "Type 'FooBar' for element 'navigator' with ID 'N' could not be loaded.")]
public void TestNavigatorNotFound()
{
Configure("InvalidNavigatorNotFound");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The enumeration member 'V' could not be found in enumeration type 'Ingenious.Mvc.UnitTest.Configuration.Configurators.XmlConfiguratorTest+Views'.")]
public void TestViewIdInvalid()
{
Configure("InvalidViewIdInvalid");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "Type 'FooBar' for element 'view' with ID 'V' could not be loaded.")]
public void TestViewNotFound()
{
Configure("InvalidViewNotFound");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "The enumeration member 'VM' could not be found in enumeration type 'Ingenious.Mvc.UnitTest.Configuration.Configurators.XmlConfiguratorTest+ViewManagers'.")]
public void TestViewManagerIdInvalid()
{
Configure("InvalidViewManagerIdInvalid");
}
[Test]
[ExpectedException(typeof(ConfigurationException), "Type 'FooBar' for element 'viewManager' with ID 'VM' could not be loaded.")]
public void TestViewManagerNotFound()
{
Configure("InvalidViewManagerNotFound");
}
[Test]
public void TestValidConfigurableFactory()
{
Configure("ValidConfigurableFactory");
Assert.AreEqual("My custom factory info", ConfigurableControllerFactoryParser.CustomConfiguration);
}
[Test]
public void TestValidNoStartingView()
{
Configure("ValidNoStartingView");
DoCommonValidAssertions();
//should be no starting views
Assert.IsNull(TaskManager.ConfigurationInfo.NavigatorInfos["N1"].StartViewInfo);
Assert.IsNull(TaskManager.ConfigurationInfo.NavigatorInfos["N2"].StartViewInfo);
}
[Test]
public void TestValid()
{
Configure("Valid");
DoCommonValidAssertions();
//starting views should be set
Assert.AreEqual("V1", TaskManager.ConfigurationInfo.NavigatorInfos[Navigators.N1].StartViewInfo.Id.ToString());
Assert.AreEqual("V2", TaskManager.ConfigurationInfo.NavigatorInfos[Navigators.N2].StartViewInfo.Id.ToString());
}
[Test]
public void TestMixedViewManagers()
{
Configure("MixedViewManagers");
ConfigurationInfo config = TaskManager.ConfigurationInfo;
//check number of items
Assert.AreEqual(2, config.NavigatorInfos.Count);
Assert.AreEqual(3, config.ViewInfos.Count);
//check navigators
Assert.AreEqual(typeof(GraphNavigator), config.NavigatorInfos["Main"].Type);
Assert.AreEqual("Forms", config.NavigatorInfos["Main"].ViewManagerInfo.Id.ToString());
Assert.AreEqual(typeof(OpenNavigator), config.NavigatorInfos["Navigation"].Type);
Assert.AreEqual("UserControls", config.NavigatorInfos["Navigation"].ViewManagerInfo.Id.ToString());
}
private void DoCommonValidAssertions()
{
ConfigurationInfo config = TaskManager.ConfigurationInfo;
//check number of items
Assert.AreEqual(2, config.NavigatorInfos.Count);
Assert.AreEqual(3, config.ViewInfos.Count);
//check factories
Assert.AreEqual(typeof(MockControllerFactory), config.ControllerFactory.GetType());
Assert.AreEqual(DefaultNavigatorFactory.Instance, config.NavigatorFactory);
Assert.AreEqual(DefaultViewFactory.Instance, config.ViewFactory);
Assert.AreEqual(DefaultViewManagerFactory.Instance, config.ViewManagerFactory);
//check navigators
Assert.AreEqual(typeof(ConfigurableNavigator), config.NavigatorInfos[Navigators.N1].Type);
Assert.AreEqual("VM1", config.NavigatorInfos[Navigators.N1].ViewManagerInfo.Id.ToString());
Assert.AreEqual(typeof(MockNavigator), config.NavigatorInfos[Navigators.N2].Type);
Assert.AreEqual("VM2", config.NavigatorInfos[Navigators.N2].ViewManagerInfo.Id.ToString());
//check views
Assert.AreEqual(typeof(MockView), config.ViewInfos[Views.V1].Type);
Assert.AreEqual("C1", config.ViewInfos[Views.V1].ControllerInfo.Id.ToString());
Assert.AreEqual(typeof(MockView), config.ViewInfos[Views.V2].Type);
Assert.AreEqual("C2", config.ViewInfos[Views.V2].ControllerInfo.Id.ToString());
Assert.AreEqual(typeof(MockView), config.ViewInfos[Views.V3].Type);
Assert.IsNull(config.ViewInfos[Views.V3].ControllerInfo);
//check custom config
Assert.AreEqual("My custom stuff", ConfigurableNavigatorParser.CustomConfiguration);
}
#region Supporting Types
private enum Controllers
{
C1,
C2
}
private enum Navigators
{
N1,
N2
}
private enum Views
{
V1,
V2,
V3
}
private enum ViewManagers
{
VM1,
VM2
}
private sealed class ControllerFactory : IControllerFactory
{
public ControllerFactory(int dummy)
{
}
public IController Create(ControllerInfo controllerInfo, Task task, INavigator navigator)
{
return null;
}
}
[XmlCustomParser(typeof(ConfigurableNavigatorParser))]
private sealed class ConfigurableNavigator : MockNavigator, ICustomConfigurable
{
private object _customConfiguration;
public object CustomConfiguration
{
get
{
return _customConfiguration;
}
}
public void SetCustomConfiguration(object customConfiguration)
{
_customConfiguration = customConfiguration;
}
}
private sealed class ConfigurableNavigatorParser : IXmlCustomParser
{
private static string _customConfiguration;
private XmlSchema _schema;
public XmlSchema Schema
{
get
{
if (_schema == null)
{
ResourceManager rm = new ResourceManager(typeof(XmlConfiguratorTest));
_schema = XmlSchema.Read(new StringReader(rm.GetString("NavigatorXsd")), null);
}
return _schema;
}
}
public string TypeName
{
get
{
return "test";
// return (XmlSchemaType) Schema.SchemaTypes[new XmlQualifiedName("test")];
}
}
public static string CustomConfiguration
{
get
{
return _customConfiguration;
}
}
public object Parse(ConfigurationInfo configurationInfo, object parent, XmlNode section)
{
XmlNode node = section.SelectSingleNode("custom");
_customConfiguration = node.InnerText;
return _customConfiguration;
}
}
[XmlCustomParser(typeof(ConfigurableControllerFactoryParser))]
private sealed class ConfigurableControllerFactory : IControllerFactory, ICustomConfigurable
{
private object _customConfiguration;
public object CustomConfiguration
{
get
{
return _customConfiguration;
}
}
public IController Create(ControllerInfo controllerInfo, Task task, INavigator navigator)
{
return DefaultControllerFactory.Instance.Create(controllerInfo, task, navigator);
}
public void SetCustomConfiguration(object customConfiguration)
{
_customConfiguration = customConfiguration;
}
}
private sealed class ConfigurableControllerFactoryParser : IXmlCustomParser
{
private static string _customConfiguration;
private XmlSchema _schema;
public XmlSchema Schema
{
get
{
if (_schema == null)
{
ResourceManager rm = new ResourceManager(typeof(XmlConfiguratorTest));
_schema = XmlSchema.Read(new StringReader(rm.GetString("ControllerFactoryXsd")), null);
}
return _schema;
}
}
public string TypeName
{
get
{
return "test";
// return (XmlSchemaType) Schema.SchemaTypes[new XmlQualifiedName("test")];
}
}
public static string CustomConfiguration
{
get
{
return _customConfiguration;
}
}
public object Parse(ConfigurationInfo configurationInfo, object parent, XmlNode section)
{
XmlNode node = section.SelectSingleNode("custom");
_customConfiguration = node.InnerText;
return _customConfiguration;
}
}
#endregion
}
}
|