You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/10/16 23:19:13 UTC

[5/8] [SSHD-360] Rework CloseableUtils code

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
index 1774841..931da8d 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -98,6 +98,7 @@ import static org.junit.Assert.assertTrue;
 public class ClientTest extends BaseTest {
 
     private SshServer sshd;
+    private SshClient client;
     private int port;
     private CountDownLatch authLatch;
     private CountDownLatch channelLatch;
@@ -158,14 +159,19 @@ public class ClientTest extends BaseTest {
                 },
                 new TcpipServerChannel.DirectTcpipFactory()));
         sshd.start();
+
+        client = SshClient.setUpDefaultClient();
     }
 
     @After
     public void tearDown() throws Exception {
         if (sshd != null) {
             sshd.stop(true);
-            Thread.sleep(50);
         }
+        if (client != null) {
+            client.stop();
+        }
+        Thread.sleep(50);
     }
 
     @Test
@@ -173,7 +179,6 @@ public class ClientTest extends BaseTest {
         sshd.getProperties().put(SshServer.WINDOW_SIZE, "1024");
         sshd.setShellFactory(new AsyncEchoShellFactory());
 
-        SshClient client = SshClient.setUpDefaultClient();
         client.getProperties().put(SshClient.WINDOW_SIZE, "1024");
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -252,11 +257,12 @@ public class ClientTest extends BaseTest {
         channel.waitFor(ClientChannel.CLOSED, 0);
 
         assertEquals(nbMessages * message.length, baosOut.size());
+
+        client.close(true);
     }
 
     @Test
     public void testCommandDeadlock() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await().isSuccess();
@@ -280,7 +286,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testClient() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await().isSuccess();
@@ -319,7 +324,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testClientInverted() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await().isSuccess();
@@ -357,7 +361,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testClientWithCustomChannel() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
         session.addPasswordIdentity("smx");
@@ -379,7 +382,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testClientClosingStream() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await().isSuccess();
@@ -418,7 +420,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testClientWithLengthyDialog() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         // Reduce window size and packet size
 //        client.getProperties().put(SshClient.WINDOW_SIZE, Integer.toString(0x20000));
 //        client.getProperties().put(SshClient.MAX_PACKET_SIZE, Integer.toString(0x1000));
@@ -470,7 +471,6 @@ public class ClientTest extends BaseTest {
 
     @Test(expected = SshException.class)
     public void testOpenChannelOnClosedSession() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await().isSuccess();
@@ -489,7 +489,6 @@ public class ClientTest extends BaseTest {
     @Test
     public void testCloseBeforeAuthSucceed() throws Exception {
         authLatch = new CountDownLatch(1);
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         AuthFuture authFuture = session.authPassword("smx", "smx");
@@ -503,7 +502,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testCloseCleanBeforeChannelOpened() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await();
@@ -522,7 +520,6 @@ public class ClientTest extends BaseTest {
     @Test
     public void testCloseImmediateBeforeChannelOpened() throws Exception {
         channelLatch = new CountDownLatch(1);
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
         session.authPassword("smx", "smx").await();
@@ -541,7 +538,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testPublicKeyAuth() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.start();
         ClientSession session = client.connect("localhost", port).await().getSession();
 
@@ -552,7 +548,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testPublicKeyAuthNew() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPublicKey.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -568,7 +563,6 @@ public class ClientTest extends BaseTest {
                 return key.equals(pair.getPublic());
             }
         });
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPublicKey.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -579,7 +573,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testPasswordAuthNew() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPassword.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -589,7 +582,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testPasswordAuthNewWithFailureOnFirstIdentity() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPassword.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -600,7 +592,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testKeyboardInteractiveAuthNew() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthKeyboardInteractive.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -610,7 +601,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testKeyboardInteractiveAuthNewWithFailureOnFirstIdentity() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthKeyboardInteractive.Factory()));
         client.start();
         ClientSession session = client.connect("smx", "localhost", port).await().getSession();
@@ -645,7 +635,6 @@ public class ClientTest extends BaseTest {
     @Test
     public void testKeyboardInteractiveInSessionUserInteractive() throws Exception {
         final AtomicInteger count = new AtomicInteger();
-        SshClient client = SshClient.setUpDefaultClient();
         client.getProperties().put(ClientFactoryManager.PASSWORD_PROMPTS, "3");
         client.setUserAuthFactories(Arrays
                         .<NamedFactory<UserAuth>> asList(new UserAuthKeyboardInteractive.Factory()));
@@ -671,7 +660,6 @@ public class ClientTest extends BaseTest {
     @Test
     public void testKeyboardInteractiveInSessionUserInteractiveFailure() throws Exception {
         final AtomicInteger count = new AtomicInteger();
-        SshClient client = SshClient.setUpDefaultClient();
         client.getProperties().put(ClientFactoryManager.PASSWORD_PROMPTS, "3");
         client.setUserAuthFactories(Arrays
                         .<NamedFactory<UserAuth>> asList(new UserAuthKeyboardInteractive.Factory()));
@@ -698,7 +686,6 @@ public class ClientTest extends BaseTest {
         TestEchoShellFactory.TestEchoShell.latch = new CountDownLatch(1);
         try
         {
-            SshClient client = SshClient.setUpDefaultClient();
             client.start();
             ClientSession session = client.connect("localhost", port).await().getSession();
             session.authPassword("smx", "smx");
@@ -729,7 +716,6 @@ public class ClientTest extends BaseTest {
 
     @Test
     public void testWaitAuth() throws Exception {
-        SshClient client = SshClient.setUpDefaultClient();
         final AtomicBoolean ok = new AtomicBoolean();
         client.setServerKeyVerifier(
                 new ServerKeyVerifier() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
index ed9270c..15da95d 100644
--- a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
@@ -82,7 +82,7 @@ public class CompressionTest extends BaseTest {
     @After
     public void tearDown() throws Exception {
         if (sshd != null) {
-            sshd.stop();
+            sshd.stop(true);
             Thread.sleep(50);
         }
         JSch.setConfig("compression.s2c", "none");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/KexTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KexTest.java b/sshd-core/src/test/java/org/apache/sshd/KexTest.java
index 107a78f..908184a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KexTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KexTest.java
@@ -70,7 +70,7 @@ public class KexTest extends BaseTest {
 
     @After
     public void tearDown() throws Exception {
-        sshd.stop();
+        sshd.stop(true);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
index de721ab..ca24c54 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
@@ -56,7 +56,7 @@ public class KeyReExchangeTest extends BaseTest {
 
     @After
     public void tearDown() throws Exception {
-        sshd.stop();
+        sshd.stop(true);
     }
 
     protected void setUp(long bytesLimit, long timeLimit) throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
index 4b62d4c..331ae04 100644
--- a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
@@ -59,7 +59,7 @@ public class LoadTest extends BaseTest {
 
     @After
     public void tearDown() throws Exception {
-        sshd.stop();
+        sshd.stop(true);
     }
 
     @Test
@@ -112,33 +112,35 @@ public class LoadTest extends BaseTest {
 
     protected void runClient(String msg) throws Exception {
         SshClient client = SshClient.setUpDefaultClient();
-        client.getProperties().put(SshClient.MAX_PACKET_SIZE, Integer.toString(1024 * 16));
-        client.getProperties().put(SshClient.WINDOW_SIZE, Integer.toString(1024 * 8));
-        client.setKeyExchangeFactories(Arrays.<NamedFactory<KeyExchange>>asList(
-                new DHG1.Factory()));
-        client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(
-                new BlowfishCBC.Factory()));
-        client.start();
-        ClientSession session = client.connect("localhost", port).await().getSession();
-        session.authPassword("sshd", "sshd").await().isSuccess();
-
-        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ByteArrayOutputStream err = new ByteArrayOutputStream();
-        channel.setOut(out);
-        channel.setErr(err);
-        channel.open().await();
-        OutputStream pipedIn = channel.getInvertedIn();
-
-        msg += "\nexit\n";
-        pipedIn.write(msg.getBytes());
-        pipedIn.flush();
-
-        channel.waitFor(ClientChannel.CLOSED, 0);
-
-        channel.close(false);
-        client.stop();
-
-        assertArrayEquals(msg.getBytes(), out.toByteArray());
+        try {
+            client.getProperties().put(SshClient.MAX_PACKET_SIZE, Integer.toString(1024 * 16));
+            client.getProperties().put(SshClient.WINDOW_SIZE, Integer.toString(1024 * 8));
+            client.setKeyExchangeFactories(Arrays.<NamedFactory<KeyExchange>>asList(
+                    new DHG1.Factory()));
+            client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(
+                    new BlowfishCBC.Factory()));
+            client.start();
+            ClientSession session = client.connect("localhost", port).await().getSession();
+            session.authPassword("sshd", "sshd").await().isSuccess();
+
+            ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            ByteArrayOutputStream err = new ByteArrayOutputStream();
+            channel.setOut(out);
+            channel.setErr(err);
+            channel.open().await();
+            OutputStream pipedIn = channel.getInvertedIn();
+
+            msg += "\nexit\n";
+            pipedIn.write(msg.getBytes());
+            pipedIn.flush();
+
+            channel.waitFor(ClientChannel.CLOSED, 0);
+
+            channel.close(false);
+            assertArrayEquals(msg.getBytes(), out.toByteArray());
+        } finally {
+            client.stop();
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/MacTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/MacTest.java b/sshd-core/src/test/java/org/apache/sshd/MacTest.java
index 486abfd..478d9be 100644
--- a/sshd-core/src/test/java/org/apache/sshd/MacTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/MacTest.java
@@ -141,7 +141,7 @@ public class MacTest extends BaseTest {
     @After
     public void tearDown() throws Exception {
         if (sshd != null) {
-            sshd.stop();
+            sshd.stop(true);
         }
     }
 
@@ -154,22 +154,25 @@ public class MacTest extends BaseTest {
         JSch.setConfig("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96,hmac-sha2-512");
         JSch.setConfig("hmac-sha2-512",  "com.jcraft.jsch.jce.HMACSHA512");
         com.jcraft.jsch.Session s = sch.getSession("smx", "localhost", port);
-        s.setUserInfo(new SimpleUserInfo("smx"));
-        s.connect();
-        com.jcraft.jsch.Channel c = s.openChannel("shell");
-        c.connect();
-        OutputStream os = c.getOutputStream();
-        InputStream is = c.getInputStream();
-        for (int i = 0; i < 10; i++) {
-            os.write("this is my command\n".getBytes());
-            os.flush();
-            byte[] data = new byte[512];
-            int len = is.read(data);
-            String str = new String(data, 0, len);
-            assertEquals("this is my command\n", str);
+        try {
+            s.setUserInfo(new SimpleUserInfo("smx"));
+            s.connect();
+            com.jcraft.jsch.Channel c = s.openChannel("shell");
+            c.connect();
+            OutputStream os = c.getOutputStream();
+            InputStream is = c.getInputStream();
+            for (int i = 0; i < 10; i++) {
+                os.write("this is my command\n".getBytes());
+                os.flush();
+                byte[] data = new byte[512];
+                int len = is.read(data);
+                String str = new String(data, 0, len);
+                assertEquals("this is my command\n", str);
+            }
+            c.disconnect();
+        } finally {
+            s.disconnect();
         }
-        c.disconnect();
-        s.disconnect();
     }
 
     static boolean checkCipher(String cipher){

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
index d739857..af8a9b2 100644
--- a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
@@ -107,11 +107,11 @@ public class PortForwardingLoadTest extends BaseTest {
     @After
     public void tearDown() throws Exception {
         if (sshd != null) {
-            sshd.stop();
+            sshd.stop(true);
             Thread.sleep(50);
         }
         if (acceptor != null) {
-            acceptor.dispose();
+            acceptor.dispose(true);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java b/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
index f41f753..3b97b43 100644
--- a/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
@@ -100,11 +100,11 @@ public class PortForwardingTest extends BaseTest {
     @After
     public void tearDown() throws Exception {
         if (sshd != null) {
-            sshd.stop();
+            sshd.stop(true);
             Thread.sleep(50);
         }
         if (acceptor != null) {
-            acceptor.dispose();
+            acceptor.dispose(true);
         }
         if (client != null) {
             client.stop();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
index 48baa8c..743da4a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
@@ -89,7 +89,7 @@ public class ScpTest extends BaseTest {
         if (session != null) {
             session.disconnect();
         }
-        sshd.stop();
+        sshd.stop(true);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index ff6aadb..1e960b7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -82,7 +82,7 @@ public class SftpTest extends BaseTest {
     @After
     public void tearDown() throws Exception {
         session.disconnect();
-        sshd.stop();
+        sshd.stop(true);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d8386644/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
index 8c37ddf..17e651e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
@@ -22,7 +22,6 @@ import java.io.File;
 import java.io.IOException;
 
 import org.apache.sshd.SshServer;
-import org.apache.sshd.common.AbstractFactoryManager;
 import org.apache.sshd.common.file.FileSystemView;
 import org.apache.sshd.common.file.SshFile;
 import org.apache.sshd.common.file.nativefs.NativeFileSystemFactory;
@@ -105,7 +104,4 @@ public class VirtualFileSystemTest extends BaseTest {
         }
     }
 
-    static class TestFactoryManager extends AbstractFactoryManager {
-
-    }
 }