From 9f68f10b9c4aca8de5a1c9f071d416a3dbbf9bd8 Mon Sep 17 00:00:00 2001 From: Holden Karau Date: Mon, 7 Apr 2014 22:45:22 -0700 Subject: [PATCH] Start rewriting the ExecutorURLClassLoaderSuite to not use the hard coded classes --- .../scala/org/apache/spark/TestUtils.scala | 32 +++++++++++++++++++ .../ExecutorURLClassLoaderSuite.scala | 9 ++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/src/test/scala/org/apache/spark/TestUtils.scala b/core/src/test/scala/org/apache/spark/TestUtils.scala index 1611d09652d40..de7b2a1cb47dd 100644 --- a/core/src/test/scala/org/apache/spark/TestUtils.scala +++ b/core/src/test/scala/org/apache/spark/TestUtils.scala @@ -41,6 +41,20 @@ object TestUtils { createJar(files, jarFile) } + /** + * Create a jar that defines classes with the given names. + * + * Note: if this is used during class loader tests, class names should be unique + * in order to avoid interference between tests. + */ + def createJarWithClassesAndValue(classNames: Seq[String], value: Integer): URL = { + val tempDir = Files.createTempDir() + val files = for (name <- classNames) yield createCompiledClassWithValue(name, value, tempDir) + val jarFile = new File(tempDir, "testJar-%s.jar".format(System.currentTimeMillis())) + createJar(files, jarFile) + } + + /** * Create a jar file that contains this set of files. All files will be located at the root * of the jar. @@ -95,4 +109,22 @@ object TestUtils { result.renameTo(out) out } + + /** Creates a compiled class with the given name. Class file will be placed in destDir. */ + def createCompiledClassWithValue(className: String, value: Integer, destDir: File): File = { + val compiler = ToolProvider.getSystemJavaCompiler + val sourceFile = new JavaSourceFromString(className, + "public class " + className + " { override String toString() { return \"" + value + "\";}}") + + // Calling this outputs a class file in pwd. It's easier to just rename the file than + // build a custom FileManager that controls the output location. + compiler.getTask(null, null, null, null, null, Seq(sourceFile)).call() + + val fileName = className + ".class" + val result = new File(fileName) + if (!result.exists()) throw new Exception("Compiled file not found: " + fileName) + val out = new File(destDir, fileName) + result.renameTo(out) + out + } } diff --git a/core/src/test/scala/org/apache/spark/executor/ExecutorURLClassLoaderSuite.scala b/core/src/test/scala/org/apache/spark/executor/ExecutorURLClassLoaderSuite.scala index c030a1bbda268..90826fa2e082d 100644 --- a/core/src/test/scala/org/apache/spark/executor/ExecutorURLClassLoaderSuite.scala +++ b/core/src/test/scala/org/apache/spark/executor/ExecutorURLClassLoaderSuite.scala @@ -22,11 +22,14 @@ import java.net.URLClassLoader import org.scalatest.FunSuite +import org.apache.spark.TestUtils + class ExecutorURLClassLoaderSuite extends FunSuite { - val spark_home = sys.env.get("SPARK_HOME").orElse(sys.props.get("spark.home")).get - val urls = List(new File(spark_home + "/core/src/test/resources/fake-spark-class.jar").toURI.toURL).toArray - val urls2 = List(new File(spark_home + "/core/src/test/resources/fake-spark-class-2.jar").toURI.toURL).toArray + val classNames = List("org.apache.spark.test.FakeClass1", + "org.apache.spark.test.FakeClass2") + val urls = List(TestUtils.createJarWithClassesAndValue(classNames, 1)).toArray + val urls2 = List(TestUtils.createJarWithClassesAndValue(classNames, 2)).toArray test("child first") { val parentLoader = new URLClassLoader(urls2, null)