From bae8b35c8f20add38e61abe5a1bc0dfae0083b82 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 6 Nov 2009 23:22:44 +0000 Subject: [PATCH] refactored the commonality out git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23552 71c3de6d-444a-0410-be80-ed276b4c234a --- .../test/java/hudson/AbstractChannelTest.java | 56 +++++++++++++++++++ core/src/test/java/hudson/FilePathTest.java | 37 +----------- core/src/test/java/hudson/LauncherTest.java | 42 ++------------ 3 files changed, 62 insertions(+), 73 deletions(-) create mode 100644 core/src/test/java/hudson/AbstractChannelTest.java diff --git a/core/src/test/java/hudson/AbstractChannelTest.java b/core/src/test/java/hudson/AbstractChannelTest.java new file mode 100644 index 0000000000..6d6a99ea0e --- /dev/null +++ b/core/src/test/java/hudson/AbstractChannelTest.java @@ -0,0 +1,56 @@ +package hudson; + +import junit.framework.TestCase; + +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.util.concurrent.Future; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import hudson.remoting.Channel; + +/** + * Test that uses a connected channel. + * + * @author Kohsuke Kawaguchi + */ +public class AbstractChannelTest extends TestCase { + /** + * Two channels that are connected to each other, but shares the same classloader. + */ + protected Channel french; + protected Channel british; + private ExecutorService executors = Executors.newCachedThreadPool(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + final PipedInputStream p1i = new PipedInputStream(); + final PipedInputStream p2i = new PipedInputStream(); + final PipedOutputStream p1o = new PipedOutputStream(p1i); + final PipedOutputStream p2o = new PipedOutputStream(p2i); + + Future f1 = executors.submit(new Callable() { + public Channel call() throws Exception { + return new Channel("This side of the channel", executors, p1i, p2o); + } + }); + Future f2 = executors.submit(new Callable() { + public Channel call() throws Exception { + return new Channel("The other side of the channel", executors, p2i, p1o); + } + }); + french = f1.get(); + british = f2.get(); + } + + @Override + protected void tearDown() throws Exception { + french.close(); // this will automatically initiate the close on the other channel, too. + french.join(); + british.join(); + executors.shutdown(); + } +} diff --git a/core/src/test/java/hudson/FilePathTest.java b/core/src/test/java/hudson/FilePathTest.java index 297e44daae..384e877fab 100644 --- a/core/src/test/java/hudson/FilePathTest.java +++ b/core/src/test/java/hudson/FilePathTest.java @@ -40,42 +40,7 @@ import org.apache.commons.io.output.NullOutputStream; /** * @author Kohsuke Kawaguchi */ -public class FilePathTest extends TestCase { - /** - * Two channels that are connected to each other, but shares the same classloader. - */ - private Channel french, british; - private ExecutorService executors = Executors.newCachedThreadPool(); - - @Override - protected void setUp() throws Exception { - super.setUp(); - final PipedInputStream p1i = new PipedInputStream(); - final PipedInputStream p2i = new PipedInputStream(); - final PipedOutputStream p1o = new PipedOutputStream(p1i); - final PipedOutputStream p2o = new PipedOutputStream(p2i); - - Future f1 = executors.submit(new Callable() { - public Channel call() throws Exception { - return new Channel("This side of the channel", executors, p1i, p2o); - } - }); - Future f2 = executors.submit(new Callable() { - public Channel call() throws Exception { - return new Channel("The other side of the channel", executors, p2i, p1o); - } - }); - french = f1.get(); - british = f2.get(); - } - - @Override - protected void tearDown() throws Exception { - french.close(); // this will automatically initiate the close on the other channel, too. - french.join(); - british.join(); - executors.shutdown(); - } +public class FilePathTest extends AbstractChannelTest { public void testCopyTo() throws Exception { File tmp = File.createTempFile("testCopyTo",""); diff --git a/core/src/test/java/hudson/LauncherTest.java b/core/src/test/java/hudson/LauncherTest.java index 3aa818fef8..10fd082bb5 100644 --- a/core/src/test/java/hudson/LauncherTest.java +++ b/core/src/test/java/hudson/LauncherTest.java @@ -24,27 +24,14 @@ package hudson; -import hudson.remoting.Channel; -import hudson.util.StreamTaskListener; import hudson.util.ProcessTree; - -import java.io.File; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.Callable; - -import junit.framework.TestCase; -import org.apache.commons.io.IOUtils; +import hudson.util.StreamTaskListener; +import hudson.remoting.Callable; import org.apache.commons.io.FileUtils; -public class LauncherTest extends TestCase { - public LauncherTest(String n) { - super(n); - } +import java.io.File; +public class LauncherTest extends AbstractChannelTest { //@Bug(4611) public void testRemoteKill() throws Exception { if (File.pathSeparatorChar != ':') { @@ -52,25 +39,6 @@ public class LauncherTest extends TestCase { return; } - // XXX setup stolen from FilePathTest; should it be refactored into common test utility? - final ExecutorService executors = Executors.newCachedThreadPool(); - final PipedInputStream p1i = new PipedInputStream(); - final PipedInputStream p2i = new PipedInputStream(); - final PipedOutputStream p1o = new PipedOutputStream(p1i); - final PipedOutputStream p2o = new PipedOutputStream(p2i); - Future f1 = executors.submit(new Callable() { - public Channel call() throws Exception { - return new Channel("This side of the channel", executors, p1i, p2o); - } - }); - Future f2 = executors.submit(new Callable() { - public Channel call() throws Exception { - return new Channel("The other side of the channel", executors, p2i, p1o); - } - }); - Channel french = f1.get(); - Channel british = f2.get(); - File tmp = File.createTempFile("testRemoteKill", ""); tmp.delete(); FilePath f = new FilePath(french, tmp.getPath()); @@ -95,7 +63,7 @@ public class LauncherTest extends TestCase { // hangs and on slave machine pgrep sleep => one process; after manual kill, script returns. } - private static final hudson.remoting.Callable NOOP = new hudson.remoting.Callable() { + private static final Callable NOOP = new Callable() { public Object call() throws Exception { return null; }