diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index d02e89b73..3cd5a05be 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -119,11 +119,23 @@ public class RedissonList implements List { @Override public V get(int index) { + checkIndex(index); return (V) connection.lindex(name, index); } + private void checkIndex(int index) { + int size = size(); + if (!isInRange(index, size)) + throw new IndexOutOfBoundsException("index: " + index + " but current size: "+ size); + } + + private boolean isInRange(int index, int size) { + return index >= 0 && index < size; + } + @Override public V set(int index, V element) { + checkIndex(index); V prev = get(index); connection.lset(name, index, element); return prev; @@ -137,6 +149,7 @@ public class RedissonList implements List { @Override public V remove(int index) { + checkIndex(index); V value = get(index); connection.lrem(name, 1, value); return value; @@ -212,6 +225,7 @@ public class RedissonList implements List { throw new IllegalStateException("Element been already deleted"); } RedissonList.this.remove(currentIndex); + currentIndex--; removeExecuted = true; } diff --git a/src/test/java/org/redisson/RedissonListTest.java b/src/test/java/org/redisson/RedissonListTest.java index 96df0c0f9..b446c4691 100644 --- a/src/test/java/org/redisson/RedissonListTest.java +++ b/src/test/java/org/redisson/RedissonListTest.java @@ -1,5 +1,7 @@ package org.redisson; +import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.hamcrest.Matchers; @@ -8,6 +10,142 @@ import org.junit.Test; public class RedissonListTest { + @Test + public void testToArray() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + list.add("1"); + list.add("4"); + list.add("2"); + list.add("5"); + list.add("3"); + + Assert.assertTrue(Arrays.equals(list.toArray(), new Object[] {"1", "4", "2", "5", "3"})); + + String[] strs = list.toArray(new String[0]); + Assert.assertTrue(Arrays.equals(strs, new String[] {"1", "4", "2", "5", "3"})); + + clear(list); + } + + + @Test + public void testIteratorRemove() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + list.add("1"); + list.add("4"); + list.add("2"); + list.add("5"); + list.add("3"); + + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + String value = iterator.next(); + if (value.equals("2")) { + iterator.remove(); + } + } + + Assert.assertThat(list, Matchers.contains("1", "4", "5", "3")); + + int iteration = 0; + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + iterator.next(); + iterator.remove(); + iteration++; + } + + Assert.assertEquals(4, iteration); + + Assert.assertEquals(0, list.size()); + Assert.assertTrue(list.isEmpty()); + + clear(list); + } + + @Test + public void testIteratorSequence() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + list.add("1"); + list.add("4"); + list.add("2"); + list.add("5"); + list.add("3"); + + checkIterator(list); + // to test "memory effect" absence + checkIterator(list); + + clear(list); + } + + private void checkIterator(List list) { + int iteration = 0; + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + String value = iterator.next(); + String val = list.get(iteration); + Assert.assertEquals(val, value); + iteration++; + } + + Assert.assertEquals(list.size(), iteration); + } + + + @Test + public void testContains() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + list.add("1"); + list.add("4"); + list.add("2"); + list.add("5"); + list.add("3"); + + Assert.assertTrue(list.contains("3")); + Assert.assertFalse(list.contains("31")); + Assert.assertTrue(list.contains("1")); + + clear(list); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetFail2() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + + list.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetFail() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + + list.get(0); + } + + @Test + public void testAddGet() { + Redisson redisson = Redisson.create(); + List list = redisson.getList("list"); + list.add("1"); + list.add("4"); + list.add("2"); + list.add("5"); + list.add("3"); + + String val1 = list.get(0); + Assert.assertEquals("1", val1); + + String val2 = list.get(3); + Assert.assertEquals("5", val2); + + clear(list); + } + + @Test public void testSize() { Redisson redisson = Redisson.create(); @@ -24,6 +162,9 @@ public class RedissonListTest { list.remove("2"); Assert.assertThat(list, Matchers.contains("1", "3", "4", "5", "6")); + list.remove("4"); + Assert.assertThat(list, Matchers.contains("1", "3", "5", "6")); + clear(list); }