Skip to content
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

Fix for deserializing composites with a CompositeSerializer #585

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,12 +3,15 @@
*/
package me.prettyprint.cassandra.serializers;

import static me.prettyprint.hector.api.ddl.ComparatorType.COMPOSITETYPE;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.ddl.ComparatorType;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.ddl.ComparatorType;
import static me.prettyprint.hector.api.ddl.ComparatorType.COMPOSITETYPE;

/**
* @author Todd Nine
Expand All @@ -17,11 +20,17 @@
public class CompositeSerializer extends AbstractSerializer<Composite> {

private static final CompositeSerializer INSTANCE = new CompositeSerializer();


private final List<Serializer<?>> serializersByPosition;

public static CompositeSerializer get() {
return INSTANCE;
}


public CompositeSerializer(Serializer<?>... serializers) {
serializersByPosition = Arrays.asList(serializers);
}

@Override
public ByteBuffer toByteBuffer(Composite obj) {

Expand All @@ -31,7 +40,7 @@ public ByteBuffer toByteBuffer(Composite obj) {
@Override
public Composite fromByteBuffer(ByteBuffer byteBuffer) {

Composite composite = new Composite();
Composite composite = createComposite();
composite.deserialize(byteBuffer);

return composite;
Expand All @@ -43,4 +52,9 @@ public ComparatorType getComparatorType() {
return COMPOSITETYPE;
}

private Composite createComposite() {
Composite composite = new Composite();
composite.setSerializersByPosition(serializersByPosition);
return composite;
}
}
@@ -0,0 +1,35 @@
package me.prettyprint.cassandra.serializers;

import me.prettyprint.hector.api.beans.Composite;
import org.junit.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;

/**
* @author Eric Zoerner <a href="mailto:ezoerner@ebuddy.com">ezoerner@ebuddy.com</a>
*/
public class CompositeSerializerTest {

/** Tests deserializing a composite with the singleton CompositeSerializer, which fails. */
@Test
public void testFromByteBufferWithSingletonCompositeSerializer() throws Exception {
Composite composite = new Composite("test", 42);
CompositeSerializer ser = CompositeSerializer.get();
ByteBuffer byteBuffer = ser.toByteBuffer(composite);

assertEquals(composite, ser.fromByteBuffer(byteBuffer));
}

/** Tests deserializing a composite with a CompositeSerializer constructed with Serializers. */
@Test
public void testFromByteBufferWithConstructedSerializer() throws Exception {
Composite composite = new Composite("test", 42);
CompositeSerializer ser = new CompositeSerializer(StringSerializer.get(), BigIntegerSerializer.get());
ByteBuffer byteBuffer = ser.toByteBuffer(composite);

assertEquals(composite, ser.fromByteBuffer(byteBuffer));

}
}