ruanhang1993 commented on a change in pull request #17184: URL: https://github.com/apache/flink/pull/17184#discussion_r704099530
########## File path: flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/extensions/ClassLoaderExtension.java ########## @@ -0,0 +1,117 @@ +/* + * 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.testutils.junit.extensions; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +/** JUnit extension to customize the classloader that a test is run with. */ +public class ClassLoaderExtension implements BeforeAllCallback, AfterAllCallback { + + private final TemporaryFolder temporaryFolder = new TemporaryFolder(); + private final Function<TemporaryFolder, URLClassLoader> temporaryClassLoaderFactory; + + private ClassLoader originalClassLoader; + private URLClassLoader temporaryClassLoader; + + private ClassLoaderExtension( + Function<TemporaryFolder, URLClassLoader> temporaryClassLoaderFactory) { + this.temporaryClassLoaderFactory = temporaryClassLoaderFactory; + } + + @Override + public void beforeAll(ExtensionContext context) throws Exception { Review comment: We are recently working at changing tests from Junit 4 to Junit 5. And the same problem occurs when the extension is used both as a ClassRule and an instance Rule. I prefer to provide a `EachExtensionWrapper`(for beforeAll, afterAll) and a `AllExtensionWrapper`(for beforeEach, AfterEach) in the module `flink-test-utils-junit` like this to solve it. -- 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]
