diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index d21e26e355..3053140dd7 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -121,6 +121,9 @@ public class CLI { return Integer.parseInt(p); } + /** + * Shuts down the channel and closes the underlying connection. + */ public void close() throws IOException, InterruptedException { channel.close(); channel.join(); diff --git a/remoting/src/main/java/hudson/remoting/Channel.java b/remoting/src/main/java/hudson/remoting/Channel.java index 6af7721553..94049f82b1 100644 --- a/remoting/src/main/java/hudson/remoting/Channel.java +++ b/remoting/src/main/java/hudson/remoting/Channel.java @@ -305,6 +305,10 @@ public class Channel implements VirtualChannel, IChannel { this(name,exec,mode,is,os,header,false); } + public Channel(String name, ExecutorService exec, Mode mode, InputStream is, OutputStream os, OutputStream header, boolean restricted) throws IOException { + this(name,exec,mode,is,os,header,restricted,null); + } + /** * Creates a new channel. * @@ -325,6 +329,12 @@ public class Channel implements VirtualChannel, IChannel { * the data goes into the "binary mode". This is useful * when the established communication channel might include some data that might * be useful for debugging/trouble-shooting. + * @param base + * Specify the classloader used for deserializing remote commands. + * This is primarily related to {@link #getRemoteProperty(Object)}. Sometimes two parties + * communicate over a channel and pass objects around as properties, but those types might not be + * visible from the classloader loading the {@link Channel} class. In such a case, specify a classloader + * so that those classes resolve. If null, {@code Channel.class.getClassLoader()} is used. * @param restricted * If true, this channel won't accept {@link Command}s that allow the remote end to execute arbitrary closures * --- instead they can only call methods on objects that are exported by this channel. @@ -334,15 +344,18 @@ public class Channel implements VirtualChannel, IChannel { * (provided that all the classes are already available in this JVM), so exactly how * safe the resulting behavior is is up to discussion. */ - public Channel(String name, ExecutorService exec, Mode mode, InputStream is, OutputStream os, OutputStream header, boolean restricted) throws IOException { - this(name,exec,mode,is,os,header,restricted,new Capability()); + public Channel(String name, ExecutorService exec, Mode mode, InputStream is, OutputStream os, OutputStream header, boolean restricted, ClassLoader base) throws IOException { + this(name,exec,mode,is,os,header,restricted,base,new Capability()); } - /*package*/ Channel(String name, ExecutorService exec, Mode mode, InputStream is, OutputStream os, OutputStream header, boolean restricted, Capability capability) throws IOException { + /*package*/ Channel(String name, ExecutorService exec, Mode mode, InputStream is, OutputStream os, OutputStream header, boolean restricted, ClassLoader base, Capability capability) throws IOException { this.name = name; this.executor = exec; this.isRestricted = restricted; + if (base==null) + base = getClass().getClassLoader(); + if(export(this,false)!=1) throw new AssertionError(); // export number 1 is reserved for the channel itself remoteChannel = RemoteInvocationHandler.wrap(this,1,IChannel.class,false,false); @@ -395,7 +408,7 @@ public class Channel implements VirtualChannel, IChannel { this.oos = oos; this.remoteCapability = cap; this.pipeWriter = createPipeWriter(); - this.ois = new ObjectInputStream(mode.wrap(is)); + this.ois = new ObjectInputStreamEx(mode.wrap(is),base); new ReaderThread(name).start(); return; diff --git a/remoting/src/test/java/hudson/remoting/ChannelRunner.java b/remoting/src/test/java/hudson/remoting/ChannelRunner.java index 1472b3c6b1..0be25f9466 100644 --- a/remoting/src/test/java/hudson/remoting/ChannelRunner.java +++ b/remoting/src/test/java/hudson/remoting/ChannelRunner.java @@ -70,7 +70,7 @@ interface ChannelRunner { Thread t = new Thread("south bridge runner") { public void run() { try { - Channel s = new Channel("south", executor, Mode.BINARY, in2, out1, null, false, createCapability()); + Channel s = new Channel("south", executor, Mode.BINARY, in2, out1, null, false, null, createCapability()); s.join(); System.out.println("south completed"); } catch (IOException e) { @@ -84,7 +84,7 @@ interface ChannelRunner { }; t.start(); - return new Channel("north", executor, Mode.BINARY, in1, out2, null, false, createCapability()); + return new Channel("north", executor, Mode.BINARY, in1, out2, null, false, null, createCapability()); } public void stop(Channel channel) throws Exception { diff --git a/test/pom.xml b/test/pom.xml index 0f103b011c..081a961691 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -176,9 +176,9 @@ THE SOFTWARE. 1.0-beta-1 - org.jvnet.hudson + org.jenkins-ci htmlunit - 2.6-hudson-2 + 2.6-jenkins-3 diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index 0fc1924775..fa55ebe2fe 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -120,9 +120,11 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -231,7 +233,7 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { /** * Remember {@link WebClient}s that are created, to release them properly. */ - private List> clients = new ArrayList>(); + private List clients = new ArrayList(); /** * JavaScript "debugger" that provides you information about the JavaScript call stack @@ -338,11 +340,10 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { protected void tearDown() throws Exception { try { // cancel pending asynchronous operations, although this doesn't really seem to be working - for (WeakReference client : clients) { - WebClient c = client.get(); - if(c==null) continue; + for (WebClient client : clients) { // unload the page to cancel asynchronous operations - c.getPage("about:blank"); + client.getPage("about:blank"); + client.closeAllWindows(); } clients.clear(); } finally { @@ -1432,7 +1433,7 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { // setJavaScriptEnabled(false); setPageCreator(HudsonPageCreator.INSTANCE); - clients.add(new WeakReference(this)); + clients.add(this); // make ajax calls run as post-action for predictable behaviors that simplify debugging setAjaxController(new AjaxController() { public boolean processSynchron(HtmlPage page, WebRequestSettings settings, boolean async) { diff --git a/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy b/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy index 8646eafb33..42f396419e 100644 --- a/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy +++ b/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy @@ -54,10 +54,16 @@ public class BuildCommandTest extends HudsonTestCase { }] as TestBuilder); // this should be asynchronous - assertEquals(0,new CLI(getURL()).execute(["build", p.name])) - started.block(); - assertTrue(p.getBuildByNumber(1).isBuilding()) - completed.signal(); + def cli = new CLI(getURL()) + try { + assertEquals(0,cli.execute(["build", p.name])) + started.block() + assertTrue(p.getBuildByNumber(1).isBuilding()) + completed.signal() + } finally { + cli.close(); + } + } /** @@ -67,16 +73,28 @@ public class BuildCommandTest extends HudsonTestCase { def p = createFreeStyleProject(); p.buildersList.add(new Shell("sleep 3")); - new CLI(getURL()).execute(["build","-s",p.name]) - assertFalse(p.getBuildByNumber(1).isBuilding()) + def cli = new CLI(getURL()) + try { + cli.execute(["build","-s",p.name]) + assertFalse(p.getBuildByNumber(1).isBuilding()) + } finally { + cli.close(); + } + } void testParameters() { def p = createFreeStyleProject(); p.addProperty(new ParametersDefinitionProperty([new StringParameterDefinition("key",null)])); - new CLI(getURL()).execute(["build","-s","-p","key=foobar",p.name]); - def b = assertBuildStatusSuccess(p.getBuildByNumber(1)); - assertEquals("foobar",b.getAction(ParametersAction.class).getParameter("key").value); + def cli = new CLI(getURL()) + try { + cli.execute(["build","-s","-p","key=foobar",p.name]) + def b = assertBuildStatusSuccess(p.getBuildByNumber(1)) + assertEquals("foobar",b.getAction(ParametersAction.class).getParameter("key").value) + } finally { + cli.close(); + }; + } } \ No newline at end of file diff --git a/test/src/test/groovy/hudson/cli/EnableJobCommandTest.groovy b/test/src/test/groovy/hudson/cli/EnableJobCommandTest.groovy index e45b98c5a4..479814cd72 100644 --- a/test/src/test/groovy/hudson/cli/EnableJobCommandTest.groovy +++ b/test/src/test/groovy/hudson/cli/EnableJobCommandTest.groovy @@ -36,10 +36,14 @@ public class EnableJobCommandTest extends HudsonTestCase { def cli = new CLI(getURL()) - cli.execute(["disable-job",p.name]) - assertTrue(p.disabled) - cli.execute(["enable-job",p.name]) - assertFalse(p.disabled) + try { + cli.execute(["disable-job",p.name]) + assertTrue(p.disabled) + cli.execute(["enable-job",p.name]) + assertFalse(p.disabled) + } finally { + cli.close(); + } } } \ No newline at end of file diff --git a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java index 20bfcddd81..c5f1915696 100644 --- a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java +++ b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java @@ -56,12 +56,17 @@ public class ItemListenerTest extends HudsonTestCase { public void testOnCreatedViaCLI() throws Exception { ByteArrayOutputStream buf = new ByteArrayOutputStream(); PrintStream out = new PrintStream(buf); - new CLI(getURL()).execute(Arrays.asList("create-job","testJob"), - new ByteArrayInputStream(("" - + "").getBytes()), - out, out); - out.flush(); - assertNotNull("job should be created: " + buf, hudson.getItem("testJob")); - assertEquals("onCreated event should be triggered: " + buf, "C", events.toString()); + CLI cli = new CLI(getURL()); + try { + cli.execute(Arrays.asList("create-job", "testJob"), + new ByteArrayInputStream(("" + + "").getBytes()), + out, out); + out.flush(); + assertNotNull("job should be created: " + buf, hudson.getItem("testJob")); + assertEquals("onCreated event should be triggered: " + buf, "C", events.toString()); + } finally { + cli.close(); + } } } diff --git a/test/src/test/java/hudson/security/CliAuthenticationTest.java b/test/src/test/java/hudson/security/CliAuthenticationTest.java index 6b7516f66b..c4f6ac281a 100644 --- a/test/src/test/java/hudson/security/CliAuthenticationTest.java +++ b/test/src/test/java/hudson/security/CliAuthenticationTest.java @@ -28,7 +28,12 @@ public class CliAuthenticationTest extends HudsonTestCase { } private int command(String... args) throws Exception { - return new CLI(getURL()).execute(args); + CLI cli = new CLI(getURL()); + try { + return cli.execute(args); + } finally { + cli.close(); + } } @TestExtension