Browse files

Merge pull request #62 from KevM/loadfromfile

LoadFromFile returns empty object when file does not exist
  • Loading branch information...
2 parents 13432f7 + a5f393f commit 227b59f26447bfc7f2ac04a07276daad418bfd3d @jeremydmiller jeremydmiller committed Mar 15, 2013
View
78 src/FubuCore.Testing/FilesSystem_load_from_file.cs
@@ -0,0 +1,78 @@
+using System;
+using System.IO;
+using System.Xml.Serialization;
+using FubuTestingSupport;
+using NUnit.Framework;
+
+namespace FubuCore.Testing
+{
+ [XmlType("serializeMe")]
+ public class SerializeMe
+ {
+ public static string SerializedXml = @"<?xml version=""1.0""?><serializeMe xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Name>Serialized Name</Name><Index>42</Index></serializeMe>";
+
+ public string Name { get; set; }
+ public int Index { get; set; }
+ }
+
+ [TestFixture]
+ public class FilesSystem_load_from_file
+ {
+ [Test]
+ public void should_deserialize_xml()
+ {
+ var fileSystem = new FileSystem();
+ var fileName = Path.GetTempFileName();
+ fileSystem.WriteStringToFile(fileName, SerializeMe.SerializedXml);
+
+ var result = fileSystem.LoadFromFile<SerializeMe>(fileName);
+
+ result.Name.ShouldEqual("Serialized Name");
+ result.Index.ShouldEqual(42);
+ }
+
+ [Test]
+ public void should_return_empty_instance_when_file_does_not_exist()
+ {
+ var fileSystem = new FileSystem();
+ const string fileName = "does not exist";
+
+ var result = fileSystem.LoadFromFile<SerializeMe>(fileName);
+
+ result.Name.ShouldBeNull();
+ result.Index.ShouldEqual(0);
+ }
+
+ [Test]
+ public void should_thrown_when_file_is_not_xml()
+ {
+ var fileSystem = new FileSystem();
+ var fileName = Path.GetTempFileName();
+ fileSystem.WriteStringToFile(fileName, "not xml!");
+
+ typeof(ApplicationException).ShouldBeThrownBy(() => fileSystem.LoadFromFile<SerializeMe>(fileName));
+ }
+
+ [Test]
+ public void load_from_file_or_throw_shuld_throw_when_file_does_not_exist()
+ {
+ var fileSystem = new FileSystem();
+ const string fileName = "does not exist";
+
+ typeof(ApplicationException).ShouldBeThrownBy(() => fileSystem.LoadFromFileOrThrow<SerializeMe>(fileName));
+ }
+ }
+
+ [TestFixture]
+ public class FilesSystem_load_from_file_or_throw
+ {
+ [Test]
+ public void should_throw_when_file_does_not_exist()
+ {
+ var fileSystem = new FileSystem();
+ const string fileName = "does not exist";
+
+ typeof(ApplicationException).ShouldBeThrownBy(() => fileSystem.LoadFromFileOrThrow<SerializeMe>(fileName));
+ }
+ }
+}
View
1 src/FubuCore.Testing/FubuCore.Testing.csproj
@@ -169,6 +169,7 @@
<Compile Include="FileChangePollingWatcherTester.cs" />
<Compile Include="FileHashingExtensionsTester.cs" />
<Compile Include="FileSetTester.cs" />
+ <Compile Include="FilesSystem_load_from_file.cs" />
<Compile Include="FileSystemTester.cs" />
<Compile Include="FlatFileWriterTester.cs" />
<Compile Include="Formatting\GetStringRequestTester.cs" />
View
15 src/FubuCore/FileSystem.cs
@@ -175,9 +175,22 @@ public void WriteObjectToFile(string filename, object target)
}
}
+ public T LoadFromFileOrThrow<T>(string filename) where T : new()
+ {
+ if (!FileExists(filename))
+ {
+ throw new ApplicationException("Unable to deserialize the contents of file {0}. It does not exist or we do not have read access to it.");
+ }
+
+ return LoadFromFile<T>(filename);
+ }
+
public T LoadFromFile<T>(string filename) where T : new()
{
- if (!FileExists(filename)) return new T();
+ if (!FileExists(filename))
+ {
+ return new T();
+ }
var serializer = new XmlSerializer(typeof (T));
View
3 src/FubuCore/IFileSystem.cs
@@ -24,7 +24,8 @@ public interface IFileSystem
string ReadStringFromFile(string filename);
void WriteObjectToFile(string filename, object target);
- T LoadFromFile<T>(string filename) where T : new();
+ T LoadFromFile<T>(string filename) where T : new();
+ T LoadFromFileOrThrow<T>(string filename) where T : new();
void CreateDirectory(string directory);

0 comments on commit 227b59f

Please sign in to comment.