Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.samza.serializers;

import java.nio.ByteBuffer;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class TestByteBufferSerde {
@Test
public void testSerde() {
ByteBufferSerde serde = new ByteBufferSerde();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

byte[] bytes = "A lazy way of creating a byte array".getBytes();
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byteBuffer.mark();
assertArrayEquals(bytes, serde.toBytes(byteBuffer));
byteBuffer.reset();
assertEquals(byteBuffer, serde.fromBytes(bytes));
}

@Test
public void testSerializationPreservesInput() {
ByteBufferSerde serde = new ByteBufferSerde();
byte[] bytes = "A lazy way of creating a byte array".getBytes();
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byteBuffer.get(); // advance position by 1
serde.toBytes(byteBuffer);

assertEquals(byteBuffer.capacity(), byteBuffer.limit());
assertEquals(1, byteBuffer.position());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.samza.serializers;

package org.apache.samza.serializers
import org.junit.Test;

import java.util.Arrays
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNull;

import org.junit.Assert._
import org.junit.Test

class TestByteSerde {
public class TestByteSerde {
@Test
def testByteSerde {
val serde = new ByteSerde
assertNull(serde.toBytes(null))
assertNull(serde.fromBytes(null))
public void testByteSerde() {
ByteSerde serde = new ByteSerde();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

val testBytes = "A lazy way of creating a byte array".getBytes()
assertArrayEquals(serde.toBytes(testBytes), testBytes)
assertArrayEquals(serde.fromBytes(testBytes), testBytes)
byte[] testBytes = "A lazy way of creating a byte array".getBytes();
assertArrayEquals(testBytes, serde.toBytes(testBytes));
assertArrayEquals(testBytes, serde.fromBytes(testBytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.samza.serializers;

package org.apache.samza.serializers
import org.junit.Test;

import org.junit.Assert._
import org.junit.Test
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

class TestIntegerSerde {

public class TestDoubleSerde {
@Test
def testIntegerSerde {
val serde = new IntegerSerde
assertEquals(null, serde.toBytes(null))
assertEquals(null, serde.fromBytes(null))
public void testDoubleSerde() {
DoubleSerde serde = new DoubleSerde();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

val fooBar = 37
val fooBarBytes = serde.toBytes(fooBar)
assertArrayEquals(Array[Byte](0, 0, 0, 37), fooBarBytes)
assertEquals(fooBar, serde.fromBytes(fooBarBytes))
Double fooBar = 9.156013e-002;
byte[] fooBarBytes = serde.toBytes(fooBar);
assertArrayEquals(new byte[]{63, -73, 112, 124, 19, -9, -82, -93}, fooBarBytes);
assertEquals(fooBar, serde.fromBytes(fooBarBytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.samza.serializers;

package org.apache.samza.serializers
import org.junit.Test;

import java.util.Arrays
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Assert._
import org.junit.Test

class TestLongSerde {
public class TestIntegerSerde {
@Test
def testLongSerde {
val serde = new LongSerde
assertEquals(null, serde.toBytes(null))
assertEquals(null, serde.fromBytes(null))
public void testIntegerSerde() {
IntegerSerde serde = new IntegerSerde();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

val fooBar = 1234123412341234L
val fooBarBytes = serde.toBytes(fooBar)
fooBarBytes.foreach(System.err.println)
assertArrayEquals(Array[Byte](0, 4, 98, 109, -65, -102, 1, -14), fooBarBytes)
assertEquals(fooBar, serde.fromBytes(fooBarBytes))
Integer fooBar = 37;
byte[] fooBarBytes = serde.toBytes(fooBar);
assertArrayEquals(new byte[]{0, 0, 0, 37}, fooBarBytes);
assertEquals(fooBar, serde.fromBytes(fooBarBytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.samza.serializers;

package org.apache.samza.serializers
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Assert._
import org.junit.Test

import scala.collection.JavaConverters._


class TestJsonSerdeV2 {
public class TestJsonSerdeV2 {
@Test
def testJsonSerdeV2ShouldWork {
val serde = new JsonSerdeV2[java.util.HashMap[String, Object]]
val obj = new java.util.HashMap[String, Object](Map[String, Object]("hi" -> "bye", "why" -> new java.lang.Integer(2)).asJava)
val bytes = serde.toBytes(obj)
assertEquals(obj, serde.fromBytes(bytes))
val serdeHashMapEntry = new JsonSerdeV2[java.util.Map.Entry[String, Object]]
obj.entrySet().asScala.foreach(entry => {
try {
val entryBytes = serdeHashMapEntry.toBytes(entry)
} catch {
case e: Exception => fail("HashMap Entry serialization failed!")
}
})
public void testJsonSerdeV2ShouldWork() {
JsonSerdeV2<HashMap<String, Object>> serde = new JsonSerdeV2<>();
HashMap<String, Object> obj = new HashMap<>();
obj.put("hi", "bye");
obj.put("why", 2);
byte[] bytes = serde.toBytes(obj);
assertEquals(obj, serde.fromBytes(bytes));
JsonSerdeV2<Map.Entry<String, Object>> serdeHashMapEntry = new JsonSerdeV2<>();
obj.entrySet().forEach(entry -> {
try {
serdeHashMapEntry.toBytes(entry);
} catch (Exception e) {
fail("HashMap Entry serialization failed!");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.samza.serializers;

package org.apache.samza.serializers
import org.junit.Test;

import java.util.Arrays
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Assert._
import org.junit.Test

class TestDoubleSerde {
public class TestLongSerde {
@Test
def testDoubleSerde {
val serde = new DoubleSerde
assertEquals(null, serde.toBytes(null))
assertEquals(null, serde.fromBytes(null))
public void testLongSerde() {
LongSerde serde = new LongSerde();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

val fooBar = 9.156013e-002
val fooBarBytes = serde.toBytes(fooBar)
fooBarBytes.foreach(System.err.println)
assertArrayEquals(Array[Byte](63, -73, 112, 124, 19, -9, -82, -93), fooBarBytes)
assertEquals(fooBar, serde.fromBytes(fooBarBytes))
Long fooBar = 1234123412341234L;
byte[] fooBarBytes = serde.toBytes(fooBar);
assertArrayEquals(new byte[]{0, 4, 98, 109, -65, -102, 1, -14}, fooBarBytes);
assertEquals(fooBar, serde.fromBytes(fooBarBytes));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.samza.serializers;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class TestSerializableSerde {
@Test
public void testSerializableSerde() {
SerializableSerde<String> serde = new SerializableSerde<>();
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

String obj = "String is serializable";

// Serialized string is prefix + string itself
List<Byte> expectedBytesList = new ArrayList<>(
Arrays.asList((byte) 0xAC, (byte) 0xED, (byte) 0x00, (byte) 0x05, (byte) 0x74, (byte) 0x00, (byte) 0x16));
for (byte b : obj.getBytes(StandardCharsets.UTF_8)) {
expectedBytesList.add(b);
}
// need to unbox to primitive byte
byte[] expected = new byte[expectedBytesList.size()];
for (int i = 0; i < expectedBytesList.size(); i++) {
expected[i] = expectedBytesList.get(i);
}

byte[] bytes = serde.toBytes(obj);

assertArrayEquals(expected, bytes);

String objRoundTrip = serde.fromBytes(bytes);
assertEquals(obj, objRoundTrip);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.samza.serializers;

import java.nio.charset.StandardCharsets;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class TestStringSerde {
@Test
public void testStringSerde() {
StringSerde serde = new StringSerde("UTF-8");
assertNull(serde.toBytes(null));
assertNull(serde.fromBytes(null));

String fooBar = "foo bar";
byte[] fooBarBytes = serde.toBytes(fooBar);
assertArrayEquals(fooBar.getBytes(StandardCharsets.UTF_8), fooBarBytes);
assertEquals(fooBar, serde.fromBytes(fooBarBytes));
}
}
Loading