view WarFoundryFactoryFactoryTest.cs @ 1:b93d6eedc0bb

* Update MonoDevelop Project file for MonoDevelop 2.0a1 * Remove PIDB file (Parser Information Database) from source control
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 16:41:09 +0000
parents faf976fe57df
children 4222cfa99c78
line wrap: on
line source

// AbstractNativeWarFoundryFactoryTest.cs
//
//  Copyright (C) 2007 IBBoard
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 of the License as published by the Free
// Software Foundation.
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
//

using System;
using NUnit.Framework;
using IBBoard;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Factories.Xml;

namespace IBBoard.WarFoundry.API.Factories
{	
	[TestFixture()]
	public class AbstractNativeWarFoundryFactoryTest
	{		
		[Test()]
		public void TestFactoryReturnsCorrectClassForDefault()
		{		
			WarFoundryFactoryFactory.GetFactoryFactory().DefaultType = GetDefaultFactoryClass();
			Assert.AreEqual(GetDefaultFactoryClass(), WarFoundryFactoryFactory.GetFactoryFactory().GetFactory().GetType(), "AbstractNativeWarFoundryFactory returned incorrect default factory type");
		}
		
		[Test()]
		[Ignore("Only one class of factory exists in current implementation")]
		public void TestFactoryReturnsCorrectClassForAlteredDefault()
		{
			WarFoundryFactoryFactory.GetFactoryFactory().DefaultType = GetDefaultFactoryClass();
			WarFoundryFactoryFactory.GetFactoryFactory().DefaultType = GetOtherFactoryClass();
			Assert.AreEqual(GetOtherFactoryClass(), WarFoundryFactoryFactory.GetFactoryFactory().GetFactory().GetType(), "AbstractNativeWarFoundryFactory returned incorrect default factory type for updated factory");
		}
		
		[Test()]
		public void TestFactoryReturnsCorrectClassForXmlFactory()
		{
			Assert.AreEqual(WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass()).GetType(), GetFactoryClass(), "AbstractNativeWarFoundryFactory failed to return correct type when passed a valid type");
		}
		
		[Test()]
		public void TestFactoryReturnsEqualFactoryForMatchingClasses()
		{
			IWarFoundryFactory first = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			IWarFoundryFactory second = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			Assert.AreEqual(first, second, "AbstractNativeWarFoundryFactory failed to return equal factory for matching string");
		}	
		
		[Test()]
		public void TestFactoryReturnsSameFactoryForMatchingClasses()
		{
			IWarFoundryFactory first = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			IWarFoundryFactory second = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			Assert.IsTrue(first == second, "AbstractNativeWarFoundryFactory failed to return cached factory");
		}
		
		[Test()]
		//[ExpectedException(typeof(ArgumentException), ExpectedMessage="was not a subtype", MatchType=MessageMatch.Contains )]
		[ExpectedException(typeof(ArgumentException))]
		public void TestFactoryThrowsArgumentExceptionForInvalidClass()
		{
			WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetInvalidFactoryClass());
			Assert.Fail("Exceptect exception was not thrown");
		}
		
		[Test()]
		//[ExpectedException(typeof(ArgumentException), ExpectedMessage="was not a subtype", MatchType=MessageMatch.Contains )]
		[ExpectedException(typeof(ArgumentException))]
		public void TestFactoryThrowsArgumentExceptionForAbstractClass()
		{
			WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(typeof(AbstractNativeWarFoundryFactory));
			Assert.Fail("Exceptect exception was not thrown");
		}
		
		[Test()]
		[Ignore("Only one class of factory exists in current implementation")]
		public void TestFactoryReturnsEqualFactoriesForSameClassAfterDifferentClasses()
		{
			IWarFoundryFactory first = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetOtherFactoryClass());
			IWarFoundryFactory second = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(GetFactoryClass());
			Assert.AreEqual(first, second, "AbstractNativeWarFoundryFactory did not return the same factory after creating a new factory of a different class");
		}
				
		private Type GetDefaultFactoryClass()
		{
			return typeof(WarFoundryXmlFactory);
		}
		
		private Type GetInvalidFactoryClass()
		{
			return typeof(String);
		}
		
		private Type GetFactoryClass()
		{
			return typeof(WarFoundryXmlFactory);
		}
		
		private Type GetOtherFactoryClass()
		{
			//TODO: Fill in when we have a second class
			return null;
		}
	}
}