Skip to content

Commit

Permalink
Add test coverage for maximum buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
jobarr-amzn committed Jan 26, 2024
1 parent 416bfac commit 8c1c6f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public ResizingPipedInputStream(final int initialBufferSize) {
if (initialBufferSize < 1) {
throw new IllegalArgumentException("Initial buffer size must be at least 1.");
}
if (initialBufferSize > _Private_IonConstants.ARRAY_MAXIMUM_SIZE) {
if (maximumBufferSize > _Private_IonConstants.ARRAY_MAXIMUM_SIZE) {
throw new IllegalArgumentException("Initial buffer size must be at most " + _Private_IonConstants.ARRAY_MAXIMUM_SIZE + ".");
}
if (maximumBufferSize < initialBufferSize) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/amazon/ion/impl/_Private_IonConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public final class _Private_IonConstants
// Integer.MAX_VALUE. True maximum array size is a JVM implementation detail, and varies by type and by JVM.
// We have this pinned at Integer.MAX_VALUE - 8 because that's a fairly common value in the JDK itself, in
// classes such as ConcurrentHashMap, InputStream, Hashtable, ByteArrayChannel, etc.
// In testing against a variety of JVMs and types the smallest maximum size I've seen is Integer.MAX_VALUE - 6
// In testing against a variety of JVMs and types the smallest maximum size I've seen is Integer.MAX_VALUE - 2,
// and the smallest maximum size I've seen reported is Integer.MAX_VALUE - 6.
public static final int ARRAY_MAXIMUM_SIZE = Integer.MAX_VALUE - 8;

private _Private_IonConstants() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import java.util.zip.GZIPOutputStream;

import static com.amazon.ion.BitUtils.bytes;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.junit.runners.Parameterized.Parameter;
import static org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class ResizingPipedInputStreamTest {
Expand Down Expand Up @@ -469,16 +467,20 @@ public void truncate() throws Exception {
assertEquals(0, input.available());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void errorsOnInvalidInitialBufferSize() {
new ResizingPipedInputStream(0);
assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(0));
}

@Test(expected = IllegalArgumentException.class)
public void errorsOnInvalidMaximumBufferSize() {
new ResizingPipedInputStream(10, 9, useBoundary);
@Test
public void errorsOnMaximumBufferSizeSmallerThanInitialSize() {
assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(10, 9, useBoundary));
}

@Test
public void errorsOnInvalidMaximumBufferSize() {
assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(10, Integer.MAX_VALUE, useBoundary));
}
@Test
public void initialBufferSizeAndCapacity() {
assertEquals(bufferSize, input.getInitialBufferSize());
Expand Down

0 comments on commit 8c1c6f8

Please sign in to comment.