Skip to content

Commit

Permalink
Fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Jun 25, 2016
1 parent f4c0cef commit 9635317
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 12 deletions.
27 changes: 25 additions & 2 deletions paldb/src/main/java/com/linkedin/paldb/impl/Serializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.linkedin.paldb.impl;

import com.linkedin.paldb.api.Serializer;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public synchronized void registerSerializer(Serializer serializer) {
* @return serializer instance or null if not found
*/
public Serializer getSerializer(Class cls) {
SerializerWrapper w = serializers.get(cls);
SerializerWrapper w = getSerializerWrapper(cls);
if (w != null) {
return w.serializer;
}
Expand Down Expand Up @@ -202,7 +203,29 @@ void clear() {
* @return serializer index
*/
int getIndex(Class cls) {
return serializers.get(cls).index;
return getSerializerWrapper(cls).index;
}

/**
* Returns the serializer and its index associated with <code>cls</code>.
*
* @param cls object clas
* @return serializer wrapper object
*/
private SerializerWrapper getSerializerWrapper(Class cls) {
SerializerWrapper w = serializers.get(cls);
if (w != null) {
return w;
} else {
// Try with interfaces implemented
for (Class c : cls.getInterfaces()) {
w = serializers.get(c);
if (w != null) {
return w;
}
}
}
return null;
}

/**
Expand Down
38 changes: 36 additions & 2 deletions paldb/src/test/java/com/linkedin/paldb/impl/TestSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import com.linkedin.paldb.api.Serializer;
import com.linkedin.paldb.utils.DataInputOutput;

import java.awt.*;
import java.io.DataInput;
import java.io.DataOutput;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -102,8 +104,7 @@ public void testObjectType() {
}

@Test
public void testSerialize()
throws Throwable {
public void testSerialize() throws Throwable {
_serializers.registerSerializer(new ColorSerializer());
DataInputOutput dio = new DataInputOutput();
Serializers.serialize(dio, _serializers);
Expand All @@ -116,6 +117,13 @@ public void testSerialize()
Assert.assertNotNull(_serializers.getSerializer(0));
}

@Test
public void testInterfaceType() throws Throwable {
SerializerWithInterface i = new SerializerWithInterface();
_serializers.registerSerializer(i);
Assert.assertSame(_serializers.getSerializer(AnInterface.class), i);
}

// HELPER

public static class ColorSerializer implements Serializer<Color> {
Expand Down Expand Up @@ -189,4 +197,30 @@ public int getWeight(Object instance) {
return 0;
}
}

public static interface AnInterface {

}

public static class AClass implements AnInterface {

}

public static class SerializerWithInterface implements Serializer<AnInterface> {

@Override
public AnInterface read(DataInput input) {
return null;
}

@Override
public void write(DataOutput output, AnInterface input) {

}

@Override
public int getWeight(AnInterface instance) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.linkedin.paldb.api.Configuration;
import com.linkedin.paldb.api.Serializer;
import com.linkedin.paldb.api.UnsupportedTypeException;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -28,6 +29,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -54,16 +56,14 @@ public void testCompressionEnabled() {
}

@Test
public void testSerializeKey()
throws IOException, ClassNotFoundException {
public void testSerializeKey() throws IOException, ClassNotFoundException {
Integer l = 1;
Object d = serialization.deserialize(serialization.serializeKey(l));
Assert.assertEquals(d, l);
}

@Test
public void testSerializeKeyDataOutput()
throws IOException, ClassNotFoundException {
public void testSerializeKeyDataOutput() throws IOException, ClassNotFoundException {
Integer l = 1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
Expand All @@ -77,8 +77,7 @@ public void testSerializeKeyDataOutput()
}

@Test
public void testSerializeValueDataOutput()
throws IOException, ClassNotFoundException {
public void testSerializeValueDataOutput() throws IOException, ClassNotFoundException {
Integer l = 1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
Expand All @@ -92,8 +91,7 @@ public void testSerializeValueDataOutput()
}

@Test(expectedExceptions = NullPointerException.class)
public void testSerializeKeyNull()
throws IOException, ClassNotFoundException {
public void testSerializeKeyNull() throws IOException, ClassNotFoundException {
serialization.serializeKey(null);
}

Expand Down Expand Up @@ -186,6 +184,54 @@ public int getWeight(Point[] instance) {
Assert.assertEquals(serialization.deserialize(buf), p);
}

@Test
public void testInnerClassSerializer() throws Throwable {
configuration.registerSerializer(new Serializer<ImplementsA>() {

@Override
public ImplementsA read(DataInput dataInput) throws IOException {
return new ImplementsA(dataInput.readInt());
}

@Override
public void write(DataOutput dataOutput, ImplementsA input) throws IOException {
dataOutput.writeInt(input.getVal());
}

@Override
public int getWeight(ImplementsA instance) {
return 0;
}
});
ImplementsA a = new ImplementsA(42);
byte[] buf = serialization.serialize(a);
Assert.assertEquals(serialization.deserialize(buf), a);
}

@Test
public void testInheritedSerializer() throws Throwable {
configuration.registerSerializer(new Serializer<A>() {

@Override
public A read(DataInput dataInput) throws IOException {
return new ImplementsA(dataInput.readInt());
}

@Override
public void write(DataOutput dataOutput, A input) throws IOException {
dataOutput.writeInt(input.getVal());
}

@Override
public int getWeight(A instance) {
return 0;
}
});
ImplementsA a = new ImplementsA(42);
byte[] buf = serialization.serialize(a);
Assert.assertEquals(serialization.deserialize(buf), a);
}

@Test
public void testNull()
throws Throwable {
Expand Down Expand Up @@ -604,4 +650,38 @@ private static char[] generateCharArray(int size) {
}
return array;
}

private static interface A {

int getVal();
}

private static class ImplementsA implements A {

int val;

public ImplementsA(int val) {
this.val = val;
}

@Override
public int getVal() {
return val;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ImplementsA that = (ImplementsA) o;

return val == that.val;
}

@Override
public int hashCode() {
return val;
}
}
}

0 comments on commit 9635317

Please sign in to comment.