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,232 +191,226 @@ 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[] {
int remaining = _workbook_size; new DocumentInputStream(_workbook_o),
new NDocumentInputStream(_workbook_n)
};
for(DocumentInputStream stream : streams) {
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 ],
( byte ) b); ( byte ) b);
remaining--; remaining--;
assertEquals("checking remaining after reading byte " + j, assertEquals("checking remaining after reading byte " + j,
remaining, stream.available()); remaining, stream.available());
} }
assertEquals(-1, stream.read());
stream.close();
try
{
stream.read();
fail("Should have caught IOException");
}
catch (IOException ignored)
{
// as expected // 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
}
}
} }
/** /**
* 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),
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
}
try // test reading zero length buffer
{ assertEquals(0, stream.read(new byte[ 0 ]));
stream.read(null); assertEquals(_workbook_size, stream.available());
fail("Should have caught NullPointerException"); byte[] buffer = new byte[ _buffer_size ];
} int offset = 0;
catch (NullPointerException ignored)
{
// as expected while (stream.available() >= buffer.length)
} {
assertEquals(_buffer_size, stream.read(buffer));
// test reading zero length buffer for (int j = 0; j < buffer.length; j++)
assertEquals(0, stream.read(new byte[ 0 ])); {
assertEquals(_workbook_size, stream.available());
byte[] buffer = new byte[ _buffer_size ];
int offset = 0;
while (stream.available() >= buffer.length)
{
assertEquals(_buffer_size, stream.read(buffer));
for (int j = 0; j < buffer.length; j++)
{
assertEquals("in main loop, byte " + offset, assertEquals("in main loop, byte " + offset,
_workbook_data[ offset ], buffer[ j ]); _workbook_data[ offset ], buffer[ j ]);
offset++; offset++;
} }
assertEquals("offset " + offset, _workbook_size - offset, assertEquals("offset " + offset, _workbook_size - offset,
stream.available()); stream.available());
} }
assertEquals(_workbook_size % _buffer_size, stream.available()); assertEquals(_workbook_size % _buffer_size, stream.available());
Arrays.fill(buffer, ( byte ) 0); Arrays.fill(buffer, ( byte ) 0);
int count = stream.read(buffer); int count = stream.read(buffer);
assertEquals(_workbook_size % _buffer_size, count); assertEquals(_workbook_size % _buffer_size, count);
for (int j = 0; j < count; j++) for (int j = 0; j < count; j++)
{ {
assertEquals("past main loop, byte " + offset, assertEquals("past main loop, byte " + offset,
_workbook_data[ offset ], buffer[ j ]); _workbook_data[ offset ], buffer[ j ]);
offset++; offset++;
} }
assertEquals(_workbook_size, offset); assertEquals(_workbook_size, offset);
for (int j = count; j < buffer.length; j++) for (int j = count; j < buffer.length; j++)
{ {
assertEquals("checking remainder, byte " + j, 0, buffer[ j ]); assertEquals("checking remainder, byte " + j, 0, buffer[ j ]);
} }
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) {
} // as expected
catch (IOException ignored) }
{ }
// 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 {
stream.read(null, 0, 1);
fail("Should have caught NullPointerException");
} catch (IllegalArgumentException ignored) {
// as expected
}
try { // test illegal offsets and lengths
stream.read(null, 0, 1); try {
fail("Should have caught NullPointerException"); stream.read(new byte[ 5 ], -4, 0);
} catch (IllegalArgumentException ignored) { fail("Should have caught IndexOutOfBoundsException");
// as expected } 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 illegal offsets and lengths // test reading zero
try assertEquals(0, stream.read(new byte[ 5 ], 0, 0));
{ assertEquals(_workbook_size, stream.available());
stream.read(new byte[ 5 ], -4, 0); byte[] buffer = new byte[ _workbook_size ];
fail("Should have caught IndexOutOfBoundsException"); int offset = 0;
}
catch (IndexOutOfBoundsException ignored)
{
// as expected while (stream.available() >= _buffer_size)
} {
try Arrays.fill(buffer, ( byte ) 0);
{ assertEquals(_buffer_size,
stream.read(new byte[ 5 ], 0, -4); stream.read(buffer, offset, _buffer_size));
fail("Should have caught IndexOutOfBoundsException"); for (int j = 0; j < offset; j++)
} {
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, stream.available());
byte[] buffer = new byte[ _workbook_size ];
int offset = 0;
while (stream.available() >= _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 ]); assertEquals("checking byte " + j, 0, buffer[ j ]);
} }
for (int j = offset; j < (offset + _buffer_size); j++) for (int j = offset; j < (offset + _buffer_size); j++)
{ {
assertEquals("checking byte " + j, _workbook_data[ j ], assertEquals("checking byte " + j, _workbook_data[ j ],
buffer[ j ]); buffer[ j ]);
} }
for (int j = offset + _buffer_size; j < buffer.length; j++) for (int j = offset + _buffer_size; j < buffer.length; j++)
{ {
assertEquals("checking byte " + j, 0, buffer[ j ]); assertEquals("checking byte " + j, 0, buffer[ j ]);
} }
offset += _buffer_size; offset += _buffer_size;
assertEquals("offset " + offset, _workbook_size - offset, assertEquals("offset " + offset, _workbook_size - offset,
stream.available()); stream.available());
} }
assertEquals(_workbook_size % _buffer_size, stream.available()); assertEquals(_workbook_size % _buffer_size, stream.available());
Arrays.fill(buffer, ( byte ) 0); Arrays.fill(buffer, ( byte ) 0);
int count = stream.read(buffer, offset, int count = stream.read(buffer, offset,
_workbook_size % _buffer_size); _workbook_size % _buffer_size);
assertEquals(_workbook_size % _buffer_size, count); assertEquals(_workbook_size % _buffer_size, count);
for (int j = 0; j < offset; j++) for (int j = 0; j < offset; j++)
{ {
assertEquals("checking byte " + j, 0, buffer[ j ]); assertEquals("checking byte " + j, 0, buffer[ j ]);
} }
for (int j = offset; j < buffer.length; j++) for (int j = offset; j < buffer.length; j++)
{ {
assertEquals("checking byte " + j, _workbook_data[ j ], assertEquals("checking byte " + j, _workbook_data[ j ],
buffer[ j ]); buffer[ j ]);
} }
assertEquals(_workbook_size, offset + count); assertEquals(_workbook_size, offset + count);
for (int j = count; j < offset; j++) for (int j = count; j < offset; j++)
{ {
assertEquals("byte " + j, 0, buffer[ 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(-1, stream.read(buffer, 0, 1));
} stream.close();
try {
stream.read(buffer, 0, 1);
fail("Should have caught IOException");
} catch (IOException ignored) {
// 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());
int count = stream.available();
assertEquals(_workbook_size, stream.available()); while (stream.available() >= _buffer_size) {
int count = stream.available(); assertEquals(_buffer_size, stream.skip(_buffer_size));
count -= _buffer_size;
while (stream.available() >= _buffer_size) assertEquals(count, stream.available());
{ }
assertEquals(_buffer_size, stream.skip(_buffer_size)); assertEquals(_workbook_size % _buffer_size,
count -= _buffer_size; stream.skip(_buffer_size));
assertEquals(count, stream.available()); assertEquals(0, stream.available());
} stream.reset();
assertEquals(_workbook_size % _buffer_size, assertEquals(_workbook_size, stream.available());
stream.skip(_buffer_size)); assertEquals(_workbook_size, stream.skip(_workbook_size * 2));
assertEquals(0, stream.available()); assertEquals(0, stream.available());
stream.reset(); stream.reset();
assertEquals(_workbook_size, stream.available()); assertEquals(_workbook_size, stream.available());
assertEquals(_workbook_size, stream.skip(_workbook_size * 2)); assertEquals(_workbook_size,
assertEquals(0, stream.available()); stream.skip(2 + ( long ) Integer.MAX_VALUE));
stream.reset(); assertEquals(0, stream.available());
assertEquals(_workbook_size, stream.available()); }
assertEquals(_workbook_size,
stream.skip(2 + ( long ) Integer.MAX_VALUE));
assertEquals(0, stream.available());
} }
/** /**