forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-6515] [runtime] Fix classloading of JavaSerializer
- Loading branch information
1 parent
609bfa1
commit 6f8022e
Showing
4 changed files
with
257 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
flink-runtime/src/test/java/org/apache/flink/runtime/state/JavaSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.runtime.state; | ||
|
||
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.File; | ||
import java.io.Serializable; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* A test that verifies that the {@link JavaSerializer} properly handles class loading. | ||
*/ | ||
public class JavaSerializerTest extends SerializerTestBase<Serializable> { | ||
|
||
/** Class loader for the object that is not in the test class path */ | ||
private static final ClassLoader CLASS_LOADER = | ||
new URLClassLoader(new URL[0], JavaSerializerTest.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 guardTest() { | ||
// make sure that this test's assumptions hold | ||
try { | ||
Class.forName(OBJECT_OUT_OF_CLASSPATH.getClass().getName()); | ||
fail("Test ineffective: The test class that should not be on the classpath is actually on the classpath."); | ||
} catch (ClassNotFoundException e) { | ||
// expected | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
@Override | ||
protected TypeSerializer<Serializable> createSerializer() { | ||
Thread.currentThread().setContextClassLoader(CLASS_LOADER); | ||
return new JavaSerializer<>(); | ||
} | ||
|
||
@Override | ||
protected int getLength() { | ||
return -1; | ||
} | ||
|
||
@Override | ||
protected Class<Serializable> getTypeClass() { | ||
return Serializable.class; | ||
} | ||
|
||
@Override | ||
protected Serializable[] getTestData() { | ||
return new Serializable[] { | ||
new Integer(42), | ||
new File("/some/path/that/I/made/up"), | ||
|
||
// an object that is not in the classpath | ||
OBJECT_OUT_OF_CLASSPATH, | ||
|
||
// an object that is in the classpath with a nested object not in the classpath | ||
new Tuple1<>(OBJECT_OUT_OF_CLASSPATH) | ||
}; | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
@Override | ||
public void testInstantiate() { | ||
// this serializer does not support instantiation | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters