Repository: geode Updated Branches: refs/heads/feature/GEODE-3109 adab0c469 -> 12f90ee88
http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java new file mode 100644 index 0000000..95244d2 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java @@ -0,0 +1,81 @@ +/* + * 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.serializer; + +import org.apache.geode.client.protocol.MessageUtil; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ServiceLoader; + +@Category(UnitTest.class) +public class ProtobufProtocolSerializerJUnitTest { + private ProtocolSerializer<ClientProtocol.Message> protocolSerializer; + + @Before + public void startup() { + ServiceLoader<ProtocolSerializer> serviceLoader = ServiceLoader.load(ProtocolSerializer.class); + for (ProtocolSerializer protocolSerializer : serviceLoader) { + if (protocolSerializer instanceof ProtobufProtocolSerializer) { + this.protocolSerializer = protocolSerializer; + } + } + } + + @Test + public void testDeserializeByteArrayToMessage() + throws IOException, InvalidProtocolMessageException { + ClientProtocol.Message expectedRequestMessage = MessageUtil.createGetRequestMessage(); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + expectedRequestMessage.writeDelimitedTo(byteArrayOutputStream); + InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + + ClientProtocol.Message actualMessage = protocolSerializer.deserialize(inputStream); + Assert.assertEquals(expectedRequestMessage, actualMessage); + } + + @Test(expected = InvalidProtocolMessageException.class) + public void testDeserializeInvalidByteThrowsException() + throws IOException, InvalidProtocolMessageException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byteArrayOutputStream.write("Some incorrect byte array".getBytes()); + InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + protocolSerializer.deserialize(inputStream); + } + + @Test + public void testSerializeMessageToByteArray() throws IOException { + ClientProtocol.Message message = MessageUtil.createGetRequestMessage(); + ByteArrayOutputStream expectedByteArrayOutputStream = new ByteArrayOutputStream(); + message.writeDelimitedTo(expectedByteArrayOutputStream); + byte[] expectedByteArray = expectedByteArrayOutputStream.toByteArray(); + + ByteArrayOutputStream actualByteArrayOutputStream = new ByteArrayOutputStream(); + protocolSerializer.serialize(message, actualByteArrayOutputStream); + Assert.assertArrayEquals(expectedByteArray, actualByteArrayOutputStream.toByteArray()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/serialization/ProtobufSerializationServiceImplTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/ProtobufSerializationServiceImplTest.java b/geode-protobuf/src/test/java/org/apache/geode/serialization/ProtobufSerializationServiceImplTest.java index a4c0a14..ef841c3 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/serialization/ProtobufSerializationServiceImplTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/ProtobufSerializationServiceImplTest.java @@ -15,6 +15,7 @@ package org.apache.geode.serialization; import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ProtobufSerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; @@ -51,7 +52,7 @@ public class ProtobufSerializationServiceImplTest { BasicTypes.EncodingType encodingType, Object data) throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { byte[] encodedValue = service.encode(encodingType, data); - Object decdoedValue = service.decode(encodingType, encodedValue); - Assert.assertEquals(data, decdoedValue); + Object decodedValue = service.decode(encodingType, encodedValue); + Assert.assertEquals(data, decodedValue); } } http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslatorJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslatorJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslatorJUnitTest.java index 1af710c..963cb85 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslatorJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslatorJUnitTest.java @@ -17,6 +17,7 @@ package org.apache.geode.serialization.protobuf.translation; import static org.junit.Assert.assertSame; import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.test.junit.categories.UnitTest; http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java index 2c37fb5..1d038f5 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java @@ -81,17 +81,11 @@ public class CodecRegistryJUnitTest { Assert.assertSame(expectedCodec, codec); } - @Test - public void testGetCodecForUnregisteredType_throwsException() { - boolean caughtException = false; - try { - SerializationType mockSerializationType = PowerMockito.mock(SerializationType.class); - codecRegistry.getCodecForType(mockSerializationType); - } catch (CodecNotRegisteredForTypeException e) { - caughtException = true; - } - Assert.assertTrue("This should have thrown a CodecNotRegisteredForTypeException", - caughtException); + @Test(expected = CodecNotRegisteredForTypeException.class) + public void testGetCodecForUnregisteredType_throwsException() + throws CodecNotRegisteredForTypeException { + SerializationType mockSerializationType = PowerMockito.mock(SerializationType.class); + codecRegistry.getCodecForType(mockSerializationType); } class DummyTypeCodec implements TypeCodec {