[infinispan-commits] Infinispan SVN: r2322 - in trunk/client/hotrod-client: src/main/java/org/infinispan/client/hotrod and 3 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Thu Sep 2 12:01:54 EDT 2010
Author: galder.zamarreno at jboss.com
Date: 2010-09-02 12:01:52 -0400 (Thu, 02 Sep 2010)
New Revision: 2322
Added:
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/marshall/
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshaller.java
trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/marshall/
trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshallerTest.java
Modified:
trunk/client/hotrod-client/pom.xml
trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java
Log:
[ISPN-508] (ProtoBuf as portable serialization for Hot Rod) Done.
Modified: trunk/client/hotrod-client/pom.xml
===================================================================
--- trunk/client/hotrod-client/pom.xml 2010-09-02 14:54:05 UTC (rev 2321)
+++ trunk/client/hotrod-client/pom.xml 2010-09-02 16:01:52 UTC (rev 2322)
@@ -14,6 +14,10 @@
<name>Infinispan Client Hotrod Module</name>
<description>Infinispan client hotrod module</description>
<packaging>jar</packaging>
+ <properties>
+ <version.avro>1.3.3</version.avro>
+ </properties>
+
<dependencies>
<dependency>
@@ -23,6 +27,18 @@
</dependency>
<dependency>
+ <groupId>commons-pool</groupId>
+ <artifactId>commons-pool</artifactId>
+ <version>${version.commons.pool}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.hadoop</groupId>
+ <artifactId>avro</artifactId>
+ <version>${version.avro}</version>
+ </dependency>
+
+ <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>infinispan-core</artifactId>
<version>${project.version}</version>
@@ -46,12 +62,6 @@
</dependency>
<dependency>
- <groupId>commons-pool</groupId>
- <artifactId>commons-pool</artifactId>
- <version>${version.commons.pool}</version>
- </dependency>
-
- <dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${version.scala}</version>
Modified: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java 2010-09-02 14:54:05 UTC (rev 2321)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java 2010-09-02 16:01:52 UTC (rev 2322)
@@ -54,7 +54,7 @@
* <li><tt>infinispan.client.hotrod.tcp_no_delay</tt>, default = true. Affects TCP NODELAY on the TCP stack.</li>
* <li><tt>infinispan.client.hotrod.ping_on_startup</tt>, default = true. If true, a ping request is sent to a back end server in order to fetch cluster's topology.</li>
* <li><tt>infinispan.client.hotrod.transport_factory</tt>, default = org.infinispan.client.hotrod.impl.transport.tcp.TcpTransportFactory - controls which transport to use. Currently only the TcpTransport is supported.</li>
- * <li><tt>infinispan.client.hotrod.marshaller</tt>, default = org.infinispan.marshall.jboss.GenericJBossMarshaller. Allows you to specify a custom {@link org.infinispan.marshall.Marshaller} implementation to serialize and deserialize user objects.</li>
+ * <li><tt>infinispan.client.hotrod.marshaller</tt>, default = org.infinispan.marshall.jboss.GenericJBossMarshaller. Allows you to specify a custom {@link org.infinispan.marshall.Marshaller} implementation to serialize and deserialize user objects. For portable serialization payloads, you should configure the marshaller to be {@link org.infinispan.client.hotrod.marshall.ApacheAvroMarshaller}</li>
* <li><tt>infinispan.client.hotrod.async_executor_factory</tt>, default = org.infinispan.client.hotrod.impl.async.DefaultAsyncExecutorFactory. Allows you to specify a custom asynchroous executor for async calls.</li>
* <li><tt>infinispan.client.hotrod.default_executor_factory.pool_size</tt>, default = 10. If the default executor is used, this configures the number of threads to initialize the executor with.</li>
* <li><tt>infinispan.client.hotrod.default_executor_factory.queue_size</tt>, default = 100000. If the default executor is used, this configures the queue size to initialize the executor with.</li>
Added: trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshaller.java
===================================================================
--- trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshaller.java (rev 0)
+++ trunk/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshaller.java 2010-09-02 16:01:52 UTC (rev 2322)
@@ -0,0 +1,362 @@
+package org.infinispan.client.hotrod.marshall;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.util.Utf8;
+import org.infinispan.CacheException;
+import org.infinispan.io.ByteBuffer;
+import org.infinispan.io.ExposedByteArrayOutputStream;
+import org.infinispan.marshall.AbstractMarshaller;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * This is a portable serialization marshaller based on Apache Avro. It supports basic type and collection marshalling.
+ * Basic types include UTF-8 String, int long, float, double, boolean and null, and the collections supported include
+ * arrays, list, map and set composed of basic types.
+ *
+ * Primitive types short and byte are not supported per se. Instead, pass integers which will be encoded efficiently
+ * using variable-length (http://lucene.apache.org/java/2_4_0/fileformats.html#VInt) zig zag
+ * (http://code.google.com/apis/protocolbuffers/docs/encoding.html#types) coding.
+ *
+ * Primitive arrays not supported except byte arrays. Instead, use their object counter partners, i.e. Integer...etc.
+ *
+ * For more detailed information, go to: http://community.jboss.org/docs/DOC-15774
+ *
+ * @author Galder Zamarreño
+ * @since 5.0
+ */
+public class ApacheAvroMarshaller extends AbstractMarshaller {
+
+ private static final Schema STRING_SCHEMA = Schema.create(Schema.Type.STRING);
+ private static final Schema INT_SCHEMA = Schema.create(Schema.Type.INT);
+ private static final Schema LONG_SCHEMA = Schema.create(Schema.Type.LONG);
+ private static final Schema FLOAT_SCHEMA = Schema.create(Schema.Type.FLOAT);
+ private static final Schema DOUBLE_SCHEMA = Schema.create(Schema.Type.DOUBLE);
+ private static final Schema BOOLEAN_SCHEMA = Schema.create(Schema.Type.BOOLEAN);
+ private static final Schema BYTES_SCHEMA = Schema.create(Schema.Type.BYTES);
+ private static final Schema NULL_SCHEMA = Schema.create(Schema.Type.NULL);
+ private static final Schema STRING_ARRAY_SCHEMA = Schema.createArray(STRING_SCHEMA);
+ private static final Schema INT_ARRAY_SCHEMA = Schema.createArray(INT_SCHEMA);
+ private static final Schema LONG_ARRAY_SCHEMA = Schema.createArray(LONG_SCHEMA);
+ private static final Schema FLOAT_ARRAY_SCHEMA = Schema.createArray(FLOAT_SCHEMA);
+ private static final Schema DOUBLE_ARRAY_SCHEMA = Schema.createArray(DOUBLE_SCHEMA);
+ private static final Schema BOOLEAN_ARRAY_SCHEMA = Schema.createArray(BOOLEAN_SCHEMA);
+
+ private static final MarshallableType STRING_TYPE = new StringMarshallableType(0);
+ private static final MarshallableType INT_TYPE = new MarshallableType(INT_SCHEMA, 1);
+ private static final MarshallableType LONG_TYPE = new MarshallableType(LONG_SCHEMA, 2);
+ private static final MarshallableType FLOAT_TYPE = new MarshallableType(FLOAT_SCHEMA, 3);
+ private static final MarshallableType DOUBLE_TYPE = new MarshallableType(DOUBLE_SCHEMA, 4);
+ private static final MarshallableType BOOLEAN_TYPE = new MarshallableType(BOOLEAN_SCHEMA, 5);
+ private static final MarshallableType BYTES_TYPE = new BytesMarshallableType(6);
+ private static final MarshallableType NULL_TYPE = new MarshallableType(NULL_SCHEMA, 7);
+ private static final MarshallableType STRING_ARRAY_TYPE = new StringArrayMarshallableType(8);
+ private static final MarshallableType INT_ARRAY_TYPE = new ArrayMarshallableType<Integer>(INT_ARRAY_SCHEMA, Integer.class, 9);
+ private static final MarshallableType LONG_ARRAY_TYPE = new ArrayMarshallableType<Long>(LONG_ARRAY_SCHEMA, Long.class, 10);
+ private static final MarshallableType DOUBLE_ARRAY_TYPE = new ArrayMarshallableType<Double>(DOUBLE_ARRAY_SCHEMA, Double.class, 11);
+ private static final MarshallableType FLOAT_ARRAY_TYPE = new ArrayMarshallableType<Float>(FLOAT_ARRAY_SCHEMA, Float.class, 12);
+ private static final MarshallableType BOOLEAN_ARRAY_TYPE = new ArrayMarshallableType<Boolean>(BOOLEAN_ARRAY_SCHEMA, Boolean.class, 13);
+ private final MarshallableType listType = new ListMarshallableType(14, this);
+ private final MarshallableType mapType = new MapMarshallableType(15, this);
+ private final MarshallableType setType = new SetMarshallableType(16, this);
+
+ private MarshallableType getType(int type) {
+ switch (type) {
+ case 0: return STRING_TYPE;
+ case 1: return INT_TYPE;
+ case 2: return LONG_TYPE;
+ case 3: return FLOAT_TYPE;
+ case 4: return DOUBLE_TYPE;
+ case 5: return BOOLEAN_TYPE;
+ case 6: return BYTES_TYPE;
+ case 7: return NULL_TYPE;
+ case 8: return STRING_ARRAY_TYPE;
+ case 9: return INT_ARRAY_TYPE;
+ case 10: return LONG_ARRAY_TYPE;
+ case 11: return DOUBLE_ARRAY_TYPE;
+ case 12: return FLOAT_ARRAY_TYPE;
+ case 13: return BOOLEAN_ARRAY_TYPE;
+ case 14: return listType;
+ case 15: return mapType;
+ case 16: return setType;
+ default: throw new CacheException("Unknown type " + type);
+ }
+ }
+
+ @Override
+ protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException {
+ ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
+ Encoder encoder = new BinaryEncoder(baos);
+ objectToBuffer(o, encoder);
+ return new ByteBuffer(baos.getRawBuffer(), 0, baos.size());
+ }
+
+ private void objectToBuffer(Object o, Encoder encoder) throws IOException {
+ if (o == null) {
+ NULL_TYPE.write(o, encoder);
+ } else {
+ Class clazz = o.getClass();
+ MarshallableType type;
+ if (clazz.equals(String.class)) type = STRING_TYPE;
+ else if (clazz.equals(byte[].class)) type = BYTES_TYPE;
+ else if (clazz.equals(Boolean.class)) type = BOOLEAN_TYPE;
+ else if (clazz.equals(Integer.class)) type = INT_TYPE;
+ else if (clazz.equals(Long.class)) type = LONG_TYPE;
+ else if (clazz.equals(Float.class)) type = FLOAT_TYPE;
+ else if (clazz.equals(Double.class)) type = DOUBLE_TYPE;
+ else if (clazz.equals(String[].class)) type = STRING_ARRAY_TYPE;
+ else if (clazz.equals(Integer[].class)) type = INT_ARRAY_TYPE;
+ else if (clazz.equals(Long[].class)) type = LONG_ARRAY_TYPE;
+ else if (clazz.equals(Float[].class)) type = FLOAT_ARRAY_TYPE;
+ else if (clazz.equals(Double[].class)) type = DOUBLE_ARRAY_TYPE;
+ else if (clazz.equals(Boolean[].class)) type = BOOLEAN_ARRAY_TYPE;
+ else if (o instanceof List) type = listType;
+ else if (o instanceof Map) type = mapType;
+ else if (o instanceof Set) type = setType;
+ else
+ throw new CacheException("Unsupported type: " + clazz);
+
+ type.write(o, encoder);
+ }
+ }
+
+ @Override
+ public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException {
+ DecoderFactory factory = new DecoderFactory(); // TODO: Could this be cached?
+ InputStream is = new ByteArrayInputStream(buf, offset, length);
+ Decoder decoder = factory.createBinaryDecoder(is, null);
+ return objectFromByteBuffer(decoder);
+ }
+
+ private Object objectFromByteBuffer(Decoder decoder) throws IOException {
+ int type = decoder.readInt();
+ return getType(type).read(decoder);
+ }
+
+ @Override
+ public boolean isMarshallable(Object o) {
+ Class clazz = o.getClass();
+ return clazz.equals(String.class) || clazz.equals(byte[].class)
+ || clazz.equals(Boolean.class) || clazz.equals(Integer.class)
+ || clazz.equals(Long.class) || clazz.equals(Float.class)
+ || clazz.equals(Double.class) || clazz.equals(String[].class)
+ || clazz.equals(Integer[].class) || clazz.equals(Long[].class)
+ || clazz.equals(Float[].class) || clazz.equals(Double[].class)
+ || clazz.equals(Boolean[].class) || o instanceof List
+ || o instanceof Map || o instanceof Set;
+ }
+
+ private static class MarshallableType {
+ final Schema schema;
+ final int id;
+
+ MarshallableType(Schema schema, int id) {
+ this.schema = schema;
+ this.id = id;
+ }
+
+ Object read(Decoder decoder) throws IOException {
+ return new GenericDatumReader(schema).read(null, decoder);
+ }
+
+ void write(Object o, Encoder encoder) throws IOException {
+ GenericDatumWriter writer = new GenericDatumWriter(schema); // TODO: Could this be cached? Maybe, but ctor is very cheap
+ encoder.writeInt(id);
+ write(writer, o, encoder);
+ }
+
+ void write(GenericDatumWriter writer, Object o, Encoder encoder) throws IOException {
+ writer.write(o, encoder);
+ }
+
+ }
+
+ private static class StringMarshallableType extends MarshallableType {
+ StringMarshallableType(int id) {
+ super(STRING_SCHEMA, id);
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ return new GenericDatumReader(schema).read(null, decoder).toString();
+ }
+
+ @Override
+ void write(GenericDatumWriter writer, Object o, Encoder encoder) throws IOException {
+ writer.write(new Utf8((String) o), encoder);
+ }
+ }
+
+ private static class BytesMarshallableType extends MarshallableType {
+ BytesMarshallableType(int id) {
+ super(BYTES_SCHEMA, id);
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ java.nio.ByteBuffer byteBuffer = (java.nio.ByteBuffer) new GenericDatumReader(schema).read(null, decoder);
+ byte[] bytes = new byte[byteBuffer.limit()]; // TODO: Limit or capacity ? Limit works
+ byteBuffer.get(bytes);
+ return bytes;
+ }
+
+ @Override
+ void write(GenericDatumWriter writer, Object o, Encoder encoder) throws IOException {
+ writer.write(java.nio.ByteBuffer.wrap((byte[]) o), encoder);
+ }
+ }
+
+ private static class StringArrayMarshallableType extends MarshallableType {
+ StringArrayMarshallableType(int id) {
+ super(STRING_ARRAY_SCHEMA, id);
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ GenericData.Array<Utf8> utf8s = (GenericData.Array<Utf8>) new GenericDatumReader(schema).read(null, decoder);
+ List<String> strings = new ArrayList<String>((int) utf8s.size());
+ for (Utf8 utf8 : utf8s)
+ strings.add(utf8.toString());
+ return strings.toArray(new String[0]);
+ }
+
+ @Override
+ void write(GenericDatumWriter writer, Object o, Encoder encoder) throws IOException {
+ String[] strings = (String[]) o;
+ GenericData.Array<Utf8> array = new GenericData.Array(strings.length, schema);
+ for (String str : strings)
+ array.add(new Utf8(str));
+ writer.write(array, encoder);
+ }
+ }
+
+ private static class ArrayMarshallableType<T> extends MarshallableType {
+ private final Class<T> type;
+
+ ArrayMarshallableType(Schema schema, Class<T> type, int id) {
+ super(schema, id);
+ this.type = type;
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ GenericData.Array<T> avroArray = (GenericData.Array<T>) new GenericDatumReader(schema).read(null, decoder);
+ List<T> list = new ArrayList<T>((int) avroArray.size());
+ for (T t : avroArray)
+ list.add(t);
+ T[] array = (T[]) Array.newInstance(type, list.size());
+ return toArray(list, array);
+ }
+
+ private <T> T[] toArray(List<T> list, T... ts) {
+ // Varargs hack!
+ return list.toArray(ts);
+ }
+
+ @Override
+ void write(GenericDatumWriter writer, Object o, Encoder encoder) throws IOException {
+ T[] array = (T[]) o;
+ GenericData.Array<T> avroArray = new GenericData.Array(array.length, schema);
+ for (T t : array)
+ avroArray.add(t);
+ writer.write(avroArray, encoder);
+ }
+ }
+
+ private static abstract class CollectionMarshallableType extends MarshallableType {
+ final ApacheAvroMarshaller marshaller;
+
+ CollectionMarshallableType(int id, ApacheAvroMarshaller marshaller) {
+ super(null, id);
+ this.marshaller = marshaller;
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ long size = decoder.readArrayStart();
+ Collection collection = createCollection((int) size);
+ for (int k = 0; k < size; k++)
+ collection.add(marshaller.objectFromByteBuffer(decoder));
+ return collection;
+ }
+
+ @Override
+ void write(Object o, Encoder encoder) throws IOException {
+ Collection collection = (Collection) o;
+ encoder.writeInt(id);
+ encoder.setItemCount(collection.size());
+ for (Object element : collection)
+ marshaller.objectToBuffer(element, encoder);
+ }
+
+ Collection createCollection(int size) {
+ return null;
+ }
+ }
+
+ private static class ListMarshallableType extends CollectionMarshallableType {
+ ListMarshallableType(int id, ApacheAvroMarshaller marshaller) {
+ super(id, marshaller);
+ }
+
+ @Override
+ Collection createCollection(int size) {
+ return new ArrayList(size);
+ }
+ }
+
+ private static class MapMarshallableType extends CollectionMarshallableType {
+ MapMarshallableType(int id, ApacheAvroMarshaller marshaller) {
+ super(id, marshaller);
+ }
+
+ @Override
+ Object read(Decoder decoder) throws IOException {
+ long size = decoder.readArrayStart();
+ Map map = new HashMap((int) size);
+ for (int i = 0; i < size; i++)
+ map.put(marshaller.objectFromByteBuffer(decoder), marshaller.objectFromByteBuffer(decoder));
+ return map;
+ }
+
+ @Override
+ void write(Object o, Encoder encoder) throws IOException {
+ Map map = (Map) o;
+ encoder.writeInt(id);
+ encoder.setItemCount(map.size());
+ for (Map.Entry entry : (Set<Map.Entry>) map.entrySet()) {
+ marshaller.objectToBuffer(entry.getKey(), encoder);
+ marshaller.objectToBuffer(entry.getValue(), encoder);
+ }
+
+ }
+ }
+
+ private class SetMarshallableType extends CollectionMarshallableType {
+ public SetMarshallableType(int id, ApacheAvroMarshaller marshaller) {
+ super(id, marshaller);
+ }
+
+ @Override
+ Collection createCollection(int size) {
+ return new HashSet(size);
+ }
+ }
+}
Added: trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshallerTest.java
===================================================================
--- trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshallerTest.java (rev 0)
+++ trunk/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/marshall/ApacheAvroMarshallerTest.java 2010-09-02 16:01:52 UTC (rev 2322)
@@ -0,0 +1,158 @@
+package org.infinispan.client.hotrod.marshall;
+
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * // TODO: Document this
+ *
+ * @author Galder Zamarreño
+ * @since 5.0
+ */
+ at Test(groups = "functional", testName = "client.hotrod.VersionAwareMarshallerTest")
+public class ApacheAvroMarshallerTest {
+
+ private final ApacheAvroMarshaller marshaller = new ApacheAvroMarshaller();
+
+ public void testStringMarshalling() {
+ String x = "Galder";
+ String y = (String) marshallUnmarshall(x);
+ assert y.equals(x);
+ }
+
+ public void testBooleanMarshalling() {
+ boolean x = true;
+ boolean y = ((Boolean) marshallUnmarshall(x)).booleanValue();
+ assert x == y;
+ assertEquality(new Boolean(false));
+ }
+
+ public void testIntegerMarshalling() {
+ int x = 99;
+ int y = ((Integer) marshallUnmarshall(x)).intValue();
+ assert x == y;
+ assertEquality(new Integer(12345));
+ }
+
+ public void testLongMarshalling() {
+ long x = 9223372036854775807L;
+ long y = ((Long) marshallUnmarshall(x)).longValue();
+ assert x == y;
+ assertEquality(new Long(72057594037927936L));
+ }
+
+ public void testFloatMarshalling() {
+ float x = 123.4f;
+ float y = ((Float) marshallUnmarshall(x)).floatValue();
+ assert x == y;
+ assertEquality(new Float(56789.9));
+ }
+
+ public void testDoubleMarshalling() {
+ double x = 1.234e2;
+ double y = ((Double) marshallUnmarshall(x)).doubleValue();
+ assert x == y;
+ assertEquality(new Double(5.678e9));
+ }
+
+ public void testNullMarshalling() {
+ assert null == marshallUnmarshall(null);
+ }
+
+ public void testBytesMarshalling() {
+ byte[] x = new byte[]{1, 2, 3, 4};
+ byte[] y = (byte[]) marshallUnmarshall(x);
+ assert Arrays.equals(x, y);
+ }
+
+ public void testStringArrayMarshalling() {
+ assertArrayEquality(new String[]{"Basque Country", "Spain", "UK", "Switzerland"});
+ }
+
+ public void testIntArrayMarshalling() {
+ assertArrayEquality(new Integer[]{1234, 5678, 9101112});
+ }
+
+ public void testLongArrayMarshalling() {
+ assertArrayEquality(new Long[]{9223372036854775807L, 72057594037927936L});
+ }
+
+ public void testBooleanArrayMarshalling() {
+ assertArrayEquality(new Boolean[] {true, false, true, true});
+ }
+
+ public void testFloatArrayMarshalling() {
+ assertArrayEquality(new Float[] {56789.9f, 1234.6f, 85894.303f, 67484.32f, 4732.4f});
+ }
+
+ public void testDoubleArrayMarshalling() {
+ assertArrayEquality(new Double[] {5.678e9, 1.623435e9, 5.654545e5, 9.6232323e1});
+ }
+
+ public void testListMarshalling() {
+ List<String> cities = new ArrayList<String>();
+ cities.add("algorta");
+ cities.add("neuchatel");
+ cities.add("ibiza");
+ assertEquality(cities);
+
+ List<Integer> numbers = new ArrayList<Integer>();
+ numbers.add(12);
+ numbers.add(3232412);
+ numbers.add(4345132);
+ numbers.add(898979);
+ assertEquality(numbers);
+
+ List<Boolean> testimony = new LinkedList<Boolean>();
+ testimony.add(false);
+ testimony.add(true);
+ testimony.add(true);
+ testimony.add(true);
+ assertEquality(testimony);
+ }
+
+ public void testMapMarshalling() {
+ Map<Long, Float> numbers = new HashMap<Long, Float>();
+ numbers.put(9223372036854775807L, 4732.4f);
+ numbers.put(72057594037927936L, 67484.32f);
+ numbers.put(7205759412424936L, 132367484.32f);
+ assertEquality(numbers);
+ }
+
+ public void testSetMarshalling() {
+ Set words = new HashSet();
+ words.add("cat");
+ words.add("dog");
+ words.add("perro");
+ words.add("txakur");
+ assertEquality(words);
+ }
+
+ private Object marshallUnmarshall(Object o) {
+ try {
+ byte[] buffer = marshaller.objectToByteBuffer(o);
+ return marshaller.objectFromByteBuffer(buffer);
+ } catch(Exception e) {
+ throw new RuntimeException("Error marshalling or unmarshalling", e);
+ }
+ }
+
+ private <T> void assertArrayEquality(T[] x) {
+ T[] y = (T[]) ApacheAvroMarshallerTest.this.marshallUnmarshall(x);
+ assert Arrays.equals(x, y);
+ }
+
+ private <T> void assertEquality(T x) {
+ T y = (T) ApacheAvroMarshallerTest.this.marshallUnmarshall(x);
+ assert x.equals(y);
+ }
+
+}
More information about the infinispan-commits
mailing list