mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 22:57:16 -04:00
parent
01f7ab708d
commit
28116ee80f
2 changed files with 41 additions and 1 deletions
|
@ -188,7 +188,7 @@ public class Accessors {
|
|||
* @param size the size of the list.
|
||||
* @return the positive integer offset for the list given by index i.
|
||||
*/
|
||||
private static int listIndex(int i, int size) {
|
||||
public static int listIndex(int i, int size) {
|
||||
if (i >= size || i < -size) {
|
||||
throw new IndexOutOfBoundsException("Index " + i + " is out of bounds for a list with size " + size);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package org.logstash;
|
||||
|
||||
import org.junit.experimental.theories.DataPoint;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.experimental.theories.Theories;
|
||||
import org.junit.experimental.theories.Theory;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -207,4 +213,38 @@ public class AccessorsTest {
|
|||
assertEquals(accessors.get("[foo][bar]"), null);
|
||||
assertEquals(accessors.get("[foo]"), "boom");
|
||||
}
|
||||
|
||||
@RunWith(Theories.class)
|
||||
public static class TestListIndexFailureCases {
|
||||
private static final int size = 10;
|
||||
|
||||
@DataPoint
|
||||
public static final int tooLarge = size;
|
||||
|
||||
@DataPoint
|
||||
public static final int tooLarge1 = size+1;
|
||||
|
||||
@DataPoint
|
||||
public static final int tooLargeNegative = -size - 1;
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Theory
|
||||
public void testListIndexOutOfBounds(int i) {
|
||||
exception.expect(IndexOutOfBoundsException.class);
|
||||
Accessors.listIndex(i, size);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestListIndex {
|
||||
public void testListIndexOutOfBounds() {
|
||||
assertEquals(Accessors.listIndex(0, 10), 0);
|
||||
assertEquals(Accessors.listIndex(1, 10), 1);
|
||||
assertEquals(Accessors.listIndex(9, 10), 9);
|
||||
assertEquals(Accessors.listIndex(-1, 10), 9);
|
||||
assertEquals(Accessors.listIndex(-9, 10), 1);
|
||||
assertEquals(Accessors.listIndex(-10, 10), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue