@@ -43,11 +43,20 @@ private[spark] object TestUtils {
4343 * Note: if this is used during class loader tests, class names should be unique
4444 * in order to avoid interference between tests.
4545 */
46- def createJarWithClasses (classNames : Seq [String ], value : String = " " ): URL = {
46+ def createJarWithClasses (
47+ classNames : Seq [String ],
48+ toStringValue : String = " " ,
49+ classNamesWithBase : Seq [(String , String )] = Seq (),
50+ classpathUrls : Seq [URL ] = Seq ()): URL = {
4751 val tempDir = Utils .createTempDir()
48- val files = for (name <- classNames) yield createCompiledClass(name, tempDir, value)
52+ val files1 = for (name <- classNames) yield {
53+ createCompiledClass(name, tempDir, toStringValue, classpathUrls = classpathUrls)
54+ }
55+ val files2 = for ((childName, baseName) <- classNamesWithBase) yield {
56+ createCompiledClass(childName, tempDir, toStringValue, baseName, classpathUrls)
57+ }
4958 val jarFile = new File (tempDir, " testJar-%s.jar" .format(System .currentTimeMillis()))
50- createJar(files , jarFile)
59+ createJar(files1 ++ files2 , jarFile)
5160 }
5261
5362
@@ -85,15 +94,26 @@ private[spark] object TestUtils {
8594 }
8695
8796 /** Creates a compiled class with the given name. Class file will be placed in destDir. */
88- def createCompiledClass (className : String , destDir : File , value : String = " " ): File = {
97+ def createCompiledClass (
98+ className : String ,
99+ destDir : File ,
100+ toStringValue : String = " " ,
101+ baseClass : String = null ,
102+ classpathUrls : Seq [URL ] = Seq ()): File = {
89103 val compiler = ToolProvider .getSystemJavaCompiler
104+ val extendsText = Option (baseClass).map { c => s " extends ${c}" }.getOrElse(" " )
90105 val sourceFile = new JavaSourceFromString (className,
91- " public class " + className + " implements java.io.Serializable {" +
92- " @Override public String toString() { return \" " + value + " \" ; }}" )
106+ " public class " + className + extendsText + " implements java.io.Serializable {" +
107+ " @Override public String toString() { return \" " + toStringValue + " \" ; }}" )
93108
94109 // Calling this outputs a class file in pwd. It's easier to just rename the file than
95110 // build a custom FileManager that controls the output location.
96- compiler.getTask(null , null , null , null , null , Seq (sourceFile)).call()
111+ val options = if (classpathUrls.nonEmpty) {
112+ Seq (" -classpath" , classpathUrls.map { _.getFile }.mkString(File .pathSeparator))
113+ } else {
114+ Seq ()
115+ }
116+ compiler.getTask(null , null , null , options, null , Seq (sourceFile)).call()
97117
98118 val fileName = className + " .class"
99119 val result = new File (fileName)
0 commit comments