GEODE-2580 : Post Kotlin changes
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/6a5e4be3 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/6a5e4be3 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/6a5e4be3 Branch: refs/heads/feature/GEODE-2580 Commit: 6a5e4be30b79c805101017c627719e6949c2dfb7 Parents: c1e22f3 Author: Udo Kohlmeyer <ukohlme...@pivotal.io> Authored: Tue May 23 16:41:19 2017 -0700 Committer: Udo Kohlmeyer <ukohlme...@pivotal.io> Committed: Tue May 23 16:41:19 2017 -0700 ---------------------------------------------------------------------- .../geode/protocol/client/EncodingTypeThingy.kt | 51 +++--- .../client/ProtobufProtocolMessageHandler.java | 147 ----------------- .../client/ProtobufProtocolMessageHandler.kt | 128 +++++++++++++++ .../geode/protocol/client/MessageUtils.java | 83 ---------- .../geode/protocol/client/MessageUtils.kt | 69 ++++++++ .../client/ProtobufProtocolIntegrationTest.java | 134 ---------------- .../client/ProtobufProtocolIntegrationTest.kt | 159 +++++++++++++++++++ ...rotobufSerializationDeserializationTest.java | 94 ----------- .../ProtobufSerializationDeserializationTest.kt | 94 +++++++++++ .../serialization/SerializationTypeTest.java | 26 ++- 10 files changed, 492 insertions(+), 493 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 index 8d65827..cfb1332 100644 --- 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 @@ -5,29 +5,40 @@ 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 +object EncodingTypeThingy { + @JvmStatic + fun getEncodingTypeForObjectKT(obj: Any?): BasicTypes.EncodingType { + return when (obj) { + is Byte -> BasicTypes.EncodingType.BYTE + is Short -> BasicTypes.EncodingType.SHORT + is Long -> BasicTypes.EncodingType.LONG + is String -> BasicTypes.EncodingType.STRING + is Int -> BasicTypes.EncodingType.INT + is PdxInstance -> { + if (obj.className == JSONFormatter.JSON_CLASSNAME) { + BasicTypes.EncodingType.JSON + } else { + BasicTypes.EncodingType.UNRECOGNIZED + } + } + is ByteArray -> BasicTypes.EncodingType.BINARY + 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() + @JvmStatic + 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("Unknown EncodingType to SerializationType conversion for $encodingType") + } } } http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 deleted file mode 100644 index ed9a7e3..0000000 --- a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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.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; -import org.apache.geode.cache.TimeoutException; -import org.apache.geode.internal.cache.tier.sockets.ClientProtocolMessageHandler; -import org.apache.geode.internal.logging.LogService; -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.SerializationType; -import org.apache.logging.log4j.Logger; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class ProtobufProtocolMessageHandler implements ClientProtocolMessageHandler { - - private static final Logger logger = LogService.getLogger(); - - private String ErrorMessageFromMessage(Message message) { - return "Error parsing message, message string: " + message.toString(); - } - - @Override - 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) { - return; - } - - if (message.getMessageTypeCase() != Message.MessageTypeCase.REQUEST) { - // TODO - logger.error(() -> "Got message of type response: " + ErrorMessageFromMessage(message)); - } - - Message responseMessage = null; - - Request request = message.getRequest(); - Request.RequestAPICase requestAPICase = request.getRequestAPICase(); - if (requestAPICase == Request.RequestAPICase.GETREQUEST) { - responseMessage = doGetRequest(request.getGetRequest(), cache); - } else if (requestAPICase == Request.RequestAPICase.PUTREQUEST) { - responseMessage = doPutRequest(request.getPutRequest(), cache); - } else { - // TODO - } - if (responseMessage != null) { - responseMessage.writeDelimitedTo(outputStream); - } - } - - private Message doPutRequest(PutRequest request, Cache cache) { - final String regionName = request.getRegionName(); - final BasicTypes.Entry entry = request.getEntry(); - assert (entry != null); - - final Region<Object, Object> region = cache.getRegion(regionName); - try { - 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); - return putResponseWithStatus(false); - } - } - - 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, Cache cache) { - Region<Object, Object> region = cache.getRegion(request.getRegionName()); - Object returnValue = region.get(deserializeEncodedValue(request.getKey())); - - if (returnValue == null) { - - return makeGetResponseMessageWithValue(new byte[0]); - } else { - // TODO types in the region? - return makeGetResponseMessageWithValue(returnValue); - } - } - - 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.EncodedValue.newBuilder() - .setEncodingType(BasicTypes.EncodingType.STRING) - .setValue(ByteString.copyFrom(serializedObject))))) - .build(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.kt ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.kt b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.kt new file mode 100644 index 0000000..ec66d44 --- /dev/null +++ b/geode-client-protobuf/src/main/java/org/apache/geode/protocol/client/ProtobufProtocolMessageHandler.kt @@ -0,0 +1,128 @@ +/* + * 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.geode.protocol.client + +import com.google.protobuf.ByteString +import org.apache.geode.cache.Cache +import org.apache.geode.cache.CacheWriterException +import org.apache.geode.cache.TimeoutException +import org.apache.geode.internal.cache.tier.sockets.ClientProtocolMessageHandler +import org.apache.geode.internal.logging.LogService +import org.apache.geode.protocol.protobuf.BasicTypes +import org.apache.geode.protocol.protobuf.ClientProtocol.* +import org.apache.geode.protocol.protobuf.RegionAPI +import org.apache.geode.protocol.protobuf.RegionAPI.* +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream + +class ProtobufProtocolMessageHandler : ClientProtocolMessageHandler { + + private fun ErrorMessageFromMessage(message: Message): String { + return "Error parsing message, message string: " + message.toString() + } + + @Throws(IOException::class) + override fun receiveMessage(inputStream: InputStream, outputStream: OutputStream, cache: Cache) { + val message = Message.parseDelimitedFrom(inputStream) ?: return + // can be null at EOF, see Parser.parseDelimitedFrom(java.io.InputStream) + + if (message.messageTypeCase != Message.MessageTypeCase.REQUEST) { + TODO("Got message of type response: ${ErrorMessageFromMessage(message)}") + } + + val request = message.request + val requestAPICase = request.requestAPICase + when (requestAPICase) { + Request.RequestAPICase.GETREQUEST -> doGetRequest(request.getRequest, cache).writeDelimitedTo(outputStream) + Request.RequestAPICase.PUTREQUEST -> doPutRequest(request.putRequest, cache).writeDelimitedTo(outputStream) + else -> TODO("The message type: $requestAPICase is not supported") + + } + } + + private fun doPutRequest(request: PutRequest, cache: Cache): Message { + val regionName = request.regionName + val entry = request.entry!! + + val region = cache.getRegion<Any, Any>(regionName) + try { + region.put(deserializeEncodedValue(entry.key), deserializeEncodedValue(entry.value)) + return putResponseWithStatus(true) + } catch (ex: TimeoutException) { + logger.warn("Caught normal-ish exception doing region put", ex) + return putResponseWithStatus(false) + } catch (ex: CacheWriterException) { + logger.warn("Caught normal-ish exception doing region put", ex) + return putResponseWithStatus(false) + } + + } + + private fun deserializeEncodedValue(encodedValue: BasicTypes.EncodedValue): Any { + val serializer = EncodingTypeThingy.serializerFromProtoEnum(encodedValue.encodingType) + return serializer.deserialize(encodedValue.value.toByteArray()) + } + + private fun serializeEncodedValue(encodingType: BasicTypes.EncodingType, + objectToSerialize: Any?): ByteArray? { + if (objectToSerialize == null) { + return null // BLECH!!! :( + } + val serializer = EncodingTypeThingy.serializerFromProtoEnum(encodingType) + return serializer.serialize(objectToSerialize) + } + + private fun putResponseWithStatus(ok: Boolean): Message { + return Message.newBuilder() + .setResponse(Response.newBuilder().setPutResponse(PutResponse.newBuilder().setSuccess(ok))) + .build() + } + + private fun doGetRequest(request: GetRequest, cache: Cache): Message { + val region = cache.getRegion<Any, Any>(request.regionName) + val returnValue = region[deserializeEncodedValue(request.key)] + + if (returnValue == null) { + + return makeGetResponseMessageWithValue(ByteArray(0)) + } else { + // TODO types in the region? + return makeGetResponseMessageWithValue(returnValue) + } + } + + private fun getEncodingTypeForObject(`object`: Any): BasicTypes.EncodingType { + return EncodingTypeThingy.getEncodingTypeForObjectKT(`object`) + } + + private fun makeGetResponseMessageWithValue(objectToReturn: Any): Message { + val encodingType = getEncodingTypeForObject(objectToReturn) + val serializedObject = serializeEncodedValue(encodingType, objectToReturn) + + val encodedValueBuilder = BasicTypes.EncodedValue.newBuilder() + .setEncodingType(encodingType) + .setValue(ByteString.copyFrom(serializedObject)) + val getResponseBuilder = RegionAPI.GetResponse.newBuilder().setResult(encodedValueBuilder) + val responseBuilder = Response.newBuilder().setGetResponse(getResponseBuilder) + return Message.newBuilder().setResponse(responseBuilder).build() + } + + companion object { + + private val logger = LogService.getLogger() + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 deleted file mode 100644 index 250c2f8..0000000 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.geode.protocol.client; - -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; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Random; - -public class MessageUtils { - public static ByteArrayInputStream loadMessageIntoInputStream(ClientProtocol.Message message) - throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - message.writeDelimitedTo(byteArrayOutputStream); - byte[] messageByteArray = byteArrayOutputStream.toByteArray(); - return new ByteArrayInputStream(messageByteArray); - } - - public static ClientProtocol.Message makeGetMessageFor(String region, String key) { - Random random = new Random(); - ClientProtocol.MessageHeader.Builder messageHeader = - ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()); - - BasicTypes.EncodedValue.Builder keyBuilder = getEncodedValueBuilder(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(); - } - - 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.EncodedValue.Builder keyBuilder = getEncodedValueBuilder(key); - BasicTypes.EncodedValue.Builder valueBuilder = getEncodedValueBuilder(value); - - RegionAPI.PutRequest.Builder putRequestBuilder = - RegionAPI.PutRequest.newBuilder().setRegionName(region) - .setEntry(BasicTypes.Entry.newBuilder().setKey(keyBuilder).setValue(valueBuilder)); - - ClientProtocol.Request.Builder request = - ClientProtocol.Request.newBuilder().setPutRequest(putRequestBuilder); - ClientProtocol.Message.Builder message = - ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request); - - return message.build(); - } - - private static BasicTypes.EncodedValue.Builder getEncodedValueBuilder(Object value) { - BasicTypes.EncodingType encodingType = EncodingTypeThingyKt.getEncodingTypeForObjectKT(value); - - return BasicTypes.EncodedValue.newBuilder().setEncodingType(encodingType) - .setValue(ByteString.copyFrom(serializerFromProtoEnum(encodingType).serialize(value))); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.kt ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.kt b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.kt new file mode 100644 index 0000000..285c9b4 --- /dev/null +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/MessageUtils.kt @@ -0,0 +1,69 @@ +/* + * 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.geode.protocol.client + +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 +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.IOException +import java.util.* + +object MessageUtils { + @Throws(IOException::class) + fun loadMessageIntoInputStream(message: ClientProtocol.Message): ByteArrayInputStream { + val byteArrayOutputStream = ByteArrayOutputStream() + message.writeDelimitedTo(byteArrayOutputStream) + val messageByteArray = byteArrayOutputStream.toByteArray() + return ByteArrayInputStream(messageByteArray) + } + + fun makeGetMessageFor(region: String, key: Any, keyEncoding: BasicTypes.EncodingType = EncodingTypeThingy.getEncodingTypeForObjectKT(key)): ClientProtocol.Message { + val random = Random() + val messageHeader = ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()) + + val keyBuilder = getEncodedValueBuilder(key, keyEncoding) + + val getRequest = RegionAPI.GetRequest.newBuilder().setRegionName(region).setKey(keyBuilder) + val request = ClientProtocol.Request.newBuilder().setGetRequest(getRequest) + + return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) + .build() + } + + fun makePutMessageFor(region: String, key: Any, value: Any, keyEncoding: BasicTypes.EncodingType = EncodingTypeThingy.getEncodingTypeForObjectKT(key), valueEncoding: BasicTypes.EncodingType = EncodingTypeThingy.getEncodingTypeForObjectKT(value)): ClientProtocol.Message { + val random = Random() + val messageHeader = ClientProtocol.MessageHeader.newBuilder().setCorrelationId(random.nextInt()) + + val keyBuilder = getEncodedValueBuilder(key, keyEncoding) + val valueBuilder = getEncodedValueBuilder(value, valueEncoding) + + val putRequestBuilder = RegionAPI.PutRequest.newBuilder().setRegionName(region) + .setEntry(BasicTypes.Entry.newBuilder().setKey(keyBuilder).setValue(valueBuilder)) + + val request = ClientProtocol.Request.newBuilder().setPutRequest(putRequestBuilder) + val message = ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) + + return message.build() + } + + private fun getEncodedValueBuilder(value: Any, encodingType: BasicTypes.EncodingType): BasicTypes.EncodedValue.Builder { + return BasicTypes.EncodedValue.newBuilder().setEncodingType(encodingType) + .setValue(ByteString.copyFrom(EncodingTypeThingy.serializerFromProtoEnum(encodingType).serialize(value))) + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 deleted file mode 100644 index a9a7407..0000000 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.geode.protocol.client; - -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; -import org.apache.geode.cache.server.CacheServer; -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; - -@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<Object, Object> 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 { - ClientProtocol.Message message = - 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, regionUnderTest.size()); - assertTrue(regionUnderTest.containsKey(testKey)); - assertEquals(testValue, regionUnderTest.get(testKey)); - } - - @Test - public void testRoundTripEmptyGetRequest() throws IOException { - ClientProtocol.Message message = MessageUtils.INSTANCE.makeGetMessageFor(testRegion, testKey); - ClientProtocol.Message response = testClient.blockingSendMessage(message); - - assertEquals(RESPONSE, response.getMessageTypeCase()); - assertEquals(GETRESPONSE, response.getResponse().getResponseAPICase()); - BasicTypes.EncodedValue value = response.getResponse().getGetResponse().getResult(); - - assertTrue(value.getValue().isEmpty()); - } - - @Test - public void testRoundTripNonEmptyGetRequest() throws IOException { - ClientProtocol.Message putMessage = - MessageUtils.INSTANCE.makePutMessageFor(testRegion, testKey, testValue); - ClientProtocol.Message putResponse = testClient.blockingSendMessage(putMessage); - testClient.printResponse(putResponse); - - ClientProtocol.Message getMessage = MessageUtils.INSTANCE - .makeGetMessageFor(testRegion, testKey); - ClientProtocol.Message getResponse = testClient.blockingSendMessage(getMessage); - - assertEquals(RESPONSE, getResponse.getMessageTypeCase()); - assertEquals(GETRESPONSE, getResponse.getResponse().getResponseAPICase()); - BasicTypes.EncodedValue value = getResponse.getResponse().getGetResponse().getResult(); - - assertEquals(value.getValue().toStringUtf8(), testValue); - } - - @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(); - props.setProperty(ConfigurationProperties.TCP_PORT, Integer.toString(port)); - props.setProperty(ConfigurationProperties.BIND_ADDRESS, "localhost"); - CacheFactory cf = new CacheFactory(props); - Cache cache = cf.create(); - CacheServer cacheServer = cache.addCacheServer(); - cacheServer.setBindAddress("localhost"); - cacheServer.start(); - return cache; - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.kt ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.kt b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.kt new file mode 100644 index 0000000..2130c88 --- /dev/null +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufProtocolIntegrationTest.kt @@ -0,0 +1,159 @@ +/* + * 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.geode.protocol.client + +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 +import org.apache.geode.distributed.ConfigurationProperties +import org.apache.geode.protocol.protobuf.BasicTypes +import org.apache.geode.protocol.protobuf.ClientProtocol.Message.MessageTypeCase.RESPONSE +import org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.GETRESPONSE +import org.apache.geode.protocol.protobuf.ClientProtocol.Response.ResponseAPICase.PUTRESPONSE +import org.apache.geode.test.junit.categories.IntegrationTest +import org.junit.After +import org.junit.Assert.* +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.io.Serializable +import java.util.* + +@Category(IntegrationTest::class) +@RunWith(JUnitQuickcheck::class) +class ProtobufProtocolIntegrationTest { + private lateinit var cache: Cache + private lateinit var testClient: NewClientProtocolTestClient + private lateinit var regionUnderTest: Region<Any, Any> + private val testRegion = "testRegion" + private val testKey = "testKey" + private val testValue = "testValue" + + @Before + @Throws(IOException::class) + fun setup() { + cache = createCacheOnPort(40404) + testClient = NewClientProtocolTestClient("localhost", 40404) + regionUnderTest = cache.createRegionFactory<Any, Any>().create(testRegion) + } + + @After + @Throws(IOException::class) + fun shutdown() { + testClient.close() + cache.close() + } + + @Test + @Throws(IOException::class) + fun testRoundTripPutRequest() { + val message = MessageUtils.makePutMessageFor(region = testRegion, key = testKey, value = testValue) + val response = testClient.blockingSendMessage(message) + testClient.printResponse(response) + + assertEquals(RESPONSE, response.messageTypeCase) + assertEquals(PUTRESPONSE, response.response.responseAPICase) + assertTrue(response.response.putResponse.success) + + assertEquals(1, regionUnderTest.size.toLong()) + assertTrue(regionUnderTest.containsKey(testKey)) + assertEquals(testValue, regionUnderTest[testKey]) + } + + @Test + @Throws(IOException::class) + fun testRoundTripEmptyGetRequest() { + val message = MessageUtils.makeGetMessageFor(region = testRegion, key = testKey) + val response = testClient.blockingSendMessage(message) + + assertEquals(RESPONSE, response.messageTypeCase) + assertEquals(GETRESPONSE, response.response.responseAPICase) + val value = response.response.getResponse.result + + assertTrue(value.value.isEmpty) + } + + @Test + @Throws(IOException::class) + fun testRoundTripNonEmptyGetRequest() { + val putMessage = MessageUtils.makePutMessageFor(testRegion, testKey, testValue) + val putResponse = testClient.blockingSendMessage(putMessage) + testClient.printResponse(putResponse) + + val getMessage = MessageUtils + .makeGetMessageFor(testRegion, testKey) + val getResponse = testClient.blockingSendMessage(getMessage) + + assertEquals(RESPONSE, getResponse.messageTypeCase) + assertEquals(GETRESPONSE, getResponse.response.responseAPICase) + val value = getResponse.response.getResponse.result + + assertEquals(value.value.toStringUtf8(), testValue) + } + + @Test + fun objectSerializationIntegrationTest() { + val inputs = listOf("Foobar", 1000.toLong(), 22, 231.toShort(), (-107).toByte(), byteArrayOf(1, 2, 3, 54, 99)) + for (key in inputs) { + for (value in inputs) { + if (key !is ByteArray) { + testMessagePutAndGet(key, value, EncodingTypeThingy.getEncodingTypeForObjectKT(value)) + } + } + } +// val jsonString = "{ \"_id\": \"5924ba3f3918de8404fc1321\", \"index\": 0, \"guid\": \"bd27d3fa-8870-4f0d-ab4d-73adf7cbe58b\", \"isActive\": false, \"balance\": \"$1,934.31\", \"picture\": \"http://placehold.it/32x32\", \"age\": 39, \"eyeColor\": \"blue\", \"name\": \"Holt Dickson\", \"gender\": \"male\", \"company\": \"INQUALA\", \"email\": \"holtdick...@inquala.com\", \"phone\": \"+1 (886) 450-2949\", \"address\": \"933 Diamond Street, Hinsdale, Palau, 2038\", \"about\": \"Cupidatat excepteur labore cillum ea reprehenderit aliquip magna duis aliquip Lorem labore. Aliquip elit ullamco aliqua fugiat aute id irure enim Lorem eu qui nisi aliquip. Et do sit cupidatat sit ut consectetur ullamco aute do nostrud in. Ea voluptate in reprehenderit sit commodo et aliquip officia id eiusmod. Quis voluptate commodo ad esse do cillum ut occaecat non.\r\n\", \"registered\": \"2017-02-01T12:28:49 +08:00\", \"latitude\": -69.313434, \"longitude\": 134.707471, \"tags\": [ \"officia\", \"qui \", \"ullamco\", \"nostrud\", \"ipsum\", \"dolor\", \"officia\" ], \"friends\": [ { \"id\": 0, \"name\": \"Vivian Beach\" }, { \"id\": 1, \"name\": \"Crystal Mills\" }, { \"id\": 2, \"name\": \"Mosley Frank\" } ], \"greeting\": \"Hello, Holt Dickson! You have 2 unread messages.\", \"favoriteFruit\": \"apple\" }" +// +// testMessagePutAndGet(testKey,jsonString,BasicTypes.EncodingType.STRING) +// val putMessage = MessageUtils.makePutMessageFor(region = testRegion, key = testKey, value = jsonString, valueEncoding = BasicTypes.EncodingType.STRING) + } + + private fun testMessagePutAndGet(key: Serializable, value: Serializable, valueEncoding: BasicTypes.EncodingType) { + val putMessage = MessageUtils.makePutMessageFor(region = testRegion, key = key, value = value, valueEncoding = valueEncoding) + val responseMessage = testClient.blockingSendMessage(putMessage) + assertTrue(responseMessage.response.putResponse.success) + + val getMessage = MessageUtils.makeGetMessageFor(region = testRegion, key = key) + val getResponse = testClient.blockingSendMessage(getMessage) + + val messageEncodingType = getResponse.response.getResponse.result.encodingType + assertEquals(valueEncoding, messageEncodingType) + + val serializer = EncodingTypeThingy.serializerFromProtoEnum(messageEncodingType) + val messageValue = getResponse.response.getResponse.result.value.toByteArray() + + val deserializeValue = serializer.deserializer.deserialize(messageValue) + when (messageEncodingType) { + BasicTypes.EncodingType.BINARY -> assertArrayEquals(value as ByteArray, deserializeValue as ByteArray) + else -> assertEquals(value, serializer.deserializer.deserialize(messageValue)) + } + } + + @Throws(IOException::class) + private fun createCacheOnPort(port: Int): Cache { + val props = Properties() + props.setProperty(ConfigurationProperties.TCP_PORT, Integer.toString(port)) + props.setProperty(ConfigurationProperties.BIND_ADDRESS, "localhost") + val cf = CacheFactory(props) + val cache = cf.create() + val cacheServer = cache.addCacheServer() + cacheServer.bindAddress = "localhost" + cacheServer.start() + return cache + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 deleted file mode 100644 index 3c8eac0..0000000 --- a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.geode.protocol.client; - - -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.test.junit.categories.UnitTest; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.mockito.Mockito; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - - -@Category(UnitTest.class) -public class ProtobufSerializationDeserializationTest { - - 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); - - } - - /** - * Given a serialized message that we've built, verify that the server part does the right call to - * the Cache it gets passed. - */ - @Test - public void testNewClientProtocolPutsOnPutMessage() throws IOException { - ClientProtocol.Message message = MessageUtils.INSTANCE - .makePutMessageFor(testRegion, testKey, testValue); - - OutputStream mockOutputStream = Mockito.mock(OutputStream.class); - - ProtobufProtocolMessageHandler newClientProtocol = new ProtobufProtocolMessageHandler(); - newClientProtocol.receiveMessage(MessageUtils.INSTANCE.loadMessageIntoInputStream(message), - mockOutputStream, mockCache); - - verify(mockRegion).put(testKey.getBytes(), testValue.getBytes()); - } - - @Test - public void testServerRespondsToPutMessage() throws IOException { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(128); - ClientProtocol.Message message = MessageUtils.INSTANCE - .makePutMessageFor(testRegion, testKey, testValue); - - ProtobufProtocolMessageHandler newClientProtocol = new ProtobufProtocolMessageHandler(); - newClientProtocol.receiveMessage(MessageUtils.INSTANCE.loadMessageIntoInputStream(message), outputStream, - mockCache); - - ClientProtocol.Message responseMessage = ClientProtocol.Message - .parseDelimitedFrom(new ByteArrayInputStream(outputStream.toByteArray())); - - assertEquals(responseMessage.getMessageTypeCase(), - ClientProtocol.Message.MessageTypeCase.RESPONSE); - assertEquals(responseMessage.getResponse().getResponseAPICase(), - ClientProtocol.Response.ResponseAPICase.PUTRESPONSE); - assertTrue(responseMessage.getResponse().getPutResponse().getSuccess()); - } -} - http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.kt ---------------------------------------------------------------------- diff --git a/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.kt b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.kt new file mode 100644 index 0000000..4a60993 --- /dev/null +++ b/geode-client-protobuf/src/test/java/org/apache/geode/protocol/client/ProtobufSerializationDeserializationTest.kt @@ -0,0 +1,94 @@ +/* + * 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.geode.protocol.client + + +import org.apache.geode.cache.Cache +import org.apache.geode.cache.Region +import org.apache.geode.protocol.protobuf.ClientProtocol +import org.apache.geode.test.junit.categories.UnitTest +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.experimental.categories.Category +import org.mockito.Mockito +import org.mockito.Mockito.`when` +import org.mockito.Mockito.verify +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.IOException +import java.io.OutputStream + + +@Category(UnitTest::class) +class ProtobufSerializationDeserializationTest { + + private lateinit var mockCache: Cache + private lateinit var mockRegion: Region<*, *> + private val testRegion = "testRegion" + private val testKey = "testKey" + private val testValue = "testValue" + + @Before + fun start() { + mockCache = Mockito.mock(Cache::class.java) + mockRegion = Mockito.mock(Region::class.java) + `when`(mockCache.getRegion<Any, Any>(testRegion)).thenReturn(mockRegion as Region<Any, Any>) + + } + + /** + * Given a serialized message that we've built, verify that the server part does the right call to + * the Cache it gets passed. + */ + @Test + @Throws(IOException::class) + fun testNewClientProtocolPutsOnPutMessage() { + val message = MessageUtils + .makePutMessageFor(region = testRegion, key = testKey, value = testValue) + + val mockOutputStream = Mockito.mock(OutputStream::class.java) + + val newClientProtocol = ProtobufProtocolMessageHandler() + newClientProtocol.receiveMessage(MessageUtils.loadMessageIntoInputStream(message), + mockOutputStream, mockCache) + + verify(mockRegion as Region<Any, Any>).put(testKey, testValue) + } + + @Test + @Throws(IOException::class) + fun testServerRespondsToPutMessage() { + val outputStream = ByteArrayOutputStream(128) + val message = MessageUtils + .makePutMessageFor(region = testRegion, key = testKey, value = testValue) + + val newClientProtocol = ProtobufProtocolMessageHandler() + newClientProtocol.receiveMessage(MessageUtils.loadMessageIntoInputStream(message), outputStream, + mockCache) + + val responseMessage = ClientProtocol.Message + .parseDelimitedFrom(ByteArrayInputStream(outputStream.toByteArray())) + + assertEquals(responseMessage.messageTypeCase, + ClientProtocol.Message.MessageTypeCase.RESPONSE) + assertEquals(responseMessage.response.responseAPICase, + ClientProtocol.Response.ResponseAPICase.PUTRESPONSE) + assertTrue(responseMessage.response.putResponse.success) + } +} + http://git-wip-us.apache.org/repos/asf/geode/blob/6a5e4be3/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 index 79b0651..d551cef 100644 --- a/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java +++ b/geode-core/src/test/java/org/apache/geode/serialization/SerializationTypeTest.java @@ -27,13 +27,13 @@ public class SerializationTypeTest { 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}; + 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, + expectedBytes = new byte[] {(byte) 0xed, (byte) 0xcd, (byte) 0x8f, 0x62, 0x16, (byte) 0x82, (byte) 0x51, 0x00}; serializedBytes = SerializationType.LONG.serialize(testValue); assertArrayEquals(expectedBytes, serializedBytes); @@ -43,13 +43,13 @@ public class SerializationTypeTest { @Test public void testIntSerialization() throws Exception { int testValue = 313216366; // otherwise known as 0x12ab4d6e bytes; - byte[] expectedBytes = new byte[]{0x12, (byte) 0xab, 0x4d, 0x6e}; + 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}; + expectedBytes = new byte[] {(byte) 0xed, 0x54, (byte) 0xb2, (byte) 0x92}; serializedBytes = SerializationType.INT.serialize(testValue); assertArrayEquals(expectedBytes, serializedBytes); assertEquals(testValue, SerializationType.INT.deserialize(expectedBytes)); @@ -58,13 +58,13 @@ public class SerializationTypeTest { @Test public void testShortSerialization() throws Exception { short testValue = 31321; // otherwise known as 0x7a59 bytes; - byte[] expectedBytes = new byte[]{0x7a, 0x59}; + 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}; + expectedBytes = new byte[] {(byte) 0xa8, (byte) 0xab}; serializedBytes = SerializationType.SHORT.serialize(testValue); assertArrayEquals(expectedBytes, serializedBytes); assertEquals(testValue, SerializationType.SHORT.deserialize(expectedBytes)); @@ -73,13 +73,13 @@ public class SerializationTypeTest { @Test public void testByteSerialization() throws Exception { byte testValue = 116; - byte[] expectedBytes = new byte[]{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}; + expectedBytes = new byte[] {-87}; serializedBytes = SerializationType.BYTE.serialize(testValue); assertArrayEquals(expectedBytes, serializedBytes); assertEquals(testValue, SerializationType.BYTE.deserialize(expectedBytes)); @@ -96,16 +96,12 @@ public class SerializationTypeTest { + "which requires a Cache.") @Test public void testJSONSerialization() { - String[] testStrings = { - "\"testString\"", - "{}", - "{\"foo\":\"bar\",\"list :\":[1,2,\"boo\"],hash:{1:2,3:4}}" - }; + 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); + String deserialized = (String) SerializationType.JSON.deserializer.deserialize(serialized); assertEquals(string, deserialized); fail("This test is not yet complete"); }