Skip to content

Commit

Permalink
Added test for ArraysAsListSerializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
chermenin committed Aug 24, 2016
1 parent add1c3c commit 6d22c5f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 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<Class<?>, List<?>> map = new HashMap<Class<?>, 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> 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);
}
}
@@ -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 &quot;AS IS&quot; 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))
}
}
}

0 comments on commit 6d22c5f

Please sign in to comment.