Skip to content

Commit f7c6213

Browse files
committed
tests for fancy classloader configurations
1 parent f94b224 commit f7c6213

File tree

9 files changed

+137
-0
lines changed

9 files changed

+137
-0
lines changed

test/files/run/t6240a.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
StepTwo.type

test/files/run/t6240a/StepOne.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.lang.ClassNotFoundException;
4+
import java.lang.NoSuchMethodException;
5+
import java.lang.IllegalAccessException;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.net.URL;
9+
import java.net.URLClassLoader;
10+
import java.net.MalformedURLException;
11+
12+
public class StepOne {
13+
public static void main(String[] args)
14+
throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
15+
String[] launchPaths = System.getProperty("launch.classpath").split(":");
16+
17+
// move away StepThree
18+
File tempDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
19+
System.setProperty("launch.step.three", tempDir.getAbsolutePath());
20+
tempDir.delete();
21+
tempDir.mkdir();
22+
File[] testClasses = new File(launchPaths[0]).listFiles();
23+
for (int i = 0; i < testClasses.length; i++) {
24+
File testClass = testClasses[i];
25+
if (testClass.getPath().contains("StepThree")) {
26+
File testClassMoved = new File(tempDir.getAbsolutePath() + "/" + testClass.getName());
27+
testClass.renameTo(testClassMoved);
28+
}
29+
}
30+
31+
// launch StepTwo
32+
URL[] launchURLs = new URL[launchPaths.length];
33+
for (int i = 0; i < launchPaths.length; i++) {
34+
launchURLs[i] = new File(launchPaths[i]).toURL();
35+
}
36+
URLClassLoader classLoader = new URLClassLoader(launchURLs, Object.class.getClassLoader());
37+
Class<?> stepTwo = classLoader.loadClass("StepTwo");
38+
Method main = stepTwo.getDeclaredMethod("main", String[].class);
39+
main.invoke(null, (Object)(new String[]{}));
40+
}
41+
}

test/files/run/t6240a/StepTwo.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import java.io.File
2+
import java.net.URLClassLoader
3+
4+
object StepTwo extends App {
5+
import scala.reflect.runtime.universe._
6+
println(typeOf[StepTwo.type])
7+
}

test/files/run/t6240a/Test.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.io.File
2+
import scala.sys.process._
3+
4+
object Test extends App {
5+
def prop(key: String) = {
6+
val value = System.getProperties.getProperty(key)
7+
assert(value != null, key)
8+
value
9+
}
10+
val testClassesDir = prop("partest.output")
11+
assert(new File(testClassesDir).exists, testClassesDir)
12+
val fullTestClassesClasspath = testClassesDir + prop("path.separator") + prop("java.class.path")
13+
val javaBinary = if (new File(prop("javacmd")).isAbsolute) prop("javacmd") else prop("java.home") + "/bin/" + prop("javacmd")
14+
assert(new File(javaBinary).exists, javaBinary)
15+
List(javaBinary, "-cp", testClassesDir, "-Dlaunch.classpath=" + fullTestClassesClasspath, "StepOne").!
16+
}

test/files/run/t6240b.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
StepThree.type

test/files/run/t6240b/StepOne.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.lang.ClassNotFoundException;
4+
import java.lang.NoSuchMethodException;
5+
import java.lang.IllegalAccessException;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.net.URL;
9+
import java.net.URLClassLoader;
10+
import java.net.MalformedURLException;
11+
12+
public class StepOne {
13+
public static void main(String[] args)
14+
throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
15+
String[] launchPaths = System.getProperty("launch.classpath").split(":");
16+
17+
// move away StepThree
18+
File tempDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
19+
System.setProperty("launch.step.three", tempDir.getAbsolutePath());
20+
tempDir.delete();
21+
tempDir.mkdir();
22+
File[] testClasses = new File(launchPaths[0]).listFiles();
23+
for (int i = 0; i < testClasses.length; i++) {
24+
File testClass = testClasses[i];
25+
if (testClass.getPath().contains("StepThree")) {
26+
File testClassMoved = new File(tempDir.getAbsolutePath() + "/" + testClass.getName());
27+
testClass.renameTo(testClassMoved);
28+
}
29+
}
30+
31+
// launch StepTwo
32+
URL[] launchURLs = new URL[launchPaths.length];
33+
for (int i = 0; i < launchPaths.length; i++) {
34+
launchURLs[i] = new File(launchPaths[i]).toURL();
35+
}
36+
URLClassLoader classLoader = new URLClassLoader(launchURLs, Object.class.getClassLoader());
37+
Class<?> stepTwo = classLoader.loadClass("StepTwo");
38+
Method main = stepTwo.getDeclaredMethod("main", String[].class);
39+
main.invoke(null, (Object)(new String[]{}));
40+
}
41+
}

test/files/run/t6240b/StepThree.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object StepThree extends App {
2+
import scala.reflect.runtime.universe._
3+
println(typeOf[StepThree.type])
4+
}

test/files/run/t6240b/StepTwo.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.io.File
2+
import java.net.URLClassLoader
3+
4+
object StepTwo extends App {
5+
val classes = new File(System.getProperty("launch.step.three"))
6+
val cl = new URLClassLoader(Array(classes.toURI.toURL), getClass.getClassLoader)
7+
val stepThree = cl.loadClass("StepThree")
8+
val main = stepThree.getDeclaredMethod("main", classOf[Array[String]])
9+
main.invoke(null, Array[String]())
10+
}

test/files/run/t6240b/Test.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.io.File
2+
import scala.sys.process._
3+
4+
object Test extends App {
5+
def prop(key: String) = {
6+
val value = System.getProperties.getProperty(key)
7+
assert(value != null, key)
8+
value
9+
}
10+
val testClassesDir = prop("partest.output")
11+
assert(new File(testClassesDir).exists, testClassesDir)
12+
val fullTestClassesClasspath = testClassesDir + prop("path.separator") + prop("java.class.path")
13+
val javaBinary = if (new File(prop("javacmd")).isAbsolute) prop("javacmd") else prop("java.home") + "/bin/" + prop("javacmd")
14+
assert(new File(javaBinary).exists, javaBinary)
15+
List(javaBinary, "-cp", testClassesDir, "-Dlaunch.classpath=" + fullTestClassesClasspath, "StepOne").!
16+
}

0 commit comments

Comments
 (0)