|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.executor |
| 19 | + |
| 20 | +import java.io.File |
| 21 | +import java.net.URLClassLoader |
| 22 | + |
| 23 | +import org.scalatest.FunSuite |
| 24 | + |
| 25 | +import org.apache.spark.TestUtils |
| 26 | + |
| 27 | +class ExecutorURLClassLoaderSuite extends FunSuite { |
| 28 | + |
| 29 | + val childClassNames = List("FakeClass1", "FakeClass2") |
| 30 | + val parentClassNames = List("FakeClass1", "FakeClass2", "FakeClass3") |
| 31 | + val urls = List(TestUtils.createJarWithClasses(childClassNames, "1")).toArray |
| 32 | + val urls2 = List(TestUtils.createJarWithClasses(parentClassNames, "2")).toArray |
| 33 | + |
| 34 | + test("child first") { |
| 35 | + val parentLoader = new URLClassLoader(urls2, null) |
| 36 | + val classLoader = new ChildExecutorURLClassLoader(urls, parentLoader) |
| 37 | + val fakeClass = classLoader.loadClass("FakeClass2").newInstance() |
| 38 | + val fakeClassVersion = fakeClass.toString |
| 39 | + assert(fakeClassVersion === "1") |
| 40 | + } |
| 41 | + |
| 42 | + test("parent first") { |
| 43 | + val parentLoader = new URLClassLoader(urls2, null) |
| 44 | + val classLoader = new ExecutorURLClassLoader(urls, parentLoader) |
| 45 | + val fakeClass = classLoader.loadClass("FakeClass1").newInstance() |
| 46 | + val fakeClassVersion = fakeClass.toString |
| 47 | + assert(fakeClassVersion === "2") |
| 48 | + } |
| 49 | + |
| 50 | + test("child first can fall back") { |
| 51 | + val parentLoader = new URLClassLoader(urls2, null) |
| 52 | + val classLoader = new ChildExecutorURLClassLoader(urls, parentLoader) |
| 53 | + val fakeClass = classLoader.loadClass("FakeClass3").newInstance() |
| 54 | + val fakeClassVersion = fakeClass.toString |
| 55 | + assert(fakeClassVersion === "2") |
| 56 | + } |
| 57 | + |
| 58 | + test("child first can fail") { |
| 59 | + val parentLoader = new URLClassLoader(urls2, null) |
| 60 | + val classLoader = new ChildExecutorURLClassLoader(urls, parentLoader) |
| 61 | + intercept[java.lang.ClassNotFoundException] { |
| 62 | + classLoader.loadClass("FakeClassDoesNotExist").newInstance() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +} |
0 commit comments