//-----------------------------------------------------------------------
//
// Copyright (c) 2011 Deltares. All rights reserved.
//
// B.S.T. The
// tom.the@deltares.nl
// 20-12-2011
// Test load and save state for DikeFlow calculation
//-----------------------------------------------------------------------
using Deltares.Standard.IO;
namespace Deltares.Dam.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Deltares.Dam.Data;
[TestFixture]
public class DupuitStateFilerTests
{
private const string saveStatePath = "SaveState";
private const string workingPath = "WorkingState";
private const string finalStateExtension = ".C049";
private const string middleStateExtension = ".C023";
private const string firstStateExtension = ".C000";
private const string fileStateMask = "*.C*";
private string[] basefilenames = { "file1", "file2", "file3" };
static private void CreateDirectoryIfNotExists(string pathName)
{
if (!Directory.Exists(pathName))
{
Directory.CreateDirectory(pathName);
}
}
private void CreateFileSet(string destinationPath, string extension)
{
CreateDirectoryIfNotExists(destinationPath);
string contentString = "test";
foreach (string basefilename in basefilenames)
{
System.IO.File.WriteAllText(Path.Combine(destinationPath, basefilename + extension), contentString);
}
}
[Test]
[ExpectedException(typeof(DupuitStateFilerException))]
public void ThrowsExceptionInSaveStateWhenSourcePathNotExists()
{
DupuitStateFiler.SaveState("Non-existing path", saveStatePath, finalStateExtension);
}
[Test]
[ExpectedException(typeof(DupuitStateFilerException))]
public void ThrowsExceptionInLoadStateWhenSourcePathNotExists()
{
DupuitStateFiler.SaveState("Non-existing path", workingPath, finalStateExtension);
}
private void CheckStateFilesCount(string path, string stateExtension)
{
string[] fileEntries = Directory.GetFiles(path, "*" + stateExtension);
int fileCount = fileEntries.Length;
Assert.AreEqual(basefilenames.Length, fileCount);
}
[Test]
public void CanSaveState()
{
CreateFileSet(workingPath, finalStateExtension);
DirectoryHelper.EmptyDirectory(saveStatePath);
DupuitStateFiler.SaveState(workingPath, saveStatePath, finalStateExtension);
CheckStateFilesCount(saveStatePath, DupuitStateFiler.endStateExtension);
}
[Test]
public void CanLoadState()
{
CreateFileSet(workingPath, finalStateExtension);
DirectoryHelper.EmptyDirectory(saveStatePath);
DupuitStateFiler.SaveState(workingPath, saveStatePath, finalStateExtension);
CheckStateFilesCount(saveStatePath, DupuitStateFiler.endStateExtension);
DirectoryHelper.EmptyDirectory(workingPath);
DupuitStateFiler.LoadState(saveStatePath, workingPath, DupuitStateFiler.endStateExtension);
CheckStateFilesCount(workingPath, DupuitStateFiler.initialStateExtension);
}
[Test]
public void CanFindLastStateExtension()
{
CreateFileSet(workingPath, firstStateExtension);
CreateFileSet(workingPath, finalStateExtension);
CreateFileSet(workingPath, middleStateExtension);
string actualFinalStateExtension = DupuitStateFiler.GetLastStateExtension(workingPath, fileStateMask);
Assert.AreEqual(finalStateExtension, actualFinalStateExtension);
}
}
}