Skip to content

Commit

Permalink
Fix #141 by explicitly close stream when Exception occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
jecc1982 committed Mar 1, 2013
1 parent 372551e commit 4ecd3e1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ExcelLibrary.Test/ExcelLibrary.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="CodeLib\FastSearchListTest.cs" />
<Compile Include="DataSetHelperTest.cs" />
<Compile Include="Issue\Issue10_Test.cs" />
<Compile Include="Issue\Issue141_Test.cs" />
<Compile Include="SimpleTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
41 changes: 41 additions & 0 deletions src/ExcelLibrary.Test/Issue/Issue141_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ExcelLibrary.CompoundDocumentFormat;
using NUnit.Framework;

namespace ExcelLibrary.Test.Issue
{
[TestFixture]
class Issue141_Test
{
[Test]
public void Test()
{
string filename = Path.GetTempFileName();

try
{
// create invalid file for read
using (var writer = new StreamWriter(filename))
writer.WriteLine("test");

try
{
// triggers exception
CompoundDocument.Open(filename);
}
catch
{
Assert.IsTrue(true, "Expection trigger.");
}
}
finally
{
// should not trigger any IO Exception
File.Delete(filename);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ public static CompoundDocument Create(string file)
public static CompoundDocument Open(string file)
{
FileStream stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
return Open(stream);

try
{
return Open(stream);
}
catch (Exception ex)
{
stream.Close();
throw ex;
}
}

public static CompoundDocument Create(Stream stream)
Expand Down

0 comments on commit 4ecd3e1

Please sign in to comment.