[hotfix] [tests] Add a lightweight test for classloading in the Kryo Serializer
Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/65fcca0a Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/65fcca0a Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/65fcca0a Branch: refs/heads/release-1.3 Commit: 65fcca0a43cf32427f8fd13d197815d1c5b7d078 Parents: 2696cf3 Author: Stephan Ewen <[email protected]> Authored: Wed May 10 12:37:28 2017 +0200 Committer: Stephan Ewen <[email protected]> Committed: Wed May 10 21:08:37 2017 +0200 ---------------------------------------------------------------------- .../kryo/KryoSerializerClassLoadingTest.java | 113 +++++++++++++++++++ 1 file changed, 113 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/65fcca0a/flink-core/src/test/java/org/apache/flink/api/java/typeutils/runtime/kryo/KryoSerializerClassLoadingTest.java ---------------------------------------------------------------------- diff --git a/flink-core/src/test/java/org/apache/flink/api/java/typeutils/runtime/kryo/KryoSerializerClassLoadingTest.java b/flink-core/src/test/java/org/apache/flink/api/java/typeutils/runtime/kryo/KryoSerializerClassLoadingTest.java new file mode 100644 index 0000000..9823e11 --- /dev/null +++ b/flink-core/src/test/java/org/apache/flink/api/java/typeutils/runtime/kryo/KryoSerializerClassLoadingTest.java @@ -0,0 +1,113 @@ +/* + * 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.api.java.typeutils.runtime.kryo; + +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.typeutils.SerializerTestBase; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple1; +import org.apache.flink.core.testutils.CommonTestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.Serializable; +import java.net.URL; +import java.net.URLClassLoader; + +import static org.junit.Assert.fail; + +/** + * This test validates that the Kryo-based serializer handles classes with custom + * class loaders correctly. + */ +public class KryoSerializerClassLoadingTest extends SerializerTestBase<Object> { + + /** Class loader for the object that is not in the test class path */ + private static final ClassLoader CLASS_LOADER = + new URLClassLoader(new URL[0], KryoSerializerClassLoadingTest.class.getClassLoader()); + + /** An object that is not in the test class path */ + private static final Serializable OBJECT_OUT_OF_CLASSPATH = + CommonTestUtils.createObjectForClassNotInClassPath(CLASS_LOADER); + + // ------------------------------------------------------------------------ + + private ClassLoader originalClassLoader; + + @Before + public void setupClassLoader() { + originalClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(CLASS_LOADER); + } + + @After + public void restoreOriginalClassLoader() { + Thread.currentThread().setContextClassLoader(originalClassLoader); + } + + // ------------------------------------------------------------------------ + + @Test + public void guardTestAssumptions() { + try { + Class.forName(OBJECT_OUT_OF_CLASSPATH.getClass().getName()); + fail("This test's assumptions are broken"); + } + catch (ClassNotFoundException ignored) { + // expected + } + } + + // ------------------------------------------------------------------------ + + @Override + protected TypeSerializer<Object> createSerializer() { + return new KryoSerializer<>(Object.class, new ExecutionConfig()); + } + + @Override + protected int getLength() { + return -1; + } + + @Override + protected Class<Object> getTypeClass() { + return Object.class; + } + + @Override + protected Object[] getTestData() { + return new Object[] { + new Integer(7), + + // an object whose class is not on the classpath + OBJECT_OUT_OF_CLASSPATH, + + // an object whose class IS on the classpath with a nested object whose class + // is NOT on the classpath + new Tuple1<>(OBJECT_OUT_OF_CLASSPATH) + }; + } + + @Override + public void testInstantiate() { + // this serializer does not support instantiation + } +}
