-
Notifications
You must be signed in to change notification settings - Fork 14
support for custom serializer #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
9739d13
2a91f66
1d6ad5c
b1bcb83
a107697
ade7b6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.arhs.spring.cache.mongo.serializer; | ||
|
||
import java.io.*; | ||
|
||
public class JavaSerializer implements Serializer { | ||
@Override | ||
public byte[] serialize(Object obj) throws IOException { | ||
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
final ObjectOutputStream output = new ObjectOutputStream(buffer)) { | ||
|
||
output.writeObject(obj); | ||
|
||
return buffer.toByteArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { | ||
try (final ByteArrayInputStream buffer = new ByteArrayInputStream(bytes); | ||
final ObjectInputStream output = new ObjectInputStream(buffer)) { | ||
return output.readObject(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.arhs.spring.cache.mongo.serializer; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this Java documentation? or complete it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* Created by dan on 3/1/17. | ||
*/ | ||
public interface Serializer { | ||
|
||
public byte[] serialize(Object obj) throws IOException; | ||
|
||
public Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.arhs.spring.cache.mongo; | ||
|
||
import com.arhs.spring.cache.mongo.serializer.JavaSerializer; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = TestConfiguration.class) | ||
public class MongoCacheBuilderTest { | ||
|
||
private static final String CACHE_NAME = "cache"; | ||
private static final String COLLECTION_NAME = "test"; | ||
private static final long TTL = 0; | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
@Test | ||
public void testBuildWithSerializer() { | ||
MongoCacheBuilder builder = new MongoCacheBuilder( | ||
COLLECTION_NAME, | ||
mongoTemplate, | ||
CACHE_NAME | ||
); | ||
builder.withSerializer(new JavaSerializer()); | ||
builder.withTTL(TTL); | ||
builder.withFlushOnBoot(false); | ||
builder.build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add assertions on the built object to verify it has been built with the correct values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
@Test | ||
public void testBuildWithoutSerializer() { | ||
MongoCacheBuilder builder = new MongoCacheBuilder( | ||
COLLECTION_NAME, | ||
mongoTemplate, | ||
CACHE_NAME | ||
); | ||
builder.withTTL(TTL); | ||
builder.withFlushOnBoot(false); | ||
builder.build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add assertions on the built object to verify it has been built with the correct values? |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.arhs.spring.cache.mongo.serializer; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
public class JavaSerializerTest { | ||
|
||
public static class SerializableBean implements Serializable { | ||
Date date; | ||
String string; | ||
Integer integer; | ||
} | ||
|
||
@Test | ||
public void testSerializeDeserialize() throws IOException, ClassNotFoundException { | ||
JavaSerializer serializer = new JavaSerializer(); | ||
|
||
Date date = new Date(); | ||
String string = "foobar"; | ||
Integer integer = 1234; | ||
|
||
SerializableBean in = new SerializableBean(); | ||
in.date = date; | ||
in.string = string; | ||
in.integer = integer; | ||
|
||
byte[] bytes = serializer.serialize(in); | ||
|
||
SerializableBean out = (SerializableBean) serializer.deserialize(bytes); | ||
|
||
Assert.assertEquals(string, out.string); | ||
Assert.assertEquals(integer, out.integer); | ||
Assert.assertEquals(date, out.date); | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add
final
keyword on this field?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done