carp84 commented on code in PR #499: URL: https://github.com/apache/flink-table-store/pull/499#discussion_r1095696131
########## flink-table-store-common/src/test/java/org/apache/flink/table/store/data/serializer/SerializerTestBase.java: ########## @@ -0,0 +1,351 @@ +/* + * 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.flink.table.store.data.serializer; + +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputSerializer; +import org.apache.flink.core.memory.DataOutputView; + +import org.apache.commons.lang3.SerializationException; +import org.apache.commons.lang3.SerializationUtils; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CyclicBarrier; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** Abstract test base for serializers. */ +public abstract class SerializerTestBase<T> { + + protected abstract Serializer<T> createSerializer(); + + protected abstract boolean deepEquals(T t1, T t2); + + protected abstract T[] getTestData(); + + // -------------------------------------------------------------------------------------------- + + @Test + protected void testCopy() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + for (T datum : testData) { + T copy = serializer.copy(datum); + checkToString(copy); + deepEquals("Copied element is not equal to the original element.", datum, copy); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializeIndividually() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + for (T value : testData) { + TestOutputView out = new TestOutputView(); + serializer.serialize(value, out); + TestInputView in = out.getInputView(); + + assertTrue("No data available during deserialization.", in.available() > 0); + + T deserialized = serializer.deserialize(in); + checkToString(deserialized); + + deepEquals("Deserialized value if wrong.", value, deserialized); + + assertTrue("Trailing data available after deserialization.", in.available() == 0); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializeAsSequenceNoReuse() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + TestOutputView out = new TestOutputView(); + for (T value : testData) { + serializer.serialize(value, out); + } + + TestInputView in = out.getInputView(); + + int num = 0; + while (in.available() > 0) { + T deserialized = serializer.deserialize(in); + checkToString(deserialized); + + deepEquals("Deserialized value if wrong.", testData[num], deserialized); + num++; + } + + assertEquals("Wrong number of elements deserialized.", testData.length, num); + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializabilityAndEquals() { + try { + Serializer<T> ser1 = getSerializer(); + Serializer<T> ser2; + try { + ser2 = SerializationUtils.clone(ser1); Review Comment: > We should use `org.apache.flink.table.store.utils.InstantiationUtil`. I believe this could fix the pre-commit build error. ########## flink-table-store-common/src/test/java/org/apache/flink/table/store/data/serializer/SerializerTestBase.java: ########## @@ -0,0 +1,351 @@ +/* + * 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.flink.table.store.data.serializer; + +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputSerializer; +import org.apache.flink.core.memory.DataOutputView; + +import org.apache.commons.lang3.SerializationException; +import org.apache.commons.lang3.SerializationUtils; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nonnull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CyclicBarrier; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** Abstract test base for serializers. */ +public abstract class SerializerTestBase<T> { + + protected abstract Serializer<T> createSerializer(); + + protected abstract boolean deepEquals(T t1, T t2); + + protected abstract T[] getTestData(); + + // -------------------------------------------------------------------------------------------- + + @Test + protected void testCopy() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + for (T datum : testData) { + T copy = serializer.copy(datum); + checkToString(copy); + deepEquals("Copied element is not equal to the original element.", datum, copy); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializeIndividually() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + for (T value : testData) { + TestOutputView out = new TestOutputView(); + serializer.serialize(value, out); + TestInputView in = out.getInputView(); + + assertTrue("No data available during deserialization.", in.available() > 0); + + T deserialized = serializer.deserialize(in); + checkToString(deserialized); + + deepEquals("Deserialized value if wrong.", value, deserialized); + + assertTrue("Trailing data available after deserialization.", in.available() == 0); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializeAsSequenceNoReuse() { + try { + Serializer<T> serializer = getSerializer(); + T[] testData = getData(); + + TestOutputView out = new TestOutputView(); + for (T value : testData) { + serializer.serialize(value, out); + } + + TestInputView in = out.getInputView(); + + int num = 0; + while (in.available() > 0) { + T deserialized = serializer.deserialize(in); + checkToString(deserialized); + + deepEquals("Deserialized value if wrong.", testData[num], deserialized); + num++; + } + + assertEquals("Wrong number of elements deserialized.", testData.length, num); + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + fail("Exception in test: " + e.getMessage()); + } + } + + @Test + void testSerializabilityAndEquals() { + try { + Serializer<T> ser1 = getSerializer(); + Serializer<T> ser2; + try { + ser2 = SerializationUtils.clone(ser1); Review Comment: > We should use `org.apache.flink.table.store.utils.InstantiationUtil`. I believe this could fix the pre-commit build error. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
