[
https://issues.apache.org/jira/browse/BEAM-3725?focusedWorklogId=101174&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-101174
]
ASF GitHub Bot logged work on BEAM-3725:
----------------------------------------
Author: ASF GitHub Bot
Created on: 11/May/18 16:06
Start Date: 11/May/18 16:06
Worklog Time Spent: 10m
Work Description: lukecwik closed pull request #4738: [BEAM-3725]
classloaderrule to simplify tests (re)setting the tccl
URL: https://github.com/apache/beam/pull/4738
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsFactoryTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsFactoryTest.java
index e4c4102d6b6..e730fc6b10a 100644
---
a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsFactoryTest.java
+++
b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsFactoryTest.java
@@ -60,6 +60,7 @@
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.PipelineRunner;
import org.apache.beam.sdk.runners.PipelineRunnerRegistrar;
+import org.apache.beam.sdk.testing.ClassLoaderRule;
import org.apache.beam.sdk.testing.CrashingRunner;
import org.apache.beam.sdk.testing.ExpectedLogs;
import org.apache.beam.sdk.testing.InterceptingUrlClassLoader;
@@ -83,6 +84,8 @@
@Rule public ExpectedException expectedException = ExpectedException.none();
@Rule public TestRule restoreSystemProperties = new
RestoreSystemProperties();
@Rule public ExpectedLogs expectedLogs =
ExpectedLogs.none(PipelineOptionsFactory.class);
+ @Rule public TestRule classLoaderRule =
+ ClassLoaderRule.build().withPipelineOptionsCacheReset().create();
@Test
public void testAutomaticRegistrationOfPipelineOptions() {
@@ -1814,19 +1817,14 @@ public void testPipelineOptionsFactoryUsesTccl() throws
Exception {
name -> name.toLowerCase(ROOT).contains("test"));
thread.setContextClassLoader(caseLoader);
PipelineOptionsFactory.resetCache();
- try {
- final PipelineOptions pipelineOptions = PipelineOptionsFactory.create();
- final Class optionType = caseLoader.loadClass(
-
"org.apache.beam.sdk.options.PipelineOptionsFactoryTest$ClassLoaderTestOptions");
- final Object options = pipelineOptions.as(optionType);
- assertSame(caseLoader, options.getClass().getClassLoader());
- assertSame(optionType.getClassLoader(),
options.getClass().getClassLoader());
- assertSame(testClassLoader,
optionType.getInterfaces()[0].getClassLoader());
-
assertTrue(Boolean.class.cast(optionType.getMethod("isOption").invoke(options)));
- } finally {
- thread.setContextClassLoader(testClassLoader);
- PipelineOptionsFactory.resetCache();
- }
+ final PipelineOptions pipelineOptions = PipelineOptionsFactory.create();
+ final Class optionType = caseLoader.loadClass(
+
"org.apache.beam.sdk.options.PipelineOptionsFactoryTest$ClassLoaderTestOptions");
+ final Object options = pipelineOptions.as(optionType);
+ assertSame(caseLoader, options.getClass().getClassLoader());
+ assertSame(optionType.getClassLoader(),
options.getClass().getClassLoader());
+ assertSame(testClassLoader,
optionType.getInterfaces()[0].getClassLoader());
+
assertTrue(Boolean.class.cast(optionType.getMethod("isOption").invoke(options)));
}
@Test
diff --git
a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ClassLoaderRule.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ClassLoaderRule.java
new file mode 100644
index 00000000000..cc314b89696
--- /dev/null
+++
b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ClassLoaderRule.java
@@ -0,0 +1,106 @@
+/*
+ * 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.beam.sdk.testing;
+
+import java.io.Serializable;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * This rule will store the classloader when the test starts
+ * and reset it on the contextual thread after the test
+ * if used as a test rule (same for the class as a class rule).
+ * If you pass it a function to create a classloader it will set it
+ * using an {@see InterceptingUrlClassLoader} before the test runs.
+ * Finally it handles the reset of the cache of pipeline options factory
+ * if set to true before and after the test.
+ */
+public class ClassLoaderRule implements TestRule, Serializable {
+ private final transient Function<ClassLoader, ClassLoader> classLoaderFn;
+ private final boolean resetPipelineOptionsCache;
+
+ private ClassLoaderRule(final Function<ClassLoader, ClassLoader>
classLoaderFn,
+ final boolean resetPipelineOptionsCache) {
+ this.classLoaderFn = classLoaderFn;
+ this.resetPipelineOptionsCache = resetPipelineOptionsCache;
+ }
+
+ public static Builder build() {
+ return new Builder();
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ final Thread thread = Thread.currentThread();
+ final ClassLoader loader = thread.getContextClassLoader();
+ if (resetPipelineOptionsCache) {
+ PipelineOptionsFactory.resetCache();
+ }
+ final ClassLoader testLoader = classLoaderFn == null ? null :
classLoaderFn.apply(loader);
+ if (classLoaderFn != null) {
+ thread.setContextClassLoader(testLoader);
+ }
+ try {
+ base.evaluate();
+ } finally {
+ thread.setContextClassLoader(loader);
+ if (AutoCloseable.class.isInstance(testLoader)) {
+ AutoCloseable.class.cast(testLoader).close();
+ }
+ if (resetPipelineOptionsCache) {
+ PipelineOptionsFactory.resetCache();
+ }
+ }
+ }
+ };
+ }
+
+ /**
+ * The {@see ClassLoaderRule} builder allowing
+ * to customize the test classloader if desired and
+ * if the pipeline options factory cache should be resetted.
+ */
+ public static final class Builder {
+ private Predicate<String> childClassesFilter;
+ private boolean resetPipelineOptionsCache;
+
+ public Builder withPipelineOptionsCacheReset() {
+ this.resetPipelineOptionsCache = true;
+ return this;
+ }
+
+ public Builder withTestChildClassesFilter(final Predicate<String>
childClassesFilter) {
+ this.childClassesFilter = childClassesFilter;
+ return this;
+ }
+
+ public ClassLoaderRule create() {
+ return new ClassLoaderRule(
+ childClassesFilter == null
+ ? null : parent -> new InterceptingUrlClassLoader(parent,
childClassesFilter),
+ resetPipelineOptionsCache);
+ }
+ }
+}
diff --git
a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/SerializableUtilsTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/SerializableUtilsTest.java
index d119a25038a..d0ebccf3d96 100644
---
a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/SerializableUtilsTest.java
+++
b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/SerializableUtilsTest.java
@@ -31,6 +31,7 @@
import org.apache.beam.sdk.coders.CoderException;
import org.apache.beam.sdk.io.BoundedSource;
import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.testing.ClassLoaderRule;
import org.apache.beam.sdk.testing.InterceptingUrlClassLoader;
import org.junit.Rule;
import org.junit.Test;
@@ -42,6 +43,7 @@
@RunWith(JUnit4.class)
public class SerializableUtilsTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
+ @Rule public ClassLoaderRule resetClassLoader =
ClassLoaderRule.build().create();
/** A class that is serializable by Java. */
private static class SerializableByJava implements Serializable {
@@ -57,7 +59,8 @@ public SerializableByJava(String stringValue, int intValue) {
@Test
public void customClassLoader() throws Exception {
// define a classloader with test-classes in it
- final ClassLoader testLoader =
Thread.currentThread().getContextClassLoader();
+ final Thread thread = Thread.currentThread();
+ final ClassLoader testLoader = thread.getContextClassLoader();
final ClassLoader loader = new InterceptingUrlClassLoader(testLoader,
MySource.class.getName());
final Class<?> source = loader.loadClass(
"org.apache.beam.sdk.util.SerializableUtilsTest$MySource");
@@ -66,13 +69,8 @@ public void customClassLoader() throws Exception {
// validate if the caller set the classloader that it works well
final Serializable customLoaderSourceInstance = Serializable.class.cast(
source.getConstructor().newInstance());
- final Thread thread = Thread.currentThread();
thread.setContextClassLoader(loader);
- try {
- assertSerializationClassLoader(loader, customLoaderSourceInstance);
- } finally {
- thread.setContextClassLoader(testLoader);
- }
+ assertSerializationClassLoader(loader, customLoaderSourceInstance);
// now let beam be a little be more fancy and try to ensure it by itself
from the incoming value
assertSerializationClassLoader(loader, customLoaderSourceInstance);
diff --git
a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/ReflectHelpersTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/ReflectHelpersTest.java
index 268104d42ac..8e71cf089ea 100644
---
a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/ReflectHelpersTest.java
+++
b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/ReflectHelpersTest.java
@@ -34,7 +34,6 @@
*/
@RunWith(JUnit4.class)
public class ReflectHelpersTest {
-
@Test
public void testClassName() {
assertEquals(getClass().getName(),
ReflectHelpers.CLASS_NAME.apply(getClass()));
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 101174)
Time Spent: 40m (was: 0.5h)
> Add a test rule which remembers and resets the thread context class loader
> --------------------------------------------------------------------------
>
> Key: BEAM-3725
> URL: https://issues.apache.org/jira/browse/BEAM-3725
> Project: Beam
> Issue Type: Improvement
> Components: sdk-java-core
> Reporter: Luke Cwik
> Assignee: Romain Manni-Bucau
> Priority: Minor
> Labels: newbie, starter
> Time Spent: 40m
> Remaining Estimate: 0h
>
> We have several tests that ensure the proper usage of the TCCL but they can
> be brittle if the test is written improperly.
>
> This task is to create a JUnit test rule and to replace the existing
> remember/reset TCCL usages in our tests.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)