From a1895c556f2b762a61fe0ce2a328ea1f677e731d Mon Sep 17 00:00:00 2001 From: Tux Date: Sun, 10 Jan 2016 20:15:40 -0500 Subject: [PATCH] Make BoundedArrayList follow the old behavior. Unit tests have been included. --- .../md_5/bungee/util/BoundedArrayList.java | 2 +- .../bungee/util/BoundedArrayListTest.java | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 proxy/src/test/java/net/md_5/bungee/util/BoundedArrayListTest.java diff --git a/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java b/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java index d4bd1107..dad5121a 100644 --- a/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java +++ b/proxy/src/main/java/net/md_5/bungee/util/BoundedArrayList.java @@ -16,7 +16,7 @@ public class BoundedArrayList extends ArrayList private void checkSize(int increment) { - Preconditions.checkState( size() + increment < maxSize, "Adding %s elements would exceed capacity of %s", increment, maxSize ); + Preconditions.checkState( size() + increment <= maxSize, "Adding %s elements would exceed capacity of %s", increment, maxSize ); } @Override diff --git a/proxy/src/test/java/net/md_5/bungee/util/BoundedArrayListTest.java b/proxy/src/test/java/net/md_5/bungee/util/BoundedArrayListTest.java new file mode 100644 index 00000000..c18073e4 --- /dev/null +++ b/proxy/src/test/java/net/md_5/bungee/util/BoundedArrayListTest.java @@ -0,0 +1,77 @@ +package net.md_5.bungee.util; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BoundedArrayListTest { + + @Test + public void testGoodAdd() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 2 ); + list.add( new Object() ); + list.add( new Object() ); + } + + @Test + public void testSizeOneAdd() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 1 ); + list.add( new Object() ); + } + + @Test(expected = IllegalStateException.class) + public void testBadAdd() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 0 ); + list.add( new Object() ); + } + + @Test + public void testGoodAdd1() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 2 ); + list.add( new Object() ); + list.add( 0, new Object() ); + } + + @Test(expected = IllegalStateException.class) + public void testBadAdd1() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 1 ); + list.add( new Object() ); + list.add( 0, new Object() ); + } + + @Test + public void testGoodAddAll() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 1 ); + list.addAll( ImmutableList.of( new Object() ) ); + } + + @Test + public void testGoodAddAll1() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 2 ); + list.add( new Object() ); + list.addAll( 0, ImmutableList.of( new Object() ) ); + } + + @Test(expected = IllegalStateException.class) + public void testBadAddAll() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>(0); + list.addAll( ImmutableList.of( new Object() ) ); + } + + @Test(expected = IllegalStateException.class) + public void testBadAddAll1() throws Exception + { + BoundedArrayList list = new BoundedArrayList<>( 1 ); + list.add( new Object() ); + list.addAll( ImmutableList.of( new Object() ) ); + } +} \ No newline at end of file