# HG changeset patch # User IBBoard # Date 1278273856 0 # Node ID b2517bb113d06030af5c3f0f020a0bef77ff0646 # Parent a82f164e26e3d823bb46b3733ea23fec418cfa3a Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type * Validate loading of different key/keyref options in army file This may be all of it, but needs checking diff -r a82f164e26e3 -r b2517bb113d0 API/Factories/Xml/SingleXmlObjectLoader.cs --- a/API/Factories/Xml/SingleXmlObjectLoader.cs Sun Jul 04 19:11:08 2010 +0000 +++ b/API/Factories/Xml/SingleXmlObjectLoader.cs Sun Jul 04 20:04:16 2010 +0000 @@ -32,6 +32,11 @@ } } + public static XmlElement CreateDocumentElementFromFile(string filePath) + { + return CreateDocumentElementFromFile(new FileInfo(filePath)); + } + public static XmlElement CreateDocumentElementFromFile(FileInfo file) { return CreateDocumentElementFromStream(file.OpenRead()); diff -r a82f164e26e3 -r b2517bb113d0 API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs --- a/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs Sun Jul 04 19:11:08 2010 +0000 +++ b/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs Sun Jul 04 20:04:16 2010 +0000 @@ -3,9 +3,11 @@ // // 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 System.Xml; +using System.Xml.Schema; +using IBBoard.IO; using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; namespace IBBoard.WarFoundry.API.Factories.Xml { @@ -15,15 +17,48 @@ [Test()] public void TestParsingExtendedDataFiles() { - TestParsingExtendedDataFile("testdata/extended-data.racex"); - TestParsingExtendedDataFile("testdata/extended-data.systemx"); - TestParsingExtendedDataFile("testdata/extended-data.armyx"); + TestValidFileLoading("testdata/extended-data.racex"); + TestValidFileLoading("testdata/extended-data.systemx"); + TestValidFileLoading("testdata/extended-data.armyx"); + } + + [Test()] + public void TestParsingValidArmyContainedID() + { + TestValidFileLoading("testdata/xml-factory-util/valid-contained-id.armyx"); + } + + private static void TestValidFileLoading(string path) + { + XmlElement document = SingleXmlObjectLoader.CreateDocumentElementFromFile(path); + Assert.IsNotNull(document); } - private static void TestParsingExtendedDataFile(string path) + [Test()] + public void TestArmyFileValidationFailsForDuplicateUnitID() + { + TestFileValidationFailure("testdata/xml-factory-util/duplicate-unit-id.armyx"); + } + + [Test()] + public void TestArmyFileValidationFailsForInvalidContainedID() { - XmlDocument document = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(new FileInfo(path).OpenRead()); - Assert.IsNotNull(document); + TestFileValidationFailure("testdata/xml-factory-util/invalid-contained-id.armyx"); + } + + private void TestFileValidationFailure(string filePath) + { + try + { + SingleXmlObjectLoader.CreateDocumentElementFromFile(filePath); + Assert.Fail("Excepted exception not thrown"); + } + catch (InvalidFileException ex) + { + Console.WriteLine(ex.Message); + Exception innerException = ex.InnerException; + Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException))); + } } } } diff -r a82f164e26e3 -r b2517bb113d0 IBBoard.WarFoundry.API.Tests.csproj --- a/IBBoard.WarFoundry.API.Tests.csproj Sun Jul 04 19:11:08 2010 +0000 +++ b/IBBoard.WarFoundry.API.Tests.csproj Sun Jul 04 20:04:16 2010 +0000 @@ -201,6 +201,15 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + @@ -235,5 +244,6 @@ + \ No newline at end of file diff -r a82f164e26e3 -r b2517bb113d0 testdata/xml-factory-util/duplicate-unit-id.armyx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/duplicate-unit-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff -r a82f164e26e3 -r b2517bb113d0 testdata/xml-factory-util/invalid-contained-id.armyx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/invalid-contained-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff -r a82f164e26e3 -r b2517bb113d0 testdata/xml-factory-util/valid-contained-id.armyx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdata/xml-factory-util/valid-contained-id.armyx Sun Jul 04 20:04:16 2010 +0000 @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file