Skip to content

Commit ebc07b6

Browse files
committed
Merge pull request #2111 from jruby/add-testcases-to-detect-root-classloader
when jruby is not coming from the thread.currentThread.contextClassLoade...
2 parents 9707f39 + 9536731 commit ebc07b6

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.jruby.embed;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.fail;
6+
7+
import java.net.URL;
8+
import java.net.URLClassLoader;
9+
10+
import javax.script.ScriptEngine;
11+
import javax.script.ScriptEngineManager;
12+
13+
import org.jruby.embed.jsr223.JRubyEngineFactory;
14+
import org.junit.AfterClass;
15+
import org.junit.BeforeClass;
16+
import org.junit.FixMethodOrder;
17+
import org.junit.Test;
18+
import org.junit.runners.MethodSorters;
19+
20+
@FixMethodOrder(MethodSorters.NAME_ASCENDING) // we need the last test method to run last
21+
public class ParentClassLoaderTest {
22+
23+
static ClassLoader cl;
24+
25+
@BeforeClass
26+
public static void setupClassLoader() {
27+
cl = Thread.currentThread().getContextClassLoader();
28+
// make sure we have classloader which does not find jruby
29+
ClassLoader c = new URLClassLoader( new URL[] {}, null );
30+
try {
31+
c.loadClass( "org.jruby.embed.ScriptingContainer" );
32+
fail( "this classloader shall not find jruby" );
33+
}
34+
catch( ClassNotFoundException expected){}
35+
// set it as context classloader
36+
Thread.currentThread().setContextClassLoader( c );
37+
}
38+
39+
@AfterClass
40+
public static void restClassLoader() {
41+
Thread.currentThread().setContextClassLoader( cl );
42+
}
43+
44+
@Test
45+
public void test1ScriptingContainer() throws Exception {
46+
try {
47+
// we do have an instance of "jruby" loaded via some other classloader
48+
ScriptingContainer instance = new ScriptingContainer(LocalContextScope.CONCURRENT);
49+
instance.runScriptlet( "$LOAD_PATH" ).toString();
50+
fail("should not come here");
51+
}
52+
catch(Exception e){
53+
//expected
54+
}
55+
}
56+
57+
@Test
58+
public void test2JRubyEngineFactoryWithWrongPropertyNameCanFail() throws Exception {
59+
try {
60+
System.setProperty(PropertyName.CLASSLOADER.toString(), "something");
61+
// we do have an instance of "jruby" loaded via some other classloader
62+
ScriptEngineManager m = new ScriptEngineManager();
63+
m.registerEngineName( "jruby", new JRubyEngineFactory() );
64+
ScriptEngine jruby = m.getEngineByName("jruby");
65+
jruby.eval("$LOAD_PATH" ).toString();
66+
fail("should not come here");
67+
}
68+
catch(Exception e){
69+
//expected
70+
}
71+
}
72+
73+
@Test
74+
public void test3JRubyEngineFactoryWithNoneClassloaderPropertyNameCanFail() throws Exception {
75+
try {
76+
System.setProperty(PropertyName.CLASSLOADER.toString(), "none");
77+
// we do have an instance of "jruby" loaded via some other classloader
78+
ScriptEngineManager m = new ScriptEngineManager();
79+
m.registerEngineName( "jruby", new JRubyEngineFactory() );
80+
ScriptEngine jruby = m.getEngineByName("jruby");
81+
jruby.eval("$LOAD_PATH" ).toString();
82+
fail("should not come here");
83+
}
84+
catch(Exception e){
85+
//expected
86+
}
87+
}
88+
89+
@Test
90+
// this test needs to be the last since JRubyEngineFactory uses ScriptingContainer with singleton
91+
// context scope, i.e. once the classloader is setup correctly it remains as it is
92+
public void test4JRubyEngineFactory() throws Exception {
93+
System.getProperties().remove(PropertyName.CLASSLOADER.toString());
94+
// we do have an instance of "jruby" loaded via some other classloader
95+
ScriptEngineManager m = new ScriptEngineManager();
96+
m.registerEngineName( "jruby", new JRubyEngineFactory() );
97+
ScriptEngine jruby = m.getEngineByName("jruby");
98+
String result = jruby.eval("$LOAD_PATH" ).toString();
99+
assertNotNull(result);
100+
101+
assertEquals(jruby.eval("JRuby.runtime.jruby_class_loader.parent" ), cl );
102+
}
103+
104+
}

0 commit comments

Comments
 (0)