RedissonList tests added

pull/6/head
Nikita 11 years ago
parent 5d38ef36a0
commit dd8282a3fb

@ -119,11 +119,23 @@ public class RedissonList<V> implements List<V> {
@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<V> implements List<V> {
@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<V> implements List<V> {
throw new IllegalStateException("Element been already deleted");
}
RedissonList.this.remove(currentIndex);
currentIndex--;
removeExecuted = true;
}

@ -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<String> 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<String> list = redisson.getList("list");
list.add("1");
list.add("4");
list.add("2");
list.add("5");
list.add("3");
for (Iterator<String> 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<String> 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<String> 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<String> list) {
int iteration = 0;
for (Iterator<String> 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<String> 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<String> list = redisson.getList("list");
list.get(0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testGetFail() {
Redisson redisson = Redisson.create();
List<String> list = redisson.getList("list");
list.get(0);
}
@Test
public void testAddGet() {
Redisson redisson = Redisson.create();
List<String> 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);
}

Loading…
Cancel
Save