Make BoundedArrayList follow the old behavior.

Unit tests have been included.
This commit is contained in:
Tux 2016-01-10 20:15:40 -05:00 committed by md_5
parent aa214c0b54
commit a1895c556f
2 changed files with 78 additions and 1 deletions

View File

@ -16,7 +16,7 @@ public class BoundedArrayList<E> extends ArrayList<E>
private void checkSize(int increment) 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 @Override

View File

@ -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<Object> list = new BoundedArrayList<>( 2 );
list.add( new Object() );
list.add( new Object() );
}
@Test
public void testSizeOneAdd() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 1 );
list.add( new Object() );
}
@Test(expected = IllegalStateException.class)
public void testBadAdd() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 0 );
list.add( new Object() );
}
@Test
public void testGoodAdd1() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 2 );
list.add( new Object() );
list.add( 0, new Object() );
}
@Test(expected = IllegalStateException.class)
public void testBadAdd1() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 1 );
list.add( new Object() );
list.add( 0, new Object() );
}
@Test
public void testGoodAddAll() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 1 );
list.addAll( ImmutableList.of( new Object() ) );
}
@Test
public void testGoodAddAll1() throws Exception
{
BoundedArrayList<Object> 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<Object> list = new BoundedArrayList<>(0);
list.addAll( ImmutableList.of( new Object() ) );
}
@Test(expected = IllegalStateException.class)
public void testBadAddAll1() throws Exception
{
BoundedArrayList<Object> list = new BoundedArrayList<>( 1 );
list.add( new Object() );
list.addAll( ImmutableList.of( new Object() ) );
}
}