NIFI-398: Fixed ArrayIndexOutOfBoundsException and added unit test to verify

This commit is contained in:
Mark Payne 2015-03-06 15:57:34 -05:00
parent 608f8007ed
commit 342ca1791a
2 changed files with 11 additions and 1 deletions

View File

@ -185,7 +185,7 @@ public class RingBuffer<T> {
public T getNewestElement() {
readLock.lock();
try {
int index = (insertionPointer == 0) ? buffer.length : insertionPointer - 1;
int index = (insertionPointer == 0) ? buffer.length - 1 : insertionPointer - 1;
return getElementData(index);
} finally {
readLock.unlock();

View File

@ -33,6 +33,16 @@ import org.junit.Test;
*/
public class TestRingBuffer {
@Test
public void testGetNewestElement() {
final RingBuffer<Integer> ringBuffer = new RingBuffer<>(10);
for (int i=0; i < 11; i++) {
ringBuffer.add(i);
assertEquals(i, ringBuffer.getNewestElement().intValue());
}
}
@Test
public void testAsList() {
final RingBuffer<Integer> ringBuffer = new RingBuffer<>(10);