Вы находитесь на странице: 1из 6

public class Factory : IFactory

{
private readonly Parser<IArgumentsValues> _commandLineParser;
private readonly IFileSystem _fileSystem;
private readonly IConsoleService _consoleService;
public Factory()
{
var arguments = new Arguments();
_commandLineParser = new Parser<IArgumentsValues>(arguments);
_fileSystem = new FileSystem();
_consoleService = new ConsoleInputService();
}
public IParser<IArgumentsValues> CommandLineParser
{
get { return _commandLineParser; }
}
public IFileSystem FileSystem
{
get { return _fileSystem; }
}
public IConsoleService ConsoleService
{
get { return _consoleService; }
}
internal WorkFlowManager NewWorkflowManager()
{
return new WorkFlowManager(this);
}
}
public interface IFactory
{
IParser<IArgumentsValues> CommandLineParser { get; }
IFileSystem FileSystem { get; }
IConsoleService ConsoleService { get; }
}
public static class Dictionaries
{
public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dictionar
y, K key)
{
return dictionary.GetValueOrDefault(key, default(V));
}
public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dictionar
y, K key, V defaultValue)
{
V result;
if (!dictionary.TryGetValue(key, out result))
{
return defaultValue;
}
return result;
}

}
public static class Safe
{
public static void FileCreate(string filePath, string content)
{
CreateDirectory(Path.GetDirectoryName(filePath));
File.WriteAllText(filePath, content);
}
public static void CreateDirectory(string directoryPath)
{
if (String.IsNullOrEmpty(directoryPath))
return;
Directory.CreateDirectory(directoryPath);
}
public static void ResetTestDirectory(string directory)
{
Directory.CreateDirectory(directory);
WaitUntil(() => Directory.Exists(directory), "Failed to create direc
tory " + directory);
}
private static void WaitUntil(Func<bool> goal, string failureMessage)
{
DateTime startTime = DateTime.Now;
while (!goal())
{
Assert.That(DateTime.Now, Is.LessThanOrEqualTo(startTime.Add(Tim
eSpan.FromSeconds(3))),
failureMessage);
Thread.Sleep(TimeSpan.FromMilliseconds(100));
}
}
}
public class TestBase
{
protected string SampleFilePath;
protected FileSystem FileSystem;
protected string DestinationFilePath;
protected const string TestDirectory = "FileSystemTest";
protected const string SampleFileContent = "some content\r\ncoucou\r\n";
protected const string SampleFileName = "sample.txt";
[SetUp]
public void SetUp()
{
Safe.ResetTestDirectory(TestDirectory);
SampleFilePath = Path(SampleFileName);
Safe.FileCreate(SampleFilePath, SampleFileContent);
DestinationFilePath = Path(@"newDirectory\copy.txt");
FileSystem = new FileSystem();
}
protected static string Path(string fileName)

{
return System.IO.Path.Combine(TestDirectory, fileName);
}
protected static string GivenADirectory()
{
var directoryPath = System.IO.Path.GetDirectoryName(@"ADirectoryPath
\");
Safe.CreateDirectory(directoryPath);
return directoryPath;
}
}
[TestFixture]
public class FileSystemTest : TestBase
{
[Test]
public void FileExistance()
{
Assert.AreEqual(SampleFileContent, FileSystem.Read(SampleFilePath),
"FileSystem.Read content");
}
[Test]
public void CreatesFiles()
{
FileSystem.Create(DestinationFilePath, SampleFileContent);
Assert.AreEqual(SampleFileContent, File.ReadAllText(DestinationFileP
ath), "Content of the created file");
}
}
public class FileSystem : IFileSystem
{
public string Read(string filePath)
{
if (File.Exists(filePath))
{
return File.ReadAllText(filePath);
}
throw new FileNotFoundException("File not found");
}
public void Create(string filePath, string content)
{
MakeSureParentDirectoryExists(filePath);
File.WriteAllText(filePath, content);
}
private static void MakeSureParentDirectoryExists(string filePath)
{
MakeSureDirectoryExists(Path.GetDirectoryName(filePath));
}
private static void MakeSureDirectoryExists(string directory)
{
if (String.IsNullOrEmpty(directory))
{
return;

}
if (Directory.Exists(directory))
{
return;
}
Directory.CreateDirectory(directory);
}
}
public class FileInputReader : IInputReader
{
private readonly FileInfo _inputFileInfo;
private readonly IFileSystem _fileSystem;
public FileInputReader(FileInfo inputFileInfo, IFileSystem fileSystem)
{
_inputFileInfo = inputFileInfo;
_fileSystem = fileSystem;
}
public string Read()
{
if (_inputFileInfo == null)
throw new NullReferenceException();
return _fileSystem.Read(_inputFileInfo.FullName);
}
}
[TestFixture]
public class FileInputReaderTest
{
private Mock<FileSystemMock> _fileSystem;
[SetUp]
public void SetUp()
{
_fileSystem = FileSystemMock.New();
}
[Test, ExpectedException(typeof(NullReferenceException))]
public void ThrowsExcpetionWhenFileINfoIsNull()
{
var fileReader = new FileInputReader(null, _fileSystem.Object);
fileReader.Read();
}
[Test]
public void ReadsFromFileIfGiven()
{
var inputFileInfo = new FileInfo("validFile");
var fileReader = new FileInputReader(inputFileInfo, _fileSystem.Obje
ct);
fileReader.Read();
_fileSystem.Verify(x => x.Read(inputFileInfo.FullName), Times.AtLeas
tOnce, "Read from file");
}
}

public class ConsoleInputReader : IInputReader


{
private readonly IConsoleService _consoleService;
public ConsoleInputReader(IConsoleService consoleService)
{
_consoleService = consoleService;
}
public string Read()
{
return _consoleService.Read();
}
}
[TestFixture]
public class ConsoleInputReaderTest
{
[Test]
public void ReadsFromConsoleService()
{
var consoleService = new Mock<IConsoleService>();
var consoleReader = new ConsoleInputReader(consoleService.Object);
consoleReader.Read();
consoleService.Verify(x=>x.Read(), Times.AtLeastOnce, "Console.Read"
);
}
}
public class ConsoleInputService : IConsoleService
{
public string Read()
{
return Console.ReadLine();
}
}
public interface IConsoleService
{
string Read();
}
public interface IInputReader
{
string Read();
}
public class FileSystemMock : IFileSystem
{
private readonly Dictionary<string, string> _fileSystemFiles = new Dicti
onary<string, string>();
public virtual string Read(string filePath)
{
return _fileSystemFiles.GetValueOrDefault(filePath, "");
}
public virtual void Create(string filePath, string content)
{
_fileSystemFiles[filePath] = content;

}
public static Mock<FileSystemMock> New()
{
return new Mock<FileSystemMock> { CallBase = true };
}
}
public interface IFileSystem
{
string Read(string filePath);
void Create(string filePath, string content);
}