mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-04-25 00:00:03 -04:00
#414: Fix for BufferedImageInputStream alignment/partial read issues.
Bonus clean-up of tests.
This commit is contained in:
+36
-8
@@ -2,7 +2,7 @@ package com.twelvemonkeys.imageio.stream;
|
||||
|
||||
import com.twelvemonkeys.io.ole2.CompoundDocument;
|
||||
import com.twelvemonkeys.io.ole2.Entry;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.imageio.stream.MemoryCacheImageInputStream;
|
||||
@@ -10,20 +10,25 @@ import java.io.IOException;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Random;
|
||||
|
||||
import static java.util.Arrays.fill;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* BufferedImageInputStreamTestCase
|
||||
* BufferedImageInputStreamTest
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: BufferedImageInputStreamTestCase.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$
|
||||
* @version $Id: BufferedImageInputStreamTest.java,v 1.0 Jun 30, 2008 3:07:42 PM haraldk Exp$
|
||||
*/
|
||||
public class BufferedImageInputStreamTestCase extends TestCase {
|
||||
protected final Random random = new Random();
|
||||
public class BufferedImageInputStreamTest {
|
||||
private final Random random = new Random(3450972865211L);
|
||||
|
||||
@Test
|
||||
public void testCreate() throws IOException {
|
||||
new BufferedImageInputStream(new ByteArrayImageInputStream(new byte[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNull() throws IOException {
|
||||
try {
|
||||
new BufferedImageInputStream(null);
|
||||
@@ -41,20 +46,23 @@ public class BufferedImageInputStreamTestCase extends TestCase {
|
||||
|
||||
// TODO: Create test that exposes read += -1 (eof) bug
|
||||
|
||||
@Test
|
||||
public void testArrayIndexOutOfBoundsBufferedReadBug() throws IOException {
|
||||
// TODO: Create a more straight forward way to prove correctness, for now this is good enough to avoid regression
|
||||
ImageInputStream input = new BufferedImageInputStream(new MemoryCacheImageInputStream(getClass().getResourceAsStream("/Thumbs-camera.db")));
|
||||
input.setByteOrder(ByteOrder.LITTLE_ENDIAN);
|
||||
Entry root = new CompoundDocument(input).getRootEntry();
|
||||
|
||||
Entry child = root.getChildEntry("Catalog");
|
||||
Entry catalog = root.getChildEntry("Catalog");
|
||||
|
||||
assertNotNull("Input stream can never be null", child.getInputStream());
|
||||
assertNotNull("Catalog should not be null", catalog);
|
||||
assertNotNull("Input stream can never be null", catalog.getInputStream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadResetReadDirectBufferBug() throws IOException {
|
||||
// Make sure we use the exact size of the buffer
|
||||
final int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE;
|
||||
int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE;
|
||||
|
||||
// Fill bytes
|
||||
byte[] bytes = new byte[size * 2];
|
||||
@@ -76,6 +84,7 @@ public class BufferedImageInputStreamTestCase extends TestCase {
|
||||
assertTrue(rangeEquals(bytes, size, result, 0, size));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBufferPositionCorrect() throws IOException {
|
||||
// Fill bytes
|
||||
byte[] bytes = new byte[1024];
|
||||
@@ -105,6 +114,25 @@ public class BufferedImageInputStreamTestCase extends TestCase {
|
||||
assertEquals(1020, stream.getStreamPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadIntegralOnBufferBoundary() throws IOException {
|
||||
// Make sure we use the exact size of the buffer
|
||||
int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE;
|
||||
|
||||
// Fill bytes
|
||||
byte[] bytes = new byte[size * 2];
|
||||
fill(bytes, size - 4, size + 4, (byte) 0xff);
|
||||
|
||||
// Create wrapper stream
|
||||
BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes));
|
||||
|
||||
// Read to fill the buffer, then seek to almost end of buffer
|
||||
assertEquals(0, stream.readInt());
|
||||
stream.seek(size - 3);
|
||||
assertEquals(0xffffffff, stream.readInt());
|
||||
assertEquals(size + 1, stream.getStreamPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test two arrays for range equality. That is, they contain the same elements for some specified range.
|
||||
*
|
||||
+18
-4
@@ -1,11 +1,12 @@
|
||||
package com.twelvemonkeys.imageio.stream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTestCase.rangeEquals;
|
||||
import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTest.rangeEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* ByteArrayImageInputStreamTestCase
|
||||
@@ -14,14 +15,16 @@ import static com.twelvemonkeys.imageio.stream.BufferedImageInputStreamTestCase.
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: ByteArrayImageInputStreamTestCase.java,v 1.0 Apr 21, 2009 10:58:48 AM haraldk Exp$
|
||||
*/
|
||||
public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
protected final Random random = new Random();
|
||||
public class ByteArrayImageInputStreamTest {
|
||||
private final Random random = new Random(1709843507234566L);
|
||||
|
||||
@Test
|
||||
public void testCreate() {
|
||||
ByteArrayImageInputStream stream = new ByteArrayImageInputStream(new byte[0]);
|
||||
assertEquals("Data length should be same as stream length", 0, stream.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNull() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(null);
|
||||
@@ -35,6 +38,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNullOffLen() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(null, 0, -1);
|
||||
@@ -48,6 +52,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNegativeOff() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(new byte[0], -1, 1);
|
||||
@@ -61,6 +66,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateBadOff() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(new byte[1], 2, 0);
|
||||
@@ -74,6 +80,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNegativeLen() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(new byte[0], 0, -1);
|
||||
@@ -87,6 +94,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateBadLen() {
|
||||
try {
|
||||
new ByteArrayImageInputStream(new byte[1], 0, 2);
|
||||
@@ -100,6 +108,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException {
|
||||
byte[] data = new byte[1024 * 1024];
|
||||
random.nextBytes(data);
|
||||
@@ -113,6 +122,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadOffsetLen() throws IOException {
|
||||
byte[] data = new byte[1024 * 1024];
|
||||
random.nextBytes(data);
|
||||
@@ -128,6 +138,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArray() throws IOException {
|
||||
byte[] data = new byte[1024 * 1024];
|
||||
random.nextBytes(data);
|
||||
@@ -144,6 +155,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadArrayOffLen() throws IOException {
|
||||
byte[] data = new byte[1024 * 1024];
|
||||
random.nextBytes(data);
|
||||
@@ -162,6 +174,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSkip() throws IOException {
|
||||
byte[] data = new byte[1024 * 14];
|
||||
random.nextBytes(data);
|
||||
@@ -179,6 +192,7 @@ public class ByteArrayImageInputStreamTestCase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSeek() throws IOException {
|
||||
byte[] data = new byte[1024 * 18];
|
||||
random.nextBytes(data);
|
||||
+22
-15
@@ -1,6 +1,6 @@
|
||||
package com.twelvemonkeys.imageio.stream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.imageio.stream.ImageInputStreamImpl;
|
||||
@@ -10,6 +10,8 @@ import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* SubImageInputStreamTestCase
|
||||
*
|
||||
@@ -17,9 +19,9 @@ import java.util.Random;
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: SubImageInputStreamTestCase.java,v 1.0 Nov 8, 2009 3:03:32 PM haraldk Exp$
|
||||
*/
|
||||
public class SubImageInputStreamTestCase extends TestCase {
|
||||
public class SubImageInputStreamTest {
|
||||
// TODO: Extract super test case for all stream tests
|
||||
private final Random random = new Random(837468l);
|
||||
private final Random random = new Random(837468L);
|
||||
|
||||
private ImageInputStream createStream(final int pSize) {
|
||||
byte[] bytes = new byte[pSize];
|
||||
@@ -34,24 +36,19 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
};
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateNullStream() throws IOException {
|
||||
try {
|
||||
new SubImageInputStream(null, 1);
|
||||
fail("Expected IllegalArgumentException with null stream");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
new SubImageInputStream(null, 1);
|
||||
fail("Expected IllegalArgumentException with null stream");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateNegativeLength() throws IOException {
|
||||
try {
|
||||
new SubImageInputStream(createStream(0), -1);
|
||||
fail("Expected IllegalArgumentException with negative length");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
new SubImageInputStream(createStream(0), -1);
|
||||
fail("Expected IllegalArgumentException with negative length");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(11), 7);
|
||||
|
||||
@@ -59,11 +56,13 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
assertEquals(7, stream.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWraphBeyondWrappedLength() throws IOException {
|
||||
SubImageInputStream stream = new SubImageInputStream(createStream(5), 6);
|
||||
assertEquals(5, stream.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapUnknownLength() throws IOException {
|
||||
SubImageInputStream stream = new SubImageInputStream(new ImageInputStreamImpl() {
|
||||
@Override
|
||||
@@ -85,6 +84,7 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
assertEquals(-1, stream.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException {
|
||||
ImageInputStream wrapped = createStream(42);
|
||||
|
||||
@@ -106,6 +106,7 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
assertEquals(25, wrapped.getStreamPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadResetRead() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(32), 16);
|
||||
stream.mark();
|
||||
@@ -121,6 +122,7 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
assertTrue(Arrays.equals(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeekNegative() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(7), 5);
|
||||
try {
|
||||
@@ -128,11 +130,13 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
fail("Expected IndexOutOfBoundsException");
|
||||
}
|
||||
catch (IndexOutOfBoundsException expected) {
|
||||
assertTrue(expected.getMessage().contains("pos"));
|
||||
}
|
||||
|
||||
assertEquals(0, stream.getStreamPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeekBeforeFlushedPos() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(7), 5);
|
||||
stream.seek(3);
|
||||
@@ -145,11 +149,13 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
fail("Expected IndexOutOfBoundsException");
|
||||
}
|
||||
catch (IndexOutOfBoundsException expected) {
|
||||
assertTrue(expected.getMessage().contains("pos"));
|
||||
}
|
||||
|
||||
assertEquals(3, stream.getStreamPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeekAfterEOF() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(7), 5);
|
||||
stream.seek(6);
|
||||
@@ -157,6 +163,7 @@ public class SubImageInputStreamTestCase extends TestCase {
|
||||
assertEquals(-1, stream.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeek() throws IOException {
|
||||
ImageInputStream stream = new SubImageInputStream(createStream(7), 5);
|
||||
stream.seek(5);
|
||||
Reference in New Issue
Block a user