1717
1818package org .apache .spark .sql .hive
1919
20+ import java .net .URL
21+
2022import org .apache .hadoop .hive .conf .HiveConf .ConfVars
2123
24+ import org .apache .spark .SparkConf
25+ import org .apache .spark .deploy .SparkHadoopUtil
2226import org .apache .spark .sql .QueryTest
2327import org .apache .spark .sql .hive .test .TestHiveSingleton
2428import org .apache .spark .sql .test .SQLTestUtils
29+ import org .apache .spark .util .{ChildFirstURLClassLoader , MutableURLClassLoader }
2530
2631class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
2732
@@ -42,4 +47,31 @@ class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton
4247 assert(hiveConf(" foo" ) === " bar" )
4348 }
4449 }
50+
51+ test(" ChildFirstURLClassLoader's parent is null" ) {
52+ val conf = new SparkConf
53+ val contextClassLoader = Thread .currentThread().getContextClassLoader
54+ val loader = new FakeChildFirstURLClassLoader (Array (), contextClassLoader)
55+ Thread .currentThread().setContextClassLoader(loader)
56+ intercept[IllegalArgumentException ](
57+ HiveUtils .newClientForMetadata(conf, SparkHadoopUtil .newConfiguration(conf)))
58+ Thread .currentThread().setContextClassLoader(contextClassLoader)
59+ }
60+
61+ test(" ChildFirstURLClassLoader's parent is null, get spark classloader instead" ) {
62+ val conf = new SparkConf
63+ val contextClassLoader = Thread .currentThread().getContextClassLoader
64+ val loader = new ChildFirstURLClassLoader (Array (), contextClassLoader)
65+ Thread .currentThread().setContextClassLoader(loader)
66+ HiveUtils .newClientForMetadata(conf, SparkHadoopUtil .newConfiguration(conf))
67+ Thread .currentThread().setContextClassLoader(contextClassLoader)
68+ }
4569}
70+
71+ /**
72+ * A Fake [[ChildFirstURLClassLoader ]] used for test
73+ * @param urls
74+ * @param parent
75+ */
76+ class FakeChildFirstURLClassLoader (urls : Array [URL ], parent : ClassLoader )
77+ extends MutableURLClassLoader (urls, null )
0 commit comments