You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ki...@apache.org on 2018/08/26 11:55:02 UTC
svn commit: r1839201 [4/5] - in /poi: site/src/documentation/content/xdocs/
trunk/src/examples/src/org/apache/poi/hpsf/examples/
trunk/src/examples/src/org/apache/poi/poifs/poibrowser/
trunk/src/java/org/apache/poi/ trunk/src/java/org/apache/poi/extrac...
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java?rev=1839201&r1=1839200&r2=1839201&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java Sun Aug 26 11:55:00 2018
@@ -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;
-
- // non-even division of _workbook_size, also non-even division of
- // any block size
- private static final int _buffer_size = 6;
+ 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;
- @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);
- }
-
- // 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));
-
+ _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);
+ }
+
// 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 TestDocumentInputStre
*/
@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")
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java?rev=1839201&r1=1839200&r2=1839201&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestFileSystemBugs.java Sun Aug 26 11:55:00 2018
@@ -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<NPOIFSFileSystem> 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<NPOIFSFileSystem> openedFSs;
+
+ @After
+ public void tearDown() {
if (openedFSs != null && !openedFSs.isEmpty()) {
for (NPOIFSFileSystem fs : openedFSs) {
try {
@@ -51,65 +55,60 @@ public final class TestFileSystemBugs ex
}
openedFSs = null;
}
- protected DirectoryNode[] openSample(String name, boolean oldFails) throws Exception {
- return openSamples(new InputStream[] {
- _samples.openResourceAsStream(name),
- _samples.openResourceAsStream(name)
- }, oldFails);
- }
- protected DirectoryNode[] openSSSample(String name, boolean oldFails) throws Exception {
- return openSamples(new InputStream[] {
- _ssSamples.openResourceAsStream(name),
- _ssSamples.openResourceAsStream(name)
- }, oldFails);
- }
- 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(String name) throws Exception {
+ try (InputStream inps = _samples.openResourceAsStream(name)) {
+ return openSample(inps);
+ }
+ }
+
+ @SuppressWarnings("SameParameterValue")
+ private DirectoryNode openSSSample(String name) throws Exception {
+ try (InputStream inps = _ssSamples.openResourceAsStream(name)) {
+ return openSample(inps);
+ }
+ }
+
+ 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);
-
- // The directory lacks a name!
- DirectoryEntry dir = (DirectoryEntry)entry;
- assertEquals("", dir.getName());
-
- // Has two children
- assertEquals(2, dir.getEntryCount());
-
- // Check them
- Iterator<Entry> it = dir.getEntries();
- entry = it.next();
- assertEquals(true, entry.isDocumentEntry());
- assertEquals(Ole10Native.OLE10_NATIVE, entry.getName());
-
- entry = it.next();
- assertEquals(true, entry.isDocumentEntry());
- assertEquals("\u0001CompObj", entry.getName());
- }
+ Entry entry = root.getEntries().next();
+ assertTrue(entry.isDirectoryEntry());
+ assertTrue(entry instanceof DirectoryEntry);
+
+ // The directory lacks a name!
+ DirectoryEntry dir = (DirectoryEntry)entry;
+ assertEquals("", dir.getName());
+
+ // Has two children
+ assertEquals(2, dir.getEntryCount());
+
+ // Check them
+ Iterator<Entry> it = dir.getEntries();
+ entry = it.next();
+ assertTrue(entry.isDocumentEntry());
+ assertEquals(Ole10Native.OLE10_NATIVE, entry.getName());
+
+ entry = it.next();
+ assertTrue(entry.isDocumentEntry());
+ assertEquals("\u0001CompObj", entry.getName());
}
/**
@@ -119,46 +118,39 @@ public final class TestFileSystemBugs ex
* 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<String,Integer> 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<String,Integer> 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<String,Integer> entries) {
for (Entry entry : dir) {
if (entry instanceof DirectoryNode) {
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java?rev=1839201&r1=1839200&r2=1839201&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java Sun Aug 26 11:55:00 2018
@@ -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<Entry> 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<sz; i++) {
@@ -1715,8 +1727,8 @@ public final class TestNPOIFSFileSystem
for (int i = 0; i < iterations; i++) {
try (InputStream inputStream = POIDataSamples.getHSMFInstance().openResourceAsStream("lots-of-recipients.msg")) {
- OPOIFSFileSystem srcFileSystem = new OPOIFSFileSystem(inputStream);
- OPOIFSFileSystem destFileSystem = new OPOIFSFileSystem();
+ NPOIFSFileSystem srcFileSystem = new NPOIFSFileSystem(inputStream);
+ NPOIFSFileSystem destFileSystem = new NPOIFSFileSystem();
copyAllEntries(srcFileSystem.getRoot(), destFileSystem.getRoot());
@@ -1727,7 +1739,6 @@ public final class TestNPOIFSFileSystem
assertTrue(file.delete());
if (i % 10 == 0) System.out.print(".");
- if (i % 800 == 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();
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java?rev=1839201&r1=1839200&r2=1839201&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java Sun Aug 26 11:55:00 2018
@@ -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.Bloc
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 e
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 e
_isClosed = true;
_is.close();
}
- public boolean isClosed() {
+ boolean isClosed() {
return _isClosed;
}
}
@@ -102,29 +107,26 @@ public final class TestPOIFSFileSystem e
* 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 e
* 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 e
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 e
// 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 e
* 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 e
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 e
* 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());
-
- // 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 try and open properly
- OPOIFSFileSystem fs = new OPOIFSFileSystem(
- _samples.openResourceAsStream("BlockSize4096.zvi"));
- assertTrue(fs.getRoot().getEntryCount() > 3);
-
- // 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();
- }
+ 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());
+
+ // 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
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(
+ _samples.openResourceAsStream("BlockSize4096.zvi"));
+ assertTrue(fs.getRoot().getEntryCount() > 3);
+
+ // Check we can get at all the contents
+ checkAllDirectoryContents(fs.getRoot());
+
+ // 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 e
}
}
+ @SuppressWarnings("SameParameterValue")
private static InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java?rev=1839201&r1=1839200&r2=1839201&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java Sun Aug 26 11:55:00 2018
@@ -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<br>
+ * 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 {
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org