From 6d22c5fe5239a41c95baaafb47374685f8f1c3a7 Mon Sep 17 00:00:00 2001 From: Alex Chermenin Date: Wed, 24 Aug 2016 23:31:56 +0300 Subject: [PATCH] Added test for ArraysAsListSerializer. --- .../com/twitter/chill/java/TestLists.java | 64 +++++++++++++++++ .../twitter/chill/java/ArraysAsListTest.scala | 72 +++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 chill-java/src/test/java/com/twitter/chill/java/TestLists.java create mode 100644 chill-java/src/test/scala/com/twitter/chill/java/ArraysAsListTest.scala diff --git a/chill-java/src/test/java/com/twitter/chill/java/TestLists.java b/chill-java/src/test/java/com/twitter/chill/java/TestLists.java new file mode 100644 index 00000000..1efe820c --- /dev/null +++ b/chill-java/src/test/java/com/twitter/chill/java/TestLists.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016 Alex Chermenin + * + * Licensed 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 com.twitter.chill.java; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class TestLists { + + private static final Kryo kryo = new Kryo(); + + private static final Map, List> map = new HashMap, List>(9); + + static { + ArraysAsListSerializer.registrar().apply(kryo); + map.put(byte.class, Arrays.asList((byte) 1, (byte) 2, (byte) 3, (byte) 4)); + map.put(short.class, Arrays.asList((short) 1, (short) 2, (short) 3, (short) 4)); + map.put(int.class, Arrays.asList(1, 2, 3, 4)); + map.put(long.class, Arrays.asList(1L, 2L, 3L, 4L)); + map.put(char.class, Arrays.asList('1', '2', '3', '4')); + map.put(float.class, Arrays.asList(1.0F, 2.0F, 3.0F, 4.0F)); + map.put(double.class, Arrays.asList(1.0, 2.0, 3.0, 4.0)); + map.put(boolean.class, Arrays.asList(true, false, true)); + map.put(String.class, Arrays.asList("one", "two", "three")); + map.put(List.class, Arrays.asList( + Arrays.asList(1, 2, 3, 4), + Arrays.asList(1.0, 2.0, 3.0, 4.0), + Arrays.asList("one", "two", "three") + )); + } + + public static T serializeAndDeserialize(T t) { + Output output = new Output(1000, -1); + kryo.writeClassAndObject(output, t); + Input input = new Input(output.toBytes()); + return (T) kryo.readClassAndObject(input); + } + + public static List getList(Class c) { + return map.get(c); + } +} diff --git a/chill-java/src/test/scala/com/twitter/chill/java/ArraysAsListTest.scala b/chill-java/src/test/scala/com/twitter/chill/java/ArraysAsListTest.scala new file mode 100644 index 00000000..84726b32 --- /dev/null +++ b/chill-java/src/test/scala/com/twitter/chill/java/ArraysAsListTest.scala @@ -0,0 +1,72 @@ +/* + * Copyright 2016 Alex Chermenin + * + * Licensed 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 com.twitter.chill.java + +import TestLists._ +import org.scalatest._ + +class ArraysAsListTest extends WordSpec with Matchers { + + "ArraysAsListSerializer" should { + + "byte[]" in { + val list = getList(java.lang.Byte.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "short[]" in { + val list = getList(java.lang.Short.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "int[]" in { + val list = getList(java.lang.Integer.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "float[]" in { + val list = getList(java.lang.Float.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "double[]" in { + val list = getList(java.lang.Double.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "char[]" in { + val list = getList(java.lang.Character.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "boolean[]" in { + val list = getList(java.lang.Boolean.TYPE) + list should equal(serializeAndDeserialize(list)) + } + + "String[]" in { + val list = getList(classOf[String]) + list should equal(serializeAndDeserialize(list)) + } + + "Object[][]" in { + val list = getList(classOf[java.util.List[_]]) + list should equal(serializeAndDeserialize(list)) + } + } +}