Enable more of the POIFS DocumetnInputStream tests to check NPOIFS as well

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1131988 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-06-05 08:16:22 +00:00
parent e9249b28c2
commit f93b671b69

View File

@ -191,11 +191,15 @@ public final class TestDocumentInputStream extends TestCase {
* test simple read method * test simple read method
*/ */
public void testReadSingleByte() throws IOException { public void testReadSingleByte() throws IOException {
DocumentInputStream stream = new DocumentInputStream(_workbook_o); DocumentInputStream[] streams = new DocumentInputStream[] {
new DocumentInputStream(_workbook_o),
new NDocumentInputStream(_workbook_n)
};
for(DocumentInputStream stream : streams) {
int remaining = _workbook_size; int remaining = _workbook_size;
for (int j = 0; j < _workbook_size; j++) // Try and read each byte in turn
{ for (int j = 0; j < _workbook_size; j++) {
int b = stream.read(); int b = stream.read();
assertTrue("checking sign of " + j, b >= 0); assertTrue("checking sign of " + j, b >= 0);
assertEquals("validating byte " + j, _workbook_data[ j ], assertEquals("validating byte " + j, _workbook_data[ j ],
@ -204,34 +208,35 @@ public final class TestDocumentInputStream extends TestCase {
assertEquals("checking remaining after reading byte " + j, assertEquals("checking remaining after reading byte " + j,
remaining, stream.available()); remaining, stream.available());
} }
// Ensure we fell off the end
assertEquals(-1, stream.read()); assertEquals(-1, stream.read());
// Check that after close we can no longer read
stream.close(); stream.close();
try try {
{
stream.read(); stream.read();
fail("Should have caught IOException"); fail("Should have caught IOException");
} } catch (IOException ignored) {
catch (IOException ignored)
{
// as expected // as expected
} }
} }
}
/** /**
* Test buffered read * Test buffered read
*/ */
public void testBufferRead() throws IOException { public void testBufferRead() throws IOException {
DocumentInputStream stream = new DocumentInputStream(_workbook_o); DocumentInputStream[] streams = new DocumentInputStream[] {
new DocumentInputStream(_workbook_o),
try new NDocumentInputStream(_workbook_n)
{ };
for(DocumentInputStream stream : streams) {
// Need to give a byte array to read
try {
stream.read(null); stream.read(null);
fail("Should have caught NullPointerException"); fail("Should have caught NullPointerException");
} } catch (NullPointerException ignored) {
catch (NullPointerException ignored)
{
// as expected // as expected
} }
@ -271,24 +276,24 @@ public final class TestDocumentInputStream extends TestCase {
} }
assertEquals(-1, stream.read(buffer)); assertEquals(-1, stream.read(buffer));
stream.close(); stream.close();
try try {
{
stream.read(buffer); stream.read(buffer);
fail("Should have caught IOException"); fail("Should have caught IOException");
} } catch (IOException ignored) {
catch (IOException ignored)
{
// as expected // as expected
} }
} }
}
/** /**
* Test complex buffered read * Test complex buffered read
*/ */
public void testComplexBufferRead() throws IOException { public void testComplexBufferRead() throws IOException {
DocumentInputStream stream = new DocumentInputStream(_workbook_o); DocumentInputStream[] streams = new DocumentInputStream[] {
new DocumentInputStream(_workbook_o),
new NDocumentInputStream(_workbook_n)
};
for(DocumentInputStream stream : streams) {
try { try {
stream.read(null, 0, 1); stream.read(null, 0, 1);
fail("Should have caught NullPointerException"); fail("Should have caught NullPointerException");
@ -297,34 +302,22 @@ public final class TestDocumentInputStream extends TestCase {
} }
// test illegal offsets and lengths // test illegal offsets and lengths
try try {
{
stream.read(new byte[ 5 ], -4, 0); stream.read(new byte[ 5 ], -4, 0);
fail("Should have caught IndexOutOfBoundsException"); fail("Should have caught IndexOutOfBoundsException");
} } catch (IndexOutOfBoundsException ignored) {
catch (IndexOutOfBoundsException ignored)
{
// as expected // as expected
} }
try try {
{
stream.read(new byte[ 5 ], 0, -4); stream.read(new byte[ 5 ], 0, -4);
fail("Should have caught IndexOutOfBoundsException"); fail("Should have caught IndexOutOfBoundsException");
} } catch (IndexOutOfBoundsException ignored) {
catch (IndexOutOfBoundsException ignored)
{
// as expected // as expected
} }
try try {
{
stream.read(new byte[ 5 ], 0, 6); stream.read(new byte[ 5 ], 0, 6);
fail("Should have caught IndexOutOfBoundsException"); fail("Should have caught IndexOutOfBoundsException");
} } catch (IndexOutOfBoundsException ignored) {
catch (IndexOutOfBoundsException ignored)
{
// as expected // as expected
} }
@ -376,31 +369,31 @@ public final class TestDocumentInputStream extends TestCase {
{ {
assertEquals("byte " + j, 0, buffer[ j ]); assertEquals("byte " + j, 0, buffer[ j ]);
} }
assertEquals(-1, stream.read(buffer, 0, 1)); assertEquals(-1, stream.read(buffer, 0, 1));
stream.close(); stream.close();
try try {
{
stream.read(buffer, 0, 1); stream.read(buffer, 0, 1);
fail("Should have caught IOException"); fail("Should have caught IOException");
} } catch (IOException ignored) {
catch (IOException ignored)
{
// as expected // as expected
} }
} }
}
/** /**
* Tests that we can skip within the stream * Tests that we can skip within the stream
*/ */
public void testSkip() throws IOException { public void testSkip() throws IOException {
DocumentInputStream stream = new DocumentInputStream(_workbook_o); DocumentInputStream[] streams = new DocumentInputStream[] {
new DocumentInputStream(_workbook_o),
new NDocumentInputStream(_workbook_n)
};
for(DocumentInputStream stream : streams) {
assertEquals(_workbook_size, stream.available()); assertEquals(_workbook_size, stream.available());
int count = stream.available(); int count = stream.available();
while (stream.available() >= _buffer_size) while (stream.available() >= _buffer_size) {
{
assertEquals(_buffer_size, stream.skip(_buffer_size)); assertEquals(_buffer_size, stream.skip(_buffer_size));
count -= _buffer_size; count -= _buffer_size;
assertEquals(count, stream.available()); assertEquals(count, stream.available());
@ -418,6 +411,7 @@ public final class TestDocumentInputStream extends TestCase {
stream.skip(2 + ( long ) Integer.MAX_VALUE)); stream.skip(2 + ( long ) Integer.MAX_VALUE));
assertEquals(0, stream.available()); assertEquals(0, stream.available());
} }
}
/** /**
* Test that we can read files at multiple levels down the tree * Test that we can read files at multiple levels down the tree