From 342ca1791a643a82ec1a1ce5abc93fcd057982b5 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Fri, 6 Mar 2015 15:57:34 -0500 Subject: [PATCH] NIFI-398: Fixed ArrayIndexOutOfBoundsException and added unit test to verify --- .../src/main/java/org/apache/nifi/util/RingBuffer.java | 2 +- .../apache/nifi/util/timebuffer/TestRingBuffer.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nifi/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/RingBuffer.java b/nifi/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/RingBuffer.java index c0bb830770..81f32ab3ed 100644 --- a/nifi/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/RingBuffer.java +++ b/nifi/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/RingBuffer.java @@ -185,7 +185,7 @@ public class RingBuffer { 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(); diff --git a/nifi/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java b/nifi/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java index fafffdd24b..5f8c4c8db9 100644 --- a/nifi/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java +++ b/nifi/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java @@ -33,6 +33,16 @@ import org.junit.Test; */ public class TestRingBuffer { + @Test + public void testGetNewestElement() { + final RingBuffer 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 ringBuffer = new RingBuffer<>(10);