zentol commented on a change in pull request #16345:
URL: https://github.com/apache/flink/pull/16345#discussion_r668628496



##########
File path: 
flink-core/src/test/java/org/apache/flink/core/classloading/ComponentClassLoaderTest.java
##########
@@ -0,0 +1,264 @@
+/*
+ * 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.core.classloading;
+
+import org.apache.flink.util.TestLogger;
+
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.Enumeration;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/** Tests for the {@link ComponentClassLoader}. */
+public class ComponentClassLoaderTest extends TestLogger {
+
+    private static final String NON_EXISTENT_CLASS_NAME = "foo.Bar";
+    private static final Class<?> CLASS_TO_LOAD = Class.class;
+    private static final Class<?> CLASS_RETURNED_BY_OWNER = 
ComponentClassLoaderTest.class;
+
+    private static final String NON_EXISTENT_RESOURCE_NAME = "foo/Bar";
+    private static String resourceToLoad;
+    private static final URL RESOURCE_RETURNED_BY_OWNER = createURL();
+
+    @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder();
+
+    @BeforeClass
+    public static void setup() throws IOException {
+        resourceToLoad = TMP.newFile("tmpfile").getName();
+    }
+
+    // 
----------------------------------------------------------------------------------------------
+    // Class loading
+    // 
----------------------------------------------------------------------------------------------
+
+    @Test(expected = ClassNotFoundException.class)
+    public void testComponentOnlyIsDefaultForClasses() throws Exception {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(NON_EXISTENT_CLASS_NAME, 
CLASS_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(new URL[0], owner, new String[0], new 
String[0]);
+
+        componentClassLoader.loadClass(NON_EXISTENT_CLASS_NAME);
+    }
+
+    @Test
+    public void testOwnerFirstClassFoundIgnoresComponent() throws Exception {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(CLASS_TO_LOAD.getName(), 
CLASS_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[0], owner, new String[] 
{CLASS_TO_LOAD.getName()}, new String[0]);
+
+        final Class<?> loadedClass = 
componentClassLoader.loadClass(CLASS_TO_LOAD.getName());
+        assertThat(loadedClass, sameInstance(CLASS_RETURNED_BY_OWNER));
+    }
+
+    @Test
+    public void testOwnerFirstClassNotFoundFallsBackToComponent() throws 
Exception {
+        TestUrlClassLoader owner = new TestUrlClassLoader();
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[0], owner, new String[] 
{CLASS_TO_LOAD.getName()}, new String[0]);
+
+        final Class<?> loadedClass = 
componentClassLoader.loadClass(CLASS_TO_LOAD.getName());
+        assertThat(loadedClass, sameInstance(CLASS_TO_LOAD));
+    }
+
+    @Test
+    public void testComponentFirstClassFoundIgnoresOwner() throws Exception {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(CLASS_TO_LOAD.getName(), 
CLASS_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[0], owner, new String[0], new String[] 
{CLASS_TO_LOAD.getName()});
+
+        final Class<?> loadedClass = 
componentClassLoader.loadClass(CLASS_TO_LOAD.getName());
+        assertThat(loadedClass, sameInstance(CLASS_TO_LOAD));
+    }
+
+    @Test
+    public void testComponentFirstClassNotFoundFallsBackToOwner() throws 
Exception {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(NON_EXISTENT_CLASS_NAME, 
CLASS_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[0], owner, new String[0], new String[] 
{NON_EXISTENT_CLASS_NAME});
+
+        final Class<?> loadedClass = 
componentClassLoader.loadClass(NON_EXISTENT_CLASS_NAME);
+        assertThat(loadedClass, sameInstance(CLASS_RETURNED_BY_OWNER));
+    }
+
+    // 
----------------------------------------------------------------------------------------------
+    // Resource loading
+    // 
----------------------------------------------------------------------------------------------
+
+    @Test
+    public void testComponentOnlyIsDefaultForResources() throws IOException {
+        TestUrlClassLoader owner = new TestUrlClassLoader();
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(new URL[0], owner, new String[0], new 
String[0]);
+
+        
assertThat(componentClassLoader.getResource(NON_EXISTENT_RESOURCE_NAME), 
nullValue());
+        assertThat(
+                
componentClassLoader.getResources(NON_EXISTENT_RESOURCE_NAME).hasMoreElements(),
+                is(false));
+    }
+
+    @Test
+    public void testOwnerFirstResourceFoundIgnoresComponent() {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(resourceToLoad, 
RESOURCE_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[] {}, owner, new String[] {resourceToLoad}, 
new String[0]);
+
+        final URL loadedResource = 
componentClassLoader.getResource(resourceToLoad);
+        assertThat(loadedResource, sameInstance(RESOURCE_RETURNED_BY_OWNER));
+    }
+
+    @Test
+    public void testOwnerFirstResourceNotFoundFallsBackToComponent() throws 
Exception {
+        TestUrlClassLoader owner = new TestUrlClassLoader();
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[] {TMP.getRoot().toURI().toURL()},
+                        owner,
+                        new String[] {resourceToLoad},
+                        new String[0]);
+
+        final URL loadedResource = 
componentClassLoader.getResource(resourceToLoad);
+        assertThat(loadedResource.toString(), containsString(resourceToLoad));
+    }
+
+    @Test
+    public void testComponentFirstResourceFoundIgnoresOwner() throws Exception 
{
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(resourceToLoad, 
RESOURCE_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(
+                        new URL[] {TMP.getRoot().toURI().toURL()},
+                        owner,
+                        new String[0],
+                        new String[] {resourceToLoad});
+
+        final URL loadedResource = 
componentClassLoader.getResource(resourceToLoad);
+        assertThat(loadedResource.toString(), containsString(resourceToLoad));
+    }
+
+    @Test
+    public void testComponentFirstResourceNotFoundFallsBackToOwner() {
+        TestUrlClassLoader owner =
+                new TestUrlClassLoader(NON_EXISTENT_RESOURCE_NAME, 
RESOURCE_RETURNED_BY_OWNER);
+
+        final ComponentClassLoader componentClassLoader =
+                new ComponentClassLoader(new URL[0], owner, new String[0], new 
String[] {"foo"});

Review comment:
       It should be `NON_EXISTENT_RESOURCE_NAME`, leftover from a prior version.




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