view API/Savers/IWarFoundryFileSaverTests.cs @ 100:5addcb8f7766

Re #324: Add saving of Race and System data to files * Add unit tests for multiple files in one zip
author IBBoard <dev@ibboard.co.uk>
date Sat, 12 Mar 2011 19:54:07 +0000
parents dd384f35a96f
children 08fd75eafb67
line wrap: on
line source

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Objects.Mock;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using ICSharpCode.SharpZipLib.Core;
using IBBoard.IO;

namespace IBBoard.WarFoundry.API.Savers
{
	public abstract class IWarFoundryFileSaverTests
	{
		[Test()]
		public void TestSaverCreatesFile()
		{
			string tempFile = Path.GetTempFileName();
			
			try
			{
				bool saved = GetSaver().Save(tempFile, new MockGameSystem());
				Assert.That(saved);
				Assert.That(File.Exists(tempFile));
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesZipFile()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem());
				ZipFile file = new ZipFile(tempFile);
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryForOneObject()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem());
				ZipFile file = new ZipFile(tempFile);
				Assert.That(file.Count, Is.EqualTo(1));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectNameForOneObject()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				IEnumerator enumerator = file.GetEnumerator();
				enumerator.MoveNext();
				ZipEntry current = (ZipEntry)enumerator.Current;
				Assert.That(current.Name, Is.EqualTo(GetEntryName(obj)));
				Assert.That(current.Name, Text.EndsWith(GetFileExtensionForType(obj)));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectContentForOneSystem()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectContentForOneArmy()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockArmy obj = MockArmy.GetMockArmy();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetArmyContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesForOneSystemOneArmy()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem(), MockArmy.GetMockArmy());
				ZipFile file = new ZipFile(tempFile);
				Assert.That(file.Count, Is.EqualTo(2));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesWithCorrectContentForOneSystemOneArmy()
		{
			//This doesn't make much sense (system and army in one file) but at the time of writing then Race isn't implemented
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem system = new MockGameSystem();
				MockArmy army = MockArmy.GetMockArmy();
				GetSaver().Save(tempFile, system, army);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(system));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentBytes()));
				zipEntry = file.GetEntry(GetEntryName(army));
				stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetArmyContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}
		
		[Test()]
		public void TestFileExtensionsAreCorrectForType()
		{
			Assert.That(GetSaver().GetFileExtension(MockArmy.GetMockArmy()), Is.EqualTo(GetArmyExtension()));
			Assert.That(GetSaver().GetFileExtension(new MockRace()), Is.EqualTo(GetRaceExtension()));
			Assert.That(GetSaver().GetFileExtension(new MockGameSystem()), Is.EqualTo(GetGameSystemExtension()));
		}

		protected abstract IWarFoundryFileSaver GetSaver();

		protected abstract string GetEntryName(WarFoundryLoadedObject obj);

		protected abstract string GetFileExtensionForType(WarFoundryLoadedObject obj);
		
		protected abstract string GetArmyExtension();
		
		protected abstract string GetRaceExtension();
		
		protected abstract string GetGameSystemExtension();

		protected abstract byte[] GetGameSystemContentBytes();
		
		protected abstract byte[] GetArmyContentBytes();
	}
}