Repository: geode Updated Branches: refs/heads/feature/GEODE-2995 02860bab2 -> fd303dff5
GEODE-2995: Added TypeCodec for all supported primitives and JSON Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/fd303dff Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/fd303dff Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/fd303dff Branch: refs/heads/feature/GEODE-2995 Commit: fd303dff536de68312b79ffaa1237642f87ce0da Parents: 02860ba Author: Udo Kohlmeyer <[email protected]> Authored: Tue Jun 13 16:58:25 2017 -0700 Committer: Udo Kohlmeyer <[email protected]> Committed: Tue Jun 13 16:58:25 2017 -0700 ---------------------------------------------------------------------- .../geode/serialization/SerializationType.java | 3 +- .../geode/serialization/codec/BinaryCodec.java | 31 +++++++++++ .../geode/serialization/codec/BooleanCodec.java | 33 ++++++++++++ .../geode/serialization/codec/ByteCodec.java | 33 ++++++++++++ .../geode/serialization/codec/DoubleCodec.java | 33 ++++++++++++ .../geode/serialization/codec/FloatCodec.java | 33 ++++++++++++ .../geode/serialization/codec/IntCodec.java | 34 ++++++++++++ .../geode/serialization/codec/JSONCodec.java | 33 ++++++++++++ .../geode/serialization/codec/LongCodec.java | 33 ++++++++++++ .../geode/serialization/codec/ShortCodec.java | 33 ++++++++++++ .../geode/serialization/codec/StringCodec.java | 35 ++++++++++++ ...codingTypeToSerializationTypeTranslator.java | 30 +++++++---- .../geode/client/protocol/EncodingHandler.java | 21 -------- .../protocol/EncodingHandlerRegistry.java | 21 -------- .../geode/client/protocol/OpsHandler.java | 7 ++- .../geode/client/protocol/OpsProcessorTest.java | 56 +++++++++++--------- .../ProtobufRequestOperationParser.java | 31 ----------- .../ProtobufRequestOperationParser.java | 31 +++++++++++ .../codec/StringCodecJUnitTest.java | 50 +++++++++++++++++ ...eToSerializationTypeTranslatorJUnitTest.java | 6 +-- 20 files changed, 470 insertions(+), 117 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java index 01e691b..1e56625 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java @@ -6,7 +6,7 @@ import java.nio.charset.Charset; public enum SerializationType { STRING(String.class), - BYTE_BLOB(byte[].class), + BINARY(byte[].class), INT(int.class), BYTE(byte.class), SHORT(short.class), @@ -16,7 +16,6 @@ public enum SerializationType { FLOAT(float.class), DOUBLE(double.class); - private static final Charset UTF8 = Charset.forName("UTF-8"); public final Class klass; SerializationType(Class klass) { http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BinaryCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BinaryCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BinaryCodec.java new file mode 100644 index 0000000..13a4b37 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BinaryCodec.java @@ -0,0 +1,31 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +public class BinaryCodec implements TypeCodec<byte[]> { + @Override + public byte[] decode(byte[] incoming) { + return incoming; + } + + @Override + public byte[] encode(byte[] incoming) { + return incoming; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java new file mode 100644 index 0000000..445bb97 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class BooleanCodec implements TypeCodec<Boolean> { + @Override + public Boolean decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).get() == 1; + } + + @Override + public byte[] encode(Boolean incoming) { + return ByteBuffer.allocate(Byte.BYTES).put(incoming ? (byte) 1 : (byte) 0).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java new file mode 100644 index 0000000..30d4238 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class ByteCodec implements TypeCodec<Byte> { + @Override + public Byte decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).get(); + } + + @Override + public byte[] encode(Byte incoming) { + return ByteBuffer.allocate(Byte.BYTES).put(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java new file mode 100644 index 0000000..e312d69 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class DoubleCodec implements TypeCodec<Double> { + @Override + public Double decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).getDouble(); + } + + @Override + public byte[] encode(Double incoming) { + return ByteBuffer.allocate(Double.BYTES).putDouble(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java new file mode 100644 index 0000000..87d2704 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class FloatCodec implements TypeCodec<Float> { + @Override + public Float decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).getFloat(); + } + + @Override + public byte[] encode(Float incoming) { + return ByteBuffer.allocate(Float.BYTES).putFloat(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java new file mode 100644 index 0000000..45f5204 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java @@ -0,0 +1,34 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +public class IntCodec implements TypeCodec<Integer> { + @Override + public Integer decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).getInt(); + } + + @Override + public byte[] encode(Integer incoming) { + return ByteBuffer.allocate(Integer.BYTES).putInt(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/JSONCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/JSONCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/JSONCodec.java new file mode 100644 index 0000000..3ef91b4 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/JSONCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.pdx.JSONFormatter; +import org.apache.geode.pdx.PdxInstance; +import org.apache.geode.serialization.TypeCodec; + +public class JSONCodec implements TypeCodec<PdxInstance> { + @Override + public PdxInstance decode(byte[] incoming) { + return JSONFormatter.fromJSON(incoming); + } + + @Override + public byte[] encode(PdxInstance incoming) { + return JSONFormatter.toJSONByteArray(incoming); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/LongCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/LongCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/LongCodec.java new file mode 100644 index 0000000..394f56f --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/LongCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class LongCodec implements TypeCodec<Long> { + @Override + public Long decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).getLong(); + } + + @Override + public byte[] encode(Long incoming) { + return ByteBuffer.allocate(Long.BYTES).putLong(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ShortCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ShortCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ShortCodec.java new file mode 100644 index 0000000..634c343 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ShortCodec.java @@ -0,0 +1,33 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.ByteBuffer; + +public class ShortCodec implements TypeCodec<Short> { + @Override + public Short decode(byte[] incoming) { + return ByteBuffer.wrap(incoming).getShort(); + } + + @Override + public byte[] encode(Short incoming) { + return ByteBuffer.allocate(Short.BYTES).putShort(incoming).array(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/StringCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/StringCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/StringCodec.java new file mode 100644 index 0000000..b4168a9 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/StringCodec.java @@ -0,0 +1,35 @@ +/* + * 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.serialization.codec; + +import org.apache.geode.serialization.TypeCodec; + +import java.nio.charset.Charset; + +public class StringCodec implements TypeCodec<String> { + private static final Charset UTF8 = Charset.forName("UTF-8"); + + @Override + public String decode(byte[] incoming) { + return new String(incoming, UTF8); + } + + @Override + public byte[] encode(String incoming) { + return incoming.getBytes(UTF8); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslator.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslator.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslator.java index cae2b20..6ad053c 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslator.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeToSerializationTypeTranslator.java @@ -22,16 +22,26 @@ public class EncodingTypeToSerializationTypeTranslator { public SerializationType getSerializationTypeForEncodingType(BasicTypes.EncodingType encodingType) throws UnsupportedEncodingTypeException { switch (encodingType) { - case INT: return SerializationType.INT; - case BYTE: return SerializationType.BYTE; - case JSON: return SerializationType.JSON; - case LONG: return SerializationType.LONG; - case FLOAT: return SerializationType.FLOAT; - case SHORT: return SerializationType.SHORT; - case BINARY: return SerializationType.BYTE_BLOB; - case DOUBLE: return SerializationType.DOUBLE; - case STRING: return SerializationType.STRING; - case BOOLEAN: return SerializationType.BOOLEAN; + case INT: + return SerializationType.INT; + case BYTE: + return SerializationType.BYTE; + case JSON: + return SerializationType.JSON; + case LONG: + return SerializationType.LONG; + case FLOAT: + return SerializationType.FLOAT; + case SHORT: + return SerializationType.SHORT; + case BINARY: + return SerializationType.BINARY; + case DOUBLE: + return SerializationType.DOUBLE; + case STRING: + return SerializationType.STRING; + case BOOLEAN: + return SerializationType.BOOLEAN; default: throw new UnsupportedEncodingTypeException( "No serialization type found for protobuf encoding type: " + encodingType); http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java deleted file mode 100644 index bc5dab3..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java +++ /dev/null @@ -1,21 +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.client.protocol; - -public interface EncodingHandler<T> { - T deserialze(byte[] incoming); - - byte[] serialize(T incoming); -} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java deleted file mode 100644 index 8bc35a5..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java +++ /dev/null @@ -1,21 +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.client.protocol; - -import org.apache.geode.serialization.SerializationType; - -public interface EncodingHandlerRegistry { - EncodingHandler getEncodingHandlerForType(SerializationType serializationType); -} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java index 73fc370..793dace 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java +++ b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java @@ -14,9 +14,8 @@ */ package org.apache.geode.client.protocol; -/** - * Created by ukohlmeyer on 6/12/17. - */ +import org.apache.geode.serialization.registry.SerializationCodecRegistry; + public interface OpsHandler<Req, Resp> { - Resp process(EncodingHandlerRegistry encodingHandlerRegistry, Req request); + Resp process(SerializationCodecRegistry serializationCodecRegistry, Req request); } http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java index ed28948..10f4f2b 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java @@ -21,60 +21,65 @@ import static org.mockito.Mockito.when; import com.google.protobuf.ByteString; import org.apache.geode.protocol.operations.OperationHandler; +//import org.apache.geode.protocol.operations.ProtobufRequestOperationParser; import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; 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.serialization.SerializationType; +import org.apache.geode.serialization.TypeCodec; +import org.apache.geode.serialization.registry.SerializationCodecRegistry; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; import org.junit.Assert; import org.junit.Test; public class OpsProcessorTest { @Test - public void testOpsProcessor() { + public void testOpsProcessor() throws CodecNotRegisteredForTypeException { OperationsHandlerRegistry opsHandlerRegistry = mock(OperationsHandlerRegistry.class); OperationHandler operationHandlerStub = mock(OperationHandler.class); - EncodingHandlerRegistry encodingHandlerRegistry = mock(EncodingHandlerRegistry.class); - EncodingHandler encodingHandler = mock(EncodingHandler.class); + SerializationCodecRegistry serializationCodecRegistry = mock(SerializationCodecRegistry.class); - when(encodingHandlerRegistry.getEncodingHandlerForType(SerializationType.STRING)) - .thenReturn(new EncodingHandler() { + when(serializationCodecRegistry.getCodecForType(SerializationType.STRING)) + .thenReturn(new TypeCodec<String>() { @Override - public Object deserialze(byte[] incoming) { + public String decode(byte[] incoming) { return new String(incoming); } @Override - public byte[] serialize(Object incoming) { - return ((String) incoming).getBytes(); + public byte[] encode(String incoming) { + return incoming.getBytes(); } }); ClientProtocol.Request messageRequest = ClientProtocol.Request.newBuilder() .setGetRequest(RegionAPI.GetRequest.newBuilder()).build(); - RegionAPI.GetResponse expectedResponse = getGetResponse(encodingHandlerRegistry); + RegionAPI.GetResponse expectedResponse = getGetResponse(serializationCodecRegistry); try { - when(opsHandlerRegistry.getOperationHandlerForOperationId(2)).thenReturn(operationHandlerStub); + when(opsHandlerRegistry.getOperationHandlerForOperationId(2)) + .thenReturn(operationHandlerStub); } catch (OperationHandlerNotRegisteredException e) { e.printStackTrace(); } - when(operationHandlerStub.process(encodingHandlerRegistry, - ProtobufRequestOperationParser.getRequestForOperationTypeID(messageRequest))) - .thenReturn(expectedResponse); +// when(operationHandlerStub.process(serializationCodecRegistry, +// ProtobufRequestOperationParser.getRequestForOperationTypeID(messageRequest))) +// .thenReturn(expectedResponse); - OpsProcessor processor = new OpsProcessor(opsHandlerRegistry, encodingHandlerRegistry); + OpsProcessor processor = new OpsProcessor(opsHandlerRegistry, serializationCodecRegistry); ClientProtocol.Response response = processor.process(messageRequest); Assert.assertEquals(expectedResponse, response.getGetResponse()); } - private RegionAPI.GetResponse getGetResponse(EncodingHandlerRegistry encodingHandlerRegistry) { + private RegionAPI.GetResponse getGetResponse(SerializationCodecRegistry serializationCodecRegistry) + throws CodecNotRegisteredForTypeException { RegionAPI.GetResponse.Builder getResponseBuilder = RegionAPI.GetResponse.newBuilder(); BasicTypes.EncodedValue.Builder encodedValueBuilder = BasicTypes.EncodedValue.newBuilder(); - EncodingHandler encodingHandler = - encodingHandlerRegistry.getEncodingHandlerForType(SerializationType.STRING); - byte[] serializedValue = encodingHandler.serialize("10"); + TypeCodec typeCodec = + serializationCodecRegistry.getCodecForType(SerializationType.STRING); + byte[] serializedValue = typeCodec.encode("10"); encodedValueBuilder.setValue(ByteString.copyFrom(serializedValue)); encodedValueBuilder.setEncodingType(BasicTypes.EncodingType.STRING); getResponseBuilder.setResult(encodedValueBuilder); @@ -83,12 +88,12 @@ public class OpsProcessorTest { private class OpsProcessor { private final OperationsHandlerRegistry opsHandlerRegistry; - private final EncodingHandlerRegistry encodingHandlerRegistry; + private final SerializationCodecRegistry serializationCodecRegistry; public OpsProcessor(OperationsHandlerRegistry opsHandlerRegistry, - EncodingHandlerRegistry encodingHandlerRegistry) { + SerializationCodecRegistry serializationCodecRegistry) { this.opsHandlerRegistry = opsHandlerRegistry; - this.encodingHandlerRegistry = encodingHandlerRegistry; + this.serializationCodecRegistry = serializationCodecRegistry; } public ClientProtocol.Response process(ClientProtocol.Request request) { @@ -99,10 +104,11 @@ public class OpsProcessorTest { e.printStackTrace(); } - Object responseMessage = opsHandler.process(encodingHandlerRegistry, - ProtobufRequestOperationParser.getRequestForOperationTypeID(request)); - return ClientProtocol.Response.newBuilder() - .setGetResponse((RegionAPI.GetResponse) responseMessage).build(); +// Object responseMessage = opsHandler.process(serializationCodecRegistry, +// ProtobufRequestOperationParser.getRequestForOperationTypeID(request)); +// return ClientProtocol.Response.newBuilder() +// .setGetResponse((RegionAPI.GetResponse) responseMessage).build(); + return null; } } } http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/client/protocol/ProtobufRequestOperationParser.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/ProtobufRequestOperationParser.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/ProtobufRequestOperationParser.java deleted file mode 100644 index 7eeff02..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/ProtobufRequestOperationParser.java +++ /dev/null @@ -1,31 +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.client.protocol; - -import org.apache.geode.protocol.protobuf.ClientProtocol; - -class ProtobufRequestOperationParser { - static Object getRequestForOperationTypeID(ClientProtocol.Request request) { - switch (request.getRequestAPICase()) { - case PUTREQUEST: - return request.getPutRequest(); - case GETREQUEST: - return request.getGetRequest(); - default: - throw new RuntimeException( - "Unknown request type: " + request.getRequestAPICase().getNumber()); - } - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java new file mode 100644 index 0000000..7bc8819 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java @@ -0,0 +1,31 @@ +/* + * 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.operations; + +import org.apache.geode.protocol.protobuf.ClientProtocol; + +class ProtobufRequestOperationParser { + static Object getRequestForOperationTypeID(ClientProtocol.Request request) { + switch (request.getRequestAPICase()) { + case PUTREQUEST: + return request.getPutRequest(); + case GETREQUEST: + return request.getGetRequest(); + default: + throw new RuntimeException( + "Unknown request type: " + request.getRequestAPICase().getNumber()); + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/StringCodecJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/StringCodecJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/StringCodecJUnitTest.java new file mode 100644 index 0000000..1c85e92 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/StringCodecJUnitTest.java @@ -0,0 +1,50 @@ +package org.apache.geode.serialization.codec; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.nio.charset.Charset; + +@Category(UnitTest.class) +public class StringCodecJUnitTest { + private static final Charset UTF8 = Charset.forName("UTF-8"); + private static final Charset UTF16 = Charset.forName("UTF-16"); + private String testString = "Test String"; + + private StringCodec stringCodec; + + @Before + public void startup() { + stringCodec = new StringCodec(); + } + + @Test + public void testStringEncoding() { + assertArrayEquals(testString.getBytes(UTF8), stringCodec.encode(testString)); + } + + @Test + public void testStringIncompatibleEncoding() { + byte[] expectedEncodedString = stringCodec.encode(testString); + byte[] incorrectEncodedString = testString.getBytes(UTF16); + assertNotEquals(expectedEncodedString.length, incorrectEncodedString.length); + } + + @Test + public void testStringDecodingWithIncorrectEncodedString() { + byte[] encodedString = testString.getBytes(UTF16); + assertNotEquals(testString, stringCodec.decode(encodedString)); + } + + @Test + public void testStringDecoding() { + byte[] encodedString = testString.getBytes(UTF8); + assertEquals(testString, stringCodec.decode(encodedString)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/fd303dff/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 72ba4ad..2d602c9 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 @@ -26,7 +26,7 @@ public class EncodingTypeToSerializationTypeTranslatorJUnitTest { translator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.BYTE)); assertSame(SerializationType.BOOLEAN, translator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.BOOLEAN)); - assertSame(SerializationType.BYTE_BLOB, + assertSame(SerializationType.BINARY, translator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.BINARY)); assertSame(SerializationType.FLOAT, translator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.FLOAT)); @@ -52,8 +52,8 @@ public class EncodingTypeToSerializationTypeTranslatorJUnitTest { EncodingTypeToSerializationTypeTranslator translator = new EncodingTypeToSerializationTypeTranslator(); for (BasicTypes.EncodingType encodingType : BasicTypes.EncodingType.values()) { - if (!(encodingType.equals(BasicTypes.EncodingType.UNRECOGNIZED) || encodingType - .equals(BasicTypes.EncodingType.INVALID))) { + if (!(encodingType.equals(BasicTypes.EncodingType.UNRECOGNIZED) + || encodingType.equals(BasicTypes.EncodingType.INVALID))) { try { translator.getSerializationTypeForEncodingType(encodingType); } catch (UnsupportedEncodingTypeException e) {
