From 95356724bfc6424657c91a8058dd1f28f9a0e1bf Mon Sep 17 00:00:00 2001 From: Andrey Bykiev Date: Thu, 27 Jul 2023 11:36:16 +0300 Subject: [PATCH 01/24] Fix TempFile usage while temp dir was deleted in same process This PR closes #926 --- main/Util/TempFile.cs | 19 +++++++---- testcases/main/Util/TestTempFile.cs | 53 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 testcases/main/Util/TestTempFile.cs diff --git a/main/Util/TempFile.cs b/main/Util/TempFile.cs index 325a6ed91..e8d347e9d 100644 --- a/main/Util/TempFile.cs +++ b/main/Util/TempFile.cs @@ -18,11 +18,14 @@ public class TempFile */ public static FileInfo CreateTempFile(String prefix, String suffix) { + string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); if (dir == null) - { - dir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "poifiles")).FullName; - } + dir = Directory.CreateDirectory(tempDir).FullName; + + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + // Generate a unique new filename string file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix); while (File.Exists(file)) @@ -38,10 +41,14 @@ public static FileInfo CreateTempFile(String prefix, String suffix) public static string GetTempFilePath(String prefix, String suffix) { + string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); + if (dir == null) - { - dir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "poifiles")).FullName; - } + dir = Directory.CreateDirectory(tempDir).FullName; + + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + Random rnd = new Random(DateTime.Now.Millisecond); Thread.Sleep(10); //return prefix + rnd.Next() + suffix; diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs new file mode 100644 index 000000000..5c3dca3eb --- /dev/null +++ b/testcases/main/Util/TestTempFile.cs @@ -0,0 +1,53 @@ +using NPOI.Util; +using NUnit.Framework; +using System.IO; +using System.Reflection; + +namespace TestCases.Util +{ + /// + /// Tests of creating temp files + /// + [TestFixture] + internal class TestTempFile + { + [Test] + public void TestCreateTempFile() + { + FileInfo fileInfo = null; + Assert.DoesNotThrow(() => fileInfo = TempFile.CreateTempFile("test", ".xls")); + + Assert.IsTrue(File.Exists(fileInfo.FullName)); + + string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); + + if (Directory.Exists(tempDirPath)) + Directory.Delete(tempDirPath, true); + + Assert.IsFalse(File.Exists(fileInfo.FullName)); + Assert.IsFalse(Directory.Exists(tempDirPath)); + + Assert.DoesNotThrow(() => TempFile.CreateTempFile("test2", ".xls")); + Assert.IsTrue(Directory.Exists(tempDirPath)); + } + + [Test] + public void TestGetTempFilePath() + { + string path = ""; + Assert.DoesNotThrow(() => path = TempFile.GetTempFilePath("test", ".xls")); + + Assert.IsTrue(!string.IsNullOrWhiteSpace(path)); + + string tempDirPath = Path.GetDirectoryName(path); + + if (Directory.Exists(tempDirPath)) + Directory.Delete(tempDirPath, true); + + Assert.IsFalse(Directory.Exists(tempDirPath)); + + Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls")); + Assert.IsTrue(Directory.Exists(tempDirPath)); + } + } +} From 8f86d2e009859630fa59e6a3fd3b3b9bad39d6e6 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev Date: Thu, 27 Jul 2023 11:38:39 +0300 Subject: [PATCH 02/24] Update TempFile.cs --- main/Util/TempFile.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main/Util/TempFile.cs b/main/Util/TempFile.cs index e8d347e9d..a2173fe50 100644 --- a/main/Util/TempFile.cs +++ b/main/Util/TempFile.cs @@ -18,10 +18,11 @@ public class TempFile */ public static FileInfo CreateTempFile(String prefix, String suffix) { - string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); - - if (dir == null) + if (string.IsNullOrWhiteSpace(dir)) + { + string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); dir = Directory.CreateDirectory(tempDir).FullName; + } if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); @@ -41,10 +42,11 @@ public static FileInfo CreateTempFile(String prefix, String suffix) public static string GetTempFilePath(String prefix, String suffix) { - string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); - - if (dir == null) + if (string.IsNullOrWhiteSpace(dir)) + { + string tempDir = Path.Combine(Path.GetTempPath(), "poifiles"); dir = Directory.CreateDirectory(tempDir).FullName; + } if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); From 77fd8d80095cd7cbd82710890c7a0623544ca194 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev Date: Thu, 27 Jul 2023 11:54:58 +0300 Subject: [PATCH 03/24] Fix CI test execution --- testcases/main/Util/TestTempFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index 5c3dca3eb..b36ccc8dd 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -1,7 +1,6 @@ using NPOI.Util; using NUnit.Framework; using System.IO; -using System.Reflection; namespace TestCases.Util { @@ -9,6 +8,7 @@ namespace TestCases.Util /// Tests of creating temp files /// [TestFixture] + [SingleThreaded] internal class TestTempFile { [Test] From 1c8a56adc225dd3e76564f3f0851c938358f52a8 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev Date: Thu, 27 Jul 2023 12:27:44 +0300 Subject: [PATCH 04/24] Trying to fix tests2 --- testcases/main/Util/TestTempFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index b36ccc8dd..df50a5004 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -8,7 +8,7 @@ namespace TestCases.Util /// Tests of creating temp files /// [TestFixture] - [SingleThreaded] + [NonParallelizable] internal class TestTempFile { [Test] From b293c63883be3bc8a2393f70c5dde3182aafe9e1 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev Date: Thu, 27 Jul 2023 13:17:35 +0300 Subject: [PATCH 05/24] trying to fix test 3 --- testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs | 1 + testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs | 1 + testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs | 1 + 3 files changed, 3 insertions(+) diff --git a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs index ccda284ae..a7ac3378e 100644 --- a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs @@ -22,6 +22,7 @@ limitations under the License. namespace TestCases.XSSF.Streaming { [TestFixture] + [NonParallelizable] public class GZIPSheetDataWriterTests { private GZIPSheetDataWriter _objectToTest; diff --git a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs index ca504036a..dcfb936e3 100644 --- a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs @@ -24,6 +24,7 @@ limitations under the License. namespace TestCases.XSSF.Streaming { [TestFixture] + [NonParallelizable] public class SheetDataWriterTests { private SheetDataWriter _objectToTest; diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index d0c37a37b..54f37a98c 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -36,6 +36,7 @@ namespace TestCases.XSSF.Streaming using TestCases.Util; [TestFixture] + [NonParallelizable] public class TestSXSSFWorkbook : BaseTestXWorkbook { From 6c8fc862b648413d760e59d275b3bca9defba8f6 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Fri, 1 Sep 2023 21:08:10 +0300 Subject: [PATCH 06/24] Update TempFile.cs --- main/Util/TempFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/Util/TempFile.cs b/main/Util/TempFile.cs index a2173fe50..dcbb88164 100644 --- a/main/Util/TempFile.cs +++ b/main/Util/TempFile.cs @@ -34,7 +34,7 @@ public static FileInfo CreateTempFile(String prefix, String suffix) file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix); Thread.Sleep(1); } - FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite); + FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite); newFile.Close(); return new FileInfo(file); From 1daa483faaa0757fd4cd9f417a7bd7da943e3ed8 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Fri, 1 Sep 2023 21:37:30 +0300 Subject: [PATCH 07/24] Update TempFile.cs --- main/Util/TempFile.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/Util/TempFile.cs b/main/Util/TempFile.cs index dcbb88164..a32abcc10 100644 --- a/main/Util/TempFile.cs +++ b/main/Util/TempFile.cs @@ -34,8 +34,8 @@ public static FileInfo CreateTempFile(String prefix, String suffix) file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix); Thread.Sleep(1); } - FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite); - newFile.Close(); + + using (FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { }; return new FileInfo(file); } From 928fa26ce2f0063c432b81141c44c67b19bf8a43 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Fri, 1 Sep 2023 22:11:31 +0300 Subject: [PATCH 08/24] Another one try --- testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs | 5 ++--- testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs index a7ac3378e..b25e84ddf 100644 --- a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs @@ -32,11 +32,10 @@ public void CleanUp() { if (_objectToTest != null) { + _objectToTest.Dispose(); + if (File.Exists(_objectToTest.TemporaryFilePath())) - { - _objectToTest.Dispose(); File.Delete(_objectToTest.TemporaryFilePath()); - } } } [Test] diff --git a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs index dcfb936e3..182fbcaa9 100644 --- a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs @@ -46,11 +46,10 @@ public void CleanUp() { if (_objectToTest != null) { + _objectToTest.Dispose(); + if (File.Exists(_objectToTest.TemporaryFilePath())) - { - _objectToTest.Dispose(); File.Delete(_objectToTest.TemporaryFilePath()); - } } } From e7faafb6fca63e11abf2a37f0c66f9c672d2e4e3 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 19:09:32 +0300 Subject: [PATCH 09/24] Remove RunSerialyAndSweepTmpFilesAttribute --- solution/NPOI.Core.Test.sln | 6 ++- .../RunSerialyAndSweepTmpFilesAttribute.cs | 53 ------------------- testcases/main/Util/TestTempFile.cs | 1 - .../Streaming/GZIPSheetDataWriterTests.cs | 1 - .../XSSF/Streaming/SheetDataWriterTests.cs | 1 - .../ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs | 1 - .../ooxml/XSSF/UserModel/TestXSSFBugs.cs | 2 +- .../ooxml/XSSF/UserModel/TestXSSFWorkbook.cs | 5 +- .../TestOPCComplianceCoreProperties.cs | 5 +- testcases/openxml4net/TestPackage.cs | 17 +++--- 10 files changed, 18 insertions(+), 74 deletions(-) delete mode 100644 testcases/main/RunSerialyAndSweepTmpFilesAttribute.cs diff --git a/solution/NPOI.Core.Test.sln b/solution/NPOI.Core.Test.sln index f7659e966..9328d5a3b 100644 --- a/solution/NPOI.Core.Test.sln +++ b/solution/NPOI.Core.Test.sln @@ -26,7 +26,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "..\build\_build.c EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NPOI.Benchmarks", "..\benchmarks\NPOI.Benchmarks\NPOI.Benchmarks.csproj", "{3DA1149D-46F8-4181-9976-E002BF2BFB76}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NPOI.Pack", "NPOI.Pack.csproj", "{6D7A6E15-C914-4FCA-B8E4-FF5C7437C2E0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NPOI.Pack", "NPOI.Pack.csproj", "{6D7A6E15-C914-4FCA-B8E4-FF5C7437C2E0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -62,6 +62,10 @@ Global {DA2CA3BD-1CAC-470C-9FA2-611A5768A76A}.Debug|Any CPU.Build.0 = Debug|Any CPU {DA2CA3BD-1CAC-470C-9FA2-611A5768A76A}.Release|Any CPU.ActiveCfg = Release|Any CPU {DA2CA3BD-1CAC-470C-9FA2-611A5768A76A}.Release|Any CPU.Build.0 = Release|Any CPU + {94B18BCF-84E8-401F-BAAB-0496AA136628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94B18BCF-84E8-401F-BAAB-0496AA136628}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94B18BCF-84E8-401F-BAAB-0496AA136628}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94B18BCF-84E8-401F-BAAB-0496AA136628}.Release|Any CPU.Build.0 = Release|Any CPU {3DA1149D-46F8-4181-9976-E002BF2BFB76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3DA1149D-46F8-4181-9976-E002BF2BFB76}.Debug|Any CPU.Build.0 = Debug|Any CPU {3DA1149D-46F8-4181-9976-E002BF2BFB76}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/testcases/main/RunSerialyAndSweepTmpFilesAttribute.cs b/testcases/main/RunSerialyAndSweepTmpFilesAttribute.cs deleted file mode 100644 index 854031fd3..000000000 --- a/testcases/main/RunSerialyAndSweepTmpFilesAttribute.cs +++ /dev/null @@ -1,53 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - -using System; -using System.IO; -using System.Threading; -using NUnit.Framework; -using NUnit.Framework.Interfaces; - -namespace TestCases -{ - [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] - public sealed class RunSerialyAndSweepTmpFilesAttribute : Attribute, ITestAction - { - private static object syncSequential = new object(); - - public ActionTargets Targets { get { return ActionTargets.Test; } } - - private static void SweepTemporaryFiles() - { - foreach (var tempFilePath in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp")) - { - File.Delete(tempFilePath); - } - } - - public void BeforeTest(ITest test) - { - Monitor.Enter(syncSequential); - SweepTemporaryFiles(); - } - - public void AfterTest(ITest test) - { - SweepTemporaryFiles(); - Monitor.Exit(syncSequential); - } - } -} diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index df50a5004..4475cc3d8 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -8,7 +8,6 @@ namespace TestCases.Util /// Tests of creating temp files /// [TestFixture] - [NonParallelizable] internal class TestTempFile { [Test] diff --git a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs index b25e84ddf..77ffa004d 100644 --- a/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs @@ -22,7 +22,6 @@ limitations under the License. namespace TestCases.XSSF.Streaming { [TestFixture] - [NonParallelizable] public class GZIPSheetDataWriterTests { private GZIPSheetDataWriter _objectToTest; diff --git a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs index 182fbcaa9..b2778ca25 100644 --- a/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs @@ -24,7 +24,6 @@ limitations under the License. namespace TestCases.XSSF.Streaming { [TestFixture] - [NonParallelizable] public class SheetDataWriterTests { private SheetDataWriter _objectToTest; diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index 54f37a98c..d0c37a37b 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -36,7 +36,6 @@ namespace TestCases.XSSF.Streaming using TestCases.Util; [TestFixture] - [NonParallelizable] public class TestSXSSFWorkbook : BaseTestXWorkbook { diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs index 16d6d577d..dd8b956a6 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs @@ -2707,7 +2707,7 @@ public void Bug57642() * 32,767 must not be -32,768, then -32,767, -32,766 * long time test, run over 1 minute. */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] [Ignore("this test doesn't make sense")] public void Bug57880() { diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs index abede86b4..f6c6543b5 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs @@ -51,7 +51,7 @@ public TestXSSFWorkbook() /** * Tests that we can save, and then re-load a new document */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] public void SaveLoadNew() { XSSFWorkbook wb1 = new XSSFWorkbook(); @@ -121,9 +121,8 @@ public void SaveLoadNew() Assert.AreEqual("hello world", sheet1.GetRow(1).GetCell(0).RichStringCellValue.String); pkg.Close(); - - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); } + [Test] public void Existing() { diff --git a/testcases/openxml4net/OPC/Compliance/TestOPCComplianceCoreProperties.cs b/testcases/openxml4net/OPC/Compliance/TestOPCComplianceCoreProperties.cs index 6f6d68326..9d91b479e 100644 --- a/testcases/openxml4net/OPC/Compliance/TestOPCComplianceCoreProperties.cs +++ b/testcases/openxml4net/OPC/Compliance/TestOPCComplianceCoreProperties.cs @@ -287,7 +287,7 @@ public void TestNoCoreProperties_saveNew() * Document with no core properties - testing at the OPC level, * from a temp-file, saving in-place */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] public void TestNoCoreProperties_saveInPlace() { String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx"; @@ -328,8 +328,7 @@ public void TestNoCoreProperties_saveInPlace() pkg.Revert(); tmp.Delete(); - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); + Assert.IsFalse(File.Exists(tmp.FullName), $"{tmp.FullName} file exists!"); } - } } \ No newline at end of file diff --git a/testcases/openxml4net/TestPackage.cs b/testcases/openxml4net/TestPackage.cs index a921e6779..5bd3d73c1 100644 --- a/testcases/openxml4net/TestPackage.cs +++ b/testcases/openxml4net/TestPackage.cs @@ -166,7 +166,7 @@ public void TestCreatePackageAddPart() * document and another part, save and re-load and * have everything Setup as expected */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] //[Ignore("add relation Uri #Sheet1!A1")] public void TestCreatePackageWithCoreDocument() { @@ -256,8 +256,6 @@ public void TestCreatePackageWithCoreDocument() { pkg.Close(); } - - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); } private void assertMSCompatibility(OPCPackage pkg) @@ -282,7 +280,7 @@ private void assertMSCompatibility(OPCPackage pkg) /** * Test namespace opening. */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] public void TestOpenPackage() { FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestOpenPackageTMP.docx"); @@ -344,7 +342,7 @@ public void TestOpenPackage() ZipFileAssert.AssertEqual(expectedFile, targetFile); File.Delete(targetFile.FullName); - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); + Assert.IsFalse(File.Exists(targetFile.FullName), $"{targetFile.FullName} file exists!"); } /** @@ -531,7 +529,7 @@ public void TestDeletePartRecursive() * Test that we can open a file by path, and then * write Changes to it. */ - [Test, RunSerialyAndSweepTmpFiles, Platform("Win")] + [Test, Platform("Win")] public void TestOpenFileThenOverWrite() { string tempFile = TempFile.GetTempFilePath("poiTesting", "tmp"); @@ -568,13 +566,13 @@ public void TestOpenFileThenOverWrite() p.Close(); File.Delete(tempFile); - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); + Assert.IsFalse(File.Exists(tempFile), $"{tempFile} file exists!"); } /** * Test that we can open a file by path, save it * to another file, then delete both */ - [Test, RunSerialyAndSweepTmpFiles] + [Test] public void TestOpenFileThenSaveDelete() { string tempFile = TempFile.GetTempFilePath("poiTesting", "tmp"); @@ -593,7 +591,8 @@ public void TestOpenFileThenSaveDelete() File.Delete(tempFile); File.Delete(tempFile2); - Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); + Assert.IsFalse(File.Exists(tempFile), $"{tempFile} file exists!"); + Assert.IsFalse(File.Exists(tempFile2), $"{tempFile2} file exists!"); } private static ContentTypeManager GetContentTypeManager(OPCPackage pkg) From a35231b9e13a9e5980315ab0fe5fefcf062fb44c Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 19:37:18 +0300 Subject: [PATCH 10/24] Update SXSSFWorkbookTests.cs --- testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs index b882c058b..d682f4762 100644 --- a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs @@ -86,6 +86,8 @@ public void IfCompressTmpFilesIsSetToFalseShouldReturnSheetDataWriter() Assert.IsTrue(result is SheetDataWriter); + if (result != null) + result.Close(); } [Test] From 99c5605634a39d3f097c69389a96d34f570694ea Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 19:47:51 +0300 Subject: [PATCH 11/24] Update SXSSFWorkbookTests.cs --- .../ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs index d682f4762..31f69c20b 100644 --- a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs @@ -31,6 +31,13 @@ class SXSSFWorkbookTests { private SXSSFWorkbook _objectToTest { get; set; } + [TearDown] + public void CleanUp() + { + if (_objectToTest != null) + _objectToTest.Dispose(); + } + [Test] public void CallingEmptyConstructorShouldInstanstiateNewXssfWorkbookDefaultRowAccessWindowSizeCompressTempFilesAsFalseAndUseSharedStringsTableFalse @@ -76,6 +83,8 @@ public void IfCompressTmpFilesIsSetToTrueShouldReturnGZIPSheetDataWriter() Assert.IsTrue(result is GZIPSheetDataWriter); + if (result != null) + result.Close(); } [Test] @@ -113,7 +122,6 @@ public void IfSettingSelectedTabShouldSetSelectedTabOfXssfWorkbook() _objectToTest.SetSelectedTab(0); Assert.IsTrue(_objectToTest.GetSheetAt(0).IsSelected); - } [Test] @@ -137,7 +145,6 @@ public void IfSettingSheetNameShouldChangeTheSheetNameAtTheSpecifiedIndex() _objectToTest.SetSheetName(0, "renamed"); Assert.AreEqual("renamed", _objectToTest.GetSheetAt(0).SheetName); - } [Test] @@ -201,10 +208,8 @@ public void IfGivenTheNameOfAnExistingSheetShouldReturnTheSheet() Assert.AreEqual("1", sheet1.SheetName); Assert.AreEqual("2", sheet2.SheetName); - } - [Test] public void IfGivenTheIndexOfAnExistingSheetShouldReturnTheSheet() { @@ -217,7 +222,6 @@ public void IfGivenTheIndexOfAnExistingSheetShouldReturnTheSheet() Assert.AreEqual("1", sheet1.SheetName); Assert.AreEqual("2", sheet2.SheetName); - } [Test] From 23060c17c89baf48491baa1af70a2e90bce81162 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 20:08:45 +0300 Subject: [PATCH 12/24] Call dispose on SheeDataWriter.Close() --- ooxml/XSSF/Streaming/SheetDataWriter.cs | 14 ++------------ .../ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs | 10 ++-------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/ooxml/XSSF/Streaming/SheetDataWriter.cs b/ooxml/XSSF/Streaming/SheetDataWriter.cs index c8098be2f..1f74e08f5 100644 --- a/ooxml/XSSF/Streaming/SheetDataWriter.cs +++ b/ooxml/XSSF/Streaming/SheetDataWriter.cs @@ -116,23 +116,13 @@ public void Close() { try { - _outputWriter.Flush(); - OutputStream.Flush(); + _outputWriter.Dispose(); + OutputStream.Dispose(); } catch { } - try - { - OutputStream.Close(); - } - catch - { - - } - - } public FileInfo TempFileInfo diff --git a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs index 31f69c20b..07395bf0f 100644 --- a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs @@ -14,15 +14,12 @@ the License. You may obtain a copy of the License at See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; using NPOI.SS.UserModel; using NPOI.XSSF.Streaming; using NPOI.XSSF.UserModel; using NUnit.Framework; +using System; +using System.IO; namespace TestCases.XSSF.Streaming { @@ -134,7 +131,6 @@ public void IfSheetNameByIndexShouldGetSheetNameFromXssfWorkbook() _objectToTest.SetSelectedTab(0); Assert.IsTrue(_objectToTest.GetSheetAt(0).IsSelected); - } [Test] @@ -457,7 +453,6 @@ public void IfWriting20WorksheetsWith10000x100CellsUsingGzipShouldNotThrowOutOfM File.Delete(savePath); } - private void AddCells(IWorkbook wb, int sheets, int rows, int columns, CellType type) { for (int j = 0; j < sheets; j++) @@ -474,7 +469,6 @@ private void AddCells(IWorkbook wb, int sheets, int rows, int columns, CellType } } - private void WriteFile(string saveAsPath, SXSSFWorkbook wb) { //Passing SXSSFWorkbook because IWorkbook does not implement .Dispose which cleans ups temporary files. From 366130ad0b8fc1a63bc56a30e594dddc9717a52b Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 20:21:12 +0300 Subject: [PATCH 13/24] Comment all SXSSFWorkbookTests --- testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs index 07395bf0f..4f70758a3 100644 --- a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs @@ -23,7 +23,7 @@ limitations under the License. namespace TestCases.XSSF.Streaming { - [TestFixture] + /*[TestFixture] class SXSSFWorkbookTests { private SXSSFWorkbook _objectToTest { get; set; } @@ -534,5 +534,5 @@ public void StreamShouldBeLeavedOpen() } } } - } + }*/ } From 114434f50b46be4d1429e2fca2050eed7a195c73 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 20:37:31 +0300 Subject: [PATCH 14/24] test --- ooxml/XSSF/Streaming/SheetDataWriter.cs | 1 + testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ooxml/XSSF/Streaming/SheetDataWriter.cs b/ooxml/XSSF/Streaming/SheetDataWriter.cs index 1f74e08f5..270ac1762 100644 --- a/ooxml/XSSF/Streaming/SheetDataWriter.cs +++ b/ooxml/XSSF/Streaming/SheetDataWriter.cs @@ -544,6 +544,7 @@ public bool Dispose() bool ret; try { + _outputWriter.Close(); OutputStream.Close(); } finally diff --git a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs index 4f70758a3..07395bf0f 100644 --- a/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs +++ b/testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs @@ -23,7 +23,7 @@ limitations under the License. namespace TestCases.XSSF.Streaming { - /*[TestFixture] + [TestFixture] class SXSSFWorkbookTests { private SXSSFWorkbook _objectToTest { get; set; } @@ -534,5 +534,5 @@ public void StreamShouldBeLeavedOpen() } } } - }*/ + } } From ec9bb51eaab9f8d46bbaf52c05050c3540b27af1 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 21:07:36 +0300 Subject: [PATCH 15/24] Update TempFile.cs --- main/Util/TempFile.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/main/Util/TempFile.cs b/main/Util/TempFile.cs index a32abcc10..1551deaa5 100644 --- a/main/Util/TempFile.cs +++ b/main/Util/TempFile.cs @@ -32,7 +32,6 @@ public static FileInfo CreateTempFile(String prefix, String suffix) while (File.Exists(file)) { file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix); - Thread.Sleep(1); } using (FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { }; From 874dee8fe3d9e7a5a5bfbf3817c9d4856eccb9a8 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 21:19:38 +0300 Subject: [PATCH 16/24] Comment TestSXSSFWorkbook --- testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index d0c37a37b..954368b9c 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -36,7 +36,7 @@ namespace TestCases.XSSF.Streaming using TestCases.Util; [TestFixture] - public class TestSXSSFWorkbook : BaseTestXWorkbook + /*public class TestSXSSFWorkbook : BaseTestXWorkbook { public TestSXSSFWorkbook() @@ -614,6 +614,6 @@ public void CreateFromReadOnlyWorkbook() Assert.AreEqual("Test Row 9", s.GetRow(9).GetCell(2).StringCellValue); } - } + }*/ } \ No newline at end of file From e5d0d2069c4ab28e306c51ac78f44661d1d7b053 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Thu, 14 Sep 2023 21:27:09 +0300 Subject: [PATCH 17/24] Update TestSXSSFWorkbook.cs --- .../ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs | 579 ------------------ 1 file changed, 579 deletions(-) diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index 954368b9c..49cd8437c 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -35,585 +35,6 @@ namespace TestCases.XSSF.Streaming using TestCases.SS.UserModel; using TestCases.Util; - [TestFixture] - /*public class TestSXSSFWorkbook : BaseTestXWorkbook - { - public TestSXSSFWorkbook() - : base(SXSSFITestDataProvider.instance) - { - - } - - [TearDown] - public void TearDown() - { - //((SXSSFITestDataProvider)_testDataProvider).Cleanup(); - } - - /** - * cloning of sheets is not supported in SXSSF - */ - - [Test] - public override void CloneSheet() - { - try - { - base.CloneSheet(); - Assert.Fail("expected exception"); - } - catch (RuntimeException e) - { - Assert.AreEqual("NotImplemented", e.Message); - } - } - [Test] - public override void SheetClone() - { - try - { - base.SheetClone(); - Assert.Fail("expected exception"); - } - catch (RuntimeException e) - { - Assert.AreEqual("NotImplemented", e.Message); - } - } - /** - * Skip this test, as SXSSF doesn't update formulas on sheet name - * Changes. - */ - - [Test] - public override void TestSetSheetName() - { - Assume.That(false, "SXSSF doesn't update formulas on sheet name Changes, as most cells probably aren't in memory at the time"); - } - - [Test] - public void ExistingWorkbook() - { - XSSFWorkbook xssfWb1 = new XSSFWorkbook(); - xssfWb1.CreateSheet("S1"); - SXSSFWorkbook wb1 = new SXSSFWorkbook(xssfWb1); - XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb1) as XSSFWorkbook; - Assert.IsTrue(wb1.Dispose()); - - SXSSFWorkbook wb2 = new SXSSFWorkbook(xssfWb2); - Assert.AreEqual(1, wb2.NumberOfSheets); - ISheet sheet = wb2.GetSheetAt(0); - Assert.IsNotNull(sheet); - Assert.AreEqual("S1", sheet.SheetName); - Assert.IsTrue(wb2.Dispose()); - xssfWb2.Close(); - xssfWb1.Close(); - - wb2.Close(); - wb1.Close(); - } - - [Test] - public void UseSharedStringsTable() - { - SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true); - - SharedStringsTable sss = POITestCase.GetFieldValue(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource"); - - Assert.IsNotNull(sss); - - IRow row = wb.CreateSheet("S1").CreateRow(0); - - row.CreateCell(0).SetCellValue("A"); - row.CreateCell(1).SetCellValue("B"); - row.CreateCell(2).SetCellValue("A"); - - XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; - sss = POITestCase.GetFieldValue(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource"); - Assert.AreEqual(2, sss.UniqueCount); - Assert.IsTrue(wb.Dispose()); - - ISheet sheet1 = xssfWorkbook.GetSheetAt(0); - Assert.AreEqual("S1", sheet1.SheetName); - Assert.AreEqual(1, sheet1.PhysicalNumberOfRows); - row = sheet1.GetRow(0); - Assert.IsNotNull(row); - ICell cell = row.GetCell(0); - Assert.IsNotNull(cell); - Assert.AreEqual("A", cell.StringCellValue); - cell = row.GetCell(1); - Assert.IsNotNull(cell); - Assert.AreEqual("B", cell.StringCellValue); - cell = row.GetCell(2); - Assert.IsNotNull(cell); - Assert.AreEqual("A", cell.StringCellValue); - - xssfWorkbook.Close(); - wb.Close(); - } - - [Test] - public void AddToExistingWorkbook() - { - XSSFWorkbook xssfWb1 = new XSSFWorkbook(); - xssfWb1.CreateSheet("S1"); - ISheet sheet = xssfWb1.CreateSheet("S2"); - IRow row = sheet.CreateRow(1); - ICell cell = row.CreateCell(1); - cell.SetCellValue("value 2_1_1"); - SXSSFWorkbook wb1 = new SXSSFWorkbook(xssfWb1); - XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb1) as XSSFWorkbook; - Assert.IsTrue(wb1.Dispose()); - xssfWb1.Close(); - - SXSSFWorkbook wb2 = new SXSSFWorkbook(xssfWb2); - // Add a row to the existing empty sheet - ISheet sheet1 = wb2.GetSheetAt(0); - IRow row1_1 = sheet1.CreateRow(1); - ICell cell1_1_1 = row1_1.CreateCell(1); - cell1_1_1.SetCellValue("value 1_1_1"); - - // Add a row to the existing non-empty sheet - ISheet sheet2 = wb2.GetSheetAt(1); - IRow row2_2 = sheet2.CreateRow(2); - ICell cell2_2_1 = row2_2.CreateCell(1); - cell2_2_1.SetCellValue("value 2_2_1"); - - // Add a sheet with one row - ISheet sheet3 = wb2.CreateSheet("S3"); - IRow row3_1 = sheet3.CreateRow(1); - ICell cell3_1_1 = row3_1.CreateCell(1); - cell3_1_1.SetCellValue("value 3_1_1"); - - XSSFWorkbook xssfWb3 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb2) as XSSFWorkbook; - wb2.Close(); - - Assert.AreEqual(3, xssfWb3.NumberOfSheets); - // Verify sheet 1 - sheet1 = xssfWb3.GetSheetAt(0); - Assert.AreEqual("S1", sheet1.SheetName); - Assert.AreEqual(1, sheet1.PhysicalNumberOfRows); - row1_1 = sheet1.GetRow(1); - Assert.IsNotNull(row1_1); - cell1_1_1 = row1_1.GetCell(1); - Assert.IsNotNull(cell1_1_1); - Assert.AreEqual("value 1_1_1", cell1_1_1.StringCellValue); - // Verify sheet 2 - sheet2 = xssfWb3.GetSheetAt(1); - Assert.AreEqual("S2", sheet2.SheetName); - Assert.AreEqual(2, sheet2.PhysicalNumberOfRows); - IRow row2_1 = sheet2.GetRow(1); - Assert.IsNotNull(row2_1); - ICell cell2_1_1 = row2_1.GetCell(1); - Assert.IsNotNull(cell2_1_1); - Assert.AreEqual("value 2_1_1", cell2_1_1.StringCellValue); - row2_2 = sheet2.GetRow(2); - Assert.IsNotNull(row2_2); - cell2_2_1 = row2_2.GetCell(1); - Assert.IsNotNull(cell2_2_1); - Assert.AreEqual("value 2_2_1", cell2_2_1.StringCellValue); - // Verify sheet 3 - sheet3 = xssfWb3.GetSheetAt(2); - Assert.AreEqual("S3", sheet3.SheetName); - Assert.AreEqual(1, sheet3.PhysicalNumberOfRows); - row3_1 = sheet3.GetRow(1); - Assert.IsNotNull(row3_1); - cell3_1_1 = row3_1.GetCell(1); - Assert.IsNotNull(cell3_1_1); - Assert.AreEqual("value 3_1_1", cell3_1_1.StringCellValue); - - xssfWb2.Close(); - xssfWb3.Close(); - wb1.Close(); - } - - [Test] - public void SheetdataWriter() - { - SXSSFWorkbook wb = new SXSSFWorkbook(); - SXSSFSheet sh = wb.CreateSheet() as SXSSFSheet; - SheetDataWriter wr = sh.SheetDataWriter; - Assert.IsTrue(wr.GetType() == typeof(SheetDataWriter)); - FileInfo tmp = wr.TempFileInfo; - Assert.IsTrue(tmp.Name.StartsWith("poi-sxssf-sheet")); - Assert.IsTrue(tmp.Name.EndsWith(".xml")); - Assert.IsTrue(wb.Dispose()); - wb.Close(); - - wb = new SXSSFWorkbook(); - wb.CompressTempFiles = (/*setter*/true); - sh = wb.CreateSheet() as SXSSFSheet; - wr = sh.SheetDataWriter; - Assert.IsTrue(wr.GetType() == typeof(GZIPSheetDataWriter)); - tmp = wr.TempFileInfo; - Assert.IsTrue(tmp.Name.StartsWith("poi-sxssf-sheet-xml")); - Assert.IsTrue(tmp.Name.EndsWith(".gz")); - Assert.IsTrue(wb.Dispose()); - wb.Close(); - - //Test escaping of Unicode control characters - wb = new SXSSFWorkbook(); - wb.CreateSheet("S1").CreateRow(0).CreateCell(0).SetCellValue("value\u0019"); - XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; - ICell cell = xssfWorkbook.GetSheet("S1").GetRow(0).GetCell(0); - Assert.AreEqual("value?", cell.StringCellValue); - - Assert.IsTrue(wb.Dispose()); - wb.Close(); - xssfWorkbook.Close(); - } - - [Test] - public void GzipSheetdataWriter() - { - SXSSFWorkbook wb = new SXSSFWorkbook(); - wb.CompressTempFiles = true; - int rowNum = 1000; - int sheetNum = 5; - for (int i = 0; i < sheetNum; i++) - { - ISheet sh = wb.CreateSheet("sheet" + i); - for (int j = 0; j < rowNum; j++) - { - IRow row = sh.CreateRow(j); - ICell cell1 = row.CreateCell(0); - cell1.SetCellValue(new CellReference(cell1).FormatAsString()); - - ICell cell2 = row.CreateCell(1); - cell2.SetCellValue(i); - - ICell cell3 = row.CreateCell(2); - cell3.SetCellValue(j); - } - } - - XSSFWorkbook xwb = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; - for (int i = 0; i < sheetNum; i++) - { - ISheet sh = xwb.GetSheetAt(i); - Assert.AreEqual("sheet" + i, sh.SheetName); - for (int j = 0; j < rowNum; j++) - { - IRow row = sh.GetRow(j); - Assert.IsNotNull(row, "row[" + j + "]"); - ICell cell1 = row.GetCell(0); - Assert.AreEqual(new CellReference(cell1).FormatAsString(), cell1.StringCellValue); - - ICell cell2 = row.GetCell(1); - Assert.AreEqual(i, (int)cell2.NumericCellValue); - - ICell cell3 = row.GetCell(2); - Assert.AreEqual(j, (int)cell3.NumericCellValue); - } - } - - Assert.IsTrue(wb.Dispose()); - xwb.Close(); - wb.Close(); - } - - protected static void assertWorkbookDispose(SXSSFWorkbook wb) - { - int rowNum = 1000; - int sheetNum = 5; - for (int i = 0; i < sheetNum; i++) - { - ISheet sh = wb.CreateSheet("sheet" + i); - for (int j = 0; j < rowNum; j++) - { - IRow row = sh.CreateRow(j); - ICell cell1 = row.CreateCell(0); - cell1.SetCellValue(new CellReference(cell1).FormatAsString()); - - ICell cell2 = row.CreateCell(1); - cell2.SetCellValue(i); - - ICell cell3 = row.CreateCell(2); - cell3.SetCellValue(j); - } - } - - foreach (ISheet sheet in wb) - { - SXSSFSheet sxSheet = (SXSSFSheet)sheet; - Assert.IsTrue(sxSheet.SheetDataWriter.TempFileInfo.Exists); - } - - Assert.IsTrue(wb.Dispose()); - - foreach (ISheet sheet in wb) - { - SXSSFSheet sxSheet = (SXSSFSheet)sheet; - Assert.IsFalse(sxSheet.SheetDataWriter.TempFileInfo.Exists); - } - } - - [Test] - public void WorkbookDispose() - { - SXSSFWorkbook wb1 = new SXSSFWorkbook(); - // the underlying Writer is SheetDataWriter - assertWorkbookDispose(wb1); - wb1.Close(); - SXSSFWorkbook wb2 = new SXSSFWorkbook(); - wb2.CompressTempFiles = (/*setter*/true); - // the underlying Writer is GZIPSheetDataWriter - assertWorkbookDispose(wb2); - wb2.Close(); - } - - [Ignore("currently writing the same sheet multiple times is not supported...")] - [Test] - public void Bug53515() - { - IWorkbook wb1 = new SXSSFWorkbook(10); - populateWorkbook(wb1); - saveTwice(wb1); - IWorkbook wb2 = new XSSFWorkbook(); - populateWorkbook(wb2); - saveTwice(wb2); - wb2.Close(); - wb1.Close(); - } - - [Ignore("Crashes the JVM because of documented JVM behavior with concurrent writing/reading of zip-files, " - + "see http://www.oracle.com/technetwork/java/javase/documentation/overview-156328.html")] - [Test] - public void Bug53515a() - { - FileInfo out1 = new FileInfo("Test.xlsx"); - out1.Delete(); - for (int i = 0; i < 2; i++) - { - Console.WriteLine("Iteration " + i); - SXSSFWorkbook wb; - if (out1.Exists) - { - wb = new SXSSFWorkbook( - (XSSFWorkbook)WorkbookFactory.Create(out1.FullName)); - } - else - { - wb = new SXSSFWorkbook(10); - } - - try - { - FileStream outSteam = new FileStream(out1.FullName, FileMode.Create, FileAccess.ReadWrite); - if (i == 0) - { - populateWorkbook(wb); - } - else - { - //System.Gc(); - //System.Gc(); - //System.Gc(); - GC.Collect(); - GC.Collect(); - GC.Collect(); - } - - wb.Write(outSteam); - // Assert.IsTrue(wb.Dispose()); - outSteam.Close(); - } - finally - { - Assert.IsTrue(wb.Dispose()); - } - wb.Close(); - } - out1.Delete(); - } - - private static void populateWorkbook(IWorkbook wb) - { - ISheet sh = wb.CreateSheet(); - for (int rownum = 0; rownum < 100; rownum++) - { - IRow row = sh.CreateRow(rownum); - for (int cellnum = 0; cellnum < 10; cellnum++) - { - ICell cell = row.CreateCell(cellnum); - String Address = new CellReference(cell).FormatAsString(); - cell.SetCellValue(Address); - } - } - } - - private static void saveTwice(IWorkbook wb) - { - for (int i = 0; i < 2; i++) - { - try - { - NullOutputStream out1 = new NullOutputStream(); - wb.Write(out1, false); - out1.Close(); - } - catch (Exception e) - { - throw new Exception("ERROR: failed on " + (i + 1) - + "th time calling " + wb.GetType().Name - + ".Write() with exception " + e.Message, e); - } - } - } - - [Ignore("Just a local test for http://stackoverflow.com/questions/33627329/apache-poi-streaming-api-using-xssf-template")] - [Test] - public void TestTemplateFile() - { - XSSFWorkbook workBook = XSSFTestDataSamples.OpenSampleWorkbook("sample.xlsx"); - SXSSFWorkbook streamingWorkBook = new SXSSFWorkbook(workBook, 10); - ISheet sheet = streamingWorkBook.GetSheet("Sheet1"); - for (int rowNum = 10; rowNum < 1000000; rowNum++) - { - IRow row = sheet.CreateRow(rowNum); - for (int cellNum = 0; cellNum < 700; cellNum++) - { - ICell cell = row.CreateCell(cellNum); - cell.SetCellValue("somEvalue"); - } - - if (rowNum % 100 == 0) - { - Console.Write("."); - if (rowNum % 10000 == 0) - { - Console.WriteLine(rowNum); - } - } - } - - FileStream fos = new FileStream("C:\\temp\\streaming.xlsx", FileMode.Create, FileAccess.ReadWrite); - streamingWorkBook.Write(fos); - fos.Close(); - - streamingWorkBook.Close(); - workBook.Close(); - } - - [Test] - public void closeDoesNotModifyWorkbook() - { - String filename = "SampleSS.xlsx"; - FileInfo file = POIDataSamples.GetSpreadSheetInstance().GetFileInfo(filename); - SXSSFWorkbook wb = null; - XSSFWorkbook xwb = null; - - // Some tests commented out because close() modifies the file - // See bug 58779 - - // String - //wb = new SXSSFWorkbook(new XSSFWorkbook(file.Path)); - //assertCloseDoesNotModifyFile(filename, wb); - - // File - //wb = new SXSSFWorkbook(new XSSFWorkbook(file)); - //assertCloseDoesNotModifyFile(filename, wb); - - // InputStream - FileStream fis = file.Open(FileMode.Open, FileAccess.ReadWrite); - try - { - xwb = new XSSFWorkbook(fis); - wb = new SXSSFWorkbook(xwb); - assertCloseDoesNotModifyFile(filename, wb); - } - finally - { - if (xwb != null) - { - xwb.Close(); - } - if (wb != null) - { - wb.Close(); - } - fis.Close(); - } - - // OPCPackage - //wb = new SXSSFWorkbook(new XSSFWorkbook(OPCPackage.open(file))); - //assertCloseDoesNotModifyFile(filename, wb); - } - /** - * Bug #59743 - * - * this is only triggered on other files apart of sheet[1,2,...].xml - * as those are either copied uncompressed or with the use of GZIPInputStream - * so we use shared strings - */ - [Test] - public void TestZipBombNotTriggeredOnUselessContent() - { - SXSSFWorkbook swb = new SXSSFWorkbook(null, 1, true, true); - SXSSFSheet s = swb.CreateSheet() as SXSSFSheet; - char[] useless = new char[32767]; - Arrays.Fill(useless, ' '); - - for (int row = 0; row < 1; row++) - { - IRow r = s.CreateRow(row); - for (int col = 0; col < 10; col++) - { - char[] prefix = HexDump.ToHex(row * 1000 + col).ToCharArray(); - Arrays.Fill(useless, 0, 10, ' '); - Array.Copy(prefix, 0, useless, 0, prefix.Length); - String ul = new String(useless); - r.CreateCell(col, CellType.String).SetCellValue(ul); - ul = null; - } - } - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - swb.Write(bos); - swb.Dispose(); - swb.Close(); - } - - /** - * To avoid accident changes to the template, you should be able - * to create a SXSSFWorkbook from a read-only XSSF one, then - * change + save that (only). See bug #60010 - * TODO Fix this to work! - */ - - [Ignore("")] - [Test] - public void CreateFromReadOnlyWorkbook() - { - FileInfo input = XSSFTestDataSamples.GetSampleFile("sample.xlsx"); - OPCPackage pkg = OPCPackage.Open(input, PackageAccess.READ); - XSSFWorkbook xssf = new XSSFWorkbook(pkg); - SXSSFWorkbook wb = new SXSSFWorkbook(xssf, 2); - - String sheetName = "Test SXSSF"; - ISheet s = wb.CreateSheet(sheetName); - for (int i = 0; i < 10; i++) - { - IRow r = s.CreateRow(i); - r.CreateCell(0).SetCellValue(true); - r.CreateCell(1).SetCellValue(2.4); - r.CreateCell(2).SetCellValue("Test Row " + i); - } - Assert.AreEqual(10, s.LastRowNum); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - wb.Write(bos); - wb.Dispose(); - wb.Close(); - - xssf = new XSSFWorkbook(new ByteArrayInputStream(bos.ToByteArray())); - s = xssf.GetSheet(sheetName); - Assert.AreEqual(10, s.LastRowNum); - Assert.AreEqual(true, s.GetRow(0).GetCell(0).BooleanCellValue); - Assert.AreEqual("Test Row 9", s.GetRow(9).GetCell(2).StringCellValue); - } - - }*/ } \ No newline at end of file From 4e117009f118b5ebce71e2bdeed5b7d3b647f111 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 11:03:32 +0300 Subject: [PATCH 18/24] Update TestTempFile.cs --- testcases/main/Util/TestTempFile.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index 4475cc3d8..ec3d9b53b 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -20,14 +20,18 @@ public void TestCreateTempFile() string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); - if (Directory.Exists(tempDirPath)) + if(Directory.Exists(tempDirPath)) Directory.Delete(tempDirPath, true); - + Assert.IsFalse(File.Exists(fileInfo.FullName)); Assert.IsFalse(Directory.Exists(tempDirPath)); - Assert.DoesNotThrow(() => TempFile.CreateTempFile("test2", ".xls")); + FileInfo file = null; + Assert.DoesNotThrow(() => file = TempFile.CreateTempFile("test2", ".xls")); Assert.IsTrue(Directory.Exists(tempDirPath)); + + if(file !=null && file.Exists) + file.Delete(); } [Test] @@ -40,13 +44,17 @@ public void TestGetTempFilePath() string tempDirPath = Path.GetDirectoryName(path); - if (Directory.Exists(tempDirPath)) + if(Directory.Exists(tempDirPath)) Directory.Delete(tempDirPath, true); Assert.IsFalse(Directory.Exists(tempDirPath)); - Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls")); + string fileName = ""; + Assert.DoesNotThrow(() => fileName = TempFile.GetTempFilePath("test", ".xls")); Assert.IsTrue(Directory.Exists(tempDirPath)); + + if(File.Exists(fileName)) + File.Delete(fileName); } } } From ede309edb06fc94cb3f580f1c52a82d1f15ef853 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 11:18:29 +0300 Subject: [PATCH 19/24] Update TestTempFile.cs --- testcases/main/Util/TestTempFile.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index ec3d9b53b..512ecafe2 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -16,14 +16,14 @@ public void TestCreateTempFile() FileInfo fileInfo = null; Assert.DoesNotThrow(() => fileInfo = TempFile.CreateTempFile("test", ".xls")); - Assert.IsTrue(File.Exists(fileInfo.FullName)); + Assert.IsTrue(fileInfo!=null && fileInfo.Exists); string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); if(Directory.Exists(tempDirPath)) Directory.Delete(tempDirPath, true); - Assert.IsFalse(File.Exists(fileInfo.FullName)); + Assert.IsFalse(fileInfo!=null && fileInfo.Exists); Assert.IsFalse(Directory.Exists(tempDirPath)); FileInfo file = null; @@ -49,12 +49,8 @@ public void TestGetTempFilePath() Assert.IsFalse(Directory.Exists(tempDirPath)); - string fileName = ""; - Assert.DoesNotThrow(() => fileName = TempFile.GetTempFilePath("test", ".xls")); + Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls")); Assert.IsTrue(Directory.Exists(tempDirPath)); - - if(File.Exists(fileName)) - File.Delete(fileName); } } } From d061292632376f6caa86a0f58d013a0ba7c4f006 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 11:31:40 +0300 Subject: [PATCH 20/24] Update TestTempFile.cs --- testcases/main/Util/TestTempFile.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index 512ecafe2..b9a537c2b 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -23,9 +23,14 @@ public void TestCreateTempFile() if(Directory.Exists(tempDirPath)) Directory.Delete(tempDirPath, true); - Assert.IsFalse(fileInfo!=null && fileInfo.Exists); Assert.IsFalse(Directory.Exists(tempDirPath)); + if(fileInfo!=null) + { + fileInfo.Refresh(); + Assert.IsFalse(fileInfo.Exists); + } + FileInfo file = null; Assert.DoesNotThrow(() => file = TempFile.CreateTempFile("test2", ".xls")); Assert.IsTrue(Directory.Exists(tempDirPath)); From 358e23cd73fa0fa38451ccaab1c22ca6a76ab0ba Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 11:51:12 +0300 Subject: [PATCH 21/24] Update TestTempFile.cs --- testcases/main/Util/TestTempFile.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index b9a537c2b..91f726fe0 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -20,8 +20,14 @@ public void TestCreateTempFile() string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); - if(Directory.Exists(tempDirPath)) - Directory.Delete(tempDirPath, true); + while(Directory.Exists(tempDirPath)) + { + try + { + Directory.Delete(tempDirPath, true); + } + catch { } + } Assert.IsFalse(Directory.Exists(tempDirPath)); @@ -49,8 +55,14 @@ public void TestGetTempFilePath() string tempDirPath = Path.GetDirectoryName(path); - if(Directory.Exists(tempDirPath)) - Directory.Delete(tempDirPath, true); + while(Directory.Exists(tempDirPath)) + { + try + { + Directory.Delete(tempDirPath, true); + } + catch { } + } Assert.IsFalse(Directory.Exists(tempDirPath)); From 8e5a924d8de3ecc76b78cfca91dae07eb56e5075 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 12:06:17 +0300 Subject: [PATCH 22/24] Update TestXSSFWorkbook.cs --- .../ooxml/XSSF/UserModel/TestXSSFWorkbook.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs index 9474f6c0d..da827d788 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs @@ -911,14 +911,16 @@ public void TestSetVBAProject() allBytes[i] = (byte)(i - 128); } - XSSFWorkbook wb1 = new XSSFWorkbook(); - wb1.CreateSheet(); - wb1.SetVBAProject(new ByteArrayInputStream(allBytes)); - file = TempFile.CreateTempFile("poi-", ".xlsm"); - Stream out1 = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite); - wb1.Write(out1); - out1.Close(); - wb1.Close(); + using(XSSFWorkbook wb1 = new XSSFWorkbook()) + { + wb1.CreateSheet(); + wb1.SetVBAProject(new ByteArrayInputStream(allBytes)); + file = TempFile.CreateTempFile("poi-", ".xlsm"); + using(Stream out1 = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite)) + { + wb1.Write(out1); + } + } // Check the package contains what we'd expect it to From 9f69b618a38f4c6e6722e41bd57cd1090940e755 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 3 Jan 2024 12:19:45 +0300 Subject: [PATCH 23/24] More tries --- testcases/main/Util/TestTempFile.cs | 11 +++++++++-- testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index 91f726fe0..9ff4df207 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -1,6 +1,7 @@ using NPOI.Util; using NUnit.Framework; using System.IO; +using System.Threading; namespace TestCases.Util { @@ -26,7 +27,10 @@ public void TestCreateTempFile() { Directory.Delete(tempDirPath, true); } - catch { } + catch + { + Thread.Sleep(5); + } } Assert.IsFalse(Directory.Exists(tempDirPath)); @@ -61,7 +65,10 @@ public void TestGetTempFilePath() { Directory.Delete(tempDirPath, true); } - catch { } + catch + { + Thread.Sleep(10); + } } Assert.IsFalse(Directory.Exists(tempDirPath)); diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs index da827d788..738b28afd 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFWorkbook.cs @@ -922,6 +922,8 @@ public void TestSetVBAProject() } } + if(file != null) + file.Refresh(); // Check the package contains what we'd expect it to OPCPackage pkg = OPCPackage.Open(file); From 9c02753710bb155b6348f48304e054684adb1fa8 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Tue, 12 Mar 2024 19:08:34 +0300 Subject: [PATCH 24/24] Update TestSXSSFWorkbook.cs --- .../ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs | 576 ++++++++++++++++++ 1 file changed, 576 insertions(+) diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index 49cd8437c..fbe18d5af 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -35,6 +35,582 @@ namespace TestCases.XSSF.Streaming using TestCases.SS.UserModel; using TestCases.Util; + [TestFixture] + public class TestSXSSFWorkbook : BaseTestXWorkbook + { + public TestSXSSFWorkbook() + : base(SXSSFITestDataProvider.instance) + { + } + [TearDown] + public void TearDown() + { + //((SXSSFITestDataProvider)_testDataProvider).Cleanup(); + } + /** + * cloning of sheets is not supported in SXSSF + */ + + [Test] + public override void CloneSheet() + { + try + { + base.CloneSheet(); + Assert.Fail("expected exception"); + } + catch(RuntimeException e) + { + Assert.AreEqual("NotImplemented", e.Message); + } + } + [Test] + public override void SheetClone() + { + try + { + base.SheetClone(); + Assert.Fail("expected exception"); + } + catch(RuntimeException e) + { + Assert.AreEqual("NotImplemented", e.Message); + } + } + /** + * Skip this test, as SXSSF doesn't update formulas on sheet name + * Changes. + */ + + [Test] + public override void TestSetSheetName() + { + Assume.That(false, "SXSSF doesn't update formulas on sheet name Changes, as most cells probably aren't in memory at the time"); + } + + [Test] + public void ExistingWorkbook() + { + XSSFWorkbook xssfWb1 = new XSSFWorkbook(); + xssfWb1.CreateSheet("S1"); + SXSSFWorkbook wb1 = new SXSSFWorkbook(xssfWb1); + XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb1) as XSSFWorkbook; + Assert.IsTrue(wb1.Dispose()); + + SXSSFWorkbook wb2 = new SXSSFWorkbook(xssfWb2); + Assert.AreEqual(1, wb2.NumberOfSheets); + ISheet sheet = wb2.GetSheetAt(0); + Assert.IsNotNull(sheet); + Assert.AreEqual("S1", sheet.SheetName); + Assert.IsTrue(wb2.Dispose()); + xssfWb2.Close(); + xssfWb1.Close(); + + wb2.Close(); + wb1.Close(); + } + + [Test] + public void UseSharedStringsTable() + { + SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true); + + SharedStringsTable sss = POITestCase.GetFieldValue(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource"); + + Assert.IsNotNull(sss); + + IRow row = wb.CreateSheet("S1").CreateRow(0); + + row.CreateCell(0).SetCellValue("A"); + row.CreateCell(1).SetCellValue("B"); + row.CreateCell(2).SetCellValue("A"); + + XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; + sss = POITestCase.GetFieldValue(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource"); + Assert.AreEqual(2, sss.UniqueCount); + Assert.IsTrue(wb.Dispose()); + + ISheet sheet1 = xssfWorkbook.GetSheetAt(0); + Assert.AreEqual("S1", sheet1.SheetName); + Assert.AreEqual(1, sheet1.PhysicalNumberOfRows); + row = sheet1.GetRow(0); + Assert.IsNotNull(row); + ICell cell = row.GetCell(0); + Assert.IsNotNull(cell); + Assert.AreEqual("A", cell.StringCellValue); + cell = row.GetCell(1); + Assert.IsNotNull(cell); + Assert.AreEqual("B", cell.StringCellValue); + cell = row.GetCell(2); + Assert.IsNotNull(cell); + Assert.AreEqual("A", cell.StringCellValue); + + xssfWorkbook.Close(); + wb.Close(); + } + + [Test] + public void AddToExistingWorkbook() + { + XSSFWorkbook xssfWb1 = new XSSFWorkbook(); + xssfWb1.CreateSheet("S1"); + ISheet sheet = xssfWb1.CreateSheet("S2"); + IRow row = sheet.CreateRow(1); + ICell cell = row.CreateCell(1); + cell.SetCellValue("value 2_1_1"); + SXSSFWorkbook wb1 = new SXSSFWorkbook(xssfWb1); + XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb1) as XSSFWorkbook; + Assert.IsTrue(wb1.Dispose()); + xssfWb1.Close(); + + SXSSFWorkbook wb2 = new SXSSFWorkbook(xssfWb2); + // Add a row to the existing empty sheet + ISheet sheet1 = wb2.GetSheetAt(0); + IRow row1_1 = sheet1.CreateRow(1); + ICell cell1_1_1 = row1_1.CreateCell(1); + cell1_1_1.SetCellValue("value 1_1_1"); + + // Add a row to the existing non-empty sheet + ISheet sheet2 = wb2.GetSheetAt(1); + IRow row2_2 = sheet2.CreateRow(2); + ICell cell2_2_1 = row2_2.CreateCell(1); + cell2_2_1.SetCellValue("value 2_2_1"); + + // Add a sheet with one row + ISheet sheet3 = wb2.CreateSheet("S3"); + IRow row3_1 = sheet3.CreateRow(1); + ICell cell3_1_1 = row3_1.CreateCell(1); + cell3_1_1.SetCellValue("value 3_1_1"); + + XSSFWorkbook xssfWb3 = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb2) as XSSFWorkbook; + wb2.Close(); + + Assert.AreEqual(3, xssfWb3.NumberOfSheets); + // Verify sheet 1 + sheet1 = xssfWb3.GetSheetAt(0); + Assert.AreEqual("S1", sheet1.SheetName); + Assert.AreEqual(1, sheet1.PhysicalNumberOfRows); + row1_1 = sheet1.GetRow(1); + Assert.IsNotNull(row1_1); + cell1_1_1 = row1_1.GetCell(1); + Assert.IsNotNull(cell1_1_1); + Assert.AreEqual("value 1_1_1", cell1_1_1.StringCellValue); + // Verify sheet 2 + sheet2 = xssfWb3.GetSheetAt(1); + Assert.AreEqual("S2", sheet2.SheetName); + Assert.AreEqual(2, sheet2.PhysicalNumberOfRows); + IRow row2_1 = sheet2.GetRow(1); + Assert.IsNotNull(row2_1); + ICell cell2_1_1 = row2_1.GetCell(1); + Assert.IsNotNull(cell2_1_1); + Assert.AreEqual("value 2_1_1", cell2_1_1.StringCellValue); + row2_2 = sheet2.GetRow(2); + Assert.IsNotNull(row2_2); + cell2_2_1 = row2_2.GetCell(1); + Assert.IsNotNull(cell2_2_1); + Assert.AreEqual("value 2_2_1", cell2_2_1.StringCellValue); + // Verify sheet 3 + sheet3 = xssfWb3.GetSheetAt(2); + Assert.AreEqual("S3", sheet3.SheetName); + Assert.AreEqual(1, sheet3.PhysicalNumberOfRows); + row3_1 = sheet3.GetRow(1); + Assert.IsNotNull(row3_1); + cell3_1_1 = row3_1.GetCell(1); + Assert.IsNotNull(cell3_1_1); + Assert.AreEqual("value 3_1_1", cell3_1_1.StringCellValue); + + xssfWb2.Close(); + xssfWb3.Close(); + wb1.Close(); + } + + [Test] + public void SheetdataWriter() + { + SXSSFWorkbook wb = new SXSSFWorkbook(); + SXSSFSheet sh = wb.CreateSheet() as SXSSFSheet; + SheetDataWriter wr = sh.SheetDataWriter; + Assert.IsTrue(wr.GetType() == typeof(SheetDataWriter)); + FileInfo tmp = wr.TempFileInfo; + Assert.IsTrue(tmp.Name.StartsWith("poi-sxssf-sheet")); + Assert.IsTrue(tmp.Name.EndsWith(".xml")); + Assert.IsTrue(wb.Dispose()); + wb.Close(); + + wb = new SXSSFWorkbook(); + wb.CompressTempFiles = (/*setter*/true); + sh = wb.CreateSheet() as SXSSFSheet; + wr = sh.SheetDataWriter; + Assert.IsTrue(wr.GetType() == typeof(GZIPSheetDataWriter)); + tmp = wr.TempFileInfo; + Assert.IsTrue(tmp.Name.StartsWith("poi-sxssf-sheet-xml")); + Assert.IsTrue(tmp.Name.EndsWith(".gz")); + Assert.IsTrue(wb.Dispose()); + wb.Close(); + + //Test escaping of Unicode control characters + wb = new SXSSFWorkbook(); + wb.CreateSheet("S1").CreateRow(0).CreateCell(0).SetCellValue("value\u0019"); + XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; + ICell cell = xssfWorkbook.GetSheet("S1").GetRow(0).GetCell(0); + Assert.AreEqual("value?", cell.StringCellValue); + + Assert.IsTrue(wb.Dispose()); + wb.Close(); + xssfWorkbook.Close(); + } + + [Test] + public void GzipSheetdataWriter() + { + SXSSFWorkbook wb = new SXSSFWorkbook(); + wb.CompressTempFiles = true; + int rowNum = 1000; + int sheetNum = 5; + for(int i = 0; i < sheetNum; i++) + { + ISheet sh = wb.CreateSheet("sheet" + i); + for(int j = 0; j < rowNum; j++) + { + IRow row = sh.CreateRow(j); + ICell cell1 = row.CreateCell(0); + cell1.SetCellValue(new CellReference(cell1).FormatAsString()); + + ICell cell2 = row.CreateCell(1); + cell2.SetCellValue(i); + + ICell cell3 = row.CreateCell(2); + cell3.SetCellValue(j); + } + } + + XSSFWorkbook xwb = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook; + for(int i = 0; i < sheetNum; i++) + { + ISheet sh = xwb.GetSheetAt(i); + Assert.AreEqual("sheet" + i, sh.SheetName); + for(int j = 0; j < rowNum; j++) + { + IRow row = sh.GetRow(j); + Assert.IsNotNull(row, "row[" + j + "]"); + ICell cell1 = row.GetCell(0); + Assert.AreEqual(new CellReference(cell1).FormatAsString(), cell1.StringCellValue); + + ICell cell2 = row.GetCell(1); + Assert.AreEqual(i, (int) cell2.NumericCellValue); + + ICell cell3 = row.GetCell(2); + Assert.AreEqual(j, (int) cell3.NumericCellValue); + } + } + + Assert.IsTrue(wb.Dispose()); + xwb.Close(); + wb.Close(); + } + + protected static void assertWorkbookDispose(SXSSFWorkbook wb) + { + int rowNum = 1000; + int sheetNum = 5; + for(int i = 0; i < sheetNum; i++) + { + ISheet sh = wb.CreateSheet("sheet" + i); + for(int j = 0; j < rowNum; j++) + { + IRow row = sh.CreateRow(j); + ICell cell1 = row.CreateCell(0); + cell1.SetCellValue(new CellReference(cell1).FormatAsString()); + + ICell cell2 = row.CreateCell(1); + cell2.SetCellValue(i); + + ICell cell3 = row.CreateCell(2); + cell3.SetCellValue(j); + } + } + + foreach(ISheet sheet in wb) + { + SXSSFSheet sxSheet = (SXSSFSheet)sheet; + Assert.IsTrue(sxSheet.SheetDataWriter.TempFileInfo.Exists); + } + + Assert.IsTrue(wb.Dispose()); + + foreach(ISheet sheet in wb) + { + SXSSFSheet sxSheet = (SXSSFSheet)sheet; + Assert.IsFalse(sxSheet.SheetDataWriter.TempFileInfo.Exists); + } + } + + [Test] + public void WorkbookDispose() + { + SXSSFWorkbook wb1 = new SXSSFWorkbook(); + // the underlying Writer is SheetDataWriter + assertWorkbookDispose(wb1); + wb1.Close(); + SXSSFWorkbook wb2 = new SXSSFWorkbook(); + wb2.CompressTempFiles = (/*setter*/true); + // the underlying Writer is GZIPSheetDataWriter + assertWorkbookDispose(wb2); + wb2.Close(); + } + + [Ignore("currently writing the same sheet multiple times is not supported...")] + [Test] + public void Bug53515() + { + IWorkbook wb1 = new SXSSFWorkbook(10); + populateWorkbook(wb1); + saveTwice(wb1); + IWorkbook wb2 = new XSSFWorkbook(); + populateWorkbook(wb2); + saveTwice(wb2); + wb2.Close(); + wb1.Close(); + } + + [Ignore("Crashes the JVM because of documented JVM behavior with concurrent writing/reading of zip-files, " + + "see http://www.oracle.com/technetwork/java/javase/documentation/overview-156328.html")] + [Test] + public void Bug53515a() + { + FileInfo out1 = new FileInfo("Test.xlsx"); + out1.Delete(); + for(int i = 0; i < 2; i++) + { + Console.WriteLine("Iteration " + i); + SXSSFWorkbook wb; + if(out1.Exists) + { + wb = new SXSSFWorkbook( + (XSSFWorkbook) WorkbookFactory.Create(out1.FullName)); + } + else + { + wb = new SXSSFWorkbook(10); + } + + try + { + FileStream outSteam = new FileStream(out1.FullName, FileMode.Create, FileAccess.ReadWrite); + if(i == 0) + { + populateWorkbook(wb); + } + else + { + //System.Gc(); + //System.Gc(); + //System.Gc(); + GC.Collect(); + GC.Collect(); + GC.Collect(); + } + + wb.Write(outSteam); + // Assert.IsTrue(wb.Dispose()); + outSteam.Close(); + } + finally + { + Assert.IsTrue(wb.Dispose()); + } + wb.Close(); + } + out1.Delete(); + } + + private static void populateWorkbook(IWorkbook wb) + { + ISheet sh = wb.CreateSheet(); + for(int rownum = 0; rownum < 100; rownum++) + { + IRow row = sh.CreateRow(rownum); + for(int cellnum = 0; cellnum < 10; cellnum++) + { + ICell cell = row.CreateCell(cellnum); + String Address = new CellReference(cell).FormatAsString(); + cell.SetCellValue(Address); + } + } + } + + private static void saveTwice(IWorkbook wb) + { + for(int i = 0; i < 2; i++) + { + try + { + NullOutputStream out1 = new NullOutputStream(); + wb.Write(out1, false); + out1.Close(); + } + catch(Exception e) + { + throw new Exception("ERROR: failed on " + (i + 1) + + "th time calling " + wb.GetType().Name + + ".Write() with exception " + e.Message, e); + } + } + } + + [Ignore("Just a local test for http://stackoverflow.com/questions/33627329/apache-poi-streaming-api-using-xssf-template")] + [Test] + public void TestTemplateFile() + { + XSSFWorkbook workBook = XSSFTestDataSamples.OpenSampleWorkbook("sample.xlsx"); + SXSSFWorkbook streamingWorkBook = new SXSSFWorkbook(workBook, 10); + ISheet sheet = streamingWorkBook.GetSheet("Sheet1"); + for(int rowNum = 10; rowNum < 1000000; rowNum++) + { + IRow row = sheet.CreateRow(rowNum); + for(int cellNum = 0; cellNum < 700; cellNum++) + { + ICell cell = row.CreateCell(cellNum); + cell.SetCellValue("somEvalue"); + } + + if(rowNum % 100 == 0) + { + Console.Write("."); + if(rowNum % 10000 == 0) + { + Console.WriteLine(rowNum); + } + } + } + + FileStream fos = new FileStream("C:\\temp\\streaming.xlsx", FileMode.Create, FileAccess.ReadWrite); + streamingWorkBook.Write(fos); + fos.Close(); + + streamingWorkBook.Close(); + workBook.Close(); + } + + [Test] + public void closeDoesNotModifyWorkbook() + { + String filename = "SampleSS.xlsx"; + FileInfo file = POIDataSamples.GetSpreadSheetInstance().GetFileInfo(filename); + SXSSFWorkbook wb = null; + XSSFWorkbook xwb = null; + + // Some tests commented out because close() modifies the file + // See bug 58779 + + // String + //wb = new SXSSFWorkbook(new XSSFWorkbook(file.Path)); + //assertCloseDoesNotModifyFile(filename, wb); + + // File + //wb = new SXSSFWorkbook(new XSSFWorkbook(file)); + //assertCloseDoesNotModifyFile(filename, wb); + + // InputStream + FileStream fis = file.Open(FileMode.Open, FileAccess.ReadWrite); + try + { + xwb = new XSSFWorkbook(fis); + wb = new SXSSFWorkbook(xwb); + assertCloseDoesNotModifyFile(filename, wb); + } + finally + { + if(xwb != null) + { + xwb.Close(); + } + if(wb != null) + { + wb.Close(); + } + fis.Close(); + } + + // OPCPackage + //wb = new SXSSFWorkbook(new XSSFWorkbook(OPCPackage.open(file))); + //assertCloseDoesNotModifyFile(filename, wb); + } + /** + * Bug #59743 + * + * this is only triggered on other files apart of sheet[1,2,...].xml + * as those are either copied uncompressed or with the use of GZIPInputStream + * so we use shared strings + */ + [Test] + public void TestZipBombNotTriggeredOnUselessContent() + { + SXSSFWorkbook swb = new SXSSFWorkbook(null, 1, true, true); + SXSSFSheet s = swb.CreateSheet() as SXSSFSheet; + char[] useless = new char[32767]; + Arrays.Fill(useless, ' '); + + for(int row = 0; row < 1; row++) + { + IRow r = s.CreateRow(row); + for(int col = 0; col < 10; col++) + { + char[] prefix = HexDump.ToHex(row * 1000 + col).ToCharArray(); + Arrays.Fill(useless, 0, 10, ' '); + Array.Copy(prefix, 0, useless, 0, prefix.Length); + String ul = new String(useless); + r.CreateCell(col, CellType.String).SetCellValue(ul); + ul = null; + } + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + swb.Write(bos); + swb.Dispose(); + swb.Close(); + } + + /** + * To avoid accident changes to the template, you should be able + * to create a SXSSFWorkbook from a read-only XSSF one, then + * change + save that (only). See bug #60010 + * TODO Fix this to work! + */ + + [Ignore("")] + [Test] + public void CreateFromReadOnlyWorkbook() + { + FileInfo input = XSSFTestDataSamples.GetSampleFile("sample.xlsx"); + OPCPackage pkg = OPCPackage.Open(input, PackageAccess.READ); + XSSFWorkbook xssf = new XSSFWorkbook(pkg); + SXSSFWorkbook wb = new SXSSFWorkbook(xssf, 2); + + String sheetName = "Test SXSSF"; + ISheet s = wb.CreateSheet(sheetName); + for(int i = 0; i < 10; i++) + { + IRow r = s.CreateRow(i); + r.CreateCell(0).SetCellValue(true); + r.CreateCell(1).SetCellValue(2.4); + r.CreateCell(2).SetCellValue("Test Row " + i); + } + Assert.AreEqual(10, s.LastRowNum); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + wb.Write(bos); + wb.Dispose(); + wb.Close(); + + xssf = new XSSFWorkbook(new ByteArrayInputStream(bos.ToByteArray())); + s = xssf.GetSheet(sheetName); + Assert.AreEqual(10, s.LastRowNum); + Assert.AreEqual(true, s.GetRow(0).GetCell(0).BooleanCellValue); + Assert.AreEqual("Test Row 9", s.GetRow(9).GetCell(2).StringCellValue); + } + + } } \ No newline at end of file