Skip to content

Commit

Permalink
updated serializers with better testing from cassandra BButil
Browse files Browse the repository at this point in the history
  • Loading branch information
zznate committed Feb 2, 2011
1 parent 7375a1d commit 1d9670c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Expand Up @@ -38,7 +38,7 @@ public Integer fromByteBuffer(ByteBuffer byteBuffer) {

@Override
public Integer fromBytes(byte[] bytes) {
ByteBuffer bb = ByteBuffer.allocate(4).put(bytes);
ByteBuffer bb = ByteBuffer.allocate(4).put(bytes, 0, 4);
bb.rewind();
return bb.getInt();
}
Expand Down
Expand Up @@ -4,6 +4,8 @@

import java.nio.ByteBuffer;

import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
import org.junit.Test;

/**
Expand All @@ -13,6 +15,8 @@
*/
public class IntegerSerializerTest {

static IntegerSerializer ext = IntegerSerializer.get();

@Test
public void testConversions() {
test(0);
Expand All @@ -23,8 +27,27 @@ public void testConversions() {
test(null);
}

@Test
public void testFromCassandra() {
assertEquals(new Integer(1), ext.fromByteBuffer(FBUtilities.toByteBuffer(1)));
assertEquals(new Integer(-1), ext.fromByteBuffer(FBUtilities.toByteBuffer(-1)));
assertEquals(new Integer(0), ext.fromByteBuffer(FBUtilities.toByteBuffer(0)));
assertEquals(new Integer(Integer.MAX_VALUE), ext.fromByteBuffer(FBUtilities.toByteBuffer(Integer.MAX_VALUE)));
assertEquals(new Integer(Integer.MIN_VALUE), ext.fromByteBuffer(FBUtilities.toByteBuffer(Integer.MIN_VALUE)));
}

@Test
public void testFromCassandraAsBytes() {
assertEquals(new Integer(1), ext.fromBytes(FBUtilities.toByteBuffer(1).array()));
assertEquals(new Integer(-1), ext.fromBytes(FBUtilities.toByteBuffer(-1).array()));
assertEquals(new Integer(0), ext.fromBytes(FBUtilities.toByteBuffer(0).array()));
assertEquals(new Integer(Integer.MAX_VALUE), ext.fromBytes(FBUtilities.toByteBuffer(Integer.MAX_VALUE).array()));
assertEquals(new Integer(Integer.MIN_VALUE), ext.fromBytes(FBUtilities.toByteBuffer(Integer.MIN_VALUE).array()));
}


private void test(Integer number) {
IntegerSerializer ext = IntegerSerializer.get();

assertEquals(number, ext.fromByteBuffer(ext.toByteBuffer(number)));

// test compatibility with ByteBuffer default byte order
Expand Down
Expand Up @@ -4,10 +4,13 @@

import java.nio.ByteBuffer;

import org.apache.cassandra.utils.FBUtilities;
import org.junit.Test;

public class LongSerializerTest {

static LongSerializer ext = LongSerializer.get();

@Test
public void testConversions() {
test(0l);
Expand All @@ -18,8 +21,17 @@ public void testConversions() {
test(null);
}

@Test
public void testFromCassandra() {
assertEquals(new Long(1), ext.fromByteBuffer(FBUtilities.toByteBuffer(1L)));
assertEquals(new Long(0), ext.fromByteBuffer(FBUtilities.toByteBuffer(0L)));
assertEquals(new Long(-1), ext.fromByteBuffer(FBUtilities.toByteBuffer(-1L)));
assertEquals(new Long(Long.MIN_VALUE), ext.fromByteBuffer(FBUtilities.toByteBuffer(Long.MIN_VALUE)));
assertEquals(new Long(Long.MAX_VALUE), ext.fromByteBuffer(FBUtilities.toByteBuffer(Long.MAX_VALUE)));
}

private void test(Long number) {
LongSerializer ext = new LongSerializer();

assertEquals(number, ext.fromByteBuffer(ext.toByteBuffer(number)));

// test compatibility with ByteBuffer default byte order
Expand Down

0 comments on commit 1d9670c

Please sign in to comment.