Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c694f1d8d171
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8df95d6ad785
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 12, 2014

  1. Copy the full SHA
    f014b03 View commit details
  2. * added a test case where thread-context-classloader did not load jru…

    …by and ScriptingContainer just works
    
    * added same test case for JRubyEngineFactory which works without commit b0a9744
    * added two more edge cases which would fail normally for JRubyEngineFactory
    mkristian committed Nov 12, 2014
    Copy the full SHA
    8df95d6 View commit details
Showing with 95 additions and 2 deletions.
  1. +3 −2 core/src/main/java/org/jruby/RubyInstanceConfig.java
  2. +92 −0 core/src/test/java/org/jruby/embed/ParentClassLoaderTest.java
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -1413,8 +1413,9 @@ public void setProfilingService( String service ) {
private ProfileOutput profileOutput = new ProfileOutput(System.err);
private String profilingService;

private ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
private ClassLoader loader = contextLoader == null ? RubyInstanceConfig.class.getClassLoader() : contextLoader;
private ClassLoader thisLoader = RubyInstanceConfig.class.getClassLoader();
// thisLoader can be null for example when jruby comes from the boot-classLoader
private ClassLoader loader = thisLoader == null ? Thread.currentThread().getContextClassLoader() : thisLoader;

// from CommandlineParser
private List<String> loadPaths = new ArrayList<String>();
92 changes: 92 additions & 0 deletions core/src/test/java/org/jruby/embed/ParentClassLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.jruby.embed;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.net.URL;
import java.net.URLClassLoader;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.jruby.embed.jsr223.JRubyEngineFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING) // we need the last test method to run last
public class ParentClassLoaderTest {

static ClassLoader cl;

@BeforeClass
public static void setupClassLoader() {
cl = Thread.currentThread().getContextClassLoader();
// make sure we have classloader which does not find jruby
ClassLoader c = new URLClassLoader( new URL[] {}, null );
try {
c.loadClass( "org.jruby.embed.ScriptingContainer" );
fail( "this classloader shall not find jruby" );
}
catch( ClassNotFoundException expected){}
// set it as context classloader
Thread.currentThread().setContextClassLoader( c );
}

@AfterClass
public static void restClassLoader() {
Thread.currentThread().setContextClassLoader( cl );
}

@Test
public void test1ScriptingContainer() throws Exception {
// we do have an instance of "jruby" loaded via some other classloader
ScriptingContainer instance = new ScriptingContainer();
String result = instance.runScriptlet( "$LOAD_PATH" ).toString();
assertNotNull(result);

assertEquals(instance.runScriptlet("JRuby.runtime.jruby_class_loader.parent" ), cl );
assertEquals(ScriptingContainer.class.getClassLoader(), cl);
}

@Test
public void test2JRubyEngineFactoryWithWrongPropertyName() throws Exception {
System.setProperty( PropertyName.CLASSLOADER.toString(), "something");
// we do have an instance of "jruby" loaded via some other classloader
ScriptEngineManager m = new ScriptEngineManager();
m.registerEngineName( "jruby", new JRubyEngineFactory() );
ScriptEngine jruby = m.getEngineByName("jruby");
String result = jruby.eval("$LOAD_PATH" ).toString();
assertNotNull(result);

assertEquals(jruby.eval("JRuby.runtime.jruby_class_loader.parent" ), cl );
}

@Test
public void test3JRubyEngineFactoryWithNoneClassloaderPropertyName() throws Exception {
System.setProperty( PropertyName.CLASSLOADER.toString(), "none");
// we do have an instance of "jruby" loaded via some other classloader
ScriptEngineManager m = new ScriptEngineManager();
m.registerEngineName( "jruby", new JRubyEngineFactory() );
ScriptEngine jruby = m.getEngineByName("jruby");
String result = jruby.eval("$LOAD_PATH" ).toString();
assertNotNull(result);

assertEquals(jruby.eval("JRuby.runtime.jruby_class_loader.parent" ), cl );
}

@Test
public void test4JRubyEngineFactory() throws Exception {
// we do have an instance of "jruby" loaded via some other classloader
ScriptEngineManager m = new ScriptEngineManager();
m.registerEngineName( "jruby", new JRubyEngineFactory() );
ScriptEngine jruby = m.getEngineByName("jruby");
String result = jruby.eval("$LOAD_PATH" ).toString();
assertNotNull(result);

assertEquals(jruby.eval("JRuby.runtime.jruby_class_loader.parent" ), cl );
}
}