Browse Source

POI:Fix some cases where POI itself or the tests leaked file-handles

pull/290/head
antony liu 6 years ago
parent
commit
aab778c187
  1. 45
      ooxml/SS/UserModel/WorkbookFactory.cs
  2. 83
      testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs
  3. 15
      testcases/ooxml/SS/TestWorkbookFactory.cs
  4. 1
      testcases/ooxml/TestPOIXMLDocument.cs

45
ooxml/SS/UserModel/WorkbookFactory.cs

@ -68,9 +68,9 @@ namespace NPOI.SS.UserModel
return new HSSFWorkbook(fs.Root, true);
}
/**
* Creates a Workbook from the given NPOIFSFileSystem, which may
* be password protected
*/
* Creates a Workbook from the given NPOIFSFileSystem, which may
* be password protected
*/
private static IWorkbook Create(NPOIFSFileSystem fs, string password)
{
DirectoryNode root = fs.Root;
@ -95,7 +95,7 @@ namespace NPOI.SS.UserModel
}
if (passwordCorrect)
{
stream = d.GetDataStream(root);
stream = new FilterInputStream1(d.GetDataStream(root), fs);
}
}
catch (Exception e)
@ -130,6 +130,22 @@ namespace NPOI.SS.UserModel
Biff8EncryptionKey.CurrentUserPassword = (null);
}
}
private class FilterInputStream1 : FilterInputStream
{
NPOIFSFileSystem fs;
public FilterInputStream1(InputStream input, NPOIFSFileSystem fs)
: base(input)
{
this.fs = fs;
}
public override void Close()
{
fs.Close();
base.Close();
}
}
/// <summary>
/// Creates an XSSFWorkbook from the given OOXML Package
/// </summary>
@ -182,12 +198,19 @@ namespace NPOI.SS.UserModel
{
return Create(file, password, false);
}
/**
* Creates the appropriate HSSFWorkbook / XSSFWorkbook from
* the given File, which must exist and be readable.
* <p>Note that for Workbooks opened this way, it is not possible
* to explicitly close the underlying File resource.
*/
/// <summary>
/// Creates the appropriate HSSFWorkbook / XSSFWorkbook from
/// the given File, which must exist and be readable.
/// </summary>
/// <param name="file"></param>
/// <param name="password"></param>
/// <param name="readOnly"></param>
/// <returns></returns>
/// <remarks>
/// Note that for Workbooks opened this way, it is not possible
/// to explicitly close the underlying File resource.
/// </remarks>
public static IWorkbook Create(string file, string password, bool readOnly)
{
if (!File.Exists(file))
@ -210,7 +233,7 @@ namespace NPOI.SS.UserModel
}
}
catch (OfficeXmlFileException e)
catch (OfficeXmlFileException)
{
// opening as .xls failed => try opening as .xlsx
OPCPackage pkg = OPCPackage.Open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE);

83
testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs

@ -30,6 +30,7 @@ namespace TestCases.SS.UserModel
using NPOI.HSSF.UserModel;
using System.Drawing;
using System.IO;
using NPOI.Util;
/**
* A base class for bugzilla issues that can be described in terms of common ss interfaces.
@ -92,15 +93,11 @@ namespace TestCases.SS.UserModel
ISheet sheet = wb.CreateSheet();
ICreationHelper factory = wb.GetCreationHelper();
String tmp1 = null;
String tmp2 = null;
String tmp3 = null;
for (int i = 0; i < num; i++)
{
tmp1 = "Test1" + i;
tmp2 = "Test2" + i;
tmp3 = "Test3" + i;
string tmp1 = "Test1" + i;
string tmp2 = "Test2" + i;
string tmp3 = "Test3" + i;
IRow row = sheet.CreateRow(i);
@ -114,9 +111,9 @@ namespace TestCases.SS.UserModel
wb = _testDataProvider.WriteOutAndReadBack(wb);
for (int i = 0; i < num; i++)
{
tmp1 = "Test1" + i;
tmp2 = "Test2" + i;
tmp3 = "Test3" + i;
string tmp1 = "Test1" + i;
string tmp2 = "Test2" + i;
string tmp3 = "Test3" + i;
IRow row = sheet.GetRow(i);
@ -202,15 +199,11 @@ namespace TestCases.SS.UserModel
[Test]
public void Test22568()
{
int r = 2000; int c = 3;
IWorkbook wb = _testDataProvider.CreateWorkbook();
ISheet sheet = wb.CreateSheet("ExcelTest");
int col_cnt = 0, rw_cnt = 0;
col_cnt = c;
rw_cnt = r;
int col_cnt = 3;
int rw_cnt = 2000;
IRow rw;
rw = sheet.CreateRow(0);
@ -464,17 +457,10 @@ namespace TestCases.SS.UserModel
/**
* Test if a > b. Fails if false.
*
* @param message
* @param a
* @param b
*/
private void assertGreaterThan(String message, double a, double b)
{
if (a > b)
{ // expected
}
else
if (a <= b)
{
String msg = "Expected: " + a + " > " + b;
Assert.Fail(message + ": " + msg);
@ -493,11 +479,10 @@ namespace TestCases.SS.UserModel
//AttributedString str = new AttributedString(txt);
//copyAttributes(font, str, 0, txt.length());
if (rt.NumFormattingRuns > 0)
{
// TODO: support rich text fragments
}
// TODO: support rich text fragments
//if (rt.NumFormattingRuns > 0)
//{
//}
//TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
//width = ((layout.getBounds().getWidth() / 1) / 8);
@ -699,7 +684,7 @@ namespace TestCases.SS.UserModel
}
for (int i = 0; i < 12; i += 3)
{
r1.GetCell(i + 0).CellStyle = (/*setter*/iPercent);
r1.GetCell(i).CellStyle = (/*setter*/iPercent);
r1.GetCell(i + 1).CellStyle = (/*setter*/d1Percent);
r1.GetCell(i + 2).CellStyle = (/*setter*/d2Percent);
}
@ -879,16 +864,17 @@ namespace TestCases.SS.UserModel
}
/**
* Should be able to write then read formulas with references
* to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
* or 'http://192.168.1.2/[blank.xls]Sheet1'!$A$1 .
* Additionally, if a reference to that file is provided, it should
* be possible to Evaluate them too
* TODO Fix this to Evaluate for XSSF
* TODO Fix this to work at all for HSSF
*/
// [Test]
public void bug46670()
* Should be able to write then read formulas with references
* to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
* or 'http://192.168.1.2/[blank.xls]Sheet1'!$A$1 .
* Additionally, if a reference to that file is provided, it should
* be possible to Evaluate them too
* TODO Fix this to Evaluate for XSSF
* TODO Fix this to work at all for HSSF
*/
[Test]
[Ignore("Fix this to evaluate for XSSF, Fix this to work at all for HSSF")]
public void Bug46670()
{
IWorkbook wb = _testDataProvider.CreateWorkbook();
ISheet s = wb.CreateSheet();
@ -1189,7 +1175,7 @@ namespace TestCases.SS.UserModel
var tmp = cn.RichStringCellValue;
Assert.Fail();
}
catch (Exception e) { }
catch (IllegalStateException) { }
Assert.AreEqual("Testing", cs.StringCellValue);
try
@ -1197,7 +1183,7 @@ namespace TestCases.SS.UserModel
var tmp = cs.NumericCellValue;
Assert.Fail();
}
catch (Exception e) { }
catch (IllegalStateException) { }
Assert.AreEqual(1.2, cfn.NumericCellValue, 0);
try
@ -1205,7 +1191,7 @@ namespace TestCases.SS.UserModel
var tmp = cfn.RichStringCellValue;
Assert.Fail();
}
catch (Exception e) { }
catch (IllegalStateException) { }
Assert.AreEqual("Testing", cfs.StringCellValue);
try
@ -1213,7 +1199,7 @@ namespace TestCases.SS.UserModel
var tmp = cfs.NumericCellValue;
Assert.Fail();
}
catch (Exception e) { }
catch (IllegalStateException) { }
wb.Close();
}
@ -1305,7 +1291,7 @@ namespace TestCases.SS.UserModel
}
[Test]
public void bug58260()
public void Bug58260()
{
//Create workbook and worksheet
IWorkbook wb = _testDataProvider.CreateWorkbook();
@ -1324,9 +1310,9 @@ namespace TestCases.SS.UserModel
{
style = wb.CreateCellStyle();
}
catch (InvalidOperationException e)
catch (IllegalStateException e)
{
throw new InvalidOperationException("Failed for row " + i, e);
throw new IllegalStateException("Failed for row " + i, e);
}
style.Alignment = HorizontalAlignment.Right;
if ((wb is HSSFWorkbook))
@ -1354,7 +1340,7 @@ namespace TestCases.SS.UserModel
wb.CreateCellStyle();
Assert.Fail("Should Assert.Fail after " + maxStyles + " styles, but did not Assert.Fail");
}
catch (InvalidOperationException e)
catch (IllegalStateException)
{
// expected here
}
@ -1446,6 +1432,7 @@ namespace TestCases.SS.UserModel
Assert.AreEqual(164, cell.CellStyle.DataFormat);
DataFormatter formatter = new DataFormatter();
Assert.AreEqual("12-312-345-123", formatter.FormatCellValue(cell));
wb.Close();
}

15
testcases/ooxml/SS/TestWorkbookFactory.cs

@ -186,7 +186,7 @@ namespace NPOI.SS
}
Assert.Fail();
}
catch (InvalidFormatException e)
catch (InvalidFormatException)
{
// Good
}
@ -317,6 +317,9 @@ namespace NPOI.SS
);
Assert.IsNotNull(wb);
Assert.IsTrue(wb is XSSFWorkbook);
Assert.IsTrue(wb.NumberOfSheets > 0);
Assert.IsNotNull(wb.GetSheetAt(0));
Assert.IsNotNull(wb.GetSheetAt(0).GetRow(0));
AssertCloseDoesNotModifyFile(xlsx, wb);
// Protected, wrong password, throws Exception
@ -328,7 +331,10 @@ namespace NPOI.SS
AssertCloseDoesNotModifyFile(xls_prot[0], wb);
Assert.Fail("Shouldn't be able to open with the wrong password");
}
catch (EncryptedDocumentException e) { }
catch (EncryptedDocumentException)
{
// expected here
}
try
{
wb = WorkbookFactory.Create(
@ -337,7 +343,10 @@ namespace NPOI.SS
AssertCloseDoesNotModifyFile(xlsx_prot[0], wb);
Assert.Fail("Shouldn't be able to open with the wrong password");
}
catch (EncryptedDocumentException e) { }
catch (EncryptedDocumentException)
{
// expected here
}
}
[Test]

1
testcases/ooxml/TestPOIXMLDocument.cs

@ -145,6 +145,7 @@ namespace NPOI.OOXML
finally
{
doc.Close();
pkg2.Close();
}
}

Loading…
Cancel
Save