aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2015-10-14 14:53:41 +0000
committerDominik Stadler <centic@apache.org>2015-10-14 14:53:41 +0000
commit0648ee7b54fc13d583810b55db64e69c1d601e7e (patch)
treef1e67c2b5278676e2315999a8e654ecb01505326 /src/testcases/org/apache/poi/hssf/usermodel
parentb4c0a91af85c5d0a4bd63105e7ed7d8daefeb2d4 (diff)
downloadpoi-0648ee7b54fc13d583810b55db64e69c1d601e7e.tar.gz
poi-0648ee7b54fc13d583810b55db64e69c1d601e7e.zip
Bug 58480: Work around problem where on Windows systems a Mapped Buffer can still lock a file even if the Channel was closed properly. Use reflection as DirectBuffer is in package sun.com and thus likely to go away with Java 9.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1708609 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/usermodel')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index a2306d7236..fec2a230a4 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -56,6 +56,9 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.TempFile;
@@ -1194,4 +1197,50 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue("Should find some images via Client or Child anchors, but did not find any at all", found);
}
+
+ @Test
+ public void testRewriteFileBug58480() throws Exception {
+ final File file = new File(
+ "build/HSSFWorkbookTest-testWriteScenario.xls");
+
+ // create new workbook
+ {
+ final Workbook workbook = new HSSFWorkbook();
+ final Sheet sheet = workbook.createSheet("foo");
+ final Row row = sheet.createRow(1);
+ row.createCell(1).setCellValue("bar");
+
+ writeAndCloseWorkbook(workbook, file);
+ }
+
+ // edit the workbook
+ {
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
+ try {
+ DirectoryNode root = fs.getRoot();
+ final Workbook workbook = new HSSFWorkbook(root, true);
+ final Sheet sheet = workbook.getSheet("foo");
+ sheet.getRow(1).createCell(2).setCellValue("baz");
+
+ writeAndCloseWorkbook(workbook, file);
+ } finally {
+ fs.close();
+ }
+ }
+ }
+
+ private void writeAndCloseWorkbook(Workbook workbook, File file)
+ throws IOException, InterruptedException {
+ final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ workbook.write(bytesOut);
+ workbook.close();
+
+ final byte[] byteArray = bytesOut.toByteArray();
+ bytesOut.close();
+
+ final FileOutputStream fileOut = new FileOutputStream(file);
+ fileOut.write(byteArray);
+ fileOut.close();
+
+ }
}