diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializer.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializer.java index 5dad43221..1eb10fde6 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializer.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializer.java @@ -15,9 +15,12 @@ */ package org.springframework.datastore.redis.serializer; +import java.io.IOException; + import org.springframework.core.convert.converter.Converter; import org.springframework.core.serializer.support.DeserializingConverter; import org.springframework.core.serializer.support.SerializingConverter; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.datastore.redis.UncategorizedRedisException; /** @@ -31,6 +34,8 @@ public class SimpleRedisSerializer implements RedisSerializer { private Converter serializer = new SerializingConverter(); private Converter deserializer = new DeserializingConverter(); + private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); + private sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); @SuppressWarnings("unchecked") @Override @@ -44,11 +49,11 @@ public class SimpleRedisSerializer implements RedisSerializer { @Override public T deserialize(String bytes) { - // try { - return deserialize(bytes.getBytes()); - // } catch (UnsupportedEncodingException ex) { - // throw new DataRetrievalFailureException("Unsupported encoding " + encoding, ex); - // } + try { + return deserialize(decoder.decodeBuffer(bytes)); + } catch (IOException ex) { + throw new DataRetrievalFailureException("Unsupported encoding ", ex); + } } @Override @@ -62,6 +67,11 @@ public class SimpleRedisSerializer implements RedisSerializer { @Override public String serializeAsString(Object object) { - return new String(serialize(object)); + try { + + return encoder.encode(serialize(object)); + } catch (Exception ex) { + throw new DataRetrievalFailureException("Unsupported encoding ", ex); + } } } \ No newline at end of file diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Address.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Address.java new file mode 100644 index 000000000..81bbd685a --- /dev/null +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Address.java @@ -0,0 +1,77 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.datastore.redis; + +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; + + /** + * Constructs a new Address instance. + * + * @param street + * @param number + */ + public Address(String street, int number) { + super(); + this.street = street; + 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; + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/Person.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Person.java similarity index 58% rename from spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/Person.java rename to spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Person.java index 48396bc17..c071ff183 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/Person.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/Person.java @@ -13,17 +13,40 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.datastore.redis.core; +package org.springframework.datastore.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 int age; + + 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; @@ -49,22 +72,22 @@ public class Person implements Serializable { this.age = age; } - public Person(String firstName, String lastName, int age) { - super(); - this.firstName = firstName; - this.lastName = lastName; - 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 + age; - result = prime * result - + ((firstName == null) ? 0 : firstName.hashCode()); - result = prime * result - + ((lastName == null) ? 0 : lastName.hashCode()); + 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; } @@ -74,22 +97,33 @@ public class Person implements Serializable { return true; if (obj == null) return false; - if (getClass() != obj.getClass()) + if (!(obj instanceof Person)) return false; Person other = (Person) obj; - if (age != other.age) + 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)) + } + else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; - } else if (!lastName.equals(other.lastName)) + } + else if (!lastName.equals(other.lastName)) return false; return true; } - -} +} \ No newline at end of file diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/connection/AbstractConnectionIntegrationTests.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/connection/AbstractConnectionIntegrationTests.java index f42513c35..7bdc0a9de 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/connection/AbstractConnectionIntegrationTests.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/connection/AbstractConnectionIntegrationTests.java @@ -22,9 +22,9 @@ import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.datastore.redis.Person; import org.springframework.datastore.redis.connection.RedisConnection; import org.springframework.datastore.redis.connection.RedisConnectionFactory; -import org.springframework.datastore.redis.core.Person; public abstract class AbstractConnectionIntegrationTests { diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/RedisTemplateIntegrationTests.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/RedisTemplateIntegrationTests.java index b569ed59e..a3c9ce377 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/RedisTemplateIntegrationTests.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/core/RedisTemplateIntegrationTests.java @@ -18,6 +18,7 @@ package org.springframework.datastore.redis.core; import org.junit.Before; import org.junit.Test; +import org.springframework.datastore.redis.Person; public class RedisTemplateIntegrationTests { diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializerTest.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializerTest.java index 9106acf86..a34e49cd8 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializerTest.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/serializer/SimpleRedisSerializerTest.java @@ -23,6 +23,8 @@ import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.datastore.redis.Address; +import org.springframework.datastore.redis.Person; public class SimpleRedisSerializerTest { @@ -127,4 +129,12 @@ public class SimpleRedisSerializerTest { assertEquals(value, serializer.deserialize(serializer.serializeAsString(value))); assertEquals(value, serializer.deserialize(serializer.serializeAsString(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.serializeAsString(p1))); + } } \ No newline at end of file diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/PersonRedisListTest.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/PersonRedisListTest.java new file mode 100644 index 000000000..cae0774b3 --- /dev/null +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/PersonRedisListTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.datastore.redis.util; + +import java.util.UUID; + +import org.springframework.datastore.redis.Address; +import org.springframework.datastore.redis.Person; +import org.springframework.datastore.redis.connection.jedis.JedisConnectionFactory; + + +/** + * Person-based Redis List test. + * + * @author Costin Leau + */ +public class PersonRedisListTest extends AbstractRedisCollectionTest { + + private JedisConnectionFactory factory; + private int counter = 0; + + @Override + AbstractRedisCollection createCollection() { + String redisName = getClass().getName(); + factory = new JedisConnectionFactory(); + factory.setPooling(false); + factory.afterPropertiesSet(); + + return new DefaultRedisList(redisName, factory.getConnection()); + } + + @Override + void destroyCollection() { + factory.destroy(); + } + + @Override + RedisStore copyStore(RedisStore store) { + return new DefaultRedisList(store.getKey(), store.getCommands()); + } + + @Override + Person getT() { + String uuid = UUID.randomUUID().toString(); + return new Person(uuid, uuid, ++counter, new Address(uuid, counter)); + } +} +