+ improved serialization by using base64 instead of the default encoding

+ added integration test for more complicated Serializable classes
This commit is contained in:
Costin Leau
2010-11-11 09:28:22 +02:00
parent 8a42496b1b
commit 6431761451
7 changed files with 220 additions and 27 deletions

View File

@@ -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<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> 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> 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);
}
}
}

View File

@@ -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 <code>Address</code> 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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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)));
}
}

View File

@@ -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<Person> {
private JedisConnectionFactory factory;
private int counter = 0;
@Override
AbstractRedisCollection<Person> createCollection() {
String redisName = getClass().getName();
factory = new JedisConnectionFactory();
factory.setPooling(false);
factory.afterPropertiesSet();
return new DefaultRedisList<Person>(redisName, factory.getConnection());
}
@Override
void destroyCollection() {
factory.destroy();
}
@Override
RedisStore copyStore(RedisStore store) {
return new DefaultRedisList<Person>(store.getKey(), store.getCommands());
}
@Override
Person getT() {
String uuid = UUID.randomUUID().toString();
return new Person(uuid, uuid, ++counter, new Address(uuid, counter));
}
}