changeset 69:b2517bb113d0

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
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jul 2010 20:04:16 +0000
parents a82f164e26e3
children 90a9f13b9627
files API/Factories/Xml/SingleXmlObjectLoader.cs API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs IBBoard.WarFoundry.API.Tests.csproj testdata/xml-factory-util/duplicate-unit-id.armyx testdata/xml-factory-util/invalid-contained-id.armyx testdata/xml-factory-util/valid-contained-id.armyx
diffstat 6 files changed, 84 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/API/Factories/Xml/SingleXmlObjectLoader.cs	Sun Jul 04 19:11:08 2010 +0000
     1.2 +++ b/API/Factories/Xml/SingleXmlObjectLoader.cs	Sun Jul 04 20:04:16 2010 +0000
     1.3 @@ -32,6 +32,11 @@
     1.4  			}
     1.5  		}
     1.6  		
     1.7 +		public static XmlElement CreateDocumentElementFromFile(string filePath)
     1.8 +		{
     1.9 +			return CreateDocumentElementFromFile(new FileInfo(filePath));
    1.10 +		}
    1.11 +		
    1.12  		public static XmlElement CreateDocumentElementFromFile(FileInfo file)
    1.13  		{
    1.14  			return CreateDocumentElementFromStream(file.OpenRead());
     2.1 --- a/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs	Sun Jul 04 19:11:08 2010 +0000
     2.2 +++ b/API/Factories/Xml/WarFoundryXmlFactoryUtilTest.cs	Sun Jul 04 20:04:16 2010 +0000
     2.3 @@ -3,9 +3,11 @@
     2.4  //  // 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.
     2.5  
     2.6  using System;
     2.7 -using System.IO;
     2.8  using System.Xml;
     2.9 +using System.Xml.Schema;
    2.10 +using IBBoard.IO;
    2.11  using NUnit.Framework;
    2.12 +using NUnit.Framework.SyntaxHelpers;
    2.13  
    2.14  namespace IBBoard.WarFoundry.API.Factories.Xml
    2.15  {
    2.16 @@ -15,15 +17,48 @@
    2.17  		[Test()]
    2.18  		public void TestParsingExtendedDataFiles()
    2.19  		{
    2.20 -			TestParsingExtendedDataFile("testdata/extended-data.racex");
    2.21 -			TestParsingExtendedDataFile("testdata/extended-data.systemx");
    2.22 -			TestParsingExtendedDataFile("testdata/extended-data.armyx");
    2.23 +			TestValidFileLoading("testdata/extended-data.racex");
    2.24 +			TestValidFileLoading("testdata/extended-data.systemx");
    2.25 +			TestValidFileLoading("testdata/extended-data.armyx");
    2.26  		}
    2.27  		
    2.28 -		private static void TestParsingExtendedDataFile(string path)
    2.29 +		[Test()]
    2.30 +		public void TestParsingValidArmyContainedID()
    2.31  		{
    2.32 -			XmlDocument document = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(new FileInfo(path).OpenRead());
    2.33 +			TestValidFileLoading("testdata/xml-factory-util/valid-contained-id.armyx");
    2.34 +		}
    2.35 +		
    2.36 +		private static void TestValidFileLoading(string path)
    2.37 +		{
    2.38 +			XmlElement document = SingleXmlObjectLoader.CreateDocumentElementFromFile(path);
    2.39  			Assert.IsNotNull(document);
    2.40  		}
    2.41 +		
    2.42 +		[Test()]
    2.43 +		public void TestArmyFileValidationFailsForDuplicateUnitID()
    2.44 +		{
    2.45 +			TestFileValidationFailure("testdata/xml-factory-util/duplicate-unit-id.armyx");
    2.46 +		}
    2.47 +
    2.48 +		[Test()]
    2.49 +		public void TestArmyFileValidationFailsForInvalidContainedID()
    2.50 +		{
    2.51 +			TestFileValidationFailure("testdata/xml-factory-util/invalid-contained-id.armyx");
    2.52 +		}
    2.53 +
    2.54 +		private void TestFileValidationFailure(string filePath)
    2.55 +		{			
    2.56 +			try
    2.57 +			{
    2.58 +				SingleXmlObjectLoader.CreateDocumentElementFromFile(filePath);
    2.59 +				Assert.Fail("Excepted exception not thrown");
    2.60 +			}
    2.61 +			catch (InvalidFileException ex)
    2.62 +			{
    2.63 +				Console.WriteLine(ex.Message);
    2.64 +				Exception innerException = ex.InnerException;
    2.65 +				Assert.That(innerException, Is.InstanceOfType(typeof(XmlSchemaValidationException)));
    2.66 +			}
    2.67 +		}
    2.68  	}
    2.69  }
     3.1 --- a/IBBoard.WarFoundry.API.Tests.csproj	Sun Jul 04 19:11:08 2010 +0000
     3.2 +++ b/IBBoard.WarFoundry.API.Tests.csproj	Sun Jul 04 20:04:16 2010 +0000
     3.3 @@ -201,6 +201,15 @@
     3.4      <None Include="testdata\xml-system-factory\invalid-default-stats.systemx">
     3.5        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     3.6      </None>
     3.7 +    <None Include="testdata\xml-factory-util\duplicate-unit-id.armyx">
     3.8 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     3.9 +    </None>
    3.10 +    <None Include="testdata\xml-factory-util\invalid-contained-id.armyx">
    3.11 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    3.12 +    </None>
    3.13 +    <None Include="testdata\xml-factory-util\valid-contained-id.armyx">
    3.14 +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    3.15 +    </None>
    3.16    </ItemGroup>
    3.17    <ItemGroup>
    3.18      <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73">
    3.19 @@ -235,5 +244,6 @@
    3.20      <Folder Include="testdata\" />
    3.21      <Folder Include="testdata\" />
    3.22      <Folder Include="testdata\xml-system-factory\" />
    3.23 +    <Folder Include="testdata\xml-factory-util\" />
    3.24    </ItemGroup>
    3.25  </Project>
    3.26 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/testdata/xml-factory-util/duplicate-unit-id.armyx	Sun Jul 04 20:04:16 2010 +0000
     4.3 @@ -0,0 +1,7 @@
     4.4 +<?xml version="1.0" encoding="UTF-8"?>
     4.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100">
     4.6 +	<units>
     4.7 +		<unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"/>
     4.8 +		<unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10"/>
     4.9 +	</units>
    4.10 +</army>
    4.11 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/testdata/xml-factory-util/invalid-contained-id.armyx	Sun Jul 04 20:04:16 2010 +0000
     5.3 @@ -0,0 +1,10 @@
     5.4 +<?xml version="1.0" encoding="UTF-8"?>
     5.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100">
     5.6 +	<units>
     5.7 +		<unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10">
     5.8 +			<contains>
     5.9 +				<containedUnit containedID="unit2" />
    5.10 +			</contains>
    5.11 +		</unit>
    5.12 +	</units>
    5.13 +</army>
    5.14 \ No newline at end of file
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/testdata/xml-factory-util/valid-contained-id.armyx	Sun Jul 04 20:04:16 2010 +0000
     6.3 @@ -0,0 +1,11 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<army xmlns="http://ibboard.co.uk/warfoundry/army" xmlns:core="http://ibboard.co.uk/warfoundry/core" xmlns:ext="http://ibboard.co.uk/warfoundry/missing" id="army" name="" system="" race="" maxPoints="100">
     6.6 +	<units>
     6.7 +		<unit id="unit1" unitType="swordsmen" unitName="Swordsment" size="10">
     6.8 +			<contains>
     6.9 +				<containedUnit containedID="unit2" />
    6.10 +			</contains>
    6.11 +		</unit>
    6.12 +		<unit id="unit2" unitType="axemen" unitName="Axemen" size="10"/>
    6.13 +	</units>
    6.14 +</army>
    6.15 \ No newline at end of file