Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 157CE200CA4 for ; Wed, 24 May 2017 01:41:30 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 144B0160BD6; Tue, 23 May 2017 23:41:30 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3E03C160BC3 for ; Wed, 24 May 2017 01:41:28 +0200 (CEST) Received: (qmail 54358 invoked by uid 500); 23 May 2017 23:41:26 -0000 Mailing-List: contact commits-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list commits@geode.apache.org Received: (qmail 54349 invoked by uid 99); 23 May 2017 23:41:26 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 May 2017 23:41:26 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 954C0DFB01; Tue, 23 May 2017 23:41:26 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: udo@apache.org To: commits@geode.apache.org Date: Tue, 23 May 2017 23:41:26 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] geode git commit: GEODE-2580 : Pre Kotlin conversion archived-at: Tue, 23 May 2017 23:41:30 -0000 Repository: geode Updated Branches: refs/heads/feature/GEODE-2580 2ccedd062 -> 6a5e4be30 GEODE-2580 : Pre Kotlin conversion Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/c1e22f37 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/c1e22f37 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/c1e22f37 Branch: refs/heads/feature/GEODE-2580 Commit: c1e22f3757f96e8bf100bd4dd39819e142782e9a Parents: 2ccedd0 Author: Udo Kohlmeyer Authored: Tue May 23 11:47:46 2017 -0700 Committer: Udo Kohlmeyer Committed: Tue May 23 11:47:46 2017 -0700 ---------------------------------------------------------------------- geode-client-protobuf/build.gradle | 14 +- .../geode/protocol/client/EncodingTypeThingy.kt | 33 ++++ .../client/NewClientProtocolTestClient.java | 8 +- .../client/ProtobufProtocolMessageHandler.java | 153 ++++++------------- .../src/main/proto/basicTypes.proto | 35 +++-- .../src/main/proto/region_API.proto | 10 +- .../geode/protocol/client/MessageUtils.java | 62 +++----- .../client/ProtobufProtocolIntegrationTest.java | 112 +++++++------- ...rotobufSerializationDeserializationTest.java | 63 ++++---- .../sockets/ClientProtocolMessageHandler.java | 4 +- .../cache/tier/sockets/ServerConnection.java | 4 +- .../geode/serialization/Deserializer.java | 2 +- .../geode/serialization/SerializationType.java | 62 +++++++- .../apache/geode/serialization/Serializer.java | 2 +- .../serialization/SerializationTypeTest.java | 113 ++++++++++++++ 15 files changed, 402 insertions(+), 275 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/build.gradle ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/build.gradle b/geode-client-protobuf/build.gradle index c82f311..79932ae 100644 --- a/geode-client-protobuf/build.gradle +++ b/geode-client-protobuf/build.gradle @@ -17,19 +17,24 @@ apply plugin: 'java' +apply plugin: 'kotlin' apply plugin: 'com.google.protobuf' apply plugin: 'idea' repositories { maven { url "https://plugins.gradle.org/m2/" } + mavenCentral() } buildscript { - repositories { + ext.kotlin_version = '1.1.1' + repositories { maven { url "https://plugins.gradle.org/m2/" } - } + mavenCentral() + } dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -48,9 +53,12 @@ dependencies { testCompile project (':geode-junit') testCompile "org.mockito:mockito-core:2.+" testCompile files(project(':geode-core').sourceSets.test.output) - // Extra proto source files for test besides the ones residing under + compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" + // Extra proto source files for test besides the ones residing under // "src/test". //testProtobuf files("lib/protos-test.tar.gz") + testCompile 'com.pholser:junit-quickcheck-core:' + project.'junit-quickcheck.version' + testCompile 'com.pholser:junit-quickcheck-generators:' + project.'junit-quickcheck.version' } protobuf { http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/EncodingTypeThingy.kt ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/EncodingTypeThingy.kt b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/EncodingTypeThingy.kt new file mode 100644 index 0000000..8d65827 --- /dev/null +++ b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/EncodingTypeThingy.kt @@ -0,0 +1,33 @@ +package org.apache.geode.protocol.client + +import org.apache.geode.pdx.JSONFormatter +import org.apache.geode.pdx.PdxInstance +import org.apache.geode.protocol.protobuf.BasicTypes +import org.apache.geode.serialization.SerializationType + +fun getEncodingTypeForObjectKT(o: Any?): BasicTypes.EncodingType { + return when (o) { + is String -> BasicTypes.EncodingType.STRING + is Int -> BasicTypes.EncodingType.INT + is PdxInstance -> { + if (o.className == JSONFormatter.JSON_CLASSNAME) BasicTypes.EncodingType.JSON else BasicTypes.EncodingType.UNRECOGNIZED + } + is ByteArray -> BasicTypes.EncodingType.BINARY + else -> BasicTypes.EncodingType.UNRECOGNIZED + } +} + +fun serializerFromProtoEnum(encodingType: BasicTypes.EncodingType): SerializationType { + return when (encodingType) { + BasicTypes.EncodingType.INT -> SerializationType.INT + BasicTypes.EncodingType.LONG -> SerializationType.LONG + BasicTypes.EncodingType.SHORT -> SerializationType.SHORT + BasicTypes.EncodingType.BYTE -> SerializationType.BYTE + BasicTypes.EncodingType.STRING -> SerializationType.STRING + BasicTypes.EncodingType.BINARY -> SerializationType.BYTE_BLOB + BasicTypes.EncodingType.JSON -> SerializationType.JSON + BasicTypes.EncodingType.FLOAT, BasicTypes.EncodingType.BOOLEAN, BasicTypes.EncodingType.DOUBLE -> TODO() + else -> TODO() + } +} + http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/NewClientProtocolTestClient.java ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/NewClientProtocolTestClient.java b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/NewClientProtocolTestClient.java index 834d2f6..0b29aa3 100644 --- a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/NewClientProtocolTestClient.java +++ b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/NewClientProtocolTestClient.java @@ -73,11 +73,11 @@ public class NewClientProtocolTestClient implements AutoCloseable { ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()); // .setSize() //we don't need to set the size because Protobuf will handle the message frame - BasicTypes.Key.Builder key = - BasicTypes.Key.newBuilder().setKey(ByteString.copyFrom(createByteArrayOfSize(64))); + BasicTypes.EncodedValue.Builder key = BasicTypes.EncodedValue.newBuilder() + .setValue(ByteString.copyFrom(createByteArrayOfSize(64))); - BasicTypes.Value.Builder value = - BasicTypes.Value.newBuilder().setValue(ByteString.copyFrom(createByteArrayOfSize(512))); + BasicTypes.EncodedValue.Builder value = BasicTypes.EncodedValue.newBuilder() + .setValue(ByteString.copyFrom(createByteArrayOfSize(512))); RegionAPI.PutRequest.Builder putRequestBuilder = RegionAPI.PutRequest.newBuilder().setRegionName("TestRegion") http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java index 85de51d..ed9a7e3 100644 --- a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java +++ b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java @@ -15,7 +15,15 @@ package org.apache.geode.protocol.client; +import static org.apache.geode.protocol.client.EncodingTypeThingyKt.getEncodingTypeForObjectKT; +import static org.apache.geode.protocol.client.EncodingTypeThingyKt.serializerFromProtoEnum; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Message; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Request; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Response; +import static org.apache.geode.protocol.protobuf.RegionAPI.PutRequest; + import com.google.protobuf.ByteString; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheWriterException; import org.apache.geode.cache.Region; @@ -26,19 +34,13 @@ import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.RegionAPI; import org.apache.geode.protocol.protobuf.RegionAPI.GetRequest; import org.apache.geode.protocol.protobuf.RegionAPI.PutResponse; -import org.apache.geode.serialization.Deserializer; -import org.apache.geode.serialization.Serializer; +import org.apache.geode.serialization.SerializationType; import org.apache.logging.log4j.Logger; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Message; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Request; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Response; -import static org.apache.geode.protocol.protobuf.RegionAPI.PutRequest; - public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHandler { private static final Logger logger = LogService.getLogger(); @@ -48,8 +50,8 @@ public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHand } @Override - public void receiveMessage(InputStream inputStream, OutputStream outputStream, - Deserializer deserializer, Serializer serializer, Cache cache) throws IOException { + public void receiveMessage(InputStream inputStream, OutputStream outputStream, Cache cache) + throws IOException { final Message message = Message.parseDelimitedFrom(inputStream); // can be null at EOF, see Parser.parseDelimitedFrom(java.io.InputStream) if (message == null) { @@ -66,9 +68,9 @@ public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHand Request request = message.getRequest(); Request.RequestAPICase requestAPICase = request.getRequestAPICase(); if (requestAPICase == Request.RequestAPICase.GETREQUEST) { - responseMessage = doGetRequest(request.getGetRequest(), deserializer, serializer, cache); + responseMessage = doGetRequest(request.getGetRequest(), cache); } else if (requestAPICase == Request.RequestAPICase.PUTREQUEST) { - responseMessage = doPutRequest(request.getPutRequest(), deserializer, cache); + responseMessage = doPutRequest(request.getPutRequest(), cache); } else { // TODO } @@ -77,16 +79,15 @@ public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHand } } - private Message doPutRequest(PutRequest request, Deserializer dataDeserializer, Cache cache) { + private Message doPutRequest(PutRequest request, Cache cache) { final String regionName = request.getRegionName(); final BasicTypes.Entry entry = request.getEntry(); - final ByteString key = entry.getKey().getKey(); - final ByteString value = entry.getValue().getValue(); + assert (entry != null); final Region region = cache.getRegion(regionName); try { - region.put(dataDeserializer.deserialize(key.toByteArray()), - dataDeserializer.deserialize(value.toByteArray())); + region.put(deserializeEncodedValue(entry.getKey()), + deserializeEncodedValue(entry.getValue())); return putResponseWithStatus(true); } catch (TimeoutException | CacheWriterException ex) { logger.warn("Caught normal-ish exception doing region put", ex); @@ -94,111 +95,53 @@ public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHand } } + private Object deserializeEncodedValue(BasicTypes.EncodedValue encodedValue) { + assert (encodedValue != null); + SerializationType serializer = serializerFromProtoEnum(encodedValue.getEncodingType()); + return serializer.deserialize(encodedValue.getValue().toByteArray()); + } + + private byte[] serializeEncodedValue(BasicTypes.EncodingType encodingType, + Object objectToSerialize) { + if (objectToSerialize == null) { + return null; // BLECH!!! :( + } + SerializationType serializer = serializerFromProtoEnum(encodingType); + return serializer.serialize(objectToSerialize); + } + private Message putResponseWithStatus(boolean ok) { return Message.newBuilder() .setResponse(Response.newBuilder().setPutResponse(PutResponse.newBuilder().setSuccess(ok))) .build(); } - private Message doGetRequest(GetRequest request, Deserializer deserializer, Serializer serializer, - Cache cache) { - String regionName = request.getRegionName(); - BasicTypes.Key key = request.getKey(); - byte[] keyBytes = key.getKey().toByteArray(); - Region region = cache.getRegion(regionName); - - Object returnValue = region.get(deserializer.deserialize(keyBytes)); + private Message doGetRequest(GetRequest request, Cache cache) { + Region region = cache.getRegion(request.getRegionName()); + Object returnValue = region.get(deserializeEncodedValue(request.getKey())); if (returnValue == null) { - return getResponseWithValue(new byte[0]); + + return makeGetResponseMessageWithValue(new byte[0]); } else { // TODO types in the region? - return getResponseWithValue(serializer.serialize(returnValue)); + return makeGetResponseMessageWithValue(returnValue); } } - private Message getResponseWithValue(byte[] value) { + private BasicTypes.EncodingType getEncodingTypeForObject(Object object) { + return getEncodingTypeForObjectKT(object); + } + + private Message makeGetResponseMessageWithValue(Object objectToReturn) { + BasicTypes.EncodingType encodingType = getEncodingTypeForObject(objectToReturn); + byte[] serializedObject = serializeEncodedValue(encodingType, objectToReturn); return Message.newBuilder() .setResponse(Response.newBuilder() .setGetResponse(RegionAPI.GetResponse.newBuilder() - .setResult(BasicTypes.Value.newBuilder().setValue(ByteString.copyFrom(value))))) + .setResult(BasicTypes.EncodedValue.newBuilder() + .setEncodingType(BasicTypes.EncodingType.STRING) + .setValue(ByteString.copyFrom(serializedObject))))) .build(); } - - public ProtobufProtocolMessageHandler() {} } - - -// public final class NewClientProtocol { -// public static void recvMessage(Cache cache, InputStream inputStream, OutputStream outputStream) { -// try { -// final DataInputStream dataInputStream = new DataInputStream(inputStream); -// final DataOutputStream dataOutputStream = new DataOutputStream(outputStream); -// -// RequestHeader header = new RequestHeader(dataInputStream); -// -// // todo: string -- assume UTF-8 from here. -// // Java modified UTF-8: unsigned short len. hope we don't run into the "modified" part. -// switch (header.requestType) { -// case MessageType.PUT: -// servePutRequest(header, cache, dataInputStream, dataOutputStream); -// break; -// case MessageType.REQUEST: // this is a GET request. -// serveGetRequest(cache, dataInputStream, dataOutputStream); -// break; -// } -// } catch (IOException e) { -// e.printStackTrace(); -// // todo error handling. -// } -// } -// -// private static void serveGetRequest(Cache cache, DataInputStream dataInputStream, -// DataOutputStream dataOutputStream) throws IOException { -// // GetRequest: Header RegionName Key CallbackArg -// final String regionName = readString(dataInputStream); -// final String key = readString(dataInputStream); -// // todo no callback arg for now -// final Region region = cache.getRegion(regionName); -// // todo anything more complex? -// -// Object val = region.get(key); -// if (val == null) { -// byte[] bytes = "Entry not found in region.".getBytes(); -// dataOutputStream.writeInt(bytes.length); // len -// dataOutputStream.writeByte(19); // ENTRY_NOT_FOUND_EXCEPTION -// dataOutputStream.write(bytes); -// } else { -// byte[] bytes = val.toString().getBytes(); -// dataOutputStream.writeInt(bytes.length); // len -// dataOutputStream.writeByte(1); // RESPONSE -// dataOutputStream.write(bytes); -// } -// dataOutputStream.flush(); -// } -// -// // response: size responseType requestId -// -// private static void servePutRequest(RequestHeader header, Cache cache, -// DataInputStream dataInputStream, DataOutputStream dataOutputStream) throws IOException { -// String regionName = readString(dataInputStream); -// // assume every object is a string. -// -// String key = readString(dataInputStream); -// // TODO: value header, callback arg? -// String value = readString(dataInputStream); -// -// Region region = cache.getRegion(regionName); -// region.put(key, value); -// -// dataOutputStream.writeInt(0); // len -// dataOutputStream.writeByte(1); // RESPONSE -// dataOutputStream.writeInt(header.requestId); -// dataOutputStream.flush(); -// } -// -// private static String readString(DataInputStream inputStream) throws IOException { -// String s = inputStream.readUTF(); -// return s; -// } -// } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/main/proto/basicTypes.proto ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/proto/basicTypes.proto b/geode-client-protobuf/src/main/proto/basicTypes.proto index 7cb4204..2f9568c 100644 --- a/geode-client-protobuf/src/main/proto/basicTypes.proto +++ b/geode-client-protobuf/src/main/proto/basicTypes.proto @@ -17,29 +17,36 @@ syntax = "proto3"; package org.apache.geode.protocol.protobuf; import "google/protobuf/any.proto"; +import "google/protobuf/empty.proto"; -message Entry{ - Key key = 1; - Value value = 2; +message Entry { + EncodedValue key = 1; + EncodedValue value = 2; } -message Key { - bytes key = 1; -} -message Value { - ValueHeader valueHeader = 1; +message EncodedValue { + EncodingType encodingType = 1; bytes value = 2; } -message ValueHeader { - int32 size = 1; - bool isPartial = 2; +enum EncodingType { + INVALID = 0; + INT = 1; + LONG = 2; + SHORT = 3; + BYTE = 4; + BOOLEAN = 5; + BINARY = 6; + FLOAT = 7; + DOUBLE = 8; + STRING = 9; + JSON = 10; } -message CallbackArguments{ +message CallbackArguments { oneof callbackArgs { - bool hasCallbackArgument = 1; - bytes callbackBytes = 2; + google.protobuf.Empty hasCallbackArgument = 1; + EncodedValue callbackValue = 2; } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/main/proto/region_API.proto ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/proto/region_API.proto b/geode-client-protobuf/src/main/proto/region_API.proto index d46e0e6..cf9536b 100644 --- a/geode-client-protobuf/src/main/proto/region_API.proto +++ b/geode-client-protobuf/src/main/proto/region_API.proto @@ -30,12 +30,12 @@ message PutResponse { message GetRequest { string regionName = 1; - Key key = 2; + EncodedValue key = 2; CallbackArguments callbackArg = 3; } message GetResponse { - Value result = 1; + EncodedValue result = 1; } message PutAllRequest { @@ -47,14 +47,14 @@ message PutAllRequest { message PutAllResponse { int32 numberOfKeysFailed = 1; - repeated Key key = 2; + repeated EncodedValue key = 2; } message GetAllRequest { string regionName = 1; int32 numberOfKeys = 2; - repeated Key key = 3; - CallbackArguments callbackArg = 4; + repeated EncodedValue key = 3; + EncodedValue callbackArg = 4; } message GetAllResponse { http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java index 7cb5ffa..250c2f8 100644 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java @@ -15,8 +15,10 @@ package org.apache.geode.protocol.client; -import com.google.protobuf.Any; +import static org.apache.geode.protocol.client.EncodingTypeThingyKt.serializerFromProtoEnum; + import com.google.protobuf.ByteString; + import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.RegionAPI; @@ -24,6 +26,7 @@ import org.apache.geode.protocol.protobuf.RegionAPI; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Random; public class MessageUtils { @@ -35,40 +38,29 @@ public class MessageUtils { return new ByteArrayInputStream(messageByteArray); } - public static ClientProtocol.Message makePutMessage() { + public static ClientProtocol.Message makeGetMessageFor(String region, String key) { Random random = new Random(); ClientProtocol.MessageHeader.Builder messageHeader = ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()); - BasicTypes.Key.Builder key = - BasicTypes.Key.newBuilder().setKey(ByteString.copyFrom(createByteArrayOfSize(64))); - - BasicTypes.Value.Builder value = - BasicTypes.Value.newBuilder().setValue(ByteString.copyFrom(createByteArrayOfSize(512))); - - RegionAPI.PutRequest.Builder putRequestBuilder = - RegionAPI.PutRequest.newBuilder().setRegionName("TestRegion") - .setEntry(BasicTypes.Entry.newBuilder().setKey(key).setValue(value)); + BasicTypes.EncodedValue.Builder keyBuilder = getEncodedValueBuilder(key); + RegionAPI.GetRequest.Builder getRequest = + RegionAPI.GetRequest.newBuilder().setRegionName(region).setKey(keyBuilder); ClientProtocol.Request.Builder request = - ClientProtocol.Request.newBuilder().setPutRequest(putRequestBuilder); - - ClientProtocol.Message.Builder message = - ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request); + ClientProtocol.Request.newBuilder().setGetRequest(getRequest); - return message.build(); + return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) + .build(); } - public static ClientProtocol.Message makePutMessageFor(String region, String key, String value) { + public static ClientProtocol.Message makePutMessageFor(String region, Object key, Object value) { Random random = new Random(); ClientProtocol.MessageHeader.Builder messageHeader = ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()); - BasicTypes.Key.Builder keyBuilder = - BasicTypes.Key.newBuilder().setKey(ByteString.copyFromUtf8(key)); - - BasicTypes.Value.Builder valueBuilder = - BasicTypes.Value.newBuilder().setValue(ByteString.copyFromUtf8(value)); + BasicTypes.EncodedValue.Builder keyBuilder = getEncodedValueBuilder(key); + BasicTypes.EncodedValue.Builder valueBuilder = getEncodedValueBuilder(value); RegionAPI.PutRequest.Builder putRequestBuilder = RegionAPI.PutRequest.newBuilder().setRegionName(region) @@ -82,28 +74,10 @@ public class MessageUtils { return message.build(); } - public static ClientProtocol.Message makeGetMessageFor(String region, String key) { - Random random = new Random(); - ClientProtocol.MessageHeader.Builder messageHeader = - ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()); - - BasicTypes.Key.Builder keyBuilder = - BasicTypes.Key.newBuilder().setKey(ByteString.copyFromUtf8(key)); - - RegionAPI.GetRequest.Builder getRequest = - RegionAPI.GetRequest.newBuilder().setRegionName(region).setKey(keyBuilder); - ClientProtocol.Request.Builder request = - ClientProtocol.Request.newBuilder().setGetRequest(getRequest); - - return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) - .build(); - } + private static BasicTypes.EncodedValue.Builder getEncodedValueBuilder(Object value) { + BasicTypes.EncodingType encodingType = EncodingTypeThingyKt.getEncodingTypeForObjectKT(value); - private static byte[] createByteArrayOfSize(int msgSize) { - byte[] array = new byte[msgSize]; - for (int i = 0; i < msgSize; i++) { - array[i] = 'a'; - } - return array; + return BasicTypes.EncodedValue.newBuilder().setEncodingType(encodingType) + .setValue(ByteString.copyFrom(serializerFromProtoEnum(encodingType).serialize(value))); } } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java index 1a9b62e..a9a7407 100644 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java @@ -15,7 +15,13 @@ package org.apache.geode.protocol.client; -import com.google.protobuf.ByteString; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Message.MessageTypeCase.RESPONSE; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.GETRESPONSE; +import static org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.PUTRESPONSE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.pholser.junit.quickcheck.runner.JUnitQuickcheck; import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.Region; @@ -24,100 +30,94 @@ import org.apache.geode.distributed.ConfigurationProperties; import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.test.junit.categories.IntegrationTest; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; import java.io.IOException; import java.util.Properties; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Message.MessageTypeCase.RESPONSE; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.GETRESPONSE; -import static org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.PUTRESPONSE; -import static org.junit.Assert.*; - @Category(IntegrationTest.class) +@RunWith(JUnitQuickcheck.class) public class ProtobufProtocolIntegrationTest { + private final static String testRegion = "testRegion"; + private final static String testKey = "testKey"; + private final static String testValue = "testValue"; + private Cache cache; + private NewClientProtocolTestClient testClient; + private Region regionUnderTest; + + @Before + public void setup() throws IOException { + cache = createCacheOnPort(40404); + testClient = new NewClientProtocolTestClient("localhost", 40404); + regionUnderTest = cache.createRegionFactory().create(testRegion); + } + + @After + public void shutdown() throws IOException { + if (testClient != null) { + testClient.close(); + } + if (cache != null) { + cache.close(); + } + } + @Test public void testRoundTripPutRequest() throws IOException { - try (Cache cache = createCacheOnPort(40404); - NewClientProtocolTestClient client = new NewClientProtocolTestClient("localhost", 40404)) { - final String testRegion = "testRegion"; - final String testKey = "testKey"; - final String testValue = "testValue"; - Region region = cache.createRegionFactory().create("testRegion"); - ClientProtocol.Message message = - MessageUtils.makePutMessageFor(testRegion, testKey, testValue); - ClientProtocol.Message response = client.blockingSendMessage(message); - client.printResponse(response); + MessageUtils.INSTANCE.makePutMessageFor(testRegion, testKey, testValue); + ClientProtocol.Message response = testClient.blockingSendMessage(message); + testClient.printResponse(response); assertEquals(RESPONSE, response.getMessageTypeCase()); assertEquals(PUTRESPONSE, response.getResponse().getResponseAPICase()); assertTrue(response.getResponse().getPutResponse().getSuccess()); - assertEquals(1, region.size()); - assertTrue(region.containsKey(testKey)); - assertEquals(testValue, region.get(testKey)); - } + assertEquals(1, regionUnderTest.size()); + assertTrue(regionUnderTest.containsKey(testKey)); + assertEquals(testValue, regionUnderTest.get(testKey)); } @Test public void testRoundTripEmptyGetRequest() throws IOException { - try (Cache cache = createCacheOnPort(40404); - NewClientProtocolTestClient client = new NewClientProtocolTestClient("localhost", 40404)) { - final String testRegion = "testRegion"; - final String testKey = "testKey"; - Region region = cache.createRegionFactory().create("testRegion"); - - ClientProtocol.Message message = MessageUtils.makeGetMessageFor(testRegion, testKey); - ClientProtocol.Message response = client.blockingSendMessage(message); + ClientProtocol.Message message = MessageUtils.INSTANCE.makeGetMessageFor(testRegion, testKey); + ClientProtocol.Message response = testClient.blockingSendMessage(message); assertEquals(RESPONSE, response.getMessageTypeCase()); assertEquals(GETRESPONSE, response.getResponse().getResponseAPICase()); - BasicTypes.Value value = response.getResponse().getGetResponse().getResult(); + BasicTypes.EncodedValue value = response.getResponse().getGetResponse().getResult(); assertTrue(value.getValue().isEmpty()); - } } @Test public void testRoundTripNonEmptyGetRequest() throws IOException { - try (Cache cache = createCacheOnPort(40404); - NewClientProtocolTestClient client = new NewClientProtocolTestClient("localhost", 40404)) { - final String testRegion = "testRegion"; - final String testKey = "testKey"; - final String testValue = "testValue"; - Region region = cache.createRegionFactory().create("testRegion"); - - ClientProtocol.Message putMessage = - MessageUtils.makePutMessageFor(testRegion, testKey, testValue); - ClientProtocol.Message putResponse = client.blockingSendMessage(putMessage); - client.printResponse(putResponse); + MessageUtils.INSTANCE.makePutMessageFor(testRegion, testKey, testValue); + ClientProtocol.Message putResponse = testClient.blockingSendMessage(putMessage); + testClient.printResponse(putResponse); - ClientProtocol.Message getMessage = MessageUtils.makeGetMessageFor(testRegion, testKey); - ClientProtocol.Message getResponse = client.blockingSendMessage(getMessage); + ClientProtocol.Message getMessage = MessageUtils.INSTANCE + .makeGetMessageFor(testRegion, testKey); + ClientProtocol.Message getResponse = testClient.blockingSendMessage(getMessage); assertEquals(RESPONSE, getResponse.getMessageTypeCase()); assertEquals(GETRESPONSE, getResponse.getResponse().getResponseAPICase()); - BasicTypes.Value value = getResponse.getResponse().getGetResponse().getResult(); + BasicTypes.EncodedValue value = getResponse.getResponse().getGetResponse().getResult(); assertEquals(value.getValue().toStringUtf8(), testValue); } - } - @Test - public void startCache() throws IOException { - try (Cache cache = createCacheOnPort(40404)) { - while (true) { - try { - Thread.sleep(100000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + @Test + public void objectSerializationIntegrationTest() { + Object[] inputs = new Object[]{ + "Foobar", 1000L, 22, (short) 231, (byte) -107, new byte[]{1,2,3,54,99} + }; } - } private Cache createCacheOnPort(int port) throws IOException { Properties props = new Properties(); http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java index 05d2216..3c8eac0 100644 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java @@ -16,21 +16,20 @@ package org.apache.geode.protocol.client; -import com.google.protobuf.InvalidProtocolBufferException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.serialization.Deserializer; -import org.apache.geode.serialization.SerializationType; import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; import org.mockito.Mockito; -import static org.apache.geode.protocol.client.MessageUtils.makePutMessage; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -39,12 +38,19 @@ import java.io.OutputStream; @Category(UnitTest.class) public class ProtobufSerializationDeserializationTest { - @Test - public void protobufSerializationSmokeTest() throws InvalidProtocolBufferException { - ClientProtocol.Message message = makePutMessage(); - byte[] bytes = message.toByteArray(); - ClientProtocol.Message message1 = ClientProtocol.Message.parseFrom(bytes); - assertEquals(message, message1); + + private Cache mockCache; + private Region mockRegion; + private final String testRegion = "testRegion"; + private final String testKey = "testKey"; + private final String testValue = "testValue"; + + @Before + public void start() { + mockCache = Mockito.mock(Cache.class); + mockRegion = Mockito.mock(Region.class); + when(mockCache.getRegion(testRegion)).thenReturn(mockRegion); + } /** @@ -53,40 +59,27 @@ public class ProtobufSerializationDeserializationTest { */ @Test public void testNewClientProtocolPutsOnPutMessage() throws IOException { - final String testRegion = "testRegion"; - final String testKey = "testKey"; - final String testValue = "testValue"; - ClientProtocol.Message message = MessageUtils.makePutMessageFor(testRegion, testKey, testValue); - - Deserializer deserializer = SerializationType.BYTE_BLOB.deserializer; - Cache mockCache = Mockito.mock(Cache.class); - Region mockRegion = Mockito.mock(Region.class); - when(mockCache.getRegion("testRegion")).thenReturn(mockRegion); + ClientProtocol.Message message = MessageUtils.INSTANCE + .makePutMessageFor(testRegion, testKey, testValue); + OutputStream mockOutputStream = Mockito.mock(OutputStream.class); ProtobufProtocolMessageHandler newClientProtocol = new ProtobufProtocolMessageHandler(); - newClientProtocol.receiveMessage(MessageUtils.loadMessageIntoInputStream(message), - mockOutputStream, deserializer, SerializationType.STRING.serializer, mockCache); + newClientProtocol.receiveMessage(MessageUtils.INSTANCE.loadMessageIntoInputStream(message), + mockOutputStream, mockCache); verify(mockRegion).put(testKey.getBytes(), testValue.getBytes()); } @Test public void testServerRespondsToPutMessage() throws IOException { - final String testRegion = "testRegion"; - final String testKey = "testKey"; - final String testValue = "testValue"; - ClientProtocol.Message message = MessageUtils.makePutMessageFor(testRegion, testKey, testValue); - - Deserializer deserializer = SerializationType.BYTE_BLOB.deserializer; - Cache mockCache = Mockito.mock(Cache.class); - Region mockRegion = Mockito.mock(Region.class); - when(mockCache.getRegion("testRegion")).thenReturn(mockRegion); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(128); + ClientProtocol.Message message = MessageUtils.INSTANCE + .makePutMessageFor(testRegion, testKey, testValue); ProtobufProtocolMessageHandler newClientProtocol = new ProtobufProtocolMessageHandler(); - newClientProtocol.receiveMessage(MessageUtils.loadMessageIntoInputStream(message), outputStream, - deserializer, SerializationType.STRING.serializer, mockCache); + newClientProtocol.receiveMessage(MessageUtils.INSTANCE.loadMessageIntoInputStream(message), outputStream, + mockCache); ClientProtocol.Message responseMessage = ClientProtocol.Message .parseDelimitedFrom(new ByteArrayInputStream(outputStream.toByteArray())); http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ClientProtocolMessageHandler.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ClientProtocolMessageHandler.java b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ClientProtocolMessageHandler.java index 3e37c4a..ca5abb2 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ClientProtocolMessageHandler.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ClientProtocolMessageHandler.java @@ -24,6 +24,6 @@ import java.io.InputStream; import java.io.OutputStream; public interface ClientProtocolMessageHandler { - void receiveMessage(InputStream inputStream, OutputStream outputStream, Deserializer deserializer, - Serializer serializer, Cache cache) throws IOException; + void receiveMessage(InputStream inputStream, OutputStream outputStream, Cache cache) + throws IOException; } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnection.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnection.java b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnection.java index a61c86c..3be1f97 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnection.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnection.java @@ -956,9 +956,7 @@ public class ServerConnection implements Runnable { InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); // TODO serialization types? - newClientProtocol.receiveMessage(inputStream, outputStream, - SerializationType.STRING.deserializer, SerializationType.STRING.serializer, - this.getCache()); + newClientProtocol.receiveMessage(inputStream, outputStream, this.getCache()); } catch (IOException e) { // TODO? } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/main/java/org/apache/geode/serialization/Deserializer.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/serialization/Deserializer.java b/geode-core/src/main/java/org/apache/geode/serialization/Deserializer.java index e4cd20c..7cf3bb7 100644 --- a/geode-core/src/main/java/org/apache/geode/serialization/Deserializer.java +++ b/geode-core/src/main/java/org/apache/geode/serialization/Deserializer.java @@ -16,5 +16,5 @@ package org.apache.geode.serialization; public interface Deserializer { - public abstract T deserialize(byte[] bytes); + T deserialize(byte[] bytes); } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/main/java/org/apache/geode/serialization/SerializationType.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/serialization/SerializationType.java b/geode-core/src/main/java/org/apache/geode/serialization/SerializationType.java index 2ea4066..2bb622f 100644 --- a/geode-core/src/main/java/org/apache/geode/serialization/SerializationType.java +++ b/geode-core/src/main/java/org/apache/geode/serialization/SerializationType.java @@ -18,11 +18,19 @@ package org.apache.geode.serialization; import org.apache.geode.pdx.JSONFormatter; import org.apache.geode.pdx.PdxInstance; -public enum SerializationType { - STRING(String.class, String::getBytes, String::new), +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +public enum SerializationType implements Serializer, Deserializer { + STRING(String.class, SerializationType::serializeString, SerializationType::deserializeString), BYTE_BLOB(byte[].class, (x) -> x, (x) -> x), + INT(int.class, SerializationType::serializeInt, SerializationType::deserializeInt), + BYTE(byte.class, SerializationType::serializeByte, SerializationType::deserializeByte), + SHORT(short.class, SerializationType::serializeShort, SerializationType::deserializeShort), + LONG(long.class, SerializationType::serializeLong, SerializationType::deserializeLong), JSON(PdxInstance.class, JSONFormatter::toJSONByteArray, JSONFormatter::fromJSON); + private static final Charset UTF8 = Charset.forName("UTF-8"); public final Class klass; public final Serializer serializer; public final Deserializer deserializer; @@ -32,5 +40,55 @@ public enum SerializationType { this.serializer = serializer; this.deserializer = deserializer; } + + @Override + public byte[] serialize(Object item) { + return this.serializer.serialize(item); + } + + @Override + public Object deserialize(byte[] bytes) { + return this.deserializer.deserialize(bytes); + } + + private static byte[] serializeString(String value) { + return value.getBytes(UTF8); + } + + private static String deserializeString(byte[] array) { + return new String(array, UTF8); + } + + private static byte[] serializeInt(int i) { + return ByteBuffer.allocate(Integer.BYTES).putInt(i).array(); + } + + private static int deserializeInt(byte[] bytes) { + return ByteBuffer.wrap(bytes).getInt(); + } + + private static byte[] serializeShort(short i) { + return ByteBuffer.allocate(Short.BYTES).putShort(i).array(); + } + + private static short deserializeShort(byte[] bytes) { + return ByteBuffer.wrap(bytes).getShort(); + } + + private static byte[] serializeLong(long i) { + return ByteBuffer.allocate(Long.BYTES).putLong(i).array(); + } + + private static long deserializeLong(byte[] bytes) { + return ByteBuffer.wrap(bytes).getLong(); + } + + private static byte[] serializeByte(byte i) { + return ByteBuffer.allocate(Byte.BYTES).put(i).array(); + } + + private static byte deserializeByte(byte[] bytes) { + return ByteBuffer.wrap(bytes).get(); + } } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/main/java/org/apache/geode/serialization/Serializer.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/serialization/Serializer.java b/geode-core/src/main/java/org/apache/geode/serialization/Serializer.java index 6be877f..0cb8d3d 100644 --- a/geode-core/src/main/java/org/apache/geode/serialization/Serializer.java +++ b/geode-core/src/main/java/org/apache/geode/serialization/Serializer.java @@ -16,5 +16,5 @@ package org.apache.geode.serialization; public interface Serializer { - public abstract byte[] serialize(T item); + byte[] serialize(T item); } http://git-wip-us.apache.org/repos/asf/geode/blob/c1e22f37/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java b/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java new file mode 100644 index 0000000..79b0651 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java @@ -0,0 +1,113 @@ +package org.apache.geode.serialization; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import com.pholser.junit.quickcheck.Property; +import com.pholser.junit.quickcheck.runner.JUnitQuickcheck; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.nio.charset.Charset; + +@RunWith(JUnitQuickcheck.class) +public class SerializationTypeTest { + @Property + public void testStringSerialization(String testString) throws Exception { + byte[] expectedBytes = testString.getBytes(Charset.forName("UTF-8")); + byte[] serializedBytes = SerializationType.STRING.serialize(testString); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testString, SerializationType.STRING.deserialize(expectedBytes)); + } + + + @Test + public void testLongSerialization() throws Exception { + long testValue = 1311768465047740160L; // otherwise known as 0x123456780abadf00 bytes; + byte[] expectedBytes = + new byte[]{0x12, 0x34, 0x56, 0x78, 0x0a, (byte) 0xba, (byte) 0xdf, 0x00}; + byte[] serializedBytes = SerializationType.LONG.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.LONG.deserialize(expectedBytes)); + + testValue = -1311234265047740160L; // otherwise known as 0xedcd8f6216825100 bytes; + expectedBytes = new byte[]{(byte) 0xed, (byte) 0xcd, (byte) 0x8f, 0x62, 0x16, (byte) 0x82, + (byte) 0x51, 0x00}; + serializedBytes = SerializationType.LONG.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.LONG.deserialize(expectedBytes)); + } + + @Test + public void testIntSerialization() throws Exception { + int testValue = 313216366; // otherwise known as 0x12ab4d6e bytes; + byte[] expectedBytes = new byte[]{0x12, (byte) 0xab, 0x4d, 0x6e}; + byte[] serializedBytes = SerializationType.INT.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.INT.deserialize(expectedBytes)); + + testValue = -313216366; // otherwise known as 0xed54b292 bytes; + expectedBytes = new byte[]{(byte) 0xed, 0x54, (byte) 0xb2, (byte) 0x92}; + serializedBytes = SerializationType.INT.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.INT.deserialize(expectedBytes)); + } + + @Test + public void testShortSerialization() throws Exception { + short testValue = 31321; // otherwise known as 0x7a59 bytes; + byte[] expectedBytes = new byte[]{0x7a, 0x59}; + byte[] serializedBytes = SerializationType.SHORT.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.SHORT.deserialize(expectedBytes)); + + testValue = -22357; // otherwise known as 0xa8ab bytes; + expectedBytes = new byte[]{(byte) 0xa8, (byte) 0xab}; + serializedBytes = SerializationType.SHORT.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.SHORT.deserialize(expectedBytes)); + } + + @Test + public void testByteSerialization() throws Exception { + byte testValue = 116; + byte[] expectedBytes = new byte[]{116}; + byte[] serializedBytes = SerializationType.BYTE.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.BYTE.deserialize(expectedBytes)); + + testValue = -87; + expectedBytes = new byte[]{-87}; + serializedBytes = SerializationType.BYTE.serialize(testValue); + assertArrayEquals(expectedBytes, serializedBytes); + assertEquals(testValue, SerializationType.BYTE.deserialize(expectedBytes)); + + } + + @Property + public void testBinarySerialization(byte[] bytes) throws Exception { + assertArrayEquals(bytes, SerializationType.BYTE_BLOB.serialize(bytes)); + assertArrayEquals(bytes, (byte[]) SerializationType.BYTE_BLOB.deserialize(bytes)); + } + + @Ignore("Not currently testable without a Cache, because JSON gets serialized to a PDXInstance, " + + "which requires a Cache.") + @Test + public void testJSONSerialization() { + String[] testStrings = { + "\"testString\"", + "{}", + "{\"foo\":\"bar\",\"list :\":[1,2,\"boo\"],hash:{1:2,3:4}}" + }; + + for (String string : testStrings) { + byte[] serialized = SerializationType.JSON.serialize(string); + String deserialized = + (String) SerializationType.JSON.deserializer.deserialize(serialized); + assertEquals(string, deserialized); + fail("This test is not yet complete"); + } + } +}