This is an automated email from the ASF dual-hosted git repository. jorgebg pushed a commit to branch TINKERPOP-2161 in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/TINKERPOP-2161 by this push: new 31f536f Fix: Uncomment tests 31f536f is described below commit 31f536f41305d2b6fc4977280f6545f0aa592ff1 Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> AuthorDate: Fri Feb 22 11:19:00 2019 +0100 Fix: Uncomment tests --- .../ser/binary/TypeSerializerFailureTests.java | 236 +++++++-------- .../ser/binary/TypeSerializerRegistryTest.java | 321 ++++++++++----------- 2 files changed, 280 insertions(+), 277 deletions(-) diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerFailureTests.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerFailureTests.java index a721750..38296e4 100644 --- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerFailureTests.java +++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerFailureTests.java @@ -1,116 +1,120 @@ -///* -// * 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.tinkerpop.gremlin.driver.ser.binary; -// -//import io.netty.buffer.UnpooledByteBufAllocator; -//import org.apache.tinkerpop.gremlin.driver.ser.SerializationException; -//import org.apache.tinkerpop.gremlin.process.remote.traversal.DefaultRemoteTraverser; -//import org.apache.tinkerpop.gremlin.process.traversal.Bytecode; -//import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet; -//import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyPath; -//import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; -//import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalMetrics; -//import org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics; -//import org.apache.tinkerpop.gremlin.structure.Vertex; -//import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceEdge; -//import org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath; -//import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex; -//import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertexProperty; -//import org.apache.tinkerpop.gremlin.util.function.Lambda; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.junit.runners.Parameterized; -// -//import java.util.Arrays; -//import java.util.Collection; -//import java.util.Collections; -//import java.util.List; -// -//import static org.junit.Assert.assertEquals; -//import static org.junit.Assert.fail; -// -//@RunWith(Parameterized.class) -//public class TypeSerializerFailureTests { -// -// private final GraphBinaryWriter writer = new GraphBinaryWriter(); -// private final UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false); -// -// @Parameterized.Parameters(name = "Value={0}") -// public static Collection input() { -// final Bytecode.Binding b = new Bytecode.Binding(null, "b"); -// -// final ReferenceVertex vertex = new ReferenceVertex("a vertex", null); -// -// final Bytecode bytecode = new Bytecode(); -// bytecode.addStep(null); -// -// final BulkSet<Object> bulkSet = new BulkSet<>(); -// bulkSet.add(vertex, 1L); -// -// final MutableMetrics metrics = new MutableMetrics("a metric", null); -// -// final Tree<Vertex> tree = new Tree<>(); -// tree.put(vertex, null); -// -// // Provide instances that are malformed for serialization to fail -// return Arrays.asList( -// b, -// vertex, -// Collections.singletonMap("one", b), -// bulkSet, -// bytecode, -// Collections.singletonList(vertex), -// new ReferenceEdge("an edge", null, vertex, vertex), -// Lambda.supplier(null), -// metrics, -// new DefaultTraversalMetrics(1L, Collections.singletonList(metrics)), -// new DefaultRemoteTraverser<>(new Object(), 1L), -// tree, -// new ReferenceVertexProperty<>("a prop", null, "value"), -// new InvalidPath() -// ); -// } -// -// @Parameterized.Parameter(value = 0) -// public Object value; -// -// @Test -// public void shouldReleaseMemoryWhenFails() { -// try { -// writer.write(value, allocator); -// fail("Should throw exception"); -// } catch (SerializationException | RuntimeException e) { -// // Do nothing -// } -// -// assertEquals(0, allocator.metric().usedHeapMemory()); -// } -// -// public static class InvalidPath extends ReferencePath { -// public InvalidPath() { -// super(EmptyPath.instance()); -// } -// -// @Override -// public List<Object> objects() { -// return Collections.singletonList(new Object()); -// } -// } -//} +/* + * 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.tinkerpop.gremlin.driver.ser.binary; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; +import org.apache.tinkerpop.gremlin.driver.ser.SerializationException; +import org.apache.tinkerpop.gremlin.process.remote.traversal.DefaultRemoteTraverser; +import org.apache.tinkerpop.gremlin.process.traversal.Bytecode; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyPath; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; +import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalMetrics; +import org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceEdge; +import org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath; +import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex; +import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertexProperty; +import org.apache.tinkerpop.gremlin.util.function.Lambda; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +@RunWith(Parameterized.class) +public class TypeSerializerFailureTests { + + private final GraphBinaryWriter writer = new GraphBinaryWriter(); + private final UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false); + + @Parameterized.Parameters(name = "Value={0}") + public static Collection input() { + final Bytecode.Binding b = new Bytecode.Binding(null, "b"); + + final ReferenceVertex vertex = new ReferenceVertex("a vertex", null); + + final Bytecode bytecode = new Bytecode(); + bytecode.addStep(null); + + final BulkSet<Object> bulkSet = new BulkSet<>(); + bulkSet.add(vertex, 1L); + + final MutableMetrics metrics = new MutableMetrics("a metric", null); + + final Tree<Vertex> tree = new Tree<>(); + tree.put(vertex, null); + + // Provide instances that are malformed for serialization to fail + return Arrays.asList( + b, + vertex, + Collections.singletonMap("one", b), + bulkSet, + bytecode, + Collections.singletonList(vertex), + new ReferenceEdge("an edge", null, vertex, vertex), + Lambda.supplier(null), + metrics, + new DefaultTraversalMetrics(1L, Collections.singletonList(metrics)), + new DefaultRemoteTraverser<>(new Object(), 1L), + tree, + new ReferenceVertexProperty<>("a prop", null, "value"), + new InvalidPath() + ); + } + + @Parameterized.Parameter(value = 0) + public Object value; + + @Test + public void shouldReleaseMemoryWhenFails() { + final ByteBuf buffer = allocator.buffer(); + try { + writer.write(value, buffer); + fail("Should throw exception"); + } catch (SerializationException | RuntimeException e) { + // We are the owner of the buffer, we should release it + buffer.release(); + } + + // Make sure all allocations where released + assertEquals(0, allocator.metric().usedHeapMemory()); + } + + public static class InvalidPath extends ReferencePath { + public InvalidPath() { + super(EmptyPath.instance()); + } + + @Override + public List<Object> objects() { + return Collections.singletonList(new Object()); + } + } +} diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistryTest.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistryTest.java index c00ddbc..08de7f2 100644 --- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistryTest.java +++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistryTest.java @@ -1,161 +1,160 @@ -///* -// * 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.tinkerpop.gremlin.driver.ser.binary; -// -//import io.netty.buffer.ByteBuf; -//import io.netty.buffer.ByteBufAllocator; -//import org.apache.tinkerpop.gremlin.driver.ser.SerializationException; -//import org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePerson; -//import org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePersonSerializer; -//import org.apache.tinkerpop.gremlin.structure.Property; -//import org.apache.tinkerpop.gremlin.structure.VertexProperty; -//import org.junit.Test; -// -//import java.util.UUID; -// -//import static junit.framework.TestCase.assertEquals; -//import static junit.framework.TestCase.assertSame; -// -//public class TypeSerializerRegistryTest { -// -// @Test -// public void shouldResolveToUserProvidedForInterfaces_1() throws SerializationException { -// final TypeSerializer<VertexProperty> expected = new TestVertexPropertySerializer(); -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .add(VertexProperty.class, expected).create(); -// -// assertSame(expected, registry.getSerializer(VertexProperty.class)); -// assertSame(expected, registry.getSerializer(DataType.VERTEXPROPERTY)); -// } -// -// @Test -// public void shouldResolveToUserProvidedForInterfaces_2() throws SerializationException { -// final TypeSerializer<Property> expected = new TestPropertySerializer(); -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .add(Property.class, expected).create(); -// -// assertSame(expected, registry.getSerializer(Property.class)); -// assertSame(expected, registry.getSerializer(DataType.PROPERTY)); -// } -// -// @Test -// public void shouldResolveToUserProvidedForClasses() throws SerializationException { -// final TypeSerializer<UUID> expected = new TestUUIDSerializer(); -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .add(UUID.class, expected).create(); -// -// assertSame(expected, registry.getSerializer(UUID.class)); -// assertSame(expected, registry.getSerializer(DataType.UUID)); -// } -// -// @Test -// public void shouldResolveToTheFirstSerializerForInterfaces() throws SerializationException { -// final TypeSerializer<VertexProperty> expectedForVertexProperty = new TestVertexPropertySerializer(); -// final TypeSerializer<Property> expectedForProperty = new TestPropertySerializer(); -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .add(VertexProperty.class, expectedForVertexProperty) -// .add(Property.class, expectedForProperty).create(); -// -// assertSame(expectedForVertexProperty, registry.getSerializer(VertexProperty.class)); -// assertSame(expectedForProperty, registry.getSerializer(Property.class)); -// assertSame(expectedForVertexProperty, registry.getSerializer(DataType.VERTEXPROPERTY)); -// assertSame(expectedForProperty, registry.getSerializer(DataType.PROPERTY)); -// } -// -// @Test -// public void shouldUseFallbackResolverWhenThereIsNoMatch() { -// final int[] called = {0}; -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .withFallbackResolver(t -> { -// called[0]++; -// return null; -// }).create(); -// -// String message = null; -// try { -// registry.getSerializer(SamplePerson.class); -// } catch (SerializationException ex) { -// message = ex.getMessage(); -// } -// -// assertEquals("Serializer for type org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePerson not found", message); -// assertEquals(1, called[0]); -// } -// -// @Test -// public void shouldUseFallbackResolverReturnValue() throws SerializationException { -// TypeSerializer expected = new SamplePersonSerializer(); -// final int[] called = {0}; -// final TypeSerializerRegistry registry = TypeSerializerRegistry.build() -// .withFallbackResolver(t -> { -// called[0]++; -// return expected; -// }).create(); -// -// TypeSerializer<SamplePerson> serializer = registry.getSerializer(SamplePerson.class); -// assertEquals(1, called[0]); -// assertSame(expected, serializer); -// } -// -// private static class TestVertexPropertySerializer extends TestBaseTypeSerializer<VertexProperty> { -// -// @Override -// public DataType getDataType() { -// return DataType.VERTEXPROPERTY; -// } -// } -// -// private static class TestPropertySerializer extends TestBaseTypeSerializer<Property> { -// -// @Override -// public DataType getDataType() { -// return DataType.PROPERTY; -// } -// } -// -// private static class TestUUIDSerializer extends TestBaseTypeSerializer<UUID> { -// -// @Override -// public DataType getDataType() { -// return DataType.UUID; -// } -// } -// -// private static abstract class TestBaseTypeSerializer<T> implements TypeSerializer<T> { -// @Override -// public T read(ByteBuf buffer, GraphBinaryReader context) { -// return null; -// } -// -// @Override -// public T readValue(ByteBuf buffer, GraphBinaryReader context, boolean nullable) { -// return null; -// } -// -// @Override -// public ByteBuf write(T value, ByteBufAllocator allocator, GraphBinaryWriter context) { -// return null; -// } -// -// @Override -// public ByteBuf writeValue(T value, ByteBufAllocator allocator, GraphBinaryWriter context, boolean nullable) { -// return null; -// } -// } -//} +/* + * 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.tinkerpop.gremlin.driver.ser.binary; + +import io.netty.buffer.ByteBuf; +import org.apache.tinkerpop.gremlin.driver.ser.SerializationException; +import org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePerson; +import org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePersonSerializer; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.junit.Test; + +import java.util.UUID; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertSame; + +public class TypeSerializerRegistryTest { + + @Test + public void shouldResolveToUserProvidedForInterfaces_1() throws SerializationException { + final TypeSerializer<VertexProperty> expected = new TestVertexPropertySerializer(); + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .add(VertexProperty.class, expected).create(); + + assertSame(expected, registry.getSerializer(VertexProperty.class)); + assertSame(expected, registry.getSerializer(DataType.VERTEXPROPERTY)); + } + + @Test + public void shouldResolveToUserProvidedForInterfaces_2() throws SerializationException { + final TypeSerializer<Property> expected = new TestPropertySerializer(); + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .add(Property.class, expected).create(); + + assertSame(expected, registry.getSerializer(Property.class)); + assertSame(expected, registry.getSerializer(DataType.PROPERTY)); + } + + @Test + public void shouldResolveToUserProvidedForClasses() throws SerializationException { + final TypeSerializer<UUID> expected = new TestUUIDSerializer(); + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .add(UUID.class, expected).create(); + + assertSame(expected, registry.getSerializer(UUID.class)); + assertSame(expected, registry.getSerializer(DataType.UUID)); + } + + @Test + public void shouldResolveToTheFirstSerializerForInterfaces() throws SerializationException { + final TypeSerializer<VertexProperty> expectedForVertexProperty = new TestVertexPropertySerializer(); + final TypeSerializer<Property> expectedForProperty = new TestPropertySerializer(); + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .add(VertexProperty.class, expectedForVertexProperty) + .add(Property.class, expectedForProperty).create(); + + assertSame(expectedForVertexProperty, registry.getSerializer(VertexProperty.class)); + assertSame(expectedForProperty, registry.getSerializer(Property.class)); + assertSame(expectedForVertexProperty, registry.getSerializer(DataType.VERTEXPROPERTY)); + assertSame(expectedForProperty, registry.getSerializer(DataType.PROPERTY)); + } + + @Test + public void shouldUseFallbackResolverWhenThereIsNoMatch() { + final int[] called = {0}; + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .withFallbackResolver(t -> { + called[0]++; + return null; + }).create(); + + String message = null; + try { + registry.getSerializer(SamplePerson.class); + } catch (SerializationException ex) { + message = ex.getMessage(); + } + + assertEquals("Serializer for type org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample.SamplePerson not found", message); + assertEquals(1, called[0]); + } + + @Test + public void shouldUseFallbackResolverReturnValue() throws SerializationException { + TypeSerializer expected = new SamplePersonSerializer(); + final int[] called = {0}; + final TypeSerializerRegistry registry = TypeSerializerRegistry.build() + .withFallbackResolver(t -> { + called[0]++; + return expected; + }).create(); + + TypeSerializer<SamplePerson> serializer = registry.getSerializer(SamplePerson.class); + assertEquals(1, called[0]); + assertSame(expected, serializer); + } + + private static class TestVertexPropertySerializer extends TestBaseTypeSerializer<VertexProperty> { + + @Override + public DataType getDataType() { + return DataType.VERTEXPROPERTY; + } + } + + private static class TestPropertySerializer extends TestBaseTypeSerializer<Property> { + + @Override + public DataType getDataType() { + return DataType.PROPERTY; + } + } + + private static class TestUUIDSerializer extends TestBaseTypeSerializer<UUID> { + + @Override + public DataType getDataType() { + return DataType.UUID; + } + } + + private static abstract class TestBaseTypeSerializer<T> implements TypeSerializer<T> { + @Override + public T read(ByteBuf buffer, GraphBinaryReader context) { + return null; + } + + @Override + public T readValue(ByteBuf buffer, GraphBinaryReader context, boolean nullable) { + return null; + } + + @Override + public void write(T value, ByteBuf buffer, GraphBinaryWriter context) { + + } + + @Override + public void writeValue(T value, ByteBuf buffer, GraphBinaryWriter context, boolean nullable) { + + } + } +}