From a0f0b2ac8741bf8d805838427beaeba32d9f9231 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 11 Nov 2010 14:21:03 +0200 Subject: [PATCH] + first stab at RedisList integration tests --- .../util/AbstractRedisCollectionTest.java | 5 +- .../redis/util/AbstractRedisListTest.java | 246 ++++++++++++++++++ 2 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisListTest.java diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java index 54a62a268..06cb093c4 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java @@ -27,6 +27,7 @@ import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.datastore.redis.connection.RedisConnection; /** @@ -36,7 +37,7 @@ import org.junit.Test; */ public abstract class AbstractRedisCollectionTest { - private AbstractRedisCollection collection; + protected AbstractRedisCollection collection; @Before public void setUp() throws Exception { @@ -60,7 +61,7 @@ public abstract class AbstractRedisCollectionTest { public void tearDown() throws Exception { // remove the collection entirely since clear() doesn't always work collection.getCommands().del(collection.getKey()); - //collection.clear(); + ((RedisConnection) collection.getCommands()).close(); destroyCollection(); } diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisListTest.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisListTest.java new file mode 100644 index 000000000..fce611b6e --- /dev/null +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisListTest.java @@ -0,0 +1,246 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.datastore.redis.util; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; + +import org.junit.Before; +import org.junit.Test; + +/** + * Integration test for RedisList + * + * @author Costin Leau + */ +public abstract class AbstractRedisListTest extends AbstractRedisCollectionTest { + + protected RedisList list; + + @SuppressWarnings("unchecked") + @Before + public void setUp() throws Exception { + super.setUp(); + list = (RedisList) collection; + } + + @Test + public void testAddIndexObjectHead() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + list.add(t1); + list.add(t2); + + assertEquals(t1, list.get(0)); + list.add(0, t3); + assertEquals(t3, list.get(0)); + } + + @Test + public void testAddIndexObjectTail() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + list.add(t1); + list.add(t2); + + assertEquals(t1, list.get(2)); + list.add(2, t3); + assertEquals(t3, list.get(2)); + } + + @Test(expected = UnsupportedOperationException.class) + public void testAddIndexObjectMiddle() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + list.add(t1); + list.add(t2); + + assertEquals(t1, list.get(0)); + list.add(1, t3); + } + + @Test + public void addAllIndexCollectionHead() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + T t4 = getT(); + + list.add(t1); + list.add(t2); + + List asList = Arrays.asList(t3, t4); + + assertEquals(t1, list.get(0)); + list.addAll(0, asList); + assertEquals(t3, list.get(0)); + } + + @Test + public void addAllIndexCollectionTail() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + T t4 = getT(); + + list.add(t1); + list.add(t2); + + List asList = Arrays.asList(t3, t4); + + assertEquals(t1, list.get(0)); + assertTrue(list.addAll(2, asList)); + assertEquals(t4, list.get(0)); + } + + @Test(expected = UnsupportedOperationException.class) + public void addAllIndexCollectionMiddle() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + T t4 = getT(); + + list.add(t1); + list.add(t2); + + List asList = Arrays.asList(t3, t4); + + assertEquals(t1, list.get(0)); + assertTrue(list.addAll(1, asList)); + } + + @Test + public void testIndexOfObject() { + T t1 = getT(); + T t2 = getT(); + + assertEquals(-1, list.indexOf(t1)); + list.add(t1); + assertEquals(0, list.indexOf(t1)); + + assertEquals(-1, list.indexOf(t2)); + list.add(t2); + assertEquals(1, list.indexOf(t1)); + } + + @Test + public void testOffer() { + T t1 = getT(); + + assertFalse(list.offer(t1)); + list.add(t1); + assertTrue(list.offer(t1)); + } + + @Test + public void testPeek() { + assertNull(list.peek()); + T t1 = getT(); + list.add(t1); + assertEquals(t1, list.peek()); + list.clear(); + assertNull(list.peek()); + } + + @Test + public void testElement() { + try { + list.element(); + fail(); + } catch (NoSuchElementException nse) { + // expected + } + + T t1 = getT(); + list.add(t1); + assertEquals(t1, list.element()); + list.clear(); + try { + list.element(); + fail(); + } catch (NoSuchElementException nse) { + // expected + } + } + + @Test + public void testPoll() { + assertNull(list.poll()); + T t1 = getT(); + list.add(t1); + assertEquals(t1, list.poll()); + assertNull(list.poll()); + } + + @Test + public void testRemove() { + try { + list.remove(); + fail(); + } catch (NoSuchElementException nse) { + // expected + } + + T t1 = getT(); + list.add(t1); + assertEquals(t1, list.remove()); + try { + list.remove(); + fail(); + } catch (NoSuchElementException nse) { + // expected + } + } + + @Test + public void testRange() { + T t1 = getT(); + T t2 = getT(); + + assertTrue(list.range(0, -1).isEmpty()); + list.add(t1); + list.add(t2); + assertEquals(2, list.range(0, -1).size()); + assertEquals(t1, list.range(0, 0).get(0)); + assertEquals(t2, list.range(1, 0).get(0)); + } + + @Test + public void testRemoveIndex() { + T t1 = getT(); + T t2 = getT(); + + assertNull(list.remove(0)); + list.add(t1); + list.add(t2); + assertNull(list.remove(2)); + assertEquals(t2, list.remove(1)); + assertEquals(t1, list.remove(0)); + } + + public RedisList trim(int start, int end) { + return list.trim(start, end); + } +} \ No newline at end of file