# HG changeset patch # User IBBoard # Date 1342035191 -3600 # Node ID 6ec46f3db2d4876af9723edbe99b1620d9d43e5e # Parent 7c21ca1482cb13b071c850d7cb857b37f7cba4b5 Re #419: Remove assumptions of a file-based install * Test loading from resources diff -r 7c21ca1482cb -r 6ec46f3db2d4 API/Factories/Mock/MockRaceAndSystemFactory.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/Mock/MockRaceAndSystemFactory.cs Wed Jul 11 20:33:11 2012 +0100 @@ -0,0 +1,92 @@ +// This file (MockSystemFactory.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2008, 2009 IBBoard. +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.IO; +using IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Objects; +using ICSharpCode.SharpZipLib.Zip; +using System.Collections.Generic; + +namespace IBBoard.WarFoundry.API.Factories.Mock +{ + public class MockRaceAndSystemFactory : AbstractNativeWarFoundryFactory + { + private static MockRaceAndSystemFactory mock; + + private GameSystem gameSystem; + + public static MockRaceAndSystemFactory GetMockFactory() + { + if (mock == null) + { + mock = new MockRaceAndSystemFactory(); + } + + return mock; + } + + public MockRaceAndSystemFactory() + { + gameSystem = new GameSystem("test", "Test System", this); + } + + protected override bool CheckCanFindArmyFileContent(ZipFile file) + { + return false; + } + + protected override bool CheckCanFindRaceFileContent(ZipFile file) + { + return true; + } + + protected override bool CheckCanFindSystemFileContent(ZipFile file) + { + return true; + } + + protected override Army CreateArmyFromStream(ZipFile file, Stream dataStream) + { + throw new NotImplementedException(); + } + + protected override GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream) + { + return gameSystem; + } + + protected override Race CreateRaceFromStream(ZipFile file, Stream dataStream) + { + return new Race("test", "Test Race", gameSystem, this); + } + + protected override ICollection GetArmyZipEntries(ZipFile file) + { + throw new NotImplementedException(); + } + + protected override ICollection GetGameSystemZipEntries(ZipFile file) + { + List entries = new List(); + ZipEntry zipEntry = file.GetEntry("data.systemx"); + if (zipEntry != null) + { + entries.Add(zipEntry); + } + return entries; + } + + protected override ICollection GetRaceZipEntries(ZipFile file) + { + List entries = new List(); + ZipEntry zipEntry = file.GetEntry("data.racex"); + if (zipEntry != null) + { + entries.Add(zipEntry); + } + return entries; + } + } +} diff -r 7c21ca1482cb -r 6ec46f3db2d4 API/Factories/Xml/WarFoundryXmlFactoryTests.cs --- a/API/Factories/Xml/WarFoundryXmlFactoryTests.cs Sat Jul 07 21:02:53 2012 +0100 +++ b/API/Factories/Xml/WarFoundryXmlFactoryTests.cs Wed Jul 11 20:33:11 2012 +0100 @@ -31,8 +31,7 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } @@ -67,8 +66,7 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } @@ -103,13 +101,12 @@ { raceCount++; } - else - if (obj is GameSystem) + else if (obj is GameSystem) { systemCount++; } } - + Assert.That(raceCount, Is.EqualTo(2)); Assert.That(systemCount, Is.EqualTo(2)); } @@ -118,5 +115,30 @@ loader.UnregisterFactory(factory); } } + + [Test()] + public void TestLoadingRaceFromResource() + { + WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory(); + GameSystem system = new GameSystem("sample", "Sample System", factory); + FixedGameSystemWarFoundryLoader fixedLoader = new FixedGameSystemWarFoundryLoader(system); + WarFoundryLoader.SetDefault(fixedLoader); + ICollection objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race")); + Assert.That(objs, Has.Count(1)); + IEnumerator enumerator = objs.GetEnumerator(); + enumerator.MoveNext(); + Assert.That(enumerator.Current, Is.TypeOf(typeof(Race))); + } + + [Test()] + public void TestLoadingSystemFromResource() + { + WarFoundryXmlFactory factory = WarFoundryXmlFactory.GetFactory(); + ICollection objs = factory.CreateObjectsFromFile(new LoadableResourceObject(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.SampleSystem.system")); + Assert.That(objs, Has.Count(1)); + IEnumerator enumerator = objs.GetEnumerator(); + enumerator.MoveNext(); + Assert.That(enumerator.Current, Is.TypeOf(typeof(GameSystem))); + } } } diff -r 7c21ca1482cb -r 6ec46f3db2d4 API/WarFoundryLoaderTest.cs --- a/API/WarFoundryLoaderTest.cs Sat Jul 07 21:02:53 2012 +0100 +++ b/API/WarFoundryLoaderTest.cs Wed Jul 11 20:33:11 2012 +0100 @@ -86,6 +86,28 @@ Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.Not.EqualTo(mockFactory1)); Assert.That(WarFoundryLoader.GetRequirementFactory(factoryID), Is.EqualTo(mockFactory2)); } + + [Test()] + public void TestRegisteringResourceFiles() + { + AbstractWarFoundryLoader loader = WarFoundryLoader.GetDefault(); + loader.RegisterFactory(new MockRaceAndSystemFactory()); + ILoadableObjectSource loadSource = new LoadableObjectSourceResourceSet(typeof(WarFoundryLoader).Assembly, "IBBoard.WarFoundry.data.Empire.race", "IBBoard.WarFoundry.data.SampleSystem.system"); + loader.AddLoadSource(loadSource); + loader.LoadFiles(); + + try + { + Assert.That(loader.GetGameSystems(), Has.Length(1)); + GameSystem gameSystem = loader.GetGameSystems()[0]; + Assert.That(loader.GetRaces(gameSystem), Has.Length(1)); + } + finally + { + loader.RemoveLoadSource(loadSource); + loader.UnregisterFactory(GetSystemFactory()); + } + } private AbstractNativeWarFoundryFactory GetSystemFactory() { diff -r 7c21ca1482cb -r 6ec46f3db2d4 IBBoard.WarFoundry.API.Tests.csproj --- a/IBBoard.WarFoundry.API.Tests.csproj Sat Jul 07 21:02:53 2012 +0100 +++ b/IBBoard.WarFoundry.API.Tests.csproj Wed Jul 11 20:33:11 2012 +0100 @@ -126,6 +126,7 @@ +