AHeise commented on a change in pull request #17184:
URL: https://github.com/apache/flink/pull/17184#discussion_r703508472



##########
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 {

Review comment:
       `ContextClassLoaderExtension`?

##########
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 talked about that offline, but now this extension cannot be used per 
test, right?
   What happens if we also implement `before/afterEach` would that allow us to 
use the extension both as a ClassRule and an instance Rule?

##########
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 {
+        temporaryFolder.create();
+        originalClassLoader = Thread.currentThread().getContextClassLoader();
+        temporaryClassLoader = 
temporaryClassLoaderFactory.apply(temporaryFolder);
+        Thread.currentThread().setContextClassLoader(temporaryClassLoader);
+    }
+
+    @Override
+    public void afterAll(ExtensionContext context) throws Exception {
+        Thread.currentThread().setContextClassLoader(originalClassLoader);
+        temporaryClassLoader.close();
+        temporaryFolder.delete();
+    }
+
+    /**
+     * Creates a new {@link ClassLoaderExtension} that puts additional service 
entries on the
+     * classpath.
+     *
+     * @param entries service entries that should be added to the classpath
+     * @return ClassLoaderExtension
+     */
+    public static ClassLoaderExtension withServiceEntries(ServiceEntry... 
entries) {

Review comment:
       If this was production code, I'd push for some builder pattern etc. I 
wouldn't expose `ServiceEntry` directly. I'd be fine to leave as is (for now). 
Some ideas:
   
   On instance method with RegisterExtension.
   ```
   new ClassLoaderExtension().withServiceEntry(serviceClass, 
serviceImplementations).withServiceEntry(...)
   ```
   
   ```
   
ClassLoaderExtension.buidler().addServiceEntry(...).addServiceEntry(...).build()
   ```
   
   Or more declaratively
   ```
   @ExtendWith(ClassLoaderExtension.class)
   class ServiceTest {
     @Test
     @WithServiceEntry(clazz, implementations = [])
     void testService() {
     }
   }
   ```




-- 
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]


Reply via email to