+ renamed org.springframework.data.keyvalue.redis to o.s.d.redis
+ eliminated Riak package + eliminate Core package
This commit is contained in:
113
src/test/java/org/springframework/data/redis/Address.java
Normal file
113
src/test/java/org/springframework/data/redis/Address.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Simple serializable class.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class Address implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4924045450477798779L;
|
||||
|
||||
private String street;
|
||||
|
||||
private Integer number;
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>Address</code> instance.
|
||||
*
|
||||
* @param street
|
||||
* @param number
|
||||
*/
|
||||
public Address(String street, int number) {
|
||||
super();
|
||||
this.street = street;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the street.
|
||||
*
|
||||
* @return Returns the street
|
||||
*/
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param street The street to set.
|
||||
*/
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number.
|
||||
*
|
||||
* @return Returns the number
|
||||
*/
|
||||
public Integer getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number The number to set.
|
||||
*/
|
||||
public void setNumber(Integer number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((number == null) ? 0 : number.hashCode());
|
||||
result = prime * result + ((street == null) ? 0 : street.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof Address))
|
||||
return false;
|
||||
Address other = (Address) obj;
|
||||
if (number == null) {
|
||||
if (other.number != null)
|
||||
return false;
|
||||
}
|
||||
else if (!number.equals(other.number))
|
||||
return false;
|
||||
if (street == null) {
|
||||
if (other.street != null)
|
||||
return false;
|
||||
}
|
||||
else if (!street.equals(other.street))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
|
||||
/**
|
||||
* Basic utility to help with the destruction of {@link RedisConnectionFactory} inside JUnit 4 tests.
|
||||
* Simply add the factory during setup and then call {@link #cleanUp()} through the <tt>@AfterClass</tt> method.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class ConnectionFactoryTracker {
|
||||
|
||||
private static Set<RedisConnectionFactory> connFactories = new LinkedHashSet<RedisConnectionFactory>();
|
||||
|
||||
public static void add(RedisConnectionFactory factory) {
|
||||
connFactories.add(factory);
|
||||
}
|
||||
|
||||
public static void cleanUp() {
|
||||
if (connFactories != null) {
|
||||
for (RedisConnectionFactory connectionFactory : connFactories) {
|
||||
try {
|
||||
((DisposableBean) connectionFactory).destroy();
|
||||
//System.out.println("Succesfully cleaned up factory " + connectionFactory);
|
||||
} catch (Exception ex) {
|
||||
System.err.println("Cannot clean factory " + connectionFactory + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
129
src/test/java/org/springframework/data/redis/Person.java
Normal file
129
src/test/java/org/springframework/data/redis/Person.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Simple serializable class.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class Person implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 92633004015631981L;
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
private Integer age;
|
||||
private Address address;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName, int age) {
|
||||
this(firstName, lastName, age, null);
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName, int age, Address address) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((address == null) ? 0 : address.hashCode());
|
||||
result = prime * result + ((age == null) ? 0 : age.hashCode());
|
||||
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
|
||||
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof Person))
|
||||
return false;
|
||||
Person other = (Person) obj;
|
||||
if (address == null) {
|
||||
if (other.address != null)
|
||||
return false;
|
||||
}
|
||||
else if (!address.equals(other.address))
|
||||
return false;
|
||||
if (age == null) {
|
||||
if (other.age != null)
|
||||
return false;
|
||||
}
|
||||
else if (!age.equals(other.age))
|
||||
return false;
|
||||
if (firstName == null) {
|
||||
if (other.firstName != null)
|
||||
return false;
|
||||
}
|
||||
else if (!firstName.equals(other.firstName))
|
||||
return false;
|
||||
if (lastName == null) {
|
||||
if (other.lastName != null)
|
||||
return false;
|
||||
}
|
||||
else if (!lastName.equals(other.lastName))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class PropertyEditorsTest {
|
||||
|
||||
private GenericXmlApplicationContext ctx;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ctx = new GenericXmlApplicationContext("/org/springframework/data/redis/pe.xml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (ctx != null)
|
||||
ctx.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInjection() throws Exception {
|
||||
RedisViewPE bean = ctx.getBean(RedisViewPE.class);
|
||||
RedisOperations<?, ?> ops = ctx.getBean(RedisOperations.class);
|
||||
|
||||
assertSame(ops.opsForValue(), bean.getValueOps());
|
||||
assertSame(ops.opsForList(), bean.getListOps());
|
||||
assertSame(ops.opsForSet(), bean.getSetOps());
|
||||
assertSame(ops.opsForZSet(), bean.getZsetOps());
|
||||
assertSame(ops, bean.getHashOps().getOperations());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis;
|
||||
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.ListOperations;
|
||||
import org.springframework.data.redis.core.SetOperations;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisViewPE {
|
||||
|
||||
private ValueOperations<String, String> valueOps;
|
||||
private ListOperations<String, String> listOps;
|
||||
private SetOperations<String, String> setOps;
|
||||
private ZSetOperations<String, Object> zsetOps;
|
||||
private HashOperations<Object, String, Object> hashOps;
|
||||
|
||||
public ValueOperations<String, String> getValueOps() {
|
||||
return valueOps;
|
||||
}
|
||||
|
||||
public void setValueOps(ValueOperations<String, String> valueOps) {
|
||||
this.valueOps = valueOps;
|
||||
}
|
||||
|
||||
public ListOperations<String, String> getListOps() {
|
||||
return listOps;
|
||||
}
|
||||
|
||||
public void setListOps(ListOperations<String, String> listOps) {
|
||||
this.listOps = listOps;
|
||||
}
|
||||
|
||||
public SetOperations<String, String> getSetOps() {
|
||||
return setOps;
|
||||
}
|
||||
|
||||
public void setSetOps(SetOperations<String, String> setOps) {
|
||||
this.setOps = setOps;
|
||||
}
|
||||
|
||||
public ZSetOperations<String, Object> getZsetOps() {
|
||||
return zsetOps;
|
||||
}
|
||||
|
||||
public void setZsetOps(ZSetOperations<String, Object> zsetOps) {
|
||||
this.zsetOps = zsetOps;
|
||||
}
|
||||
|
||||
public HashOperations<Object, String, Object> getHashOps() {
|
||||
return hashOps;
|
||||
}
|
||||
|
||||
public void setHashOps(HashOperations<Object, String, Object> hashOps) {
|
||||
this.hashOps = hashOps;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class SettingsUtils {
|
||||
private final static Properties DEFAULTS = new Properties();
|
||||
private static final Properties SETTINGS;
|
||||
|
||||
static {
|
||||
DEFAULTS.put("host", "localhost");
|
||||
DEFAULTS.put("port", "6379");
|
||||
|
||||
SETTINGS = new Properties(DEFAULTS);
|
||||
|
||||
try {
|
||||
SETTINGS.load(SettingsUtils.class.getResourceAsStream("/org/springframework/data/redis/test.properties"));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot read settings");
|
||||
}
|
||||
}
|
||||
|
||||
public static String getHost() {
|
||||
return SETTINGS.getProperty("host");
|
||||
}
|
||||
|
||||
public static int getPort() {
|
||||
return Integer.valueOf(SETTINGS.getProperty("port"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class NamespaceTest {
|
||||
|
||||
private GenericXmlApplicationContext ctx;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ctx = new GenericXmlApplicationContext("/org/springframework/data/redis/config/namespace.xml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (ctx != null)
|
||||
ctx.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanityTest() throws Exception {
|
||||
RedisMessageListenerContainer container = ctx.getBean(RedisMessageListenerContainer.class);
|
||||
assertTrue(container.isRunning());
|
||||
//Thread.sleep(TimeUnit.SECONDS.toMillis(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithMessages() throws Exception {
|
||||
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
|
||||
template.convertAndSend("x1", "[X]test");
|
||||
template.convertAndSend("z1", "[Z]test");
|
||||
//Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
||||
}
|
||||
|
||||
public void testErrorHandler() throws Exception {
|
||||
StubErrorHandler handler = ctx.getBean(StubErrorHandler.class);
|
||||
|
||||
int index = handler.throwables.size();
|
||||
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
|
||||
template.convertAndSend("exception", "test1");
|
||||
handler.throwables.pollLast(3, TimeUnit.SECONDS);
|
||||
assertEquals(index + 1, handler.throwables.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.config;
|
||||
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import org.springframework.util.ErrorHandler;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class StubErrorHandler implements ErrorHandler {
|
||||
|
||||
public BlockingDeque<Throwable> throwables = new LinkedBlockingDeque<Throwable>();
|
||||
|
||||
@Override
|
||||
public void handleError(Throwable t) {
|
||||
throwables.add(t);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.connection;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.Address;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.StringRedisConnection;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
public abstract class AbstractConnectionIntegrationTests {
|
||||
|
||||
protected StringRedisConnection connection;
|
||||
protected RedisSerializer serializer = new JdkSerializationRedisSerializer();
|
||||
protected RedisSerializer stringSerializer = new StringRedisSerializer();
|
||||
|
||||
private static final String listName = "test-list";
|
||||
private static final byte[] EMPTY_ARRAY = new byte[0];
|
||||
|
||||
protected abstract RedisConnectionFactory getConnectionFactory();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
connection = new DefaultStringRedisConnection(getConnectionFactory().getConnection());
|
||||
ConnectionFactoryTracker.add(getConnectionFactory());
|
||||
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
connection.close();
|
||||
connection = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLPush() throws Exception {
|
||||
byte[] val = "bar".getBytes();
|
||||
Long index = connection.lPush(listName.getBytes(), val);
|
||||
if (index != null) {
|
||||
assertEquals((Long) (index + 1), connection.lPush(listName.getBytes(), val));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAndGet() {
|
||||
String key = "foo";
|
||||
String value = "blabla";
|
||||
connection.set(key.getBytes(), value.getBytes());
|
||||
assertEquals(value, new String(connection.get(key.getBytes())));
|
||||
}
|
||||
|
||||
private boolean isJredis() {
|
||||
return connection.getClass().getSimpleName().startsWith("Jredis");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testByteValue() {
|
||||
String value = UUID.randomUUID().toString();
|
||||
Person person = new Person(value, value, 1, new Address(value, 2));
|
||||
String key = getClass() + ":byteValue";
|
||||
byte[] rawKey = stringSerializer.serialize(key);
|
||||
|
||||
connection.set(rawKey, serializer.serialize(person));
|
||||
byte[] rawValue = connection.get(rawKey);
|
||||
assertNotNull(rawValue);
|
||||
assertEquals(person, serializer.deserialize(rawValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPingPong() throws Exception {
|
||||
assertEquals("PONG", connection.ping());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInfo() throws Exception {
|
||||
Properties info = connection.info();
|
||||
assertNotNull(info);
|
||||
assertTrue("at least 5 settings should be present", info.size() >= 5);
|
||||
String version = info.getProperty("redis_version");
|
||||
assertNotNull(version);
|
||||
System.out.println(info);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullKey() throws Exception {
|
||||
connection.decr(EMPTY_ARRAY);
|
||||
try {
|
||||
connection.decr((String) null);
|
||||
} catch (Exception ex) {
|
||||
// excepted
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValue() throws Exception {
|
||||
byte[] key = UUID.randomUUID().toString().getBytes();
|
||||
connection.append(key, EMPTY_ARRAY);
|
||||
try {
|
||||
connection.append(key, null);
|
||||
} catch (DataAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashNullKey() throws Exception {
|
||||
byte[] key = UUID.randomUUID().toString().getBytes();
|
||||
connection.hExists(key, EMPTY_ARRAY);
|
||||
try {
|
||||
connection.hExists(key, null);
|
||||
} catch (DataAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashNullValue() throws Exception {
|
||||
byte[] key = UUID.randomUUID().toString().getBytes();
|
||||
byte[] field = "random".getBytes();
|
||||
|
||||
connection.hSet(key, field, EMPTY_ARRAY);
|
||||
try {
|
||||
connection.hSet(key, field, null);
|
||||
} catch (DataAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSerialization() throws Exception {
|
||||
String[] keys = new String[] { "~", "[" };
|
||||
List<String> mGet = connection.mGet(keys);
|
||||
assertEquals(2, mGet.size());
|
||||
assertNull(mGet.get(0));
|
||||
assertNull(mGet.get(1));
|
||||
|
||||
StringRedisTemplate stringTemplate = new StringRedisTemplate(getConnectionFactory());
|
||||
List<String> multiGet = stringTemplate.opsForValue().multiGet(Arrays.asList(keys));
|
||||
assertEquals(2, multiGet.size());
|
||||
assertNull(multiGet.get(0));
|
||||
assertNull(multiGet.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullCollections() throws Exception {
|
||||
connection.openPipeline();
|
||||
assertNull(connection.keys("~*"));
|
||||
assertNull(connection.hKeys("~"));
|
||||
connection.closePipeline();
|
||||
}
|
||||
|
||||
// pub sub test
|
||||
@Test
|
||||
public void testPubSub() throws Exception {
|
||||
|
||||
final BlockingDeque<Message> queue = new LinkedBlockingDeque<Message>();
|
||||
|
||||
final MessageListener ml = new MessageListener() {
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
queue.add(message);
|
||||
System.out.println("received message");
|
||||
}
|
||||
};
|
||||
|
||||
final byte[] channel = "foo.tv".getBytes();
|
||||
final RedisConnection subConn = getConnectionFactory().getConnection();
|
||||
|
||||
assertNotSame(connection, subConn);
|
||||
|
||||
|
||||
final AtomicBoolean flag = new AtomicBoolean(true);
|
||||
|
||||
Runnable listener = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
subConn.subscribe(ml, channel);
|
||||
System.out.println("Subscribed");
|
||||
while (flag.get()) {
|
||||
try {
|
||||
Thread.currentThread().sleep(2000);
|
||||
} catch (Exception ex) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Thread th = new Thread(listener, "listener");
|
||||
th.start();
|
||||
|
||||
try {
|
||||
Thread.sleep(1500);
|
||||
connection.publish(channel, "one".getBytes());
|
||||
connection.publish(channel, "two".getBytes());
|
||||
connection.publish(channel, "I see you".getBytes());
|
||||
System.out.println("Done publishing...");
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Done waiting ...");
|
||||
} finally {
|
||||
flag.set(false);
|
||||
}
|
||||
System.out.println(queue);
|
||||
assertEquals(3, queue.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPubSubWithNamedChannels() {
|
||||
final byte[] expectedChannel = "channel1".getBytes();
|
||||
final byte[] expectedMessage = "msg".getBytes();
|
||||
|
||||
MessageListener listener = new MessageListener() {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
assertArrayEquals(expectedChannel, message.getChannel());
|
||||
assertArrayEquals(expectedMessage, message.getBody());
|
||||
}
|
||||
};
|
||||
|
||||
Thread th = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// sleep 1 second to let the registration happen
|
||||
try {
|
||||
Thread.currentThread().sleep(2000);
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
// open a new connection
|
||||
RedisConnection connection2 = getConnectionFactory().getConnection();
|
||||
connection2.publish(expectedMessage, expectedChannel);
|
||||
connection2.close();
|
||||
// unsubscribe connection
|
||||
connection.getSubscription().unsubscribe();
|
||||
}
|
||||
});
|
||||
|
||||
th.start();
|
||||
connection.subscribe(listener, expectedChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPubSubWithPatterns() {
|
||||
final byte[] expectedPattern = "channel*".getBytes();
|
||||
final byte[] expectedMessage = "msg".getBytes();
|
||||
|
||||
MessageListener listener = new MessageListener() {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
assertArrayEquals(expectedPattern, pattern);
|
||||
assertArrayEquals(expectedMessage, message.getBody());
|
||||
System.out.println("Received message '" + new String(message.getBody()) + "'");
|
||||
}
|
||||
};
|
||||
|
||||
Thread th = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// sleep 1 second to let the registration happen
|
||||
try {
|
||||
Thread.currentThread().sleep(1500);
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
// open a new connection
|
||||
RedisConnection connection2 = getConnectionFactory().getConnection();
|
||||
connection2.publish(expectedMessage, "channel1".getBytes());
|
||||
connection2.publish(expectedMessage, "channel2".getBytes());
|
||||
connection2.close();
|
||||
// unsubscribe connection
|
||||
connection.getSubscription().pUnsubscribe(expectedPattern);
|
||||
}
|
||||
});
|
||||
|
||||
th.start();
|
||||
connection.pSubscribe(listener, expectedPattern);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.connection.jedis;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
import redis.clients.jedis.BinaryJedis;
|
||||
import redis.clients.jedis.Transaction;
|
||||
|
||||
public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrationTests {
|
||||
|
||||
JedisConnectionFactory factory;
|
||||
|
||||
public JedisConnectionIntegrationTests() {
|
||||
factory = new JedisConnectionFactory();
|
||||
factory.setUsePool(true);
|
||||
|
||||
factory.setPort(SettingsUtils.getPort());
|
||||
factory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisConnectionFactory getConnectionFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMulti() throws Exception {
|
||||
byte[] key = "key".getBytes();
|
||||
byte[] value = "value".getBytes();
|
||||
|
||||
BinaryJedis jedis = (BinaryJedis) connection.getNativeConnection();
|
||||
Transaction multi = jedis.multi();
|
||||
//connection.set(key, value);
|
||||
multi.set(value, key);
|
||||
System.out.println(multi.exec());
|
||||
|
||||
connection.multi();
|
||||
connection.set(value, key);
|
||||
System.out.println(connection.exec());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void setAdd() {
|
||||
// connection.sadd("s1", "1");
|
||||
// connection.sadd("s1", "2");
|
||||
// connection.sadd("s1", "3");
|
||||
// connection.sadd("s2", "2");
|
||||
// connection.sadd("s2", "3");
|
||||
// Set<String> intersection = connection.sinter("s1", "s2");
|
||||
// System.out.println(intersection);
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void setIntersectionTests() {
|
||||
// RedisTemplate template = new RedisTemplate(clientFactory);
|
||||
// RedisSet s1 = new RedisSet(template, "s1");
|
||||
// s1.add("1");
|
||||
// s1.add("2");
|
||||
// s1.add("3");
|
||||
// RedisSet s2 = new RedisSet(template, "s2");
|
||||
// s2.add("2");
|
||||
// s2.add("3");
|
||||
// Set s3 = s1.intersection("s3", s1, s2);
|
||||
// for (Object object : s3) {
|
||||
// System.out.println(object);
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.connection.jredis;
|
||||
|
||||
import org.jredis.JRedis;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
|
||||
public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrationTests {
|
||||
|
||||
JredisConnectionFactory factory;
|
||||
|
||||
public JRedisConnectionIntegrationTests() {
|
||||
factory = new JredisConnectionFactory();
|
||||
factory.setPort(SettingsUtils.getPort());
|
||||
factory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
factory.setUsePool(true);
|
||||
factory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisConnectionFactory getConnectionFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRaw() throws Exception {
|
||||
JRedis jr = (JRedis) factory.getConnection().getNativeConnection();
|
||||
|
||||
System.out.println(jr.dbsize());
|
||||
System.out.println(jr.exists("foobar"));
|
||||
jr.set("foobar", "barfoo");
|
||||
System.out.println(jr.get("foobar"));
|
||||
}
|
||||
|
||||
@Ignore("JRedis does not support pipelining")
|
||||
public void testNullCollections() {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testNullKey() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testNullValue() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testHashNullKey() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testHashNullValue() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testNullSerialization() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testPubSub() throws Exception {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testPubSubWithPatterns() {
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public void testPubSubWithNamedChannels() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.connection.rjc;
|
||||
|
||||
import org.idevlab.rjc.Session;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.rjc.RjcConnectionFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RjcConnectionIntegrationTests extends AbstractConnectionIntegrationTests {
|
||||
|
||||
RjcConnectionFactory factory;
|
||||
|
||||
public RjcConnectionIntegrationTests() {
|
||||
factory = new RjcConnectionFactory();
|
||||
factory.setPort(SettingsUtils.getPort());
|
||||
factory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
factory.setUsePool(false);
|
||||
factory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisConnectionFactory getConnectionFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRaw() throws Exception {
|
||||
Session jr = (Session) factory.getConnection().getNativeConnection();
|
||||
|
||||
System.out.println(jr.dbSize());
|
||||
System.out.println(jr.exists("foobar"));
|
||||
jr.set("foobar", "barfoo");
|
||||
System.out.println(jr.get("foobar"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.SessionCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class SessionTest {
|
||||
|
||||
@Test
|
||||
public void testSession() throws Exception {
|
||||
final RedisConnection conn = mock(RedisConnection.class);
|
||||
RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
|
||||
|
||||
when(factory.getConnection()).thenReturn(conn);
|
||||
final StringRedisTemplate template = new StringRedisTemplate(factory);
|
||||
|
||||
template.execute(new SessionCallback<Object>() {
|
||||
@Override
|
||||
public Object execute(RedisOperations operations) {
|
||||
checkConnection(template, conn);
|
||||
template.discard();
|
||||
assertSame(template, operations);
|
||||
checkConnection(template, conn);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkConnection(RedisTemplate<?, ?> template, final RedisConnection expectedConnection) {
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
assertSame(expectedConnection, connection);
|
||||
return null;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.core;
|
||||
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.springframework.data.redis.core.query.SortQueryBuilder;
|
||||
|
||||
public class SortTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
public void testBasicDSL() throws Exception {
|
||||
SortQueryBuilder.sort("list").build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.CollectionTestParams;
|
||||
import org.springframework.data.redis.support.collections.ObjectFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class TemplateTest {
|
||||
private ObjectFactory<Object> objFactory;
|
||||
private RedisTemplate template;
|
||||
|
||||
public TemplateTest(ObjectFactory<Object> objFactory, RedisTemplate template) {
|
||||
this.objFactory = objFactory;
|
||||
this.template = template;
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
return CollectionTestParams.testParams();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeys() throws Exception {
|
||||
assertTrue(template.keys("*") != null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.rjc.RjcConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.ObjectFactory;
|
||||
import org.springframework.data.redis.support.collections.PersonObjectFactory;
|
||||
import org.springframework.data.redis.support.collections.StringObjectFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class PubSubTestParams {
|
||||
|
||||
public static Collection<Object[]> testParams() {
|
||||
// create Jedis Factory
|
||||
ObjectFactory<String> stringFactory = new StringObjectFactory();
|
||||
ObjectFactory<Person> personFactory = new PersonObjectFactory();
|
||||
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setUsePool(true);
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jedisConnFactory.setDatabase(2);
|
||||
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> stringTemplate = new StringRedisTemplate(jedisConnFactory);
|
||||
RedisTemplate<String, Person> personTemplate = new RedisTemplate<String, Person>();
|
||||
personTemplate.setConnectionFactory(jedisConnFactory);
|
||||
personTemplate.afterPropertiesSet();
|
||||
|
||||
// create RJC
|
||||
|
||||
RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory();
|
||||
rjcConnFactory.setUsePool(false);
|
||||
rjcConnFactory.setPort(SettingsUtils.getPort());
|
||||
rjcConnFactory.setHostName(SettingsUtils.getHost());
|
||||
rjcConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> stringTemplateRJC = new StringRedisTemplate(rjcConnFactory);
|
||||
RedisTemplate<String, Person> personTemplateRJC = new RedisTemplate<String, Person>();
|
||||
personTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
personTemplateRJC.afterPropertiesSet();
|
||||
|
||||
|
||||
return Arrays.asList(new Object[][] { { stringFactory, stringTemplate }, { personFactory, personTemplate },
|
||||
{ stringFactory, stringTemplateRJC }, { personFactory, personTemplateRJC }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.support.collections.ObjectFactory;
|
||||
|
||||
/**
|
||||
* Base test class for PubSub integration tests
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class PubSubTests<T> {
|
||||
|
||||
private static final String CHANNEL = "pubsub::test";
|
||||
|
||||
protected RedisMessageListenerContainer container;
|
||||
protected ObjectFactory<T> factory;
|
||||
protected RedisTemplate template;
|
||||
|
||||
private final BlockingDeque<String> bag = new LinkedBlockingDeque<String>(99);
|
||||
|
||||
private final Object handler = new Object() {
|
||||
void handleMessage(String message) {
|
||||
bag.add(message);
|
||||
}
|
||||
};
|
||||
|
||||
private final MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
adapter.setSerializer(template.getValueSerializer());
|
||||
|
||||
container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(template.getConnectionFactory());
|
||||
container.setBeanName("container");
|
||||
container.addMessageListener(adapter, Arrays.asList(new ChannelTopic(CHANNEL)));
|
||||
container.afterPropertiesSet();
|
||||
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
container.destroy();
|
||||
}
|
||||
|
||||
public PubSubTests(ObjectFactory<T> factory, RedisTemplate template) {
|
||||
this.factory = factory;
|
||||
this.template = template;
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
return PubSubTestParams.testParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new instance of T
|
||||
* @return
|
||||
*/
|
||||
protected T getT() {
|
||||
return factory.instance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainerSubscribe() throws Exception {
|
||||
String payload1 = "do";
|
||||
String payload2 = "re mi";
|
||||
|
||||
template.convertAndSend(CHANNEL, payload1);
|
||||
template.convertAndSend(CHANNEL, payload2);
|
||||
|
||||
Set<String> set = new LinkedHashSet<String>();
|
||||
set.add(bag.poll(1, TimeUnit.SECONDS));
|
||||
set.add(bag.poll(1, TimeUnit.SECONDS));
|
||||
|
||||
System.out.println(set);
|
||||
|
||||
assertTrue(set.contains(payload1));
|
||||
assertTrue(set.contains(payload2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageBatch() throws Exception {
|
||||
int COUNT = 10;
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
template.convertAndSend(CHANNEL, "message=" + i);
|
||||
}
|
||||
|
||||
Thread.sleep(1000);
|
||||
assertEquals(COUNT, bag.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener.adapter;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class ContainerXmlSetupTest {
|
||||
|
||||
@Test
|
||||
public void testContainerSetup() throws Exception {
|
||||
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(
|
||||
"/org/springframework/data/redis/listener/container.xml");
|
||||
RedisMessageListenerContainer container = ctx.getBean("redisContainer", RedisMessageListenerContainer.class);
|
||||
assertTrue(container.isRunning());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener.adapter;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.data.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Unit test for MessageListenerAdapter.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class MessageListenerTest {
|
||||
|
||||
private static final RedisSerializer serializer = new StringRedisSerializer();
|
||||
private static final String CHANNEL = "some::test:";
|
||||
private static final byte[] RAW_CHANNEL = serializer.serialize(CHANNEL);
|
||||
private static final String PAYLOAD = "do re mi";
|
||||
private static final byte[] RAW_PAYLOAD = serializer.serialize(PAYLOAD);
|
||||
private static final Message STRING_MSG = new DefaultMessage(RAW_CHANNEL, RAW_PAYLOAD);
|
||||
|
||||
private MessageListenerAdapter adapter;
|
||||
|
||||
public static interface Delegate {
|
||||
void handleMessage(String argument);
|
||||
|
||||
void customMethod(String arg);
|
||||
}
|
||||
|
||||
@Mock
|
||||
private Delegate target;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.adapter = new MessageListenerAdapter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatWhenNoDelegateIsSuppliedTheDelegateIsAssumedToBeTheMessageListenerAdapterItself()
|
||||
throws Exception {
|
||||
assertSame(adapter, adapter.getDelegate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatTheDefaultMessageHandlingMethodNameIsTheConstantDefault() throws Exception {
|
||||
assertEquals(MessageListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD, adapter.getDefaultListenerMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdapterWithListenerAndDefaultMessage() throws Exception {
|
||||
MessageListener mock = mock(MessageListener.class);
|
||||
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(mock);
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
verify(mock).onMessage(STRING_MSG, null);
|
||||
}
|
||||
|
||||
|
||||
public void testRawMessage() throws Exception {
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(target);
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
|
||||
verify(target).handleMessage(PAYLOAD);
|
||||
}
|
||||
|
||||
|
||||
public void testCustomMethod() throws Exception {
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(target);
|
||||
adapter.setDefaultListenerMethod("customMethod");
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
|
||||
verify(target).customMethod(PAYLOAD);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener.adapter;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisMDP {
|
||||
|
||||
public void handleMessage(String message) {
|
||||
System.out.println("Received message " + message);
|
||||
}
|
||||
|
||||
public void anotherHandle(String message) {
|
||||
System.out.println("[*] Received message " + message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.listener.adapter;
|
||||
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class ThrowableMessageListener implements MessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
throw new IllegalStateException("throwing exception for message " + message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.mapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.Address;
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.hash.HashMapper;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractHashMapperTest {
|
||||
protected abstract HashMapper mapperFor(Class t);
|
||||
|
||||
private void test(Object o) {
|
||||
HashMapper<Object, Object, Object> mapper = mapperFor(o.getClass());
|
||||
Map hash = mapper.toHash(o);
|
||||
System.out.println("object hash " + hash.size() + " is " + hash);
|
||||
assertEquals(o, mapper.fromHash(hash));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleBean() throws Exception {
|
||||
test(new Address("Broadway", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedBean() throws Exception {
|
||||
test(new Person("George", "Enescu", 74, new Address("liveni", 19)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.mapping;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.hash.BeanUtilsHashMapper;
|
||||
import org.springframework.data.redis.hash.HashMapper;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class BeanUtilsHashMapperTest extends AbstractHashMapperTest {
|
||||
|
||||
@Override
|
||||
protected HashMapper mapperFor(Class t) {
|
||||
return new BeanUtilsHashMapper(t);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void testNestedBean() throws Exception {
|
||||
super.testNestedBean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.mapping;
|
||||
|
||||
import org.springframework.data.redis.hash.HashMapper;
|
||||
import org.springframework.data.redis.hash.JacksonHashMapper;
|
||||
|
||||
public class JacksonHashMapperTest extends AbstractHashMapperTest {
|
||||
|
||||
@Override
|
||||
protected HashMapper mapperFor(Class t) {
|
||||
return new JacksonHashMapper(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.serializer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.Address;
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.OxmSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
|
||||
|
||||
public class SimpleRedisSerializerTests {
|
||||
|
||||
private static class A implements Serializable {
|
||||
private Integer value = Integer.valueOf(30);
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((value == null) ? 0 : value.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
A other = (A) obj;
|
||||
if (value == null) {
|
||||
if (other.value != null)
|
||||
return false;
|
||||
}
|
||||
else if (!value.equals(other.value))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class B implements Serializable {
|
||||
private String name = getClass().getName();
|
||||
private A a = new A();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((a == null) ? 0 : a.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
B other = (B) obj;
|
||||
if (a == null) {
|
||||
if (other.a != null)
|
||||
return false;
|
||||
}
|
||||
else if (!a.equals(other.a))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
}
|
||||
else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private RedisSerializer serializer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
serializer = new JdkSerializationRedisSerializer();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
serializer = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicSerializationRoundtrip() throws Exception {
|
||||
Integer integer = new Integer(300);
|
||||
verifySerializedObjects(new Integer(300), new Double(200), new B());
|
||||
}
|
||||
|
||||
private void verifySerializedObjects(Object... objects) {
|
||||
for (Object object : objects) {
|
||||
assertEquals("Incorrectly (de)serialized object " + object, object,
|
||||
serializer.deserialize(serializer.serialize(object)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEncodedSerialization() {
|
||||
String value = UUID.randomUUID().toString();
|
||||
assertEquals(value, serializer.deserialize(serializer.serialize(value)));
|
||||
assertEquals(value, serializer.deserialize(serializer.serialize(value)));
|
||||
assertEquals(value, serializer.deserialize(serializer.serialize(value)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersonSerialization() throws Exception {
|
||||
String value = UUID.randomUUID().toString();
|
||||
Person p1 = new Person(value, value, 1, new Address(value, 2));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOxmSerializer() throws Exception {
|
||||
XStreamMarshaller xstream = new XStreamMarshaller();
|
||||
xstream.afterPropertiesSet();
|
||||
|
||||
OxmSerializer serializer = new OxmSerializer(xstream, xstream);
|
||||
|
||||
String value = UUID.randomUUID().toString();
|
||||
Person p1 = new Person(value, value, 1, new Address(value, 2));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonSerializer() throws Exception {
|
||||
JacksonJsonRedisSerializer<Person> serializer = new JacksonJsonRedisSerializer<Person>(Person.class);
|
||||
String value = UUID.randomUUID().toString();
|
||||
Person p1 = new Person(value, value, 1, new Address(value, 2));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.core.BoundKeyOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.ObjectFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class BoundKeyOperationsTest {
|
||||
private BoundKeyOperations<Object> keyOps;
|
||||
private ObjectFactory<Object> objFactory;
|
||||
private RedisTemplate template;
|
||||
|
||||
public BoundKeyOperationsTest(BoundKeyOperations<Object> keyOps, ObjectFactory<Object> objFactory,
|
||||
RedisTemplate template) {
|
||||
this.objFactory = objFactory;
|
||||
this.keyOps = keyOps;
|
||||
this.template = template;
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@After
|
||||
public void stop() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
return BoundKeyParams.testParams();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRename() throws Exception {
|
||||
Object key = keyOps.getKey();
|
||||
assertNotNull(key);
|
||||
Object newName = objFactory.instance();
|
||||
keyOps.rename(newName);
|
||||
assertEquals(newName, keyOps.getKey());
|
||||
keyOps.rename(key);
|
||||
assertEquals(key, keyOps.getKey());
|
||||
}
|
||||
@Test
|
||||
public void testExpire() throws Exception {
|
||||
assertEquals(Long.valueOf(-1), keyOps.getExpire());
|
||||
if (keyOps.expire(10, TimeUnit.SECONDS)) {
|
||||
long expire = keyOps.getExpire().longValue();
|
||||
assertTrue(expire <= 10 && expire > 5);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersist() throws Exception {
|
||||
keyOps.persist();
|
||||
assertEquals(Long.valueOf(-1), keyOps.getExpire());
|
||||
if (keyOps.expire(10, TimeUnit.SECONDS)) {
|
||||
assertTrue(keyOps.getExpire().longValue() > 0);
|
||||
}
|
||||
keyOps.persist();
|
||||
assertEquals(-1, keyOps.getExpire().longValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisList;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisMap;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisSet;
|
||||
import org.springframework.data.redis.support.collections.RedisList;
|
||||
import org.springframework.data.redis.support.collections.StringObjectFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class BoundKeyParams {
|
||||
|
||||
public static Collection<Object[]> testParams() {
|
||||
// create Jedis Factory
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
// jredis factory
|
||||
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
|
||||
jredisConnFactory.setUsePool(true);
|
||||
jredisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jredisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jredisConnFactory.afterPropertiesSet();
|
||||
|
||||
StringRedisTemplate templateJS = new StringRedisTemplate(jedisConnFactory);
|
||||
StringRedisTemplate templateJR = new StringRedisTemplate(jredisConnFactory);
|
||||
|
||||
StringObjectFactory sof = new StringObjectFactory();
|
||||
|
||||
DefaultRedisMap mapJS = new DefaultRedisMap("bound:key:map", templateJS);
|
||||
|
||||
DefaultRedisSet setJS = new DefaultRedisSet("bound:key:set", templateJS);
|
||||
|
||||
RedisList list = new DefaultRedisList("bound:key:list", templateJS);
|
||||
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ new RedisAtomicInteger("bound:key:int", jedisConnFactory), sof, templateJS },
|
||||
{ new RedisAtomicLong("bound:key:long", jedisConnFactory), sof, templateJS },
|
||||
{ list, sof, templateJS }, { setJS, sof, templateJS }, { mapJS, sof, templateJS } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.atomic;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AtomicCountersParam {
|
||||
|
||||
public static Collection<Object[]> testParams() {
|
||||
// create Jedis Factory
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
// jredis factory
|
||||
// JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
|
||||
// jredisConnFactory.setUsePool(true);
|
||||
// jredisConnFactory.setPort(SettingsUtils.getPort());
|
||||
// jredisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
// jredisConnFactory.afterPropertiesSet();
|
||||
|
||||
return Arrays.asList(new Object[][] { { jedisConnFactory } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support.atomic;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicTests {
|
||||
|
||||
private RedisAtomicInteger intCounter;
|
||||
private RedisAtomicLong longCounter;
|
||||
private RedisConnectionFactory factory;
|
||||
|
||||
|
||||
public RedisAtomicTests(RedisConnectionFactory factory) {
|
||||
intCounter = new RedisAtomicInteger(getClass().getSimpleName() + ":int", factory);
|
||||
longCounter = new RedisAtomicLong(getClass().getSimpleName() + ":long", factory);
|
||||
this.factory = factory;
|
||||
ConnectionFactoryTracker.add(factory);
|
||||
}
|
||||
|
||||
@After
|
||||
public void stop() {
|
||||
RedisConnection connection = factory.getConnection();
|
||||
connection.flushDb();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
return AtomicCountersParam.testParams();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntCheckAndSet() throws Exception {
|
||||
intCounter.set(0);
|
||||
assertFalse(intCounter.compareAndSet(1, 10));
|
||||
assertTrue(intCounter.compareAndSet(0, 10));
|
||||
assertTrue(intCounter.compareAndSet(10, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongCheckAndSet() throws Exception {
|
||||
longCounter.set(0);
|
||||
assertFalse(longCounter.compareAndSet(1, 10));
|
||||
assertTrue(longCounter.compareAndSet(0, 10));
|
||||
assertTrue(longCounter.compareAndSet(10, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongIncrement() throws Exception {
|
||||
longCounter.set(0);
|
||||
assertEquals(1, longCounter.incrementAndGet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntIncrement() throws Exception {
|
||||
intCounter.set(0);
|
||||
assertEquals(1, intCounter.incrementAndGet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongCustomIncrement() throws Exception {
|
||||
longCounter.set(0);
|
||||
long delta = 5;
|
||||
assertEquals(delta, longCounter.addAndGet(delta));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntCustomIncrement() throws Exception {
|
||||
intCounter.set(0);
|
||||
int delta = 5;
|
||||
assertEquals(delta, intCounter.addAndGet(delta));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadExistingValue() throws Exception {
|
||||
longCounter.set(5);
|
||||
RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory);
|
||||
assertEquals(longCounter.get(), keyCopy.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.AbstractRedisCollection;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
|
||||
|
||||
/**
|
||||
* Base test for Redis collections.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public abstract class AbstractRedisCollectionTests<T> {
|
||||
|
||||
protected AbstractRedisCollection<T> collection;
|
||||
protected ObjectFactory<T> factory;
|
||||
protected RedisTemplate template;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
collection = createCollection();
|
||||
}
|
||||
|
||||
abstract AbstractRedisCollection<T> createCollection();
|
||||
|
||||
abstract RedisStore copyStore(RedisStore store);
|
||||
|
||||
|
||||
public AbstractRedisCollectionTests(ObjectFactory<T> factory, RedisTemplate template) {
|
||||
this.factory = factory;
|
||||
this.template = template;
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
return CollectionTestParams.testParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new instance of T
|
||||
* @return
|
||||
*/
|
||||
protected T getT() {
|
||||
return factory.instance();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// remove the collection entirely since clear() doesn't always work
|
||||
collection.getOperations().delete(Collections.singleton(collection.getKey()));
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
T t1 = getT();
|
||||
assertThat(collection.add(t1), is(true));
|
||||
assertThat(collection, hasItem(t1));
|
||||
assertEquals(1, collection.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testAddAll() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2, t3);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
assertThat(collection, hasItem(t1));
|
||||
assertThat(collection, hasItem(t2));
|
||||
assertThat(collection, hasItem(t3));
|
||||
assertEquals(collection.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
T t1 = getT();
|
||||
assertEquals(0, collection.size());
|
||||
collection.add(t1);
|
||||
assertEquals(1, collection.size());
|
||||
collection.clear();
|
||||
assertEquals(0, collection.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsObject() {
|
||||
T t1 = getT();
|
||||
assertThat(collection, not(hasItem(t1)));
|
||||
assertThat(collection.add(t1), is(true));
|
||||
assertThat(collection, hasItem(t1));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testContainsAll() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2, t3);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
assertThat(collection.containsAll(list), is(true));
|
||||
assertThat(collection, hasItems(t1, t2, t3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
//assertEquals(collection, copyStore(collection));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
assertThat(collection.hashCode(), not(equalTo(collection.getKey().hashCode())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
assertEquals(0, collection.size());
|
||||
assertTrue(collection.isEmpty());
|
||||
collection.add(getT());
|
||||
assertEquals(1, collection.size());
|
||||
assertFalse(collection.isEmpty());
|
||||
collection.clear();
|
||||
assertTrue(collection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2, t3, t4);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
Iterator<T> iterator = collection.iterator();
|
||||
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
assertEquals(t4, iterator.next());
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveObject() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
assertEquals(0, collection.size());
|
||||
assertThat(collection.add(t1), is(true));
|
||||
assertThat(collection.add(t2), is(true));
|
||||
assertEquals(2, collection.size());
|
||||
assertThat(collection.remove(t3), is(false));
|
||||
assertThat(collection.remove(t2), is(true));
|
||||
assertThat(collection.remove(t2), is(false));
|
||||
assertEquals(1, collection.size());
|
||||
assertThat(collection.remove(t1), is(true));
|
||||
assertEquals(0, collection.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAll() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2, t3);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
assertThat(collection.containsAll(list), is(true));
|
||||
assertThat(collection, hasItems(t1, t2, t3));
|
||||
|
||||
List<T> newList = Arrays.asList(getT(), getT());
|
||||
List<T> partialList = Arrays.asList(getT(), t1, getT());
|
||||
|
||||
assertThat(collection.removeAll(newList), is(false));
|
||||
assertThat(collection.removeAll(partialList), is(true));
|
||||
assertThat(collection, not(hasItem(t1)));
|
||||
assertThat(collection, hasItems(t2, t3));
|
||||
assertThat(collection.removeAll(list), is(true));
|
||||
assertThat(collection, not(hasItems(t2, t3)));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testRetainAll() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2);
|
||||
List<T> newList = Arrays.asList(t2, t3);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
assertThat(collection, hasItems(t1, t2));
|
||||
assertThat(collection.retainAll(newList), is(true));
|
||||
assertThat(collection, not(hasItem(t1)));
|
||||
assertThat(collection, hasItem(t2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
assertEquals(0, collection.size());
|
||||
assertTrue(collection.isEmpty());
|
||||
collection.add(getT());
|
||||
assertEquals(1, collection.size());
|
||||
collection.add(getT());
|
||||
collection.add(getT());
|
||||
assertEquals(3, collection.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArray() {
|
||||
Object[] expectedArray = new Object[] { getT(), getT(), getT() };
|
||||
List<T> list = (List<T>) Arrays.asList(expectedArray);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
|
||||
Object[] array = collection.toArray();
|
||||
assertArrayEquals(expectedArray, array);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArrayWithGenerics() {
|
||||
Object[] expectedArray = new Object[] { getT(), getT(), getT() };
|
||||
List<T> list = (List<T>) Arrays.asList(expectedArray);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
|
||||
Object[] array = collection.toArray(new Object[expectedArray.length]);
|
||||
assertArrayEquals(expectedArray, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
String name = collection.toString();
|
||||
collection.add(getT());
|
||||
assertEquals(name, collection.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKey() throws Exception {
|
||||
assertNotNull(collection.getKey());
|
||||
}
|
||||
|
||||
protected boolean isJredis() {
|
||||
return template.getConnectionFactory().getClass().getSimpleName().startsWith("Jredis");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisList;
|
||||
import org.springframework.data.redis.support.collections.RedisList;
|
||||
|
||||
/**
|
||||
* Integration test for RedisList
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionTests<T> {
|
||||
|
||||
protected RedisList<T> list;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>AbstractRedisListTests</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param template
|
||||
*/
|
||||
public AbstractRedisListTests(ObjectFactory<T> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
list = (RedisList<T>) 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(t2, list.get(1));
|
||||
list.add(2, t3);
|
||||
assertEquals(t3, list.get(2));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.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<T> asList = Arrays.asList(t3, t4);
|
||||
|
||||
assertEquals(t1, list.get(0));
|
||||
list.addAll(0, asList);
|
||||
// verify insertion order
|
||||
assertEquals(t3, list.get(0));
|
||||
assertEquals(t4, list.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addAllIndexCollectionTail() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
List<T> asList = Arrays.asList(t3, t4);
|
||||
|
||||
assertEquals(t1, list.get(0));
|
||||
assertTrue(list.addAll(2, asList));
|
||||
|
||||
// verify insertion order
|
||||
assertEquals(t3, list.get(2));
|
||||
assertEquals(t4, list.get(3));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void addAllIndexCollectionMiddle() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
List<T> asList = Arrays.asList(t3, t4);
|
||||
|
||||
assertEquals(t1, list.get(0));
|
||||
assertTrue(list.addAll(1, asList));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
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();
|
||||
|
||||
assertTrue(list.offer(t1));
|
||||
assertTrue(list.contains(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 testPop() {
|
||||
testPoll();
|
||||
}
|
||||
|
||||
@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, 1).get(0));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrim() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
|
||||
assertTrue(list.trim(0, 0).isEmpty());
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, list.trim(0, 0).size());
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(t1, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCappedCollection() throws Exception {
|
||||
RedisList<T> cappedList = new DefaultRedisList<T>(template.boundListOps(collection.getKey() + ":capped"), 1);
|
||||
T first = getT();
|
||||
cappedList.offer(first);
|
||||
assertEquals(1, cappedList.size());
|
||||
cappedList.add(getT());
|
||||
assertEquals(1, cappedList.size());
|
||||
T last = getT();
|
||||
cappedList.add(last);
|
||||
assertEquals(1, cappedList.size());
|
||||
assertEquals(first, cappedList.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddFirst() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
list.addFirst(t1);
|
||||
list.addFirst(t2);
|
||||
list.addFirst(t3);
|
||||
|
||||
Iterator<T> iterator = list.iterator();
|
||||
assertEquals(t3, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t1, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddLast() {
|
||||
testAdd();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescendingIterator() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
list.add(t3);
|
||||
|
||||
Iterator<T> iterator = list.descendingIterator();
|
||||
assertEquals(t3, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t1, iterator.next());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDrainToCollectionWithMaxElements() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
list.add(t3);
|
||||
|
||||
List<T> c = new ArrayList<T>();
|
||||
|
||||
list.drainTo(c, 2);
|
||||
assertEquals(1, list.size());
|
||||
assertThat(list, hasItem(t3));
|
||||
assertEquals(2, c.size());
|
||||
assertThat(c, hasItems(t1, t2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDrainToCollection() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
list.add(t3);
|
||||
|
||||
List<T> c = new ArrayList<T>();
|
||||
|
||||
list.drainTo(c);
|
||||
assertTrue(list.isEmpty());
|
||||
assertEquals(3, c.size());
|
||||
assertThat(c, hasItems(t1, t2, t3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirst() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
assertEquals(t1, list.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLast() {
|
||||
testAdd();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfferFirst() {
|
||||
testAddFirst();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfferLast() {
|
||||
testAddLast();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeekFirst() {
|
||||
testPeek();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeekLast() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
assertEquals(t2, list.peekLast());
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPollFirst() {
|
||||
testPoll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPollLast() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
T last = list.pollLast();
|
||||
assertEquals(t2, last);
|
||||
assertEquals(1, list.size());
|
||||
assertThat(list, hasItem(t1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPut() {
|
||||
testOffer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutFirst() {
|
||||
testAdd();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutLast() {
|
||||
testPut();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemainingCapacity() {
|
||||
assertEquals(Integer.MAX_VALUE, list.remainingCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveFirst() {
|
||||
testPop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveFirstOccurrence() {
|
||||
testRemove();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveLast() {
|
||||
testPollLast();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRmoveLastOccurrence() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
list.add(t1);
|
||||
list.add(t2);
|
||||
|
||||
list.removeLastOccurrence(t2);
|
||||
assertEquals(3, list.size());
|
||||
Iterator<T> iterator = list.iterator();
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t1, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTake() {
|
||||
testPoll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTakeFirst() {
|
||||
testTake();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTakeLast() {
|
||||
testPollLast();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,421 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisMap;
|
||||
import org.springframework.data.redis.support.collections.RedisMap;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
|
||||
/**
|
||||
* Integration test for Redis Map.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public abstract class AbstractRedisMapTests<K, V> {
|
||||
|
||||
protected RedisMap<K, V> map;
|
||||
protected ObjectFactory<K> keyFactory;
|
||||
protected ObjectFactory<V> valueFactory;
|
||||
protected RedisTemplate template;
|
||||
|
||||
abstract RedisMap<K, V> createMap();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
map = createMap();
|
||||
}
|
||||
|
||||
public AbstractRedisMapTests(ObjectFactory<K> keyFactory, ObjectFactory<V> valueFactory, RedisTemplate template) {
|
||||
this.keyFactory = keyFactory;
|
||||
this.valueFactory = valueFactory;
|
||||
this.template = template;
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
protected K getKey() {
|
||||
return keyFactory.instance();
|
||||
}
|
||||
|
||||
protected V getValue() {
|
||||
return valueFactory.instance();
|
||||
}
|
||||
|
||||
protected RedisStore copyStore(RedisStore store) {
|
||||
return new DefaultRedisMap(store.getKey(), store.getOperations());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// remove the collection entirely since clear() doesn't always work
|
||||
map.getOperations().delete(Collections.singleton(map.getKey()));
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
map.clear();
|
||||
assertEquals(0, map.size());
|
||||
map.put(getKey(), getValue());
|
||||
assertEquals(1, map.size());
|
||||
map.clear();
|
||||
assertEquals(0, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsKey() {
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
assertFalse(map.containsKey(k1));
|
||||
assertFalse(map.containsKey(k2));
|
||||
map.put(k1, getValue());
|
||||
assertTrue(map.containsKey(k1));
|
||||
map.put(k2, getValue());
|
||||
assertTrue(map.containsKey(k2));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testContainsValue() {
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
assertFalse(map.containsValue(v1));
|
||||
assertFalse(map.containsValue(v2));
|
||||
map.put(getKey(), v1);
|
||||
assertTrue(map.containsValue(v1));
|
||||
map.put(getKey(), v2);
|
||||
assertTrue(map.containsValue(v2));
|
||||
}
|
||||
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
RedisStore clone = copyStore(map);
|
||||
assertEquals(clone, map);
|
||||
assertEquals(clone, clone);
|
||||
assertEquals(map, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEquals() {
|
||||
RedisOperations<String, ?> ops = map.getOperations();
|
||||
RedisStore newInstance = new DefaultRedisMap<K, V>(ops.<K, V> boundHashOps(map.getKey() + ":new"));
|
||||
assertFalse(map.equals(newInstance));
|
||||
assertFalse(newInstance.equals(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
K k1 = getKey();
|
||||
V v1 = getValue();
|
||||
|
||||
assertNull(map.get(UUID.randomUUID().toString()));
|
||||
assertNull(map.get(k1));
|
||||
map.put(k1, v1);
|
||||
assertEquals(v1, map.get(k1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKey() {
|
||||
assertNotNull(map.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOperations() {
|
||||
assertEquals(template, map.getOperations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
assertThat(map.hashCode(), not(equalTo(map.getKey().hashCode())));
|
||||
assertEquals(map.hashCode(), copyStore(map).hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrement() {
|
||||
assumeTrue(!isJredis());
|
||||
K k1 = getKey();
|
||||
V v1 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
try {
|
||||
Long value = map.increment(k1, 1);
|
||||
System.out.println("Value is " + value);
|
||||
} catch (InvalidDataAccessApiUsageException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
map.clear();
|
||||
assertTrue(map.isEmpty());
|
||||
map.put(getKey(), getValue());
|
||||
assertFalse(map.isEmpty());
|
||||
map.clear();
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeySet() {
|
||||
map.clear();
|
||||
assertTrue(map.keySet().isEmpty());
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
K k3 = getKey();
|
||||
|
||||
map.put(k1, getValue());
|
||||
map.put(k2, getValue());
|
||||
map.put(k3, getValue());
|
||||
|
||||
Set<K> keySet = map.keySet();
|
||||
assertThat(keySet, hasItems(k1, k2, k3));
|
||||
assertEquals(3, keySet.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPut() {
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
map.put(k2, v2);
|
||||
|
||||
assertEquals(v1, map.get(k1));
|
||||
assertEquals(v2, map.get(k2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutAll() {
|
||||
assumeTrue(!isJredis());
|
||||
Map<K, V> m = new LinkedHashMap<K, V>();
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
m.put(k1, v1);
|
||||
m.put(k2, v2);
|
||||
|
||||
assertNull(map.get(k1));
|
||||
assertNull(map.get(k2));
|
||||
|
||||
map.putAll(m);
|
||||
|
||||
assertEquals(v1, map.get(k1));
|
||||
assertEquals(v2, map.get(k2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
assertNull(map.remove(k1));
|
||||
assertNull(map.remove(k2));
|
||||
|
||||
map.put(k1, v1);
|
||||
map.put(k2, v2);
|
||||
|
||||
assertEquals(v1, map.remove(k1));
|
||||
assertNull(map.remove(k1));
|
||||
assertNull(map.get(k1));
|
||||
|
||||
assertEquals(v2, map.remove(k2));
|
||||
assertNull(map.remove(k2));
|
||||
assertNull(map.get(k2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
assertEquals(0, map.size());
|
||||
map.put(getKey(), getValue());
|
||||
assertEquals(1, map.size());
|
||||
K k = getKey();
|
||||
map.put(k, getValue());
|
||||
assertEquals(2, map.size());
|
||||
map.remove(k);
|
||||
assertEquals(1, map.size());
|
||||
|
||||
map.clear();
|
||||
assertEquals(0, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValues() {
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
V v3 = getValue();
|
||||
|
||||
map.put(getKey(), v1);
|
||||
map.put(getKey(), v2);
|
||||
|
||||
Collection<V> values = map.values();
|
||||
assertEquals(2, values.size());
|
||||
assertThat(values, hasItems(v1, v2));
|
||||
|
||||
map.put(getKey(), v3);
|
||||
values = map.values();
|
||||
assertEquals(3, values.size());
|
||||
assertThat(values, hasItems(v1, v2, v3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntrySet() {
|
||||
assumeTrue(!isJredis());
|
||||
Set<Entry<K, V>> entries = map.entrySet();
|
||||
assertTrue(entries.isEmpty());
|
||||
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
map.put(k2, v1);
|
||||
|
||||
entries = map.entrySet();
|
||||
|
||||
Set<K> keys = new LinkedHashSet<K>();
|
||||
Collection<V> values = new ArrayList<V>();
|
||||
|
||||
for (Entry<K, V> entry : entries) {
|
||||
keys.add(entry.getKey());
|
||||
values.add(entry.getValue());
|
||||
}
|
||||
|
||||
assertEquals(2, keys.size());
|
||||
|
||||
assertThat(keys, hasItems(k1, k2));
|
||||
assertThat(values, hasItem(v1));
|
||||
assertThat(values, not(hasItem(v2)));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testConcurrentPutIfAbsent() {
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
assertNull(map.get(k1));
|
||||
assertNull(map.putIfAbsent(k1, v1));
|
||||
assertEquals(v1, map.putIfAbsent(k1, v2));
|
||||
assertEquals(v1, map.get(k1));
|
||||
|
||||
assertNull(map.putIfAbsent(k2, v2));
|
||||
assertEquals(v2, map.putIfAbsent(k2, v1));
|
||||
|
||||
assertEquals(v2, map.get(k2));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testConcurrentRemove() {
|
||||
K k1 = getKey();
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
assertFalse(map.remove(k1, v1));
|
||||
assertEquals(v1, map.get(k1));
|
||||
assertTrue(map.remove(k1, v1));
|
||||
assertNull(map.get(k1));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testConcurrentReplaceTwoArgs() {
|
||||
K k1 = getKey();
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
|
||||
assertFalse(map.replace(k1, v2, v1));
|
||||
assertEquals(v1, map.get(k1));
|
||||
assertTrue(map.replace(k1, v1, v2));
|
||||
assertEquals(v2, map.get(k1));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testConcurrentReplaceOneArg() {
|
||||
K k1 = getKey();
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
assertNull(map.replace(k1, v1));
|
||||
map.put(k1, v1);
|
||||
assertNull(map.replace(getKey(), v1));
|
||||
assertEquals(v1, map.replace(k1, v2));
|
||||
assertEquals(v2, map.get(k1));
|
||||
|
||||
}
|
||||
|
||||
private boolean isJredis() {
|
||||
return template.getConnectionFactory().getClass().getSimpleName().startsWith("Jredis");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisSet;
|
||||
import org.springframework.data.redis.support.collections.RedisSet;
|
||||
|
||||
/**
|
||||
* Integration test for Redis set.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTests<T> {
|
||||
|
||||
protected RedisSet<T> set;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new <code>AbstractRedisSetTests</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param template
|
||||
*/
|
||||
public AbstractRedisSetTests(ObjectFactory<T> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
set = (RedisSet<T>) collection;
|
||||
}
|
||||
|
||||
private RedisSet<T> createSetFor(String key) {
|
||||
return new DefaultRedisSet<T>((BoundSetOperations<String, T>) set.getOperations().boundSetOps(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiff() {
|
||||
RedisSet<T> diffSet1 = createSetFor("test:set:diff1");
|
||||
RedisSet<T> diffSet2 = createSetFor("test:set:diff2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
set.add(t3);
|
||||
|
||||
diffSet1.add(t2);
|
||||
diffSet2.add(t3);
|
||||
|
||||
Set<T> diff = set.diff(Arrays.asList(diffSet1, diffSet2));
|
||||
assertEquals(1, diff.size());
|
||||
assertThat(diff, hasItem(t1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffAndStore() {
|
||||
RedisSet<T> diffSet1 = createSetFor("test:set:diff1");
|
||||
RedisSet<T> diffSet2 = createSetFor("test:set:diff2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
set.add(t3);
|
||||
|
||||
diffSet1.add(t2);
|
||||
diffSet2.add(t3);
|
||||
diffSet2.add(t4);
|
||||
|
||||
String resultName = "test:set:diff:result:1";
|
||||
RedisSet<T> diff = set.diffAndStore(Arrays.asList(diffSet1, diffSet2), resultName);
|
||||
|
||||
assertEquals(1, diff.size());
|
||||
assertThat(diff, hasItem(t1));
|
||||
assertEquals(resultName, diff.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersect() {
|
||||
RedisSet<T> intSet1 = createSetFor("test:set:int1");
|
||||
RedisSet<T> intSet2 = createSetFor("test:set:int2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
set.add(t3);
|
||||
|
||||
intSet1.add(t2);
|
||||
intSet1.add(t4);
|
||||
intSet2.add(t2);
|
||||
intSet2.add(t3);
|
||||
|
||||
Set<T> inter = set.intersect(Arrays.asList(intSet1, intSet2));
|
||||
assertEquals(1, inter.size());
|
||||
assertThat(inter, hasItem(t2));
|
||||
}
|
||||
|
||||
|
||||
public void testIntersectAndStore() {
|
||||
RedisSet<T> intSet1 = createSetFor("test:set:int1");
|
||||
RedisSet<T> intSet2 = createSetFor("test:set:int2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
set.add(t3);
|
||||
|
||||
intSet1.add(t2);
|
||||
intSet1.add(t4);
|
||||
intSet2.add(t2);
|
||||
intSet2.add(t3);
|
||||
|
||||
String resultName = "test:set:intersect:result:1";
|
||||
RedisSet<T> inter = set.intersectAndStore(Arrays.asList(intSet1, intSet2), resultName);
|
||||
assertEquals(1, inter.size());
|
||||
assertThat(inter, hasItem(t2));
|
||||
assertEquals(resultName, inter.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnion() {
|
||||
RedisSet<T> unionSet1 = createSetFor("test:set:union1");
|
||||
RedisSet<T> unionSet2 = createSetFor("test:set:union2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
|
||||
unionSet1.add(t2);
|
||||
unionSet1.add(t4);
|
||||
unionSet2.add(t3);
|
||||
|
||||
Set<T> union = set.union(Arrays.asList(unionSet1, unionSet2));
|
||||
assertEquals(4, union.size());
|
||||
assertThat(union, hasItems(t1, t2, t3, t4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnionAndStore() {
|
||||
RedisSet<T> unionSet1 = createSetFor("test:set:union1");
|
||||
RedisSet<T> unionSet2 = createSetFor("test:set:union2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
set.add(t1);
|
||||
set.add(t2);
|
||||
|
||||
unionSet1.add(t2);
|
||||
unionSet1.add(t4);
|
||||
unionSet2.add(t3);
|
||||
|
||||
String resultName = "test:set:union:result:1";
|
||||
RedisSet<T> union = set.unionAndStore(Arrays.asList(unionSet1, unionSet2), resultName);
|
||||
assertEquals(4, union.size());
|
||||
assertThat(union, hasItems(t1, t2, t3, t4));
|
||||
assertEquals(resultName, union.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
List<T> list = Arrays.asList(t1, t2, t3, t4);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
Iterator<T> iterator = collection.iterator();
|
||||
|
||||
List<T> result = new ArrayList<T>(list);
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
result.remove(iterator.next());
|
||||
}
|
||||
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArray() {
|
||||
Object[] expectedArray = new Object[] { getT(), getT(), getT() };
|
||||
List<T> list = (List<T>) Arrays.asList(expectedArray);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
|
||||
Object[] array = collection.toArray();
|
||||
|
||||
List<T> result = new ArrayList<T>(list);
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
result.remove(array[i]);
|
||||
}
|
||||
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArrayWithGenerics() {
|
||||
Object[] expectedArray = new Object[] { getT(), getT(), getT() };
|
||||
List<T> list = (List<T>) Arrays.asList(expectedArray);
|
||||
|
||||
assertThat(collection.addAll(list), is(true));
|
||||
|
||||
Object[] array = collection.toArray(new Object[expectedArray.length]);
|
||||
List<T> result = new ArrayList<T>(list);
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
result.remove(array[i]);
|
||||
}
|
||||
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.core.BoundZSetOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
|
||||
import org.springframework.data.redis.support.collections.RedisZSet;
|
||||
|
||||
/**
|
||||
* Integration test for Redis ZSet.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTests<T> {
|
||||
|
||||
protected RedisZSet<T> zSet;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>AbstractRedisZSetTest</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param template
|
||||
*/
|
||||
public AbstractRedisZSetTest(ObjectFactory<T> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
zSet = (RedisZSet<T>) collection;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddWithScore() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
Iterator<T> iterator = zSet.iterator();
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1);
|
||||
zSet.add(t2);
|
||||
zSet.add(t3);
|
||||
|
||||
Double d = new Double("1");
|
||||
|
||||
assertEquals(d, zSet.score(t1));
|
||||
assertEquals(d, zSet.score(t2));
|
||||
assertEquals(d, zSet.score(t3));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFirst() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
assertEquals(3, zSet.size());
|
||||
assertEquals(t1, zSet.first());
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void testFirstException() throws Exception {
|
||||
zSet.first();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLast() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
assertEquals(3, zSet.size());
|
||||
assertEquals(t3, zSet.last());
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void testLastException() throws Exception {
|
||||
zSet.last();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRank() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
assertEquals(Long.valueOf(0), zSet.rank(t1));
|
||||
assertEquals(Long.valueOf(1), zSet.rank(t2));
|
||||
assertEquals(Long.valueOf(2), zSet.rank(t3));
|
||||
assertNull(zSet.rank(getT()));
|
||||
//assertNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReverseRank() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
assertEquals(Long.valueOf(0), zSet.reverseRank(t3));
|
||||
assertEquals(Long.valueOf(1), zSet.reverseRank(t2));
|
||||
assertEquals(Long.valueOf(2), zSet.reverseRank(t1));
|
||||
assertNull(zSet.rank(getT()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScore() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 3);
|
||||
zSet.add(t2, 4);
|
||||
zSet.add(t3, 5);
|
||||
|
||||
assertNull(zSet.score(getT()));
|
||||
assertEquals(Double.valueOf(3), zSet.score(t1));
|
||||
assertEquals(Double.valueOf(4), zSet.score(t2));
|
||||
assertEquals(Double.valueOf(5), zSet.score(t3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultScore() {
|
||||
assertEquals(1, zSet.getDefaultScore(), 0);
|
||||
}
|
||||
|
||||
private RedisZSet<T> createZSetFor(String key) {
|
||||
return new DefaultRedisZSet<T>((BoundZSetOperations<String, T>) zSet.getOperations().boundZSetOps(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersectAndStore() {
|
||||
assumeTrue(!isJredis());
|
||||
RedisZSet<T> interSet1 = createZSetFor("test:zset:inter1");
|
||||
RedisZSet<T> interSet2 = createZSetFor("test:zset:inter");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
|
||||
interSet1.add(t2, 2);
|
||||
interSet1.add(t4, 3);
|
||||
interSet2.add(t2, 2);
|
||||
interSet2.add(t3, 3);
|
||||
|
||||
String resultName = "test:zset:inter:result:1";
|
||||
RedisZSet<T> inter = zSet.intersectAndStore(Arrays.asList(interSet1, interSet2), resultName);
|
||||
|
||||
assertEquals(1, inter.size());
|
||||
assertThat(inter, hasItem(t2));
|
||||
assertEquals(Double.valueOf(6), inter.score(t2));
|
||||
assertEquals(resultName, inter.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRange() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
|
||||
Set<T> range = zSet.range(1, 2);
|
||||
assertEquals(2, range.size());
|
||||
Iterator<T> iterator = range.iterator();
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReverseRange() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
|
||||
Set<T> range = zSet.reverseRange(1, 2);
|
||||
assertEquals(2, range.size());
|
||||
Iterator<T> iterator = range.iterator();
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t1, iterator.next());
|
||||
}
|
||||
|
||||
public void testRangeByScore() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
|
||||
Set<T> range = zSet.rangeByScore(1.5, 3.5);
|
||||
assertEquals(2, range.size());
|
||||
assertThat(range, hasItems(t2, t3));
|
||||
|
||||
Iterator<T> iterator = range.iterator();
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
zSet.add(t4, 4);
|
||||
|
||||
zSet.remove(1, 2);
|
||||
|
||||
assertEquals(2, zSet.size());
|
||||
Iterator<T> iterator = zSet.iterator();
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t4, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveByScore() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
zSet.add(t4, 4);
|
||||
|
||||
zSet.removeByScore(1.5, 2.5);
|
||||
|
||||
assertEquals(3, zSet.size());
|
||||
Iterator<T> iterator = zSet.iterator();
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
assertEquals(t4, iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnionAndStore() {
|
||||
assumeTrue(!isJredis());
|
||||
RedisZSet<T> unionSet1 = createZSetFor("test:zset:union1");
|
||||
RedisZSet<T> unionSet2 = createZSetFor("test:zset:union2");
|
||||
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
|
||||
unionSet1.add(t2, 2);
|
||||
unionSet1.add(t4, 5);
|
||||
unionSet2.add(t3, 6);
|
||||
|
||||
String resultName = "test:zset:union:result:1";
|
||||
RedisZSet<T> union = zSet.unionAndStore(Arrays.asList(unionSet1, unionSet2), resultName);
|
||||
assertEquals(4, union.size());
|
||||
assertThat(union, hasItems(t1, t2, t3, t4));
|
||||
assertEquals(resultName, union.getKey());
|
||||
|
||||
assertEquals(Double.valueOf(1), union.score(t1));
|
||||
assertEquals(Double.valueOf(4), union.score(t2));
|
||||
assertEquals(Double.valueOf(6), union.score(t3));
|
||||
assertEquals(Double.valueOf(5), union.score(t4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
zSet.add(t4, 4);
|
||||
|
||||
Iterator<T> iterator = collection.iterator();
|
||||
|
||||
assertEquals(t1, iterator.next());
|
||||
assertEquals(t2, iterator.next());
|
||||
assertEquals(t3, iterator.next());
|
||||
assertEquals(t4, iterator.next());
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArray() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
zSet.add(t4, 4);
|
||||
|
||||
Object[] array = collection.toArray();
|
||||
assertArrayEquals(new Object[] { t1, t2, t3, t4 }, array);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToArrayWithGenerics() {
|
||||
T t1 = getT();
|
||||
T t2 = getT();
|
||||
T t3 = getT();
|
||||
T t4 = getT();
|
||||
|
||||
zSet.add(t1, 1);
|
||||
zSet.add(t2, 2);
|
||||
zSet.add(t3, 3);
|
||||
zSet.add(t4, 4);
|
||||
|
||||
Object[] array = collection.toArray(new Object[zSet.size()]);
|
||||
assertArrayEquals(new Object[] { t1, t2, t3, t4 }, array);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.rjc.RjcConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.OxmSerializer;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public abstract class CollectionTestParams {
|
||||
|
||||
public static Collection<Object[]> testParams() {
|
||||
// XStream serializer
|
||||
XStreamMarshaller xstream = new XStreamMarshaller();
|
||||
try {
|
||||
xstream.afterPropertiesSet();
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Cannot init XStream", ex);
|
||||
}
|
||||
OxmSerializer serializer = new OxmSerializer(xstream, xstream);
|
||||
JacksonJsonRedisSerializer<Person> jsonSerializer = new JacksonJsonRedisSerializer<Person>(Person.class);
|
||||
|
||||
// create Jedis Factory
|
||||
ObjectFactory<String> stringFactory = new StringObjectFactory();
|
||||
ObjectFactory<Person> personFactory = new PersonObjectFactory();
|
||||
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setUsePool(true);
|
||||
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> stringTemplate = new StringRedisTemplate(jedisConnFactory);
|
||||
RedisTemplate<String, Person> personTemplate = new RedisTemplate<String, Person>();
|
||||
personTemplate.setConnectionFactory(jedisConnFactory);
|
||||
personTemplate.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> xstreamStringTemplate = new RedisTemplate<String, String>();
|
||||
xstreamStringTemplate.setConnectionFactory(jedisConnFactory);
|
||||
xstreamStringTemplate.setDefaultSerializer(serializer);
|
||||
xstreamStringTemplate.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xstreamPersonTemplate = new RedisTemplate<String, Person>();
|
||||
xstreamPersonTemplate.setConnectionFactory(jedisConnFactory);
|
||||
xstreamPersonTemplate.setValueSerializer(serializer);
|
||||
xstreamPersonTemplate.afterPropertiesSet();
|
||||
|
||||
// json
|
||||
RedisTemplate<String, Person> jsonPersonTemplate = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplate.setConnectionFactory(jedisConnFactory);
|
||||
jsonPersonTemplate.setValueSerializer(jsonSerializer);
|
||||
jsonPersonTemplate.afterPropertiesSet();
|
||||
|
||||
// jredis
|
||||
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
|
||||
jredisConnFactory.setUsePool(true);
|
||||
|
||||
jredisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jredisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
jredisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> stringTemplateJR = new StringRedisTemplate(jredisConnFactory);
|
||||
RedisTemplate<String, Person> personTemplateJR = new RedisTemplate<String, Person>();
|
||||
personTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
personTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xstreamStringTemplateJR = new RedisTemplate<String, Person>();
|
||||
xstreamStringTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
xstreamStringTemplateJR.setDefaultSerializer(serializer);
|
||||
xstreamStringTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xstreamPersonTemplateJR = new RedisTemplate<String, Person>();
|
||||
xstreamPersonTemplateJR.setValueSerializer(serializer);
|
||||
xstreamPersonTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
xstreamPersonTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateJR = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateJR.setValueSerializer(jsonSerializer);
|
||||
jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
jsonPersonTemplateJR.afterPropertiesSet();
|
||||
|
||||
|
||||
// rjc
|
||||
RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory();
|
||||
rjcConnFactory.setUsePool(true);
|
||||
rjcConnFactory.setPort(SettingsUtils.getPort());
|
||||
rjcConnFactory.setHostName(SettingsUtils.getHost());
|
||||
rjcConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> stringTemplateRJC = new StringRedisTemplate(rjcConnFactory);
|
||||
RedisTemplate<String, Person> personTemplateRJC = new RedisTemplate<String, Person>();
|
||||
personTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
personTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xstreamStringTemplateRJC = new RedisTemplate<String, Person>();
|
||||
xstreamStringTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
xstreamStringTemplateRJC.setDefaultSerializer(serializer);
|
||||
xstreamStringTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xstreamPersonTemplateRJC = new RedisTemplate<String, Person>();
|
||||
xstreamPersonTemplateRJC.setValueSerializer(serializer);
|
||||
xstreamPersonTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
xstreamPersonTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateRJC = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateRJC.setValueSerializer(jsonSerializer);
|
||||
jsonPersonTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
jsonPersonTemplateRJC.afterPropertiesSet();
|
||||
|
||||
return Arrays.asList(new Object[][] { { stringFactory, stringTemplateRJC },
|
||||
{ personFactory, personTemplateRJC }, { stringFactory, stringTemplateJR },
|
||||
{ personFactory, personTemplateJR }, { stringFactory, stringTemplate },
|
||||
{ personFactory, personTemplate }, { stringFactory, xstreamStringTemplate },
|
||||
{ personFactory, xstreamPersonTemplate }, { stringFactory, xstreamStringTemplateJR },
|
||||
{ personFactory, xstreamPersonTemplateJR }, { personFactory, jsonPersonTemplate },
|
||||
{ personFactory, jsonPersonTemplateJR }, { stringFactory, xstreamStringTemplateRJC },
|
||||
{ personFactory, xstreamPersonTemplateRJC }, { personFactory, jsonPersonTemplateRJC } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
/**
|
||||
* Simple object factory.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public interface ObjectFactory<T> {
|
||||
|
||||
T instance();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.redis.Address;
|
||||
import org.springframework.data.redis.Person;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class PersonObjectFactory implements ObjectFactory<Person> {
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
@Override
|
||||
public Person instance() {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
return new Person(uuid, uuid, ++counter, new Address(uuid, counter));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisList;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisMap;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisSet;
|
||||
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean;
|
||||
import org.springframework.data.redis.support.collections.RedisProperties;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisCollectionFactoryBeanTests {
|
||||
|
||||
protected ObjectFactory<String> factory = new StringObjectFactory();
|
||||
protected StringRedisTemplate template;
|
||||
protected RedisStore col;
|
||||
|
||||
public RedisCollectionFactoryBeanTests() {
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setUsePool(true);
|
||||
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
this.template = new StringRedisTemplate(jedisConnFactory);
|
||||
ConnectionFactoryTracker.add(jedisConnFactory);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
ConnectionFactoryTracker.cleanUp();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// clean up the whole db
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private RedisStore createCollection(String key) {
|
||||
return createCollection(key, null);
|
||||
}
|
||||
|
||||
private RedisStore createCollection(String key, CollectionType type) {
|
||||
RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean();
|
||||
fb.setKey(key);
|
||||
fb.setTemplate(template);
|
||||
fb.setType(type);
|
||||
fb.afterPropertiesSet();
|
||||
|
||||
return fb.getObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNone() throws Exception {
|
||||
RedisStore store = createCollection("nosrt", CollectionType.PROPERTIES);
|
||||
assertThat(store, instanceOf(RedisProperties.class));
|
||||
|
||||
store = createCollection("nosrt", CollectionType.MAP);
|
||||
assertThat(store, instanceOf(DefaultRedisMap.class));
|
||||
|
||||
store = createCollection("nosrt", CollectionType.SET);
|
||||
assertThat(store, instanceOf(DefaultRedisSet.class));
|
||||
|
||||
store = createCollection("nosrt", CollectionType.LIST);
|
||||
assertThat(store, instanceOf(DefaultRedisList.class));
|
||||
|
||||
store = createCollection("nosrt");
|
||||
assertThat(store, instanceOf(DefaultRedisList.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testExistingCol() throws Exception {
|
||||
String key = "set";
|
||||
String val = "value";
|
||||
|
||||
template.boundSetOps(key).add(val);
|
||||
RedisStore col = createCollection(key);
|
||||
assertThat(col, is(DefaultRedisSet.class));
|
||||
|
||||
key = "map";
|
||||
template.boundHashOps(key).put(val, val);
|
||||
col = createCollection(key);
|
||||
assertThat(col, is(DefaultRedisMap.class));
|
||||
|
||||
col = createCollection(key, CollectionType.PROPERTIES);
|
||||
assertThat(col, is(RedisProperties.class));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.AbstractRedisCollection;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisList;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
|
||||
/**
|
||||
* Parameterized instance of Redis tests.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisListTests extends AbstractRedisListTests<Object> {
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisListTests</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param connFactory
|
||||
*/
|
||||
public RedisListTests(ObjectFactory<Object> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
@Override
|
||||
RedisStore copyStore(RedisStore store) {
|
||||
return new DefaultRedisList(store.getKey().toString(), store.getOperations());
|
||||
}
|
||||
|
||||
@Override
|
||||
AbstractRedisCollection<Object> createCollection() {
|
||||
String redisName = getClass().getName();
|
||||
return new DefaultRedisList(redisName, template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.rjc.RjcConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.OxmSerializer;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisMap;
|
||||
import org.springframework.data.redis.support.collections.RedisMap;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
|
||||
/**
|
||||
* Integration test for RedisMap.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisMapTests extends AbstractRedisMapTests<Object, Object> {
|
||||
|
||||
public RedisMapTests(ObjectFactory<Object> keyFactory, ObjectFactory<Object> valueFactory, RedisTemplate template) {
|
||||
super(keyFactory, valueFactory, template);
|
||||
}
|
||||
|
||||
@Override
|
||||
RedisMap<Object, Object> createMap() {
|
||||
String redisName = getClass().getSimpleName();
|
||||
return new DefaultRedisMap<Object, Object>(redisName, template);
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
// XStream serializer
|
||||
XStreamMarshaller xstream = new XStreamMarshaller();
|
||||
try {
|
||||
xstream.afterPropertiesSet();
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Cannot init XStream", ex);
|
||||
}
|
||||
OxmSerializer serializer = new OxmSerializer(xstream, xstream);
|
||||
JacksonJsonRedisSerializer<Person> jsonSerializer = new JacksonJsonRedisSerializer<Person>(Person.class);
|
||||
JacksonJsonRedisSerializer<String> jsonStringSerializer = new JacksonJsonRedisSerializer<String>(String.class);
|
||||
|
||||
// create Jedis Factory
|
||||
ObjectFactory<String> stringFactory = new StringObjectFactory();
|
||||
ObjectFactory<Person> personFactory = new PersonObjectFactory();
|
||||
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setUsePool(false);
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate genericTemplate = new RedisTemplate();
|
||||
genericTemplate.setConnectionFactory(jedisConnFactory);
|
||||
genericTemplate.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> xstreamGenericTemplate = new RedisTemplate<String, String>();
|
||||
xstreamGenericTemplate.setConnectionFactory(jedisConnFactory);
|
||||
xstreamGenericTemplate.setDefaultSerializer(serializer);
|
||||
xstreamGenericTemplate.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplate = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplate.setConnectionFactory(jedisConnFactory);
|
||||
jsonPersonTemplate.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplate.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplate.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplate.afterPropertiesSet();
|
||||
|
||||
// JRedis
|
||||
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
|
||||
jredisConnFactory.setUsePool(true);
|
||||
jredisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jredisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jredisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate genericTemplateJR = new RedisTemplate();
|
||||
genericTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
genericTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xGenericTemplateJR = new RedisTemplate<String, Person>();
|
||||
xGenericTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
xGenericTemplateJR.setDefaultSerializer(serializer);
|
||||
xGenericTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateJR = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
jsonPersonTemplateJR.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplateJR.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplateJR.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplateJR.afterPropertiesSet();
|
||||
|
||||
// RJC
|
||||
|
||||
// rjc
|
||||
RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory();
|
||||
rjcConnFactory.setUsePool(true);
|
||||
rjcConnFactory.setPort(SettingsUtils.getPort());
|
||||
rjcConnFactory.setHostName(SettingsUtils.getHost());
|
||||
rjcConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate genericTemplateRJC = new RedisTemplate();
|
||||
genericTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
genericTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> xGenericTemplateRJC = new RedisTemplate<String, Person>();
|
||||
xGenericTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
xGenericTemplateRJC.setDefaultSerializer(serializer);
|
||||
xGenericTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateRJC = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
jsonPersonTemplateRJC.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplateRJC.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplateRJC.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplateRJC.afterPropertiesSet();
|
||||
|
||||
|
||||
return Arrays.asList(new Object[][] { { stringFactory, stringFactory, genericTemplate },
|
||||
{ personFactory, personFactory, genericTemplate }, { stringFactory, personFactory, genericTemplate },
|
||||
{ personFactory, stringFactory, genericTemplate },
|
||||
{ personFactory, stringFactory, xstreamGenericTemplate },
|
||||
{ stringFactory, stringFactory, genericTemplateJR },
|
||||
{ personFactory, personFactory, genericTemplateJR },
|
||||
{ stringFactory, personFactory, genericTemplateJR },
|
||||
{ personFactory, stringFactory, genericTemplateJR },
|
||||
{ personFactory, stringFactory, xGenericTemplateJR },
|
||||
{ personFactory, stringFactory, jsonPersonTemplate },
|
||||
{ personFactory, stringFactory, jsonPersonTemplateJR },
|
||||
{ stringFactory, stringFactory, genericTemplateRJC },
|
||||
{ personFactory, personFactory, genericTemplateRJC },
|
||||
{ stringFactory, personFactory, genericTemplateRJC },
|
||||
{ personFactory, stringFactory, genericTemplateRJC },
|
||||
{ personFactory, stringFactory, xGenericTemplateRJC },
|
||||
{ personFactory, stringFactory, jsonPersonTemplateRJC } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support.collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.redis.Person;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.rjc.RjcConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.OxmSerializer;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisPropertiesTests extends RedisMapTests {
|
||||
|
||||
protected Properties defaults = new Properties();
|
||||
protected RedisProperties props;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisPropertiesTests</code> instance.
|
||||
*
|
||||
* @param keyFactory
|
||||
* @param valueFactory
|
||||
* @param template
|
||||
*/
|
||||
public RedisPropertiesTests(ObjectFactory<Object> keyFactory, ObjectFactory<Object> valueFactory,
|
||||
RedisTemplate template) {
|
||||
super(keyFactory, valueFactory, template);
|
||||
}
|
||||
|
||||
@Override
|
||||
RedisMap<Object, Object> createMap() {
|
||||
String redisName = getClass().getSimpleName();
|
||||
props = new RedisProperties(defaults, redisName, new StringRedisTemplate(template.getConnectionFactory()));
|
||||
return props;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisStore copyStore(RedisStore store) {
|
||||
return new RedisProperties(store.getKey(), store.getOperations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOperations() {
|
||||
assertTrue(map.getOperations() instanceof StringRedisTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesLoad() throws Exception {
|
||||
InputStream stream = getClass().getResourceAsStream(
|
||||
"/org/springframework/data/redis/support/collections/props.properties");
|
||||
|
||||
assertNotNull(stream);
|
||||
|
||||
int size = props.size();
|
||||
|
||||
try {
|
||||
props.load(stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
assertEquals("bar", props.get("foo"));
|
||||
assertEquals("head", props.get("bucket"));
|
||||
assertEquals("island", props.get("lotus"));
|
||||
assertEquals(size + 3, props.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPropertiesLoadXml() throws Exception {
|
||||
InputStream stream = getClass().getResourceAsStream(
|
||||
"/org/springframework/data/keyvalue/redis/support/collections/props.properties");
|
||||
|
||||
assertNotNull(stream);
|
||||
|
||||
int size = props.size();
|
||||
|
||||
try {
|
||||
props.loadFromXML(stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
assertEquals("bar", props.get("foo"));
|
||||
assertEquals("head", props.get("bucket"));
|
||||
assertEquals("island", props.get("lotus"));
|
||||
assertEquals(size + 3, props.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesSave() throws Exception {
|
||||
props.setProperty("x", "y");
|
||||
props.setProperty("a", "b");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
props.store(writer, "no-comment");
|
||||
//System.out.println(writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testPropertiesSaveXml() throws Exception {
|
||||
props.setProperty("x", "y");
|
||||
props.setProperty("a", "b");
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
props.storeToXML(bos, "comment");
|
||||
System.out.println(bos.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProperty() throws Exception {
|
||||
String property = props.getProperty("a");
|
||||
assertNull(property);
|
||||
defaults.put("a", "x");
|
||||
assertEquals("x", props.getProperty("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPropertyDefault() throws Exception {
|
||||
assertEquals("x", props.getProperty("a", "x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProperty() throws Exception {
|
||||
assertNull(props.getProperty("a"));
|
||||
defaults.setProperty("a", "x");
|
||||
assertEquals("x", props.getProperty("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesList() throws Exception {
|
||||
defaults.setProperty("a", "b");
|
||||
props.setProperty("x", "y");
|
||||
StringWriter wr = new StringWriter();
|
||||
props.list(new PrintWriter(wr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyNames() throws Exception {
|
||||
String key1="foo";
|
||||
String key2="x";
|
||||
String key3 = "d";
|
||||
|
||||
String val ="o";
|
||||
|
||||
defaults.setProperty(key3, val);
|
||||
props.setProperty(key1, val);
|
||||
props.setProperty(key2, val);
|
||||
|
||||
Enumeration<?> names = props.propertyNames();
|
||||
Set<Object> keys = new LinkedHashSet<Object>();
|
||||
keys.add(names.nextElement());
|
||||
keys.add(names.nextElement());
|
||||
keys.add(names.nextElement());
|
||||
|
||||
assertFalse(names.hasMoreElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringPropertyNames() throws Exception {
|
||||
String key1 = "foo";
|
||||
String key2 = "x";
|
||||
String key3 = "d";
|
||||
|
||||
String val = "o";
|
||||
|
||||
defaults.setProperty(key3, val);
|
||||
props.setProperty(key1, val);
|
||||
props.setProperty(key2, val);
|
||||
|
||||
Set<String> keys = props.stringPropertyNames();
|
||||
assertTrue(keys.contains(key1));
|
||||
assertTrue(keys.contains(key2));
|
||||
assertTrue(keys.contains(key3));
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
// XStream serializer
|
||||
XStreamMarshaller xstream = new XStreamMarshaller();
|
||||
try {
|
||||
xstream.afterPropertiesSet();
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Cannot init XStream", ex);
|
||||
}
|
||||
OxmSerializer serializer = new OxmSerializer(xstream, xstream);
|
||||
JacksonJsonRedisSerializer<Person> jsonSerializer = new JacksonJsonRedisSerializer<Person>(Person.class);
|
||||
JacksonJsonRedisSerializer<String> jsonStringSerializer = new JacksonJsonRedisSerializer<String>(String.class);
|
||||
|
||||
// create Jedis Factory
|
||||
ObjectFactory<String> stringFactory = new StringObjectFactory();
|
||||
|
||||
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
|
||||
jedisConnFactory.setUsePool(false);
|
||||
|
||||
jedisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
|
||||
jedisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> genericTemplate = new StringRedisTemplate(jedisConnFactory);
|
||||
|
||||
RedisTemplate<String, String> xstreamGenericTemplate = new RedisTemplate<String, String>();
|
||||
xstreamGenericTemplate.setConnectionFactory(jedisConnFactory);
|
||||
xstreamGenericTemplate.setDefaultSerializer(serializer);
|
||||
xstreamGenericTemplate.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplate = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplate.setConnectionFactory(jedisConnFactory);
|
||||
jsonPersonTemplate.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplate.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplate.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplate.afterPropertiesSet();
|
||||
|
||||
// JRedis
|
||||
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
|
||||
jredisConnFactory.setUsePool(true);
|
||||
jredisConnFactory.setPort(SettingsUtils.getPort());
|
||||
jredisConnFactory.setHostName(SettingsUtils.getHost());
|
||||
jredisConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> genericTemplateJR = new StringRedisTemplate(jredisConnFactory);
|
||||
RedisTemplate<String, Person> xGenericTemplateJR = new RedisTemplate<String, Person>();
|
||||
xGenericTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
xGenericTemplateJR.setDefaultSerializer(serializer);
|
||||
xGenericTemplateJR.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateJR = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory);
|
||||
jsonPersonTemplateJR.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplateJR.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplateJR.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplateJR.afterPropertiesSet();
|
||||
|
||||
// RJC
|
||||
|
||||
// rjc
|
||||
RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory();
|
||||
rjcConnFactory.setUsePool(true);
|
||||
rjcConnFactory.setPort(SettingsUtils.getPort());
|
||||
rjcConnFactory.setHostName(SettingsUtils.getHost());
|
||||
rjcConnFactory.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, String> genericTemplateRJC = new StringRedisTemplate(jredisConnFactory);
|
||||
RedisTemplate<String, Person> xGenericTemplateRJC = new RedisTemplate<String, Person>();
|
||||
xGenericTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
xGenericTemplateRJC.setDefaultSerializer(serializer);
|
||||
xGenericTemplateRJC.afterPropertiesSet();
|
||||
|
||||
RedisTemplate<String, Person> jsonPersonTemplateRJC = new RedisTemplate<String, Person>();
|
||||
jsonPersonTemplateRJC.setConnectionFactory(rjcConnFactory);
|
||||
jsonPersonTemplateRJC.setDefaultSerializer(jsonSerializer);
|
||||
jsonPersonTemplateRJC.setHashKeySerializer(jsonSerializer);
|
||||
jsonPersonTemplateRJC.setHashValueSerializer(jsonStringSerializer);
|
||||
jsonPersonTemplateRJC.afterPropertiesSet();
|
||||
|
||||
|
||||
return Arrays.asList(new Object[][] { { stringFactory, stringFactory, genericTemplate },
|
||||
{ stringFactory, stringFactory, genericTemplate }, { stringFactory, stringFactory, genericTemplate },
|
||||
{ stringFactory, stringFactory, genericTemplate },
|
||||
{ stringFactory, stringFactory, xstreamGenericTemplate },
|
||||
{ stringFactory, stringFactory, genericTemplateJR },
|
||||
{ stringFactory, stringFactory, genericTemplateJR },
|
||||
{ stringFactory, stringFactory, genericTemplateJR },
|
||||
{ stringFactory, stringFactory, genericTemplateJR },
|
||||
{ stringFactory, stringFactory, xGenericTemplateJR },
|
||||
{ stringFactory, stringFactory, jsonPersonTemplate },
|
||||
{ stringFactory, stringFactory, jsonPersonTemplateJR },
|
||||
{ stringFactory, stringFactory, genericTemplateRJC },
|
||||
{ stringFactory, stringFactory, genericTemplateRJC },
|
||||
{ stringFactory, stringFactory, genericTemplateRJC },
|
||||
{ stringFactory, stringFactory, genericTemplateRJC },
|
||||
{ stringFactory, stringFactory, xGenericTemplateRJC },
|
||||
{ stringFactory, stringFactory, jsonPersonTemplateRJC } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.AbstractRedisCollection;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisSet;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
|
||||
/**
|
||||
* Parameterized instance of Redis tests.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisSetTests extends AbstractRedisSetTests<Object> {
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisSetTests</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param template
|
||||
*/
|
||||
public RedisSetTests(ObjectFactory<Object> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
@Override
|
||||
RedisStore copyStore(RedisStore store) {
|
||||
return new DefaultRedisSet(store.getKey().toString(), store.getOperations());
|
||||
}
|
||||
|
||||
@Override
|
||||
AbstractRedisCollection<Object> createCollection() {
|
||||
String redisName = getClass().getName();
|
||||
return new DefaultRedisSet(redisName, template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.collections.AbstractRedisCollection;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
|
||||
import org.springframework.data.redis.support.collections.RedisStore;
|
||||
|
||||
/**
|
||||
* Parameterized instance of Redis sorted set tests.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class RedisZSetTests extends AbstractRedisZSetTest<Object> {
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisZSetTests</code> instance.
|
||||
*
|
||||
* @param factory
|
||||
* @param template
|
||||
*/
|
||||
public RedisZSetTests(ObjectFactory<Object> factory, RedisTemplate template) {
|
||||
super(factory, template);
|
||||
}
|
||||
|
||||
@Override
|
||||
RedisStore copyStore(RedisStore store) {
|
||||
return new DefaultRedisZSet(store.getKey().toString(), store.getOperations());
|
||||
}
|
||||
|
||||
@Override
|
||||
AbstractRedisCollection<Object> createCollection() {
|
||||
String redisName = getClass().getName();
|
||||
return new DefaultRedisZSet(redisName, template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.data.redis.support.collections;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* String object factory based on UUID.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class StringObjectFactory implements ObjectFactory<String> {
|
||||
|
||||
@Override
|
||||
public String instance() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2011 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.data.redis.support.collections;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class SupportXmlTests {
|
||||
|
||||
@Test
|
||||
public void testContainerSetup() throws Exception {
|
||||
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(
|
||||
"/org/springframework/data/redis/support/collections/container.xml");
|
||||
|
||||
RedisList list = ctx.getBean("non-existing", RedisList.class);
|
||||
RedisProperties props = ctx.getBean("props", RedisProperties.class);
|
||||
Map map = ctx.getBean("map", Map.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user