Permalink
Browse files

Added load from file or throw alternative

  • Loading branch information...
1 parent e8136c5 commit a5f393fd7984893cda07d335fdb8cd550ec93948 @KevM KevM committed Feb 11, 2013
Showing with 28 additions and 2 deletions.
  1. +12 −0 src/FubuCore.Testing/FilesSystem_load_from_file.cs
  2. +14 −1 src/FubuCore/FileSystem.cs
  3. +2 −1 src/FubuCore/IFileSystem.cs
@@ -61,6 +61,18 @@ public void load_from_file_or_throw_shuld_throw_when_file_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));
+ }
}
}
@@ -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));
@@ -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 a5f393f

Please sign in to comment.