Tests writing and reading back a proper dictionary with an invalid
- * codepage. (HPSF writes Unicode dictionaries only.)
- * @throws IOException
- * @throws HPSFException
+ * Tests writing and reading back a proper dictionary with an invalid
+ * codepage. (HPSF writes Unicode dictionaries only.)
*/
- @Test(expected=IllegalPropertySetDataException.class)
+ @Test(expected=UnsupportedEncodingException.class)
public void dictionaryWithInvalidCodepage() throws IOException, HPSFException {
final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit();
/* Write: */
- final OutputStream out = new FileOutputStream(copy);
-
- final POIFSFileSystem poiFs = new POIFSFileSystem();
+
final PropertySet ps1 = new PropertySet();
final Section s = ps1.getSections().get(0);
final Map m = new HashMap<>(3, 1.0f);
- m.put(Long.valueOf(1), "String 1");
- m.put(Long.valueOf(2), "String 2");
- m.put(Long.valueOf(3), "String 3");
+ m.put(1L, "String 1");
+ m.put(2L, "String 2");
+ m.put(3L, "String 3");
- try {
+ try (OutputStream out = new FileOutputStream(copy);
+ POIFSFileSystem poiFs = new POIFSFileSystem()) {
s.setDictionary(m);
s.setFormatID(DocumentSummaryInformation.FORMAT_ID[0]);
int codepage = 12345;
- s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
- Integer.valueOf(codepage));
+ s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage);
poiFs.createDocument(ps1.toInputStream(), "Test");
poiFs.writeFilesystem(out);
- } finally {
- poiFs.close();
- out.close();
}
}
diff --git a/src/testcases/org/apache/poi/hpsf/basic/Util.java b/src/testcases/org/apache/poi/hpsf/basic/Util.java
index daa58d789d..af4e4462a9 100644
--- a/src/testcases/org/apache/poi/hpsf/basic/Util.java
+++ b/src/testcases/org/apache/poi/hpsf/basic/Util.java
@@ -18,8 +18,9 @@
package org.apache.poi.hpsf.basic;
+import static org.junit.Assert.fail;
+
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -28,7 +29,6 @@ import java.util.List;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
-import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.util.IOUtils;
@@ -39,6 +39,9 @@ import org.apache.poi.util.IOUtils;
*/
final class Util {
+ private Util() {
+ }
+
/**
* Reads a set of files from a POI filesystem and returns them
* as an array of {@link POIFile} instances. This method loads all
@@ -58,24 +61,20 @@ final class Util {
*
* @exception IOException if an I/O exception occurs
*/
- public static List readPOIFiles(final File poiFs, final String... poiFiles)
- throws FileNotFoundException, IOException {
+ static List readPOIFiles(final File poiFs, final String... poiFiles) throws IOException {
final List files = new ArrayList<>();
POIFSReader r = new POIFSReader();
- POIFSReaderListener pfl = new POIFSReaderListener() {
- @Override
- public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
- try {
- final POIFile f = new POIFile();
- f.setName(event.getName());
- f.setPath(event.getPath());
- final InputStream in = event.getStream();
- f.setBytes(IOUtils.toByteArray(in));
- in.close();
- files.add(f);
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
+ POIFSReaderListener pfl = event -> {
+ try {
+ final POIFile f = new POIFile();
+ f.setName(event.getName());
+ f.setPath(event.getPath());
+ final InputStream in = event.getStream();
+ f.setBytes(IOUtils.toByteArray(in));
+ in.close();
+ files.add(f);
+ } catch (IOException ex) {
+ fail(ex.getMessage());
}
};
if (poiFiles.length == 0) {
@@ -88,12 +87,8 @@ final class Util {
}
/* Read the POI filesystem. */
- FileInputStream stream = new FileInputStream(poiFs);
- try {
- r.read(stream);
- } finally {
- stream.close();
- }
+ r.read(poiFs);
+
return files;
}
@@ -110,29 +105,23 @@ final class Util {
* @return The property sets. The elements are ordered in the same way
* as the files in the POI filesystem.
*
- * @exception FileNotFoundException if the file containing the POI
- * filesystem does not exist
- *
* @exception IOException if an I/O exception occurs
*/
- public static List readPropertySets(final File poiFs) throws IOException {
+ static List readPropertySets(final File poiFs) throws IOException {
final List files = new ArrayList<>(7);
final POIFSReader r = new POIFSReader();
- POIFSReaderListener pfl = new POIFSReaderListener() {
- @Override
- public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
- try {
- final POIFile f = new POIFile();
- f.setName(event.getName());
- f.setPath(event.getPath());
- final InputStream in = event.getStream();
- if (PropertySet.isPropertySetStream(in)) {
- f.setBytes(IOUtils.toByteArray(in));
- files.add(f);
- }
- } catch (Exception ex) {
- throw new RuntimeException(ex);
+ final POIFSReaderListener pfl = event -> {
+ try {
+ final POIFile f = new POIFile();
+ f.setName(event.getName());
+ f.setPath(event.getPath());
+ final InputStream in = event.getStream();
+ if (PropertySet.isPropertySetStream(in)) {
+ f.setBytes(IOUtils.toByteArray(in));
+ files.add(f);
}
+ } catch (Exception ex) {
+ fail(ex.getMessage());
}
};
@@ -140,12 +129,7 @@ final class Util {
r.registerListener(pfl);
/* Read the POI filesystem. */
- InputStream is = new FileInputStream(poiFs);
- try {
- r.read(is);
- } finally {
- is.close();
- }
+ r.read(poiFs);
return files;
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index 53802f5069..f7c8682d3e 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -68,7 +68,6 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
@@ -89,7 +88,6 @@ import org.apache.poi.ss.usermodel.SheetVisibility;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil;
-import org.junit.After;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
@@ -1022,7 +1020,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
assertEquals(4, wb.getNumberOfFontsAsInt());
- HSSFFont f1 = wb.getFontAt((short) 0);
+ HSSFFont f1 = wb.getFontAt(0);
assertFalse(f1.getBold());
// Check that asking for the same font
@@ -1617,7 +1615,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
@Test
public void bug46904() throws Exception {
try {
- OPOIFSFileSystem fs = new OPOIFSFileSystem(
+ POIFSFileSystem fs = new POIFSFileSystem(
HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
@@ -2505,7 +2503,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
@Test
public void bug53432() throws IOException {
- Workbook wb1 = new HSSFWorkbook(); //or new HSSFWorkbook();
+ HSSFWorkbook wb1 = new HSSFWorkbook(); //or new HSSFWorkbook();
wb1.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
assertEquals(wb1.getAllPictures().size(), 1);
wb1.close();
@@ -2513,13 +2511,13 @@ public final class TestBugs extends BaseTestBugzillaIssues {
wb1.close();
wb1 = new HSSFWorkbook();
- Workbook wb2 = writeOutAndReadBack((HSSFWorkbook) wb1);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
assertEquals(wb2.getAllPictures().size(), 0);
wb2.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
assertEquals(wb2.getAllPictures().size(), 1);
- Workbook wb3 = writeOutAndReadBack((HSSFWorkbook) wb2);
+ HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
wb2.close();
assertEquals(wb3.getAllPictures().size(), 1);
@@ -3093,8 +3091,8 @@ public final class TestBugs extends BaseTestBugzillaIssues {
@Test
public void test61287() throws IOException {
- final Workbook wb = HSSFTestDataSamples.openSampleWorkbook("61287.xls");
- ExcelExtractor ex = new ExcelExtractor((HSSFWorkbook) wb);
+ final HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("61287.xls");
+ ExcelExtractor ex = new ExcelExtractor(wb);
String text = ex.getText();
assertContains(text, "\u8D44\u4EA7\u8D1F\u503A\u8868");
wb.close();
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index fdcd8b21a8..7450581f5b 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -29,7 +29,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -38,7 +37,6 @@ import java.util.Collection;
import java.util.List;
import junit.framework.AssertionFailedError;
-
import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hpsf.ClassID;
@@ -56,7 +54,6 @@ import org.apache.poi.hssf.record.WindowOneRecord;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
@@ -95,7 +92,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
/**
* Tests for {@link HSSFWorkbook#isHidden()} etc
- * @throws IOException
*/
@Test
public void hidden() throws IOException {
@@ -103,23 +99,23 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
WindowOneRecord w1 = wb.getWorkbook().getWindowOne();
- assertEquals(false, wb.isHidden());
- assertEquals(false, w1.getHidden());
+ assertFalse(wb.isHidden());
+ assertFalse(w1.getHidden());
wb.setHidden(true);
- assertEquals(true, wb.isHidden());
- assertEquals(true, w1.getHidden());
+ assertTrue(wb.isHidden());
+ assertTrue(w1.getHidden());
HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
w1 = wbBack.getWorkbook().getWindowOne();
wbBack.setHidden(true);
- assertEquals(true, wbBack.isHidden());
- assertEquals(true, w1.getHidden());
+ assertTrue(wbBack.isHidden());
+ assertTrue(w1.getHidden());
wbBack.setHidden(false);
- assertEquals(false, wbBack.isHidden());
- assertEquals(false, w1.getHidden());
+ assertFalse(wbBack.isHidden());
+ assertFalse(w1.getHidden());
wbBack.close();
wb.close();
@@ -257,30 +253,30 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.setSelectedTabs(selected);
assertCollectionsEquals(selected, wb.getSelectedTabs());
- assertEquals(true, sheet0.isSelected());
- assertEquals(false, sheet1.isSelected());
- assertEquals(true, sheet2.isSelected());
- assertEquals(true, sheet3.isSelected());
- assertEquals(false, sheet4.isSelected());
- assertEquals(false, sheet5.isSelected());
+ assertTrue(sheet0.isSelected());
+ assertFalse(sheet1.isSelected());
+ assertTrue(sheet2.isSelected());
+ assertTrue(sheet3.isSelected());
+ assertFalse(sheet4.isSelected());
+ assertFalse(sheet5.isSelected());
selected = arrayToList(new int[] { 1, 3, 5 });
wb.setSelectedTabs(selected);
// previous selection should be cleared
assertCollectionsEquals(selected, wb.getSelectedTabs());
- assertEquals(false, sheet0.isSelected());
- assertEquals(true, sheet1.isSelected());
- assertEquals(false, sheet2.isSelected());
- assertEquals(true, sheet3.isSelected());
- assertEquals(false, sheet4.isSelected());
- assertEquals(true, sheet5.isSelected());
-
- assertEquals(true, sheet0.isActive());
- assertEquals(false, sheet2.isActive());
+ assertFalse(sheet0.isSelected());
+ assertTrue(sheet1.isSelected());
+ assertFalse(sheet2.isSelected());
+ assertTrue(sheet3.isSelected());
+ assertFalse(sheet4.isSelected());
+ assertTrue(sheet5.isSelected());
+
+ assertTrue(sheet0.isActive());
+ assertFalse(sheet2.isActive());
wb.setActiveSheet(2);
- assertEquals(false, sheet0.isActive());
- assertEquals(true, sheet2.isActive());
+ assertFalse(sheet0.isActive());
+ assertTrue(sheet2.isActive());
/*{ // helpful if viewing this workbook in excel:
sheet0.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Sheet0"));
@@ -383,7 +379,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
* records to be written with invalid offset indexes. Excel does not like this, and such
* errors are particularly hard to track down. This test ensures that HSSFWorkbook throws
* a specific exception as soon as the situation is detected. See bugzilla 45066
- * @throws IOException
*/
@Test
public void sheetSerializeSizeMismatch_bug45066() throws IOException {
@@ -496,7 +491,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
* result returned by getRecordSize() differs from result returned by serialize()
*/
private static final class BadlyBehavedRecord extends Record {
- public BadlyBehavedRecord() {
+ BadlyBehavedRecord() {
//
}
@Override
@@ -576,7 +571,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(bytes));
ClassID clsid2 = fs2.getRoot().getStorageClsid();
- assertTrue(clsid1.equals(clsid2));
+ assertEquals(clsid1, clsid2);
fs2.close();
wb.close();
@@ -625,108 +620,89 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
public void differentPOIFS() throws Exception {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
- POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"));
- try {
- files[0] = poifsFileSystem.getRoot();
- NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"));
- try {
- files[1] = npoifsFileSystem.getRoot();
-
- // Open without preserving nodes
- for(DirectoryNode dir : files) {
- HSSFWorkbook workbook = new HSSFWorkbook(dir, false);
- HSSFSheet sheet = workbook.getSheetAt(0);
- HSSFCell cell = sheet.getRow(0).getCell(0);
- assertEquals("replaceMe", cell .getRichStringCellValue().getString());
-
- workbook.close();
- }
-
- // Now re-check with preserving
- for(DirectoryNode dir : files) {
- HSSFWorkbook workbook = new HSSFWorkbook(dir, true);
- HSSFSheet sheet = workbook.getSheetAt(0);
- HSSFCell cell = sheet.getRow(0).getCell(0);
- assertEquals("replaceMe", cell .getRichStringCellValue().getString());
-
- workbook.close();
- }
- } finally {
- npoifsFileSystem.close();
- }
- } finally {
- poifsFileSystem.close();
- }
+ try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"))) {
+ files[0] = poifsFileSystem.getRoot();
+ try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"))) {
+ files[1] = npoifsFileSystem.getRoot();
+
+ // Open without preserving nodes
+ for (DirectoryNode dir : files) {
+ HSSFWorkbook workbook = new HSSFWorkbook(dir, false);
+ HSSFSheet sheet = workbook.getSheetAt(0);
+ HSSFCell cell = sheet.getRow(0).getCell(0);
+ assertEquals("replaceMe", cell.getRichStringCellValue().getString());
+
+ workbook.close();
+ }
+
+ // Now re-check with preserving
+ for (DirectoryNode dir : files) {
+ HSSFWorkbook workbook = new HSSFWorkbook(dir, true);
+ HSSFSheet sheet = workbook.getSheetAt(0);
+ HSSFCell cell = sheet.getRow(0).getCell(0);
+ assertEquals("replaceMe", cell.getRichStringCellValue().getString());
+
+ workbook.close();
+ }
+ }
+ }
}
@Test
public void wordDocEmbeddedInXls() throws IOException {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
- POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"));
- try {
- files[0] = poifsFileSystem.getRoot();
- NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"));
- try {
- files[1] = npoifsFileSystem.getRoot();
-
- // Check the embedded parts
- for(DirectoryNode root : files) {
- HSSFWorkbook hw = new HSSFWorkbook(root, true);
- List objects = hw.getAllEmbeddedObjects();
- boolean found = false;
- for (HSSFObjectData embeddedObject : objects) {
- if (embeddedObject.hasDirectoryEntry()) {
- DirectoryEntry dir = embeddedObject.getDirectory();
- if (dir instanceof DirectoryNode) {
- DirectoryNode dNode = (DirectoryNode) dir;
- if (hasEntry(dNode, "WordDocument")) {
- found = true;
- }
- }
- }
- }
- assertTrue(found);
-
- hw.close();
- }
- } finally {
- npoifsFileSystem.close();
- }
- } finally {
- poifsFileSystem.close();
- }
+ try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))) {
+ files[0] = poifsFileSystem.getRoot();
+ try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"))) {
+ files[1] = npoifsFileSystem.getRoot();
+
+ // Check the embedded parts
+ for (DirectoryNode root : files) {
+ HSSFWorkbook hw = new HSSFWorkbook(root, true);
+ List objects = hw.getAllEmbeddedObjects();
+ boolean found = false;
+ for (HSSFObjectData embeddedObject : objects) {
+ if (embeddedObject.hasDirectoryEntry()) {
+ DirectoryEntry dir = embeddedObject.getDirectory();
+ if (dir instanceof DirectoryNode) {
+ DirectoryNode dNode = (DirectoryNode) dir;
+ if (dNode.hasEntry("WordDocument")) {
+ found = true;
+ break;
+ }
+ }
+ }
+ }
+ assertTrue(found);
+
+ hw.close();
+ }
+ }
+ }
}
/**
* Checks that we can open a workbook with NPOIFS, and write it out
* again (via POIFS) and have it be valid
- * @throws IOException
*/
@Test
public void writeWorkbookFromNPOIFS() throws IOException {
- InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls");
- try {
- NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
- try {
- // Start as NPOIFS
- HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
- assertEquals(3, wb.getNumberOfSheets());
- assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
-
- // Will switch to POIFS
- HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
- assertEquals(3, wbBack.getNumberOfSheets());
- assertEquals("Root xls", wbBack.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
- wbBack.close();
-
- wb.close();
- } finally {
- fs.close();
- }
- } finally {
- is.close();
- }
+ try (InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls");
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(is)) {
+ // Start as NPOIFS
+ HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
+ assertEquals(3, wb.getNumberOfSheets());
+ assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+
+ // Will switch to POIFS
+ HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ assertEquals(3, wbBack.getNumberOfSheets());
+ assertEquals("Root xls", wbBack.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+ wbBack.close();
+
+ wb.close();
+ }
}
@Test
@@ -795,7 +771,9 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.setSheetOrder("other sheet", 0);
// names
+ //noinspection ConstantConditions
assertEquals("'first sheet'!D1", wb.getName("name1").getRefersToFormula());
+ //noinspection ConstantConditions
assertEquals("'other sheet'!C1", wb.getName("name2").getRefersToFormula());
// cells
@@ -811,15 +789,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.close();
}
- private boolean hasEntry(DirectoryNode dirNode, String entryName) {
- try {
- dirNode.getEntry(entryName);
- return true;
- } catch (FileNotFoundException e) {
- return false;
- }
- }
-
@Test
public void clonePictures() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
@@ -854,11 +823,8 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
// Should throw exception about invalid POIFSFileSystem
@Test(expected=IllegalArgumentException.class)
public void emptyDirectoryNode() throws IOException {
- POIFSFileSystem fs = new POIFSFileSystem();
- try {
+ try (POIFSFileSystem fs = new POIFSFileSystem()) {
new HSSFWorkbook(fs).close();
- } finally {
- fs.close();
}
}
@@ -1093,7 +1059,8 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.close();
}
- private void expectName(HSSFWorkbook wb, String name, String expect) {
+ @SuppressWarnings("SameParameterValue")
+ private void expectName(HSSFWorkbook wb, String name, String expect) {
final HSSFName hssfName = wb.getName(name);
assertNotNull(hssfName);
assertEquals(expect, hssfName.getRefersToFormula());
@@ -1149,16 +1116,13 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
// edit the workbook
{
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
- try {
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false)) {
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();
}
}
} finally {
@@ -1239,18 +1203,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
}
wb.close();
- // Can't work for OPOIFS
- OPOIFSFileSystem ofs = new OPOIFSFileSystem(
- POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
- wb = new HSSFWorkbook(ofs.getRoot(), true);
- try {
- wb.write();
- fail("Shouldn't work for OPOIFSFileSystem");
- } catch (IllegalStateException e) {
- // expected here
- }
- wb.close();
-
// Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem(
POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"), true);
@@ -1268,16 +1220,9 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
public void inPlaceWrite() throws Exception {
// Setup as a copy of a known-good file
final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
- InputStream inputStream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls");
- try {
- FileOutputStream outputStream = new FileOutputStream(file);
- try {
- IOUtils.copy(inputStream, outputStream);
- } finally {
- outputStream.close();
- }
- } finally {
- inputStream.close();
+ try (InputStream inputStream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls");
+ FileOutputStream outputStream = new FileOutputStream(file)) {
+ IOUtils.copy(inputStream, outputStream);
}
// Open from the temp file in read-write mode
diff --git a/src/testcases/org/apache/poi/poifs/eventfilesystem/TestPOIFSReaderRegistry.java b/src/testcases/org/apache/poi/poifs/eventfilesystem/TestPOIFSReaderRegistry.java
index d920d2ce8c..7b88fbc650 100644
--- a/src/testcases/org/apache/poi/poifs/eventfilesystem/TestPOIFSReaderRegistry.java
+++ b/src/testcases/org/apache/poi/poifs/eventfilesystem/TestPOIFSReaderRegistry.java
@@ -17,20 +17,22 @@
package org.apache.poi.poifs.eventfilesystem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
-import junit.framework.TestCase;
-
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
+import org.junit.Test;
/**
* Class to test POIFSReaderRegistry functionality
*
* @author Marc Johnson
*/
-public final class TestPOIFSReaderRegistry extends TestCase {
+public final class TestPOIFSReaderRegistry {
private final POIFSReaderListener[] listeners =
{
new Listener(), new Listener(), new Listener(), new Listener()
@@ -56,13 +58,14 @@ public final class TestPOIFSReaderRegistry extends TestCase {
/**
* Test empty registry
*/
+ @Test
public void testEmptyRegistry() {
POIFSReaderRegistry registry = new POIFSReaderRegistry();
for (POIFSDocumentPath path : paths) {
for (String name : names) {
Iterator listeners =
- registry.getListeners(path, name);
+ registry.getListeners(path, name).iterator();
assertTrue(!listeners.hasNext());
}
@@ -72,6 +75,7 @@ public final class TestPOIFSReaderRegistry extends TestCase {
/**
* Test mixed registration operations
*/
+ @Test
public void testMixedRegistrationOperations() {
POIFSReaderRegistry registry = new POIFSReaderRegistry();
@@ -93,21 +97,20 @@ public final class TestPOIFSReaderRegistry extends TestCase {
{
for (int n = 0; n < names.length; n++)
{
- Iterator listeners =
+ Iterable listeners =
registry.getListeners(paths[ k ], names[ n ]);
if (k == n)
{
- assertTrue(!listeners.hasNext());
+ assertTrue(!listeners.iterator().hasNext());
}
else
{
Set registeredListeners =
new HashSet<>();
- while (listeners.hasNext())
- {
- registeredListeners.add(listeners.next());
+ for (POIFSReaderListener rl : listeners) {
+ registeredListeners.add(rl);
}
assertEquals(this.listeners.length - 1,
registeredListeners.size());
@@ -132,14 +135,13 @@ public final class TestPOIFSReaderRegistry extends TestCase {
}
for (POIFSDocumentPath path : paths) {
for (String name : names) {
- Iterator listeners =
+ Iterable listeners =
registry.getListeners(path, name);
Set registeredListeners =
new HashSet<>();
- while (listeners.hasNext())
- {
- registeredListeners.add(listeners.next());
+ for (POIFSReaderListener rl : listeners) {
+ registeredListeners.add(rl);
}
assertEquals(this.listeners.length,
registeredListeners.size());
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
index 871aada677..7f071886d2 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
@@ -21,7 +21,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
- * Tests for org.apache.poi.poifs.filesystem
+ * Tests for org.apache.poi.poifs.filesystem
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
@@ -29,7 +29,6 @@ import org.junit.runners.Suite;
, TestDocument.class
, TestDocumentDescriptor.class
, TestDocumentInputStream.class
- , TestDocumentNode.class
, TestDocumentOutputStream.class
, TestEmptyDocument.class
, TestNotOLE2Exception.class
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java
index 240e945a5d..ed2ce941ab 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java
@@ -19,7 +19,7 @@
package org.apache.poi.poifs.filesystem;
-import java.io.FileInputStream;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -38,42 +38,19 @@ import org.apache.poi.util.IOUtils;
* @author Marc Johnson (mjohnson at apache dot org)
*/
-public class ReaderWriter
+public final class ReaderWriter
implements POIFSReaderListener, POIFSWriterListener
{
- private final POIFSFileSystem filesystem;
private final DirectoryEntry root;
// keys are DocumentDescriptors, values are byte[]s
- private final Map dataMap;
+ private final Map dataMap = new HashMap<>();
- /**
- * Constructor ReaderWriter
- *
- *
- * @param filesystem
- *
- */
-
- ReaderWriter(final POIFSFileSystem filesystem)
- {
- this.filesystem = filesystem;
- root = this.filesystem.getRoot();
- dataMap = new HashMap<>();
+ private ReaderWriter(final POIFSFileSystem filesystem) {
+ root = filesystem.getRoot();
}
- /**
- * Method main
- *
- *
- * @param args
- *
- * @exception IOException
- *
- */
-
- public static void main(String [] args)
- throws IOException
+ public static void main(String [] args) throws IOException
{
if (args.length != 2)
{
@@ -86,10 +63,8 @@ public class ReaderWriter
POIFSFileSystem filesystem = new POIFSFileSystem();
reader.registerListener(new ReaderWriter(filesystem));
- FileInputStream istream = new FileInputStream(args[ 0 ]);
- reader.read(istream);
- istream.close();
+ reader.read(new File(args[ 0 ]));
FileOutputStream ostream = new FileOutputStream(args[ 1 ]);
filesystem.writeFilesystem(ostream);
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java
index f670b9a668..14422e5b74 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java
@@ -17,218 +17,131 @@
package org.apache.poi.poifs.filesystem;
+import static org.apache.poi.poifs.common.POIFSConstants.LARGER_BIG_BLOCK_SIZE;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.stream.IntStream;
import org.apache.poi.poifs.property.DocumentProperty;
-import org.apache.poi.poifs.storage.RawDataBlock;
-
-import junit.framework.TestCase;
+import org.apache.poi.poifs.storage.RawDataUtil;
+import org.apache.poi.util.IOUtils;
+import org.junit.Test;
/**
- * Class to test OPOIFSDocument functionality
+ * Class to test POIFSDocument functionality
*/
-public final class TestDocument extends TestCase {
+public class TestDocument {
/**
* Integration test -- really about all we can do
*/
- public void testOPOIFSDocument() throws IOException {
+ @Test
+ public void testNPOIFSDocument() throws IOException {
- // verify correct number of blocks get created for document
- // that is exact multituple of block size
- OPOIFSDocument document;
- byte[] array = new byte[ 4096 ];
+ try (NPOIFSFileSystem poifs = new NPOIFSFileSystem()) {
- for (int j = 0; j < array.length; j++)
- {
- array[ j ] = ( byte ) j;
- }
- document = new OPOIFSDocument("foo", new SlowInputStream(new ByteArrayInputStream(array)));
- checkDocument(document, array);
-
- // verify correct number of blocks get created for document
- // that is not an exact multiple of block size
- array = new byte[ 4097 ];
- for (int j = 0; j < array.length; j++)
- {
- array[ j ] = ( byte ) j;
- }
- document = new OPOIFSDocument("bar", new ByteArrayInputStream(array));
- checkDocument(document, array);
-
- // verify correct number of blocks get created for document
- // that is small
- array = new byte[ 4095 ];
- for (int j = 0; j < array.length; j++)
- {
- array[ j ] = ( byte ) j;
- }
- document = new OPOIFSDocument("_bar", new ByteArrayInputStream(array));
- checkDocument(document, array);
-
- // verify correct number of blocks get created for document
- // that is rather small
- array = new byte[ 199 ];
- for (int j = 0; j < array.length; j++)
- {
- array[ j ] = ( byte ) j;
- }
- document = new OPOIFSDocument("_bar2",
- new ByteArrayInputStream(array));
- checkDocument(document, array);
-
- // verify that output is correct
- array = new byte[ 4097 ];
- for (int j = 0; j < array.length; j++)
- {
- array[ j ] = ( byte ) j;
- }
- document = new OPOIFSDocument("foobar",
- new ByteArrayInputStream(array));
- checkDocument(document, array);
- document.setStartBlock(0x12345678); // what a big file!!
- DocumentProperty property = document.getDocumentProperty();
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
-
- property.writeData(stream);
- byte[] output = stream.toByteArray();
- byte[] array2 =
- {
- ( byte ) 'f', ( byte ) 0, ( byte ) 'o', ( byte ) 0, ( byte ) 'o',
- ( byte ) 0, ( byte ) 'b', ( byte ) 0, ( byte ) 'a', ( byte ) 0,
- ( byte ) 'r', ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 14,
- ( byte ) 0, ( byte ) 2, ( byte ) 1, ( byte ) -1, ( byte ) -1,
- ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1,
- ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1, ( byte ) -1,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0x78, ( byte ) 0x56, ( byte ) 0x34,
- ( byte ) 0x12, ( byte ) 1, ( byte ) 16, ( byte ) 0, ( byte ) 0,
- ( byte ) 0, ( byte ) 0, ( byte ) 0, ( byte ) 0
- };
-
- assertEquals(array2.length, output.length);
- for (int j = 0; j < output.length; j++)
- {
- assertEquals("Checking property offset " + j, array2[ j ],
- output[ j ]);
- }
- }
+ // verify correct number of blocks get created for document
+ // that is exact multiple of block size
+ checkDocument(poifs, LARGER_BIG_BLOCK_SIZE);
- private static OPOIFSDocument makeCopy(OPOIFSDocument document, byte[] input, byte[] data)
- throws IOException {
- OPOIFSDocument copy = null;
-
- if (input.length >= 4096)
- {
- RawDataBlock[] blocks =
- new RawDataBlock[ (input.length + 511) / 512 ];
- ByteArrayInputStream stream = new ByteArrayInputStream(data);
- int index = 0;
-
- while (true)
- {
- RawDataBlock block = new RawDataBlock(stream);
-
- if (block.eof())
- {
- break;
- }
- blocks[ index++ ] = block;
- }
- copy = new OPOIFSDocument("test" + input.length, blocks,
- input.length);
- }
- else
- {
- copy = new OPOIFSDocument("test"+input.length, document.getSmallBlocks(), input.length);
+ // verify correct number of blocks get created for document
+ // that is not an exact multiple of block size
+ checkDocument(poifs, LARGER_BIG_BLOCK_SIZE + 1);
+
+ // verify correct number of blocks get created for document
+ // that is small
+ checkDocument(poifs, LARGER_BIG_BLOCK_SIZE - 1);
+
+ // verify correct number of blocks get created for document
+ // that is rather small
+ checkDocument(poifs, 199);
+
+
+ // verify that output is correct
+ NPOIFSDocument document = checkDocument(poifs, LARGER_BIG_BLOCK_SIZE + 1);
+ DocumentProperty property = document.getDocumentProperty();
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+
+ property.writeData(stream);
+ byte[] output = stream.toByteArray();
+ byte[] array2 = RawDataUtil.decompress("H4sIAAAAAAAAAEtlyGMoYShiqGSwYCAH8DEwMf5HAsToMQdiRgEIGwCDyzEQgAAAAA==");
+
+ assertArrayEquals(array2, output);
}
- return copy;
}
- private static void checkDocument(final OPOIFSDocument document, final byte[] input)
- throws IOException {
- int big_blocks = 0;
- int small_blocks = 0;
- int total_output = 0;
+ private static NPOIFSDocument checkDocument(final NPOIFSFileSystem poifs, final int size) throws IOException {
+ final byte[] input = new byte[size];
+ IntStream.range(0, size).forEach(i -> input[i] = (byte)i);
- if (input.length >= 4096)
- {
- big_blocks = (input.length + 511) / 512;
- total_output = big_blocks * 512;
- }
- else
- {
- small_blocks = (input.length + 63) / 64;
- total_output = 0;
+ NPOIFSDocument document = ((DocumentNode)poifs.createDocument(
+ new SlowInputStream(new ByteArrayInputStream(input)),
+ "entry"+poifs.getRoot().getEntryCount())).getDocument();
+
+ final int blockSize = (size >= 4096) ? 512 : 64;
+ final int blockCount = (size + (blockSize-1)) / blockSize;
+
+ final byte[] bytCpy = checkValues(blockCount, document, input);
+ final NPOIFSDocument copied = makeCopy(document,bytCpy);
+
+ checkValues(blockCount, copied, input);
+
+ return document;
+ }
+
+ private static NPOIFSDocument makeCopy(NPOIFSDocument document, byte[] input) throws IOException {
+ NPOIFSFileSystem poifs = document.getFileSystem();
+ String name = "test" + input.length;
+ DirectoryNode root = poifs.getRoot();
+ if (root.hasEntry(name)) {
+ root.deleteEntry((EntryNode)root.getEntry(name));
}
- checkValues(
- big_blocks, small_blocks, total_output,
- makeCopy(
- document, input,
- checkValues(
- big_blocks, small_blocks, total_output, document,
- input)), input);
+ return ((DocumentNode)root
+ .createDocument(name, new ByteArrayInputStream(input)))
+ .getDocument();
}
- private static byte[] checkValues(int big_blocks, int small_blocks, int total_output,
- OPOIFSDocument document, byte[] input) throws IOException {
+ private static byte[] checkValues(final int blockCountExp, NPOIFSDocument document, byte[] input) throws IOException {
+ assertNotNull(document);
+ assertNotNull(document.getDocumentProperty().getDocument());
assertEquals(document, document.getDocumentProperty().getDocument());
- int increment = ( int ) Math.sqrt(input.length);
-
- for (int j = 1; j <= input.length; j += increment)
- {
- byte[] buffer = new byte[ j ];
- int offset = 0;
-
- for (int k = 0; k < (input.length / j); k++)
- {
- document.read(buffer, offset);
- for (int n = 0; n < buffer.length; n++)
- {
- assertEquals("checking byte " + (k * j) + n,
- input[ (k * j) + n ], buffer[ n ]);
- }
- offset += j;
- }
- }
- assertEquals(big_blocks, document.countBlocks());
- assertEquals(small_blocks, document.getSmallBlocks().length);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- document.writeBlocks(stream);
- byte[] output = stream.toByteArray();
+ ByteArrayInputStream bis = new ByteArrayInputStream(input);
- assertEquals(total_output, output.length);
- int limit = Math.min(total_output, input.length);
+ int blockCountAct = 0, bytesRemaining = input.length;
+ for (ByteBuffer bb : document) {
+ assertTrue(bytesRemaining > 0);
+ int bytesAct = Math.min(bb.remaining(), bytesRemaining);
+ assertTrue(bytesAct <= document.getDocumentBlockSize());
+ byte[] bufAct = new byte[bytesAct];
+ bb.get(bufAct);
- for (int j = 0; j < limit; j++)
- {
- assertEquals("Checking document offset " + j, input[ j ],
- output[ j ]);
+ byte[] bufExp = new byte[bytesAct];
+ int bytesExp = bis.read(bufExp, 0, bytesAct);
+ assertEquals(bytesExp, bytesAct);
+
+ assertArrayEquals(bufExp, bufAct);
+ blockCountAct++;
+ bytesRemaining -= bytesAct;
}
- for (int j = limit; j < output.length; j++)
- {
- assertEquals("Checking document offset " + j, ( byte ) -1,
- output[ j ]);
+
+ assertEquals(blockCountExp, blockCountAct);
+
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ try (DocumentInputStream dis = document.getFileSystem().createDocumentInputStream(
+ document.getDocumentProperty().getName())) {
+ IOUtils.copy(dis, stream);
}
+
+ byte[] output = stream.toByteArray();
+ assertArrayEquals(input, stream.toByteArray());
return output;
}
}
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
index a4b64c56e2..87a122f4d3 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
@@ -18,19 +18,17 @@
package org.apache.poi.poifs.filesystem;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.poifs.property.DirectoryProperty;
-import org.apache.poi.poifs.storage.RawDataBlock;
import org.apache.poi.util.SuppressForbidden;
import org.junit.Before;
import org.junit.Test;
@@ -39,448 +37,369 @@ import org.junit.Test;
* Class to test DocumentInputStream functionality
*/
public final class TestDocumentInputStream {
- private DocumentNode _workbook_n;
- private DocumentNode _workbook_o;
- private byte[] _workbook_data;
- private static final int _workbook_size = 5000;
+ private DocumentNode _workbook_n;
+ private byte[] _workbook_data;
+ private static final int _workbook_size = 5000;
- // non-even division of _workbook_size, also non-even division of
- // any block size
- private static final int _buffer_size = 6;
+ // non-even division of _workbook_size, also non-even division of
+ // any block size
+ private static final int _buffer_size = 6;
- @Before
- public void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
int blocks = (_workbook_size + 511) / 512;
- _workbook_data = new byte[ 512 * blocks ];
- Arrays.fill(_workbook_data, ( byte ) -1);
- for (int j = 0; j < _workbook_size; j++)
- {
- _workbook_data[ j ] = ( byte ) (j * j);
+ _workbook_data = new byte[512 * blocks];
+ Arrays.fill(_workbook_data, (byte) -1);
+ for (int j = 0; j < _workbook_size; j++) {
+ _workbook_data[j] = (byte) (j * j);
}
-
- // Create the Old POIFS Version
- RawDataBlock[] rawBlocks = new RawDataBlock[ blocks ];
- ByteArrayInputStream stream =
- new ByteArrayInputStream(_workbook_data);
-
- for (int j = 0; j < blocks; j++)
- {
- rawBlocks[ j ] = new RawDataBlock(stream);
- }
- OPOIFSDocument document = new OPOIFSDocument("Workbook", rawBlocks,
- _workbook_size);
-
- _workbook_o = new DocumentNode(
- document.getDocumentProperty(),
- new DirectoryNode(
- new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null));
-
+
// Now create the NPOIFS Version
byte[] _workbook_data_only = new byte[_workbook_size];
System.arraycopy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size);
-
+
NPOIFSFileSystem npoifs = new NPOIFSFileSystem();
// Make it easy when debugging to see what isn't the doc
byte[] minus1 = new byte[512];
- Arrays.fill(minus1, (byte)-1);
+ Arrays.fill(minus1, (byte) -1);
npoifs.getBlockAt(-1).put(minus1);
npoifs.getBlockAt(0).put(minus1);
npoifs.getBlockAt(1).put(minus1);
-
+
// Create the NPOIFS document
- _workbook_n = (DocumentNode)npoifs.createDocument(
- new ByteArrayInputStream(_workbook_data_only),
- "Workbook"
+ _workbook_n = (DocumentNode) npoifs.createDocument(
+ new ByteArrayInputStream(_workbook_data_only),
+ "Workbook"
);
}
- /**
+ /**
* test constructor
*/
@Test
public void testConstructor() throws IOException {
- DocumentInputStream ostream = new ODocumentInputStream(_workbook_o);
- DocumentInputStream nstream = new NDocumentInputStream(_workbook_n);
-
- assertEquals(_workbook_size, _workbook_o.getSize());
- assertEquals(_workbook_size, _workbook_n.getSize());
-
- assertEquals(_workbook_size, available(ostream));
- assertEquals(_workbook_size, available(nstream));
-
- ostream.close();
- nstream.close();
+ try (DocumentInputStream nstream = new NDocumentInputStream(_workbook_n)) {
+ assertEquals(_workbook_size, _workbook_n.getSize());
+ assertEquals(_workbook_size, available(nstream));
+ }
}
/**
* test available() behavior
*/
- @Test
+ @Test(expected = IllegalStateException.class)
public void testAvailable() throws IOException {
- DocumentInputStream ostream = new DocumentInputStream(_workbook_o);
DocumentInputStream nstream = new NDocumentInputStream(_workbook_n);
-
- assertEquals(_workbook_size, available(ostream));
assertEquals(_workbook_size, available(nstream));
- ostream.close();
nstream.close();
-
- try {
- available(ostream);
- fail("Should have caught IOException");
- } catch (IllegalStateException ignored) {
- // as expected
- }
- try {
- available(nstream);
- fail("Should have caught IOException");
- } catch (IllegalStateException ignored) {
- // as expected
- }
+
+ available(nstream);
}
/**
* test mark/reset/markSupported.
*/
+ @SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testMarkFunctions() throws IOException {
- byte[] buffer = new byte[ _workbook_size / 5 ];
+ byte[] buffer = new byte[_workbook_size / 5];
byte[] small_buffer = new byte[212];
-
- DocumentInputStream[] streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- // Read a fifth of it, and check all's correct
- stream.read(buffer);
- for (int j = 0; j < buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j ], buffer[ j ]
- );
- }
- assertEquals(_workbook_size - buffer.length, available(stream));
-
- // Reset, and check the available goes back to being the
- // whole of the stream
- stream.reset();
- assertEquals(_workbook_size, available(stream));
-
-
- // Read part of a block
- stream.read(small_buffer);
- for (int j = 0; j < small_buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j ], small_buffer[ j ]
- );
- }
- assertEquals(_workbook_size - small_buffer.length, available(stream));
- stream.mark(0);
-
- // Read the next part
- stream.read(small_buffer);
- for (int j = 0; j < small_buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j+small_buffer.length ], small_buffer[ j ]
- );
- }
- assertEquals(_workbook_size - 2*small_buffer.length, available(stream));
-
- // Reset, check it goes back to where it was
- stream.reset();
- assertEquals(_workbook_size - small_buffer.length, available(stream));
-
- // Read
- stream.read(small_buffer);
- for (int j = 0; j < small_buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j+small_buffer.length ], small_buffer[ j ]
- );
- }
- assertEquals(_workbook_size - 2*small_buffer.length, available(stream));
-
-
- // Now read at various points
- Arrays.fill(small_buffer, ( byte ) 0);
- stream.read(small_buffer, 6, 8);
- stream.read(small_buffer, 100, 10);
- stream.read(small_buffer, 150, 12);
- int pos = small_buffer.length * 2;
- for (int j = 0; j < small_buffer.length; j++) {
- byte exp = 0;
- if(j>= 6 && j<6+8) {
- exp = _workbook_data[pos];
- pos++;
- }
- if(j>= 100 && j<100+10) {
- exp = _workbook_data[pos];
- pos++;
- }
- if(j>= 150 && j<150+12) {
- exp = _workbook_data[pos];
- pos++;
- }
-
- assertEquals("checking byte " + j, exp, small_buffer[j]);
- }
+
+ DocumentInputStream stream = new NDocumentInputStream(_workbook_n);
+ // Read a fifth of it, and check all's correct
+ stream.read(buffer);
+ for (int j = 0; j < buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j], buffer[j]
+ );
+ }
+ assertEquals(_workbook_size - buffer.length, available(stream));
+
+ // Reset, and check the available goes back to being the
+ // whole of the stream
+ stream.reset();
+ assertEquals(_workbook_size, available(stream));
+
+
+ // Read part of a block
+ stream.read(small_buffer);
+ for (int j = 0; j < small_buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j], small_buffer[j]
+ );
}
-
+ assertEquals(_workbook_size - small_buffer.length, available(stream));
+ stream.mark(0);
+
+ // Read the next part
+ stream.read(small_buffer);
+ for (int j = 0; j < small_buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j + small_buffer.length], small_buffer[j]
+ );
+ }
+ assertEquals(_workbook_size - 2 * small_buffer.length, available(stream));
+
+ // Reset, check it goes back to where it was
+ stream.reset();
+ assertEquals(_workbook_size - small_buffer.length, available(stream));
+
+ // Read
+ stream.read(small_buffer);
+ for (int j = 0; j < small_buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j + small_buffer.length], small_buffer[j]
+ );
+ }
+ assertEquals(_workbook_size - 2 * small_buffer.length, available(stream));
+
+
+ // Now read at various points
+ Arrays.fill(small_buffer, (byte) 0);
+ stream.read(small_buffer, 6, 8);
+ stream.read(small_buffer, 100, 10);
+ stream.read(small_buffer, 150, 12);
+ int pos = small_buffer.length * 2;
+ for (int j = 0; j < small_buffer.length; j++) {
+ byte exp = 0;
+ if (j >= 6 && j < 6 + 8) {
+ exp = _workbook_data[pos];
+ pos++;
+ }
+ if (j >= 100 && j < 100 + 10) {
+ exp = _workbook_data[pos];
+ pos++;
+ }
+ if (j >= 150 && j < 150 + 12) {
+ exp = _workbook_data[pos];
+ pos++;
+ }
+
+ assertEquals("checking byte " + j, exp, small_buffer[j]);
+ }
+
// Now repeat it with spanning multiple blocks
- streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- // Read several blocks work
- buffer = new byte[ _workbook_size / 5 ];
- stream.read(buffer);
- for (int j = 0; j < buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j ], buffer[ j ]
- );
- }
- assertEquals(_workbook_size - buffer.length, available(stream));
-
- // Read all of it again, check it began at the start again
- stream.reset();
- assertEquals(_workbook_size, available(stream));
-
- stream.read(buffer);
- for (int j = 0; j < buffer.length; j++) {
- assertEquals(
- "checking byte " + j,
- _workbook_data[ j ], buffer[ j ]
- );
- }
-
- // Mark our position, and read another whole buffer
- stream.mark(12);
- stream.read(buffer);
- assertEquals(_workbook_size - (2 * buffer.length),
- available(stream));
- for (int j = buffer.length; j < (2 * buffer.length); j++)
- {
- assertEquals("checking byte " + j, _workbook_data[ j ],
- buffer[ j - buffer.length ]);
- }
-
- // Reset, should go back to only one buffer full read
- stream.reset();
- assertEquals(_workbook_size - buffer.length, available(stream));
-
- // Read the buffer again
- stream.read(buffer);
- assertEquals(_workbook_size - (2 * buffer.length),
- available(stream));
- for (int j = buffer.length; j < (2 * buffer.length); j++)
- {
- assertEquals("checking byte " + j, _workbook_data[ j ],
- buffer[ j - buffer.length ]);
- }
- assertTrue(stream.markSupported());
+ stream = new NDocumentInputStream(_workbook_n);
+ // Read several blocks work
+ buffer = new byte[_workbook_size / 5];
+ stream.read(buffer);
+ for (int j = 0; j < buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j], buffer[j]
+ );
}
+ assertEquals(_workbook_size - buffer.length, available(stream));
+
+ // Read all of it again, check it began at the start again
+ stream.reset();
+ assertEquals(_workbook_size, available(stream));
+
+ stream.read(buffer);
+ for (int j = 0; j < buffer.length; j++) {
+ assertEquals(
+ "checking byte " + j,
+ _workbook_data[j], buffer[j]
+ );
+ }
+
+ // Mark our position, and read another whole buffer
+ stream.mark(12);
+ stream.read(buffer);
+ assertEquals(_workbook_size - (2 * buffer.length),
+ available(stream));
+ for (int j = buffer.length; j < (2 * buffer.length); j++) {
+ assertEquals("checking byte " + j, _workbook_data[j],
+ buffer[j - buffer.length]);
+ }
+
+ // Reset, should go back to only one buffer full read
+ stream.reset();
+ assertEquals(_workbook_size - buffer.length, available(stream));
+
+ // Read the buffer again
+ stream.read(buffer);
+ assertEquals(_workbook_size - (2 * buffer.length),
+ available(stream));
+ for (int j = buffer.length; j < (2 * buffer.length); j++) {
+ assertEquals("checking byte " + j, _workbook_data[j],
+ buffer[j - buffer.length]);
+ }
+ assertTrue(stream.markSupported());
}
/**
* test simple read method
*/
- @Test
+ @SuppressWarnings("ResultOfMethodCallIgnored")
+ @Test(expected = IOException.class)
public void testReadSingleByte() throws IOException {
- DocumentInputStream[] streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- int remaining = _workbook_size;
-
- // Try and read each byte in turn
- for (int j = 0; j < _workbook_size; j++) {
- int b = stream.read();
- assertTrue("checking sign of " + j, b >= 0);
- assertEquals("validating byte " + j, _workbook_data[ j ],
- ( byte ) b);
- remaining--;
- assertEquals("checking remaining after reading byte " + j,
- remaining, available(stream));
- }
-
- // Ensure we fell off the end
- assertEquals(-1, stream.read());
-
- // Check that after close we can no longer read
- stream.close();
- try {
- stream.read();
- fail("Should have caught IOException");
- } catch (IOException ignored) {
- // as expected
- }
- }
+ DocumentInputStream stream = new NDocumentInputStream(_workbook_n);
+ int remaining = _workbook_size;
+
+ // Try and read each byte in turn
+ for (int j = 0; j < _workbook_size; j++) {
+ int b = stream.read();
+ assertTrue("checking sign of " + j, b >= 0);
+ assertEquals("validating byte " + j, _workbook_data[j],
+ (byte) b);
+ remaining--;
+ assertEquals("checking remaining after reading byte " + j,
+ remaining, available(stream));
+ }
+
+ // Ensure we fell off the end
+ assertEquals(-1, stream.read());
+
+ // Check that after close we can no longer read
+ stream.close();
+ stream.read();
}
/**
* Test buffered read
*/
+ @SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testBufferRead() throws IOException {
- DocumentInputStream[] streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- // Need to give a byte array to read
- try {
- stream.read(null);
- fail("Should have caught NullPointerException");
- } catch (NullPointerException ignored) {
- // as expected
- }
-
- // test reading zero length buffer
- assertEquals(0, stream.read(new byte[ 0 ]));
- assertEquals(_workbook_size, available(stream));
- byte[] buffer = new byte[ _buffer_size ];
- int offset = 0;
-
- while (available(stream) >= buffer.length)
- {
- assertEquals(_buffer_size, stream.read(buffer));
- for (byte element : buffer) {
+ DocumentInputStream stream = new NDocumentInputStream(_workbook_n);
+ // Need to give a byte array to read
+ try {
+ stream.read(null);
+ fail("Should have caught NullPointerException");
+ } catch (NullPointerException ignored) {
+ // as expected
+ }
+
+ // test reading zero length buffer
+ assertEquals(0, stream.read(new byte[0]));
+ assertEquals(_workbook_size, available(stream));
+ byte[] buffer = new byte[_buffer_size];
+ int offset = 0;
+
+ while (available(stream) >= buffer.length) {
+ assertEquals(_buffer_size, stream.read(buffer));
+ for (byte element : buffer) {
assertEquals("in main loop, byte " + offset,
- _workbook_data[ offset ], element);
+ _workbook_data[offset], element);
offset++;
- }
- assertEquals("offset " + offset, _workbook_size - offset,
- available(stream));
- }
- assertEquals(_workbook_size % _buffer_size, available(stream));
- Arrays.fill(buffer, ( byte ) 0);
- int count = stream.read(buffer);
-
- assertEquals(_workbook_size % _buffer_size, count);
- for (int j = 0; j < count; j++)
- {
- assertEquals("past main loop, byte " + offset,
- _workbook_data[ offset ], buffer[ j ]);
- offset++;
- }
- assertEquals(_workbook_size, offset);
- for (int j = count; j < buffer.length; j++)
- {
- assertEquals("checking remainder, byte " + j, 0, buffer[ j ]);
- }
- assertEquals(-1, stream.read(buffer));
- stream.close();
- try {
- stream.read(buffer);
- fail("Should have caught IOException");
- } catch (IOException ignored) {
- // as expected
- }
- }
+ }
+ assertEquals("offset " + offset, _workbook_size - offset,
+ available(stream));
+ }
+ assertEquals(_workbook_size % _buffer_size, available(stream));
+ Arrays.fill(buffer, (byte) 0);
+ int count = stream.read(buffer);
+
+ assertEquals(_workbook_size % _buffer_size, count);
+ for (int j = 0; j < count; j++) {
+ assertEquals("past main loop, byte " + offset,
+ _workbook_data[offset], buffer[j]);
+ offset++;
+ }
+ assertEquals(_workbook_size, offset);
+ for (int j = count; j < buffer.length; j++) {
+ assertEquals("checking remainder, byte " + j, 0, buffer[j]);
+ }
+ assertEquals(-1, stream.read(buffer));
+ stream.close();
+ try {
+ stream.read(buffer);
+ fail("Should have caught IOException");
+ } catch (IOException ignored) {
+ // as expected
+ }
}
/**
* Test complex buffered read
*/
+ @SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testComplexBufferRead() throws IOException {
- DocumentInputStream[] streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- try {
- stream.read(null, 0, 1);
- fail("Should have caught NullPointerException");
- } catch (IllegalArgumentException ignored) {
- // as expected
- }
-
- // test illegal offsets and lengths
- try {
- stream.read(new byte[ 5 ], -4, 0);
- fail("Should have caught IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException ignored) {
- // as expected
- }
- try {
- stream.read(new byte[ 5 ], 0, -4);
- fail("Should have caught IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException ignored) {
- // as expected
- }
- try {
- stream.read(new byte[ 5 ], 0, 6);
- fail("Should have caught IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException ignored) {
- // as expected
- }
-
- // test reading zero
- assertEquals(0, stream.read(new byte[ 5 ], 0, 0));
- assertEquals(_workbook_size, available(stream));
- byte[] buffer = new byte[ _workbook_size ];
- int offset = 0;
-
- while (available(stream) >= _buffer_size)
- {
- Arrays.fill(buffer, ( byte ) 0);
- assertEquals(_buffer_size,
- stream.read(buffer, offset, _buffer_size));
- for (int j = 0; j < offset; j++)
- {
- assertEquals("checking byte " + j, 0, buffer[ j ]);
- }
- for (int j = offset; j < (offset + _buffer_size); j++)
- {
- assertEquals("checking byte " + j, _workbook_data[ j ],
- buffer[ j ]);
- }
- for (int j = offset + _buffer_size; j < buffer.length; j++)
- {
- assertEquals("checking byte " + j, 0, buffer[ j ]);
- }
- offset += _buffer_size;
- assertEquals("offset " + offset, _workbook_size - offset,
- available(stream));
- }
- assertEquals(_workbook_size % _buffer_size, available(stream));
- Arrays.fill(buffer, ( byte ) 0);
- int count = stream.read(buffer, offset,
+ DocumentInputStream stream = new NDocumentInputStream(_workbook_n);
+ try {
+ stream.read(null, 0, 1);
+ fail("Should have caught NullPointerException");
+ } catch (IllegalArgumentException ignored) {
+ // as expected
+ }
+
+ // test illegal offsets and lengths
+ try {
+ stream.read(new byte[5], -4, 0);
+ fail("Should have caught IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException ignored) {
+ // as expected
+ }
+ try {
+ stream.read(new byte[5], 0, -4);
+ fail("Should have caught IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException ignored) {
+ // as expected
+ }
+ try {
+ stream.read(new byte[5], 0, 6);
+ fail("Should have caught IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException ignored) {
+ // as expected
+ }
+
+ // test reading zero
+ assertEquals(0, stream.read(new byte[5], 0, 0));
+ assertEquals(_workbook_size, available(stream));
+ byte[] buffer = new byte[_workbook_size];
+ int offset = 0;
+
+ while (available(stream) >= _buffer_size) {
+ Arrays.fill(buffer, (byte) 0);
+ assertEquals(_buffer_size,
+ stream.read(buffer, offset, _buffer_size));
+ for (int j = 0; j < offset; j++) {
+ assertEquals("checking byte " + j, 0, buffer[j]);
+ }
+ for (int j = offset; j < (offset + _buffer_size); j++) {
+ assertEquals("checking byte " + j, _workbook_data[j],
+ buffer[j]);
+ }
+ for (int j = offset + _buffer_size; j < buffer.length; j++) {
+ assertEquals("checking byte " + j, 0, buffer[j]);
+ }
+ offset += _buffer_size;
+ assertEquals("offset " + offset, _workbook_size - offset,
+ available(stream));
+ }
+ assertEquals(_workbook_size % _buffer_size, available(stream));
+ Arrays.fill(buffer, (byte) 0);
+ int count = stream.read(buffer, offset,
_workbook_size % _buffer_size);
- assertEquals(_workbook_size % _buffer_size, count);
- for (int j = 0; j < offset; j++)
- {
- assertEquals("checking byte " + j, 0, buffer[ j ]);
- }
- for (int j = offset; j < buffer.length; j++)
- {
- assertEquals("checking byte " + j, _workbook_data[ j ],
- buffer[ j ]);
- }
- assertEquals(_workbook_size, offset + count);
- for (int j = count; j < offset; j++)
- {
- assertEquals("byte " + j, 0, buffer[ j ]);
- }
-
- assertEquals(-1, stream.read(buffer, 0, 1));
- stream.close();
- try {
- stream.read(buffer, 0, 1);
- fail("Should have caught IOException");
- } catch (IOException ignored) {
- // as expected
- }
- }
+ assertEquals(_workbook_size % _buffer_size, count);
+ for (int j = 0; j < offset; j++) {
+ assertEquals("checking byte " + j, 0, buffer[j]);
+ }
+ for (int j = offset; j < buffer.length; j++) {
+ assertEquals("checking byte " + j, _workbook_data[j],
+ buffer[j]);
+ }
+ assertEquals(_workbook_size, offset + count);
+ for (int j = count; j < offset; j++) {
+ assertEquals("byte " + j, 0, buffer[j]);
+ }
+
+ assertEquals(-1, stream.read(buffer, 0, 1));
+ stream.close();
+ try {
+ stream.read(buffer, 0, 1);
+ fail("Should have caught IOException");
+ } catch (IOException ignored) {
+ // as expected
+ }
}
/**
@@ -488,82 +407,67 @@ public final class TestDocumentInputStream {
*/
@Test
public void testSkip() throws IOException {
- DocumentInputStream[] streams = new DocumentInputStream[] {
- new DocumentInputStream(_workbook_o),
- new NDocumentInputStream(_workbook_n)
- };
- for(DocumentInputStream stream : streams) {
- assertEquals(_workbook_size, available(stream));
- int count = available(stream);
-
- while (available(stream) >= _buffer_size) {
- assertEquals(_buffer_size, stream.skip(_buffer_size));
- count -= _buffer_size;
- assertEquals(count, available(stream));
- }
- assertEquals(_workbook_size % _buffer_size,
+ DocumentInputStream stream = new NDocumentInputStream(_workbook_n);
+ assertEquals(_workbook_size, available(stream));
+ int count = available(stream);
+
+ while (available(stream) >= _buffer_size) {
+ assertEquals(_buffer_size, stream.skip(_buffer_size));
+ count -= _buffer_size;
+ assertEquals(count, available(stream));
+ }
+ assertEquals(_workbook_size % _buffer_size,
stream.skip(_buffer_size));
- assertEquals(0, available(stream));
- stream.reset();
- assertEquals(_workbook_size, available(stream));
- assertEquals(_workbook_size, stream.skip(_workbook_size * 2));
- assertEquals(0, available(stream));
- stream.reset();
- assertEquals(_workbook_size, available(stream));
- assertEquals(_workbook_size,
- stream.skip(2 + ( long ) Integer.MAX_VALUE));
- assertEquals(0, available(stream));
- }
+ assertEquals(0, available(stream));
+ stream.reset();
+ assertEquals(_workbook_size, available(stream));
+ assertEquals(_workbook_size, stream.skip(_workbook_size * 2));
+ assertEquals(0, available(stream));
+ stream.reset();
+ assertEquals(_workbook_size, available(stream));
+ assertEquals(_workbook_size,
+ stream.skip(2 + (long) Integer.MAX_VALUE));
+ assertEquals(0, available(stream));
}
-
+
/**
* Test that we can read files at multiple levels down the tree
*/
@Test
public void testReadMultipleTreeLevels() throws Exception {
- final POIDataSamples _samples = POIDataSamples.getPublisherInstance();
- File sample = _samples.getFile("Sample.pub");
-
- DocumentInputStream stream;
-
- NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample);
- try {
- OPOIFSFileSystem opoifs = new OPOIFSFileSystem(new FileInputStream(sample));
-
- // Ensure we have what we expect on the root
- assertEquals(npoifs, npoifs.getRoot().getNFileSystem());
- assertEquals(npoifs, npoifs.getRoot().getFileSystem());
- assertEquals(null, npoifs.getRoot().getOFileSystem());
- assertEquals(null, opoifs.getRoot().getFileSystem());
- assertEquals(opoifs, opoifs.getRoot().getOFileSystem());
- assertEquals(null, opoifs.getRoot().getNFileSystem());
-
- // Check inside
- for(DirectoryNode root : new DirectoryNode[] { opoifs.getRoot(), npoifs.getRoot() }) {
- // Top Level
- Entry top = root.getEntry("Contents");
- assertEquals(true, top.isDocumentEntry());
- stream = root.createDocumentInputStream(top);
- stream.read();
-
- // One Level Down
- DirectoryNode escher = (DirectoryNode)root.getEntry("Escher");
- Entry one = escher.getEntry("EscherStm");
- assertEquals(true, one.isDocumentEntry());
- stream = escher.createDocumentInputStream(one);
- stream.read();
-
- // Two Levels Down
- DirectoryNode quill = (DirectoryNode)root.getEntry("Quill");
- DirectoryNode quillSub = (DirectoryNode)quill.getEntry("QuillSub");
- Entry two = quillSub.getEntry("CONTENTS");
- assertEquals(true, two.isDocumentEntry());
- stream = quillSub.createDocumentInputStream(two);
- stream.read();
- }
- } finally {
- npoifs.close();
- }
+ final POIDataSamples _samples = POIDataSamples.getPublisherInstance();
+ File sample = _samples.getFile("Sample.pub");
+
+ DocumentInputStream stream;
+
+ try (NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample)) {
+ // Ensure we have what we expect on the root
+ assertEquals(npoifs, npoifs.getRoot().getNFileSystem());
+ assertEquals(npoifs, npoifs.getRoot().getFileSystem());
+
+ // Check inside
+ DirectoryNode root = npoifs.getRoot();
+ // Top Level
+ Entry top = root.getEntry("Contents");
+ assertTrue(top.isDocumentEntry());
+ stream = root.createDocumentInputStream(top);
+ assertNotEquals(-1, stream.read());
+
+ // One Level Down
+ DirectoryNode escher = (DirectoryNode) root.getEntry("Escher");
+ Entry one = escher.getEntry("EscherStm");
+ assertTrue(one.isDocumentEntry());
+ stream = escher.createDocumentInputStream(one);
+ assertNotEquals(-1, stream.read());
+
+ // Two Levels Down
+ DirectoryNode quill = (DirectoryNode) root.getEntry("Quill");
+ DirectoryNode quillSub = (DirectoryNode) quill.getEntry("QuillSub");
+ Entry two = quillSub.getEntry("CONTENTS");
+ assertTrue(two.isDocumentEntry());
+ stream = quillSub.createDocumentInputStream(two);
+ assertNotEquals(-1, stream.read());
+ }
}
@SuppressForbidden("just for testing")
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java
deleted file mode 100644
index 69d167793a..0000000000
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java
+++ /dev/null
@@ -1,73 +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.
-==================================================================== */
-
-package org.apache.poi.poifs.filesystem;
-
-import java.io.*;
-
-import junit.framework.*;
-
-import org.apache.poi.poifs.property.DirectoryProperty;
-import org.apache.poi.poifs.property.DocumentProperty;
-import org.apache.poi.poifs.storage.RawDataBlock;
-
-/**
- * Class to test DocumentNode functionality
- *
- * @author Marc Johnson
- */
-public final class TestDocumentNode extends TestCase {
-
- /**
- * test constructor
- */
- public void testConstructor() throws IOException {
- DirectoryProperty property1 = new DirectoryProperty("directory");
- RawDataBlock[] rawBlocks = new RawDataBlock[ 4 ];
- ByteArrayInputStream stream =
- new ByteArrayInputStream(new byte[ 2048 ]);
-
- for (int j = 0; j < 4; j++)
- {
- rawBlocks[ j ] = new RawDataBlock(stream);
- }
- OPOIFSDocument document = new OPOIFSDocument("document", rawBlocks,
- 2000);
- DocumentProperty property2 = document.getDocumentProperty();
- DirectoryNode parent = new DirectoryNode(property1, (POIFSFileSystem)null, null);
- DocumentNode node = new DocumentNode(property2, parent);
-
- // verify we can retrieve the document
- assertEquals(property2.getDocument(), node.getDocument());
-
- // verify we can get the size
- assertEquals(property2.getSize(), node.getSize());
-
- // verify isDocumentEntry returns true
- assertTrue(node.isDocumentEntry());
-
- // verify isDirectoryEntry returns false
- assertTrue(!node.isDirectoryEntry());
-
- // verify getName behaves correctly
- assertEquals(property2.getName(), node.getName());
-
- // verify getParent behaves correctly
- assertEquals(parent, node.getParent());
- }
-}
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java b/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java
index ebf7fb1d09..3dda0dead8 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java
@@ -17,6 +17,9 @@
package org.apache.poi.poifs.filesystem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@@ -26,20 +29,21 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import junit.framework.TestCase;
-
import org.apache.poi.POIDataSamples;
+import org.junit.After;
+import org.junit.Test;
/**
* Tests bugs across both POIFSFileSystem and NPOIFSFileSystem
*/
-public final class TestFileSystemBugs extends TestCase {
- protected static POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
- protected static POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
-
- protected List openedFSs;
- @Override
- protected void tearDown() throws Exception {
+public final class TestFileSystemBugs {
+ private static POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
+ private static POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
+
+ private List openedFSs;
+
+ @After
+ public void tearDown() {
if (openedFSs != null && !openedFSs.isEmpty()) {
for (NPOIFSFileSystem fs : openedFSs) {
try {
@@ -51,65 +55,60 @@ public final class TestFileSystemBugs extends TestCase {
}
openedFSs = null;
}
- protected DirectoryNode[] openSample(String name, boolean oldFails) throws Exception {
- return openSamples(new InputStream[] {
- _samples.openResourceAsStream(name),
- _samples.openResourceAsStream(name)
- }, oldFails);
+
+ private DirectoryNode openSample(String name) throws Exception {
+ try (InputStream inps = _samples.openResourceAsStream(name)) {
+ return openSample(inps);
+ }
}
- protected DirectoryNode[] openSSSample(String name, boolean oldFails) throws Exception {
- return openSamples(new InputStream[] {
- _ssSamples.openResourceAsStream(name),
- _ssSamples.openResourceAsStream(name)
- }, oldFails);
+
+ @SuppressWarnings("SameParameterValue")
+ private DirectoryNode openSSSample(String name) throws Exception {
+ try (InputStream inps = _ssSamples.openResourceAsStream(name)) {
+ return openSample(inps);
+ }
}
- protected DirectoryNode[] openSamples(InputStream[] inps, boolean oldFails) throws Exception {
- NPOIFSFileSystem nfs = new NPOIFSFileSystem(inps[0]);
- if (openedFSs == null) openedFSs = new ArrayList<>();
- openedFSs.add(nfs);
-
- OPOIFSFileSystem ofs = null;
- try {
- ofs = new OPOIFSFileSystem(inps[1]);
- if (oldFails) fail("POIFSFileSystem should have failed but didn't");
- } catch (Exception e) {
- if (!oldFails) throw e;
+
+ private DirectoryNode openSample(InputStream inps) throws Exception {
+ NPOIFSFileSystem nfs = new NPOIFSFileSystem(inps);
+ if (openedFSs == null) {
+ openedFSs = new ArrayList<>();
}
+ openedFSs.add(nfs);
- if (ofs == null) return new DirectoryNode[] { nfs.getRoot() };
- return new DirectoryNode[] { ofs.getRoot(), nfs.getRoot() };
+ return nfs.getRoot();
}
/**
* Test that we can open files that come via Lotus notes.
* These have a top level directory without a name....
*/
+ @Test
public void testNotesOLE2Files() throws Exception {
// Check the contents
- for (DirectoryNode root : openSample("Notes.ole2", false)) {
- assertEquals(1, root.getEntryCount());
+ DirectoryNode root = openSample("Notes.ole2");
+ assertEquals(1, root.getEntryCount());
- Entry entry = root.getEntries().next();
- assertTrue(entry.isDirectoryEntry());
- assertTrue(entry instanceof DirectoryEntry);
+ Entry entry = root.getEntries().next();
+ assertTrue(entry.isDirectoryEntry());
+ assertTrue(entry instanceof DirectoryEntry);
- // The directory lacks a name!
- DirectoryEntry dir = (DirectoryEntry)entry;
- assertEquals("", dir.getName());
+ // The directory lacks a name!
+ DirectoryEntry dir = (DirectoryEntry)entry;
+ assertEquals("", dir.getName());
- // Has two children
- assertEquals(2, dir.getEntryCount());
+ // Has two children
+ assertEquals(2, dir.getEntryCount());
- // Check them
- Iterator it = dir.getEntries();
- entry = it.next();
- assertEquals(true, entry.isDocumentEntry());
- assertEquals(Ole10Native.OLE10_NATIVE, entry.getName());
+ // Check them
+ Iterator it = dir.getEntries();
+ entry = it.next();
+ assertTrue(entry.isDocumentEntry());
+ assertEquals(Ole10Native.OLE10_NATIVE, entry.getName());
- entry = it.next();
- assertEquals(true, entry.isDocumentEntry());
- assertEquals("\u0001CompObj", entry.getName());
- }
+ entry = it.next();
+ assertTrue(entry.isDocumentEntry());
+ assertEquals("\u0001CompObj", entry.getName());
}
/**
@@ -119,46 +118,39 @@ public final class TestFileSystemBugs extends TestCase {
* Note - only works for NPOIFSFileSystem, POIFSFileSystem
* can't cope with this level of corruption
*/
+ @Test
public void testCorruptedProperties() throws Exception {
- for (DirectoryNode root : openSample("unknown_properties.msg", true)) {
- assertEquals(42, root.getEntryCount());
- }
+ DirectoryNode root = openSample("unknown_properties.msg");
+ assertEquals(42, root.getEntryCount());
}
/**
* With heavily nested documents, ensure we still re-write the same
*/
+ @Test
public void testHeavilyNestedReWrite() throws Exception {
- for (DirectoryNode root : openSSSample("ex42570-20305.xls", false)) {
- // Record the structure
- Map entries = new HashMap<>();
- fetchSizes("/", root, entries);
-
- // Prepare to copy
- DirectoryNode dest;
- if (root.getNFileSystem() != null) {
- dest = (new NPOIFSFileSystem()).getRoot();
- } else {
- dest = (new OPOIFSFileSystem()).getRoot();
- }
-
- // Copy over
- EntryUtils.copyNodes(root, dest);
-
- // Re-load, always as NPOIFS
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- if (root.getNFileSystem() != null) {
- root.getNFileSystem().writeFilesystem(baos);
- } else {
- root.getOFileSystem().writeFilesystem(baos);
- }
- NPOIFSFileSystem read = new NPOIFSFileSystem(
- new ByteArrayInputStream(baos.toByteArray()));
-
- // Check the structure matches
- checkSizes("/", read.getRoot(), entries);
- }
+ DirectoryNode root = openSSSample("ex42570-20305.xls");
+ // Record the structure
+ Map entries = new HashMap<>();
+ fetchSizes("/", root, entries);
+
+ // Prepare to copy
+ DirectoryNode dest = new NPOIFSFileSystem().getRoot();
+
+ // Copy over
+ EntryUtils.copyNodes(root, dest);
+
+ // Re-load, always as NPOIFS
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ root.getNFileSystem().writeFilesystem(baos);
+
+ NPOIFSFileSystem read = new NPOIFSFileSystem(
+ new ByteArrayInputStream(baos.toByteArray()));
+
+ // Check the structure matches
+ checkSizes("/", read.getRoot(), entries);
}
+
private void fetchSizes(String path, DirectoryNode dir, Map entries) {
for (Entry entry : dir) {
if (entry instanceof DirectoryNode) {
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
index 1a199cb54c..3bb90d0989 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
@@ -17,6 +17,26 @@
package org.apache.poi.poifs.filesystem;
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.PropertySet;
@@ -34,14 +54,6 @@ import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
-import java.io.*;
-import java.nio.ByteBuffer;
-import java.util.Iterator;
-
-import static org.hamcrest.core.IsCollectionContaining.hasItem;
-import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.*;
-
/**
* Tests for the new NIO POIFSFileSystem implementation
*/
@@ -52,7 +64,7 @@ public final class TestNPOIFSFileSystem {
* Returns test files with 512 byte and 4k block sizes, loaded
* both from InputStreams and Files
*/
- protected NPOIFSFileSystem[] get512and4kFileAndInput() throws IOException {
+ private NPOIFSFileSystem[] get512and4kFileAndInput() throws IOException {
NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
@@ -60,7 +72,7 @@ public final class TestNPOIFSFileSystem {
return new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD};
}
- protected static void assertBATCount(NPOIFSFileSystem fs, int expectedBAT, int expectedXBAT) throws IOException {
+ private static void assertBATCount(NPOIFSFileSystem fs, int expectedBAT, int expectedXBAT) throws IOException {
int foundBAT = 0;
int foundXBAT = 0;
int sz = (int)(fs.size() / fs.getBigBlockSize());
@@ -75,7 +87,7 @@ public final class TestNPOIFSFileSystem {
assertEquals("Wrong number of BATs", expectedBAT, foundBAT);
assertEquals("Wrong number of XBATs with " + expectedBAT + " BATs", expectedXBAT, foundXBAT);
}
- protected void assertContentsMatches(byte[] expected, DocumentEntry doc) throws IOException {
+ private void assertContentsMatches(byte[] expected, DocumentEntry doc) throws IOException {
NDocumentInputStream inp = new NDocumentInputStream(doc);
byte[] contents = new byte[doc.getSize()];
assertEquals(doc.getSize(), inp.read(contents));
@@ -85,21 +97,21 @@ public final class TestNPOIFSFileSystem {
assertThat(expected, equalTo(contents));
}
}
-
- protected static HeaderBlock writeOutAndReadHeader(NPOIFSFileSystem fs) throws IOException {
+
+ private static HeaderBlock writeOutAndReadHeader(NPOIFSFileSystem fs) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fs.writeFilesystem(baos);
return new HeaderBlock(new ByteArrayInputStream(baos.toByteArray()));
}
- protected static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException {
+ static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
original.writeFilesystem(baos);
return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
}
- protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException {
+ private static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException {
final File file = TempFile.createTempFile("TestPOIFS", ".ole2");
try (OutputStream fout = new FileOutputStream(file)) {
original.writeFilesystem(fout);
@@ -179,7 +191,7 @@ public final class TestNPOIFSFileSystem {
assertEquals("Image", prop.getName());
prop = pi.next();
assertEquals("Tags", prop.getName());
- assertEquals(false, pi.hasNext());
+ assertFalse(pi.hasNext());
// Check the SBAT (Small Blocks FAT) was properly processed
@@ -250,7 +262,7 @@ public final class TestNPOIFSFileSystem {
assertEquals("Image", prop.getName());
prop = pi.next();
assertEquals("Tags", prop.getName());
- assertEquals(false, pi.hasNext());
+ assertFalse(pi.hasNext());
// Check the SBAT (Small Blocks FAT) was properly processed
@@ -422,7 +434,7 @@ public final class TestNPOIFSFileSystem {
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
// Our first BAT block has spares
- assertEquals(true, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+ assertTrue(fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
// First free one is 100
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(100));
@@ -463,7 +475,7 @@ public final class TestNPOIFSFileSystem {
}
// Check our BAT knows it's free
- assertEquals(true, fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+ assertTrue(fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
// Allocate all the spare ones
for(int i=100; i<128; i++) {
@@ -471,9 +483,9 @@ public final class TestNPOIFSFileSystem {
}
// BAT is now full, but there's only the one
- assertEquals(false, fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
fail("Should only be one BAT");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -483,9 +495,9 @@ public final class TestNPOIFSFileSystem {
// Now ask for a free one, will need to extend the file
assertEquals(129, fs1.getFreeBlock());
-
- assertEquals(false, fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
- assertEquals(true, fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+
+ assertFalse(fs1.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+ assertTrue(fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs1.getNextBlock(128));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs1.getNextBlock(129));
@@ -502,10 +514,10 @@ public final class TestNPOIFSFileSystem {
fs1.setNextBlock(free, POIFSConstants.END_OF_CHAIN);
}
}
-
- assertEquals(false, fs1.getBATBlockAndIndex(109*128-1).getBlock().hasFreeSectors());
+
+ assertFalse(fs1.getBATBlockAndIndex(109 * 128 - 1).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs1.getBATBlockAndIndex(109*128).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(109 * 128).getBlock().hasFreeSectors());
fail("Should only be 109 BATs");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -525,10 +537,10 @@ public final class TestNPOIFSFileSystem {
free = fs1.getFreeBlock();
assertTrue("Had: " + free, free > 0);
- assertEquals(false, fs1.getBATBlockAndIndex(109*128-1).getBlock().hasFreeSectors());
- assertEquals(true, fs1.getBATBlockAndIndex(110*128-1).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(109 * 128 - 1).getBlock().hasFreeSectors());
+ assertTrue(fs1.getBATBlockAndIndex(110 * 128 - 1).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs1.getBATBlockAndIndex(110*128).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(110 * 128).getBlock().hasFreeSectors());
fail("Should only be 110 BATs");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -552,9 +564,9 @@ public final class TestNPOIFSFileSystem {
}
// Should now have 109+127 = 236 BATs
- assertEquals(false, fs1.getBATBlockAndIndex(236*128-1).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(236 * 128 - 1).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs1.getBATBlockAndIndex(236*128).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(236 * 128).getBlock().hasFreeSectors());
fail("Should only be 236 BATs");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -566,10 +578,10 @@ public final class TestNPOIFSFileSystem {
free = fs1.getFreeBlock();
assertTrue("Had: " + free, free > 0);
- assertEquals(false, fs1.getBATBlockAndIndex(236*128-1).getBlock().hasFreeSectors());
- assertEquals(true, fs1.getBATBlockAndIndex(237*128-1).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(236 * 128 - 1).getBlock().hasFreeSectors());
+ assertTrue(fs1.getBATBlockAndIndex(237 * 128 - 1).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs1.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors());
+ assertFalse(fs1.getBATBlockAndIndex(237 * 128).getBlock().hasFreeSectors());
fail("Should only be 237 BATs");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -590,10 +602,10 @@ public final class TestNPOIFSFileSystem {
// Check that it is seen correctly
assertBATCount(fs2, 237, 2);
- assertEquals(false, fs2.getBATBlockAndIndex(236*128-1).getBlock().hasFreeSectors());
- assertEquals(true, fs2.getBATBlockAndIndex(237*128-1).getBlock().hasFreeSectors());
+ assertFalse(fs2.getBATBlockAndIndex(236 * 128 - 1).getBlock().hasFreeSectors());
+ assertTrue(fs2.getBATBlockAndIndex(237 * 128 - 1).getBlock().hasFreeSectors());
try {
- assertEquals(false, fs2.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors());
+ assertFalse(fs2.getBATBlockAndIndex(237 * 128).getBlock().hasFreeSectors());
fail("Should only be 237 BATs");
} catch(IndexOutOfBoundsException e) {
// expected here
@@ -620,12 +632,12 @@ public final class TestNPOIFSFileSystem {
Entry si = root.getEntry("\u0005SummaryInformation");
Entry image = root.getEntry("Image");
Entry tags = root.getEntry("Tags");
-
- assertEquals(false, thumbnail.isDirectoryEntry());
- assertEquals(false, dsi.isDirectoryEntry());
- assertEquals(false, si.isDirectoryEntry());
- assertEquals(true, image.isDirectoryEntry());
- assertEquals(false, tags.isDirectoryEntry());
+
+ assertFalse(thumbnail.isDirectoryEntry());
+ assertFalse(dsi.isDirectoryEntry());
+ assertFalse(si.isDirectoryEntry());
+ assertTrue(image.isDirectoryEntry());
+ assertFalse(tags.isDirectoryEntry());
// Check via the iterator
Iterator it = root.getEntries();
@@ -652,8 +664,8 @@ public final class TestNPOIFSFileSystem {
for(NPOIFSFileSystem fs : get512and4kFileAndInput()) {
DirectoryEntry root = fs.getRoot();
Entry si = root.getEntry("\u0005SummaryInformation");
-
- assertEquals(true, si.isDocumentEntry());
+
+ assertTrue(si.isDocumentEntry());
DocumentNode doc = (DocumentNode)si;
// Check we can read it
@@ -665,9 +677,9 @@ public final class TestNPOIFSFileSystem {
SummaryInformation inf = (SummaryInformation)ps;
// Check some bits in it
- assertEquals(null, inf.getApplicationName());
- assertEquals(null, inf.getAuthor());
- assertEquals(null, inf.getSubject());
+ assertNull(inf.getApplicationName());
+ assertNull(inf.getAuthor());
+ assertNull(inf.getSubject());
assertEquals(131333, inf.getOSVersion());
// Finish with this one
@@ -676,7 +688,7 @@ public final class TestNPOIFSFileSystem {
// Try the other summary information
si = root.getEntry("\u0005DocumentSummaryInformation");
- assertEquals(true, si.isDocumentEntry());
+ assertTrue(si.isDocumentEntry());
doc = (DocumentNode)si;
assertContentsMatches(null, doc);
@@ -1541,7 +1553,7 @@ public final class TestNPOIFSFileSystem {
DirectoryEntry vbaProj = (DirectoryEntry)src.getRoot().getEntry("_VBA_PROJECT_CUR");
assertEquals(3, vbaProj.getEntryCount());
// Can't delete yet, has stuff
- assertEquals(false, vbaProj.delete());
+ assertFalse(vbaProj.delete());
// Recursively delete
_recursiveDeletee(vbaProj);
@@ -1554,7 +1566,7 @@ public final class TestNPOIFSFileSystem {
}
private void _recursiveDeletee(Entry entry) throws IOException {
if (entry.isDocumentEntry()) {
- assertEquals(true, entry.delete());
+ assertTrue(entry.delete());
return;
}
@@ -1564,7 +1576,7 @@ public final class TestNPOIFSFileSystem {
Entry ce = dir.getEntry(name);
_recursiveDeletee(ce);
}
- assertEquals(true, dir.delete());
+ assertTrue(dir.delete());
}
@SuppressWarnings("unused")
private int _countChildren(DirectoryProperty p) {
@@ -1677,24 +1689,24 @@ public final class TestNPOIFSFileSystem {
fs.createDocument(new DummyDataInputStream(s2gb), "Big");
}
- protected static class DummyDataInputStream extends InputStream {
- protected final long maxSize;
- protected long size;
- public DummyDataInputStream(long maxSize) {
+ private static final class DummyDataInputStream extends InputStream {
+ private final long maxSize;
+ private long size;
+ private DummyDataInputStream(long maxSize) {
this.maxSize = maxSize;
this.size = 0;
}
- public int read() throws IOException {
+ public int read() {
if (size >= maxSize) return -1;
size++;
return (int)(size % 128);
}
- public int read(byte[] b) throws IOException {
+ public int read(byte[] b) {
return read(b, 0, b.length);
}
- public int read(byte[] b, int offset, int len) throws IOException {
+ public int read(byte[] b, int offset, int len) {
if (size >= maxSize) return -1;
int sz = (int)Math.min(len, maxSize-size);
for (int i=0; i 0) System.out.println();
}
}
@@ -1754,7 +1765,6 @@ public final class TestNPOIFSFileSystem {
assertTrue(file.delete());
if (i % 10 == 0) System.out.print(".");
- if (i % 800 == 0 && i > 0) System.out.println();
}
}
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
index d1ae24851c..3c3f6b83aa 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
@@ -17,14 +17,16 @@
package org.apache.poi.poifs.filesystem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
-import junit.framework.TestCase;
-
import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.common.POIFSBigBlockSize;
@@ -34,18 +36,21 @@ import org.apache.poi.poifs.storage.BlockAllocationTableReader;
import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.poifs.storage.RawDataBlockList;
import org.apache.poi.util.IOUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
/**
* Tests for the older OPOIFS-based POIFSFileSystem
*/
-public final class TestPOIFSFileSystem extends TestCase {
+public final class TestPOIFSFileSystem {
private final POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
/**
* Mock exception used to ensure correct error handling
*/
private static final class MyEx extends RuntimeException {
- public MyEx() {
+ MyEx() {
// no fields to initialise
}
}
@@ -60,7 +65,7 @@ public final class TestPOIFSFileSystem extends TestCase {
private int _currentIx;
private boolean _isClosed;
- public TestIS(InputStream is, int failIndex) {
+ TestIS(InputStream is, int failIndex) {
_is = is;
_failIndex = failIndex;
_currentIx = 0;
@@ -93,7 +98,7 @@ public final class TestPOIFSFileSystem extends TestCase {
_isClosed = true;
_is.close();
}
- public boolean isClosed() {
+ boolean isClosed() {
return _isClosed;
}
}
@@ -102,29 +107,26 @@ public final class TestPOIFSFileSystem extends TestCase {
* Test for undesired behaviour observable as of svn revision 618865 (5-Feb-2008).
* POIFSFileSystem was not closing the input stream.
*/
- public void testAlwaysClose() {
+ @Test
+ public void testAlwaysClose() throws IOException {
TestIS testIS;
// Normal case - read until EOF and close
testIS = new TestIS(openSampleStream("13224.xls"), -1);
- try {
- new OPOIFSFileSystem(testIS);
- } catch (IOException e) {
- throw new RuntimeException(e);
+ try (NPOIFSFileSystem ignored = new NPOIFSFileSystem(testIS)){
+ assertTrue("input stream was not closed", testIS.isClosed());
}
- assertTrue("input stream was not closed", testIS.isClosed());
// intended to crash after reading 10000 bytes
testIS = new TestIS(openSampleStream("13224.xls"), 10000);
- try {
- new OPOIFSFileSystem(testIS);
+ try (NPOIFSFileSystem ignored = new NPOIFSFileSystem(testIS)){
fail("ex should have been thrown");
- } catch (IOException e) {
- throw new RuntimeException(e);
} catch (MyEx e) {
// expected
+ assertTrue("input stream was not closed", testIS.isClosed()); // but still should close
+ } catch (Exception e) {
+ fail("MyEx is expected to be thrown");
}
- assertTrue("input stream was not closed", testIS.isClosed()); // but still should close
}
/**
@@ -138,6 +140,7 @@ public final class TestPOIFSFileSystem extends TestCase {
* The other is to fix the handling of the last block in
* POIFS, since it seems to be slight wrong
*/
+ @Test
public void testShortLastBlock() throws Exception {
String[] files = new String[] {
"ShortLastBlock.qwp", "ShortLastBlock.wps"
@@ -145,7 +148,7 @@ public final class TestPOIFSFileSystem extends TestCase {
for (String file : files) {
// Open the file up
- OPOIFSFileSystem fs = new OPOIFSFileSystem(
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(
_samples.openResourceAsStream(file)
);
@@ -156,26 +159,24 @@ public final class TestPOIFSFileSystem extends TestCase {
// Check sizes
}
}
-
+
+ @Rule
+ public ExpectedException expectedEx = ExpectedException.none();
+
/**
* Check that we do the right thing when the list of which
* sectors are BAT blocks points off the list of
* sectors that exist in the file.
*/
+ @Test
public void testFATandDIFATsectors() throws Exception {
// Open the file up
- try {
- InputStream stream = _samples.openResourceAsStream("ReferencesInvalidSectors.mpp");
- try {
- new OPOIFSFileSystem(stream);
- fail("File is corrupt and shouldn't have been opened");
- } finally {
- stream.close();
- }
- } catch (IOException e) {
- String msg = e.getMessage();
- assertTrue(msg.startsWith("Your file contains 695 sectors"));
- }
+ expectedEx.expect(IndexOutOfBoundsException.class);
+ expectedEx.expectMessage("Block 1148 not found");
+ try (InputStream stream = _samples.openResourceAsStream("ReferencesInvalidSectors.mpp")) {
+ new NPOIFSFileSystem(stream);
+ fail("File is corrupt and shouldn't have been opened");
+ }
}
/**
@@ -184,9 +185,10 @@ public final class TestPOIFSFileSystem extends TestCase {
* However, because a file needs to be at least 6.875mb big
* to have an XBAT in it, we don't have a test one. So, generate it.
*/
+ @Test
public void testBATandXBAT() throws Exception {
byte[] hugeStream = new byte[8*1024*1024];
- OPOIFSFileSystem fs = new OPOIFSFileSystem();
+ NPOIFSFileSystem fs = new NPOIFSFileSystem();
fs.getRoot().createDocument(
"BIG", new ByteArrayInputStream(hugeStream)
);
@@ -229,8 +231,7 @@ public final class TestPOIFSFileSystem extends TestCase {
assertEquals(fsData.length / 512, blockList.blockCount() + 1); // Header not counted
// Now load it and check
- fs = null;
- fs = new OPOIFSFileSystem(
+ fs = new NPOIFSFileSystem(
new ByteArrayInputStream(fsData)
);
@@ -244,41 +245,39 @@ public final class TestPOIFSFileSystem extends TestCase {
* Most OLE2 files use 512byte blocks. However, a small number
* use 4k blocks. Check that we can open these.
*/
+ @Test
public void test4KBlocks() throws Exception {
POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
- InputStream inp = _samples.openResourceAsStream("BlockSize4096.zvi");
- try {
- // First up, check that we can process the header properly
- HeaderBlock header_block = new HeaderBlock(inp);
- POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
- assertEquals(4096, bigBlockSize.getBigBlockSize());
+ try (InputStream inp = _samples.openResourceAsStream("BlockSize4096.zvi")) {
+ // First up, check that we can process the header properly
+ HeaderBlock header_block = new HeaderBlock(inp);
+ POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
+ assertEquals(4096, bigBlockSize.getBigBlockSize());
- // Check the fat info looks sane
- assertEquals(1, header_block.getBATArray().length);
- assertEquals(1, header_block.getBATCount());
- assertEquals(0, header_block.getXBATCount());
+ // Check the fat info looks sane
+ assertEquals(1, header_block.getBATArray().length);
+ assertEquals(1, header_block.getBATCount());
+ assertEquals(0, header_block.getXBATCount());
- // Now check we can get the basic fat
- RawDataBlockList data_blocks = new RawDataBlockList(inp,
- bigBlockSize);
- assertEquals(15, data_blocks.blockCount());
+ // Now check we can get the basic fat
+ RawDataBlockList data_blocks = new RawDataBlockList(inp,
+ bigBlockSize);
+ assertEquals(15, data_blocks.blockCount());
- // Now try and open properly
- OPOIFSFileSystem fs = new OPOIFSFileSystem(
- _samples.openResourceAsStream("BlockSize4096.zvi"));
- assertTrue(fs.getRoot().getEntryCount() > 3);
+ // Now try and open properly
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(
+ _samples.openResourceAsStream("BlockSize4096.zvi"));
+ assertTrue(fs.getRoot().getEntryCount() > 3);
- // Check we can get at all the contents
- checkAllDirectoryContents(fs.getRoot());
+ // Check we can get at all the contents
+ checkAllDirectoryContents(fs.getRoot());
- // Finally, check we can do a similar 512byte one too
- fs = new OPOIFSFileSystem(
- _samples.openResourceAsStream("BlockSize512.zvi"));
- assertTrue(fs.getRoot().getEntryCount() > 3);
- checkAllDirectoryContents(fs.getRoot());
- } finally {
- inp.close();
- }
+ // Finally, check we can do a similar 512byte one too
+ fs = new NPOIFSFileSystem(
+ _samples.openResourceAsStream("BlockSize512.zvi"));
+ assertTrue(fs.getRoot().getEntryCount() > 3);
+ checkAllDirectoryContents(fs.getRoot());
+ }
}
private void checkAllDirectoryContents(DirectoryEntry dir) throws IOException {
for(Entry entry : dir) {
@@ -293,6 +292,7 @@ public final class TestPOIFSFileSystem extends TestCase {
}
}
+ @SuppressWarnings("SameParameterValue")
private static InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
diff --git a/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java b/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
index 582b3af78e..4189215720 100644
--- a/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
+++ b/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
@@ -20,7 +20,7 @@ package org.apache.poi.poifs.storage;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
- * Tests for org.apache.poi.poifs.storage
+ * Tests for org.apache.poi.poifs.storage
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
@@ -33,11 +33,7 @@ import org.junit.runners.Suite;
TestHeaderBlockWriting.class,
TestPropertyBlock.class,
TestRawDataBlock.class,
- TestRawDataBlockList.class,
- TestSmallBlockTableReader.class,
- TestSmallBlockTableWriter.class,
- TestSmallDocumentBlock.class,
- TestSmallDocumentBlockList.class
+ TestRawDataBlockList.class
})
public class AllPOIFSStorageTests {
}
diff --git a/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java b/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
index 6a249afc1a..233d2b0dd9 100644
--- a/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
+++ b/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
@@ -17,196 +17,58 @@
package org.apache.poi.poifs.storage;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
import org.apache.poi.poifs.common.POIFSBigBlockSize;
import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
+import org.junit.Test;
/**
* Class to test BlockAllocationTableReader functionality
- *
- * @author Marc Johnson
*/
-public final class TestBlockAllocationTableReader extends TestCase {
+public class TestBlockAllocationTableReader {
/**
* Test small block allocation table constructor
*/
+ @Test
public void testSmallBATConstructor() throws IOException {
// need to create an array of raw blocks containing the SBAT,
// and a small document block list
- String[] sbat_data = {
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF",
- "FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF",
- "FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF",
- "FE FF FF FF 22 00 00 00 FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF FE FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
- };
+ final String sbat_data = "H4sIAAAAAAAAAPv/nzjwj4ZYiYGBAZfcKKAtAAC/sexrAAIAAA==";
- RawDataBlock[] sbats = { new RawDataBlock(makeDataStream(sbat_data)) };
-
- String[] sbt_data = {
- "08 00 28 00 6A 61 6D 65 73 2D 55 37 37 32 37 39 32 2D 28 31 36 2D 4F 63 74 2D 32 30 30 31 40 31",
- "36 2D 34 31 2D 33 33 29 2E 5A 44 46 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "07 00 00 00 00 00 80 27 E2 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "07 00 00 00 00 00 80 27 E2 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "0B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "03 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 02 00 20 31 08 00 05 00 53 61 76 65 64 08 00 17 00 53 2E 48 55 53 53 41 49 4E 20 41 20 44",
- "45 56 20 4F 46 46 52 20 55 4B 08 00 0B 00 31 36 2D 4F 63 74 2D 32 30 30 31 08 00 05 00 35 2E 33",
- "2E 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 05 00 6A 61 6D 65 73 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 03 00 47 42 50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 1D 00 28 41 29 31 36 2D 4F 63 74 2D 32 30 30 31 20 74 6F 20 31 36 2D 4F 63 74 2D 32 30 30",
- "31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 01 00 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00",
- "02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00",
- "02 00 00 00 08 00 00 00 02 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 03 00 47 42 50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 17 00 53 2E 48 55 53 53 41 49 4E 20 41 20 44 45 56 20 4F 46 46 52 20 55 4B 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "02 00 00 00 02 00 00 00 02 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
- };
+ RawDataBlock[] sbats = { new RawDataBlock(new ByteArrayInputStream(RawDataUtil.decompress(sbat_data))) };
- RawDataBlock[] sbts = new RawDataBlock[7];
- InputStream sbt_input = makeDataStream(sbt_data);
+ final String sbt_data =
+ "H4sIAAAAAAAAAONg0GDISsxNLdYNNTc3Mrc00tUwNNP1Ty7RNTIwMHQAsk0MdY2NNfWiXNwYsAB2MNmg/sgBmyxhQB395AMm" +
+ "BkaK9HNQaD83hfqZKXY/E4OCIQcDK0NwYllqCgeDOEOwnkdocLCjp5+Co4KLa5iCv5tbkEKoNwfQrUhJA6TFVM9Yz4gy94OM" +
+ "Aac/svVTaj8zg7tTAAX6ZRk0HDWRAkahJF8BiUtQPyMDITX4ABMFegeDfsrjjzLAxCBBoX7KwED7n/LwG2j7KSv/Bt79A2s/" +
+ "NdzPQUWaVDDQ/h/o+meop5+hrx9ng4ku9jOhYVIBM4X2j4KhDQAtwD4rAA4AAA==";
- for (int j = 0; j < 7; j++) {
- sbts[j] = new RawDataBlock(sbt_input);
+ InputStream sbt_input = new ByteArrayInputStream(RawDataUtil.decompress(sbt_data));
+
+ BlockListImpl small_blocks = new RawDataBlockList(sbt_input, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+ int blockCount = small_blocks.blockCount();
+ ListManagedBlock[] lmb = new ListManagedBlock[7*blockCount];
+ for (int i=0; i documents = new ArrayList<>();
-
- documents.add(
- new OPOIFSDocument(
- "doc340", new ByteArrayInputStream(new byte[ 340 ])));
- documents.add(
- new OPOIFSDocument(
- "doc5000", new ByteArrayInputStream(new byte[ 5000 ])));
- documents
- .add(new OPOIFSDocument("doc0",
- new ByteArrayInputStream(new byte[ 0 ])));
- documents
- .add(new OPOIFSDocument("doc1",
- new ByteArrayInputStream(new byte[ 1 ])));
- documents
- .add(new OPOIFSDocument("doc2",
- new ByteArrayInputStream(new byte[ 2 ])));
- documents
- .add(new OPOIFSDocument("doc3",
- new ByteArrayInputStream(new byte[ 3 ])));
- documents
- .add(new OPOIFSDocument("doc4",
- new ByteArrayInputStream(new byte[ 4 ])));
- documents
- .add(new OPOIFSDocument("doc5",
- new ByteArrayInputStream(new byte[ 5 ])));
- documents
- .add(new OPOIFSDocument("doc6",
- new ByteArrayInputStream(new byte[ 6 ])));
- documents
- .add(new OPOIFSDocument("doc7",
- new ByteArrayInputStream(new byte[ 7 ])));
- documents
- .add(new OPOIFSDocument("doc8",
- new ByteArrayInputStream(new byte[ 8 ])));
- documents
- .add(new OPOIFSDocument("doc9",
- new ByteArrayInputStream(new byte[ 9 ])));
-
- HeaderBlock header = new HeaderBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
- RootProperty root = new PropertyTable(header).getRoot();
- SmallBlockTableWriter sbtw = new SmallBlockTableWriter(
- POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, documents,root);
- BlockAllocationTableWriter bat = sbtw.getSBAT();
-
- // 15 small blocks: 6 for doc340, 0 for doc5000 (too big), 0
- // for doc0 (no storage needed), 1 each for doc1 through doc9
- assertEquals(15 * 64, root.getSize());
-
- // 15 small blocks rounds up to 2 big blocks
- assertEquals(2, sbtw.countBlocks());
- int start_block = 1000 + root.getStartBlock();
-
- sbtw.setStartBlock(start_block);
- assertEquals(start_block, root.getStartBlock());
- }
-}
diff --git a/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlock.java b/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlock.java
deleted file mode 100644
index c637e79706..0000000000
--- a/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlock.java
+++ /dev/null
@@ -1,223 +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.
-==================================================================== */
-
-package org.apache.poi.poifs.storage;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.apache.poi.poifs.common.POIFSConstants;
-
-/**
- * Class to test SmallDocumentBlock functionality
- *
- * @author Marc Johnson
- */
-public final class TestSmallDocumentBlock extends TestCase {
- static final private byte[] _testdata;
- static final private int _testdata_size = 2999;
-
- static
- {
- _testdata = new byte[ _testdata_size ];
- for (int j = 0; j < _testdata.length; j++)
- {
- _testdata[ j ] = ( byte ) j;
- }
- }
-
- /**
- * Test conversion from DocumentBlocks
- */
- public void testConvert1()
- throws IOException
- {
- ByteArrayInputStream stream = new ByteArrayInputStream(_testdata);
- List documents = new ArrayList<>();
-
- while (true)
- {
- DocumentBlock block = new DocumentBlock(stream,POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
-
- documents.add(block);
- if (block.partiallyRead())
- {
- break;
- }
- }
- SmallDocumentBlock[] results =
- SmallDocumentBlock.convert(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS,
- documents.toArray(new DocumentBlock[ 0 ]), _testdata_size);
-
- assertEquals("checking correct result size: ",
- (_testdata_size + 63) / 64, results.length);
- ByteArrayOutputStream output = new ByteArrayOutputStream();
-
- for (SmallDocumentBlock result : results) {
- result.writeBlocks(output);
- }
- byte[] output_array = output.toByteArray();
-
- assertEquals("checking correct output size: ", 64 * results.length,
- output_array.length);
- int index = 0;
-
- for (; index < _testdata_size; index++)
- {
- assertEquals("checking output " + index, _testdata[ index ],
- output_array[ index ]);
- }
- for (; index < output_array.length; index++)
- {
- assertEquals("checking output " + index, ( byte ) 0xff,
- output_array[ index ]);
- }
- }
-
- /**
- * Test conversion from byte array
- */
- public void testConvert2()
- throws IOException
- {
- for (int j = 0; j < 320; j++)
- {
- byte[] array = new byte[ j ];
-
- for (int k = 0; k < j; k++)
- {
- array[ k ] = ( byte ) k;
- }
- SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(
- POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, array, 319);
-
- assertEquals(5, blocks.length);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
-
- for (SmallDocumentBlock block : blocks) {
- block.writeBlocks(stream);
- }
- stream.close();
- byte[] output = stream.toByteArray();
-
- for (int k = 0; k < array.length; k++)
- {
- assertEquals(String.valueOf(k), array[ k ], output[ k ]);
- }
- for (int k = array.length; k < 320; k++)
- {
- assertEquals(String.valueOf(k), ( byte ) 0xFF, output[ k ]);
- }
- }
- }
-
- /**
- * test fill
- */
- public void testFill()
- throws IOException
- {
- for (int j = 0; j <= 8; j++)
- {
- List blocks = new ArrayList<>();
-
- for (int k = 0; k < j; k++)
- {
- blocks.add(new SmallDocumentBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS));
- }
- int result = SmallDocumentBlock.fill(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blocks);
-
- assertEquals("correct big block count: ", (j + 7) / 8, result);
- assertEquals("correct small block count: ", 8 * result,
- blocks.size());
- for (int m = j; m < blocks.size(); m++)
- {
- BlockWritable block = blocks.get(m);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
-
- block.writeBlocks(stream);
- byte[] output = stream.toByteArray();
-
- assertEquals("correct output size (block[ " + m + " ]): ",
- 64, output.length);
- for (int n = 0; n < 64; n++)
- {
- assertEquals("correct value (block[ " + m + " ][ " + n
- + " ]): ", ( byte ) 0xff, output[ n ]);
- }
- }
- }
- }
-
- /**
- * test calcSize
- */
-
- public void testCalcSize()
- {
- for (int j = 0; j < 10; j++)
- {
- assertEquals("testing " + j, j * 64,
- SmallDocumentBlock.calcSize(j));
- }
- }
-
- /**
- * test extract method
- *
- * @exception IOException
- */
-
- public void testExtract()
- throws IOException
- {
- byte[] data = new byte[ 512 ];
- int offset = 0;
-
- for (int j = 0; j < 8; j++)
- {
- for (int k = 0; k < 64; k++)
- {
- data[ offset++ ] = ( byte ) (k + j);
- }
- }
- RawDataBlock[] blocks =
- {
- new RawDataBlock(new ByteArrayInputStream(data))
- };
- List output = SmallDocumentBlock.extract(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS,blocks);
-
- offset = 0;
- for (SmallDocumentBlock block : output)
- {
- byte[] out_data = block.getData();
-
- assertEquals("testing block at offset " + offset, 64,
- out_data.length);
- for (byte b : out_data) {
- assertEquals("testing byte at offset " + offset,
- data[ offset ], b);
- offset++;
- }
- }
- }
-}
diff --git a/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlockList.java b/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlockList.java
deleted file mode 100644
index 5301214e91..0000000000
--- a/src/testcases/org/apache/poi/poifs/storage/TestSmallDocumentBlockList.java
+++ /dev/null
@@ -1,67 +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.
-==================================================================== */
-
-package org.apache.poi.poifs.storage;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-
-import org.apache.poi.poifs.common.POIFSConstants;
-
-import junit.framework.TestCase;
-
-/**
- * Class to test SmallDocumentBlockList functionality
- *
- * @author Marc Johnson
- */
-public final class TestSmallDocumentBlockList extends TestCase {
-
- public void testConstructor() throws IOException {
- byte[] data = new byte[ 2560 ];
-
- for (int j = 0; j < 2560; j++)
- {
- data[ j ] = ( byte ) j;
- }
- ByteArrayInputStream stream = new ByteArrayInputStream(data);
- RawDataBlock[] blocks = new RawDataBlock[ 5 ];
-
- for (int j = 0; j < 5; j++)
- {
- blocks[ j ] = new RawDataBlock(stream);
- }
- SmallDocumentBlockList sdbl =
- new SmallDocumentBlockList(SmallDocumentBlock.extract(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS,blocks));
-
- // proof we added the blocks
- for (int j = 0; j < 40; j++)
- {
- sdbl.remove(j);
- }
- try
- {
- sdbl.remove(41);
- fail("there should have been an Earth-shattering ka-boom!");
- }
- catch (IOException ignored)
- {
-
- // it better have thrown one!!
- }
- }
-}
--
2.39.5