diff options
Diffstat (limited to 'bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java')
-rwxr-xr-x | bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java | 106 |
1 files changed, 44 insertions, 62 deletions
diff --git a/bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java b/bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java index bebcd222f..005d9cbad 100755 --- a/bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java +++ b/bundles/archived/org.eclipse.equinox.console.tests/src/org/eclipse/equinox/console/telnet/TelnetConnectionTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2012 SAP AG + * Copyright (c) 2011, 2017 SAP AG and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -27,73 +27,55 @@ import java.net.Socket; public class TelnetConnectionTests { private static final String HOST = "localhost"; - private static final int TEST_CONTENT = 100; - private static final int IAC = 255; + private static final int TEST_CONTENT = 100; + private static final int IAC = 255; - @Test - public void testTelneConnection() throws Exception { - ServerSocket servSocket = null; - Socket socketClient = null; - Socket socketServer = null; - TelnetConnection connection = null; - OutputStream outClient = null; - OutputStream outServer = null; + @Test + public void testTelneConnection() throws Exception { - try { - servSocket = new ServerSocket(0); - socketClient = new Socket(HOST, servSocket.getLocalPort()); - socketServer = servSocket.accept(); + try (ServerSocket servSocket = new ServerSocket(0); + Socket socketClient = new Socket(HOST, servSocket.getLocalPort()); + Socket socketServer = servSocket.accept(); + CommandSession session = EasyMock.createMock(CommandSession.class)) { - CommandSession session = EasyMock.createMock(CommandSession.class); - session.put((String)EasyMock.anyObject(), EasyMock.anyObject()); - EasyMock.expectLastCall().times(3); - EasyMock.expect(session.execute((String)EasyMock.anyObject())).andReturn(null); - session.close(); - EasyMock.expectLastCall(); - EasyMock.replay(session); - - CommandProcessor processor = EasyMock.createMock(CommandProcessor.class); - EasyMock.expect(processor.createSession((ConsoleInputStream) EasyMock.anyObject(), (PrintStream) EasyMock.anyObject(), (PrintStream) EasyMock.anyObject())).andReturn(session); - EasyMock.replay(processor); - - connection = new TelnetConnection(socketServer, processor, null); - connection.start(); + session.put((String) EasyMock.anyObject(), EasyMock.anyObject()); + EasyMock.expectLastCall().times(3); + EasyMock.expect(session.execute((String) EasyMock.anyObject())).andReturn(null); + session.close(); + EasyMock.expectLastCall(); + EasyMock.replay(session); - outClient = socketClient.getOutputStream(); - outClient.write(TEST_CONTENT); - outClient.write('\n'); - outClient.flush(); + CommandProcessor processor = EasyMock.createMock(CommandProcessor.class); + EasyMock.expect(processor.createSession((ConsoleInputStream) EasyMock.anyObject(), + (PrintStream) EasyMock.anyObject(), (PrintStream) EasyMock.anyObject())).andReturn(session); + EasyMock.replay(processor); - InputStream input = socketServer.getInputStream(); - int in = input.read(); - Assert.assertTrue("Server received [" + in + "] instead of " + TEST_CONTENT + " from the telnet client.", in == TEST_CONTENT); + try (TelnetConnection connection = new TelnetConnection(socketServer, processor, null)) { + connection.start(); - input = socketClient.getInputStream(); - in = input.read(); - // here IAC is expected, since when the output stream in TelnetConsoleSession is created, several telnet - // commands are written to it, each of them starting with IAC - Assert.assertTrue("Client receive telnet responses from the server unexpected value [" + in + "] instead of " + IAC + ".", in == IAC); - connection.telnetNegotiationFinished(); - Thread.sleep(5000); - EasyMock.verify(session, processor); - } finally { - if (socketClient != null) { - socketClient.close(); - } - if (outClient != null) { - outClient.close(); - } - if (outServer != null) { - outServer.close(); - } + try (OutputStream outClient = socketClient.getOutputStream()) { + outClient.write(TEST_CONTENT); + outClient.write('\n'); + outClient.flush(); - if (socketServer != null) { - socketServer.close(); - } + InputStream input = socketServer.getInputStream(); + int in = input.read(); + Assert.assertTrue( + "Server received [" + in + "] instead of " + TEST_CONTENT + " from the telnet client.", + in == TEST_CONTENT); - if (servSocket != null) { - servSocket.close(); - } - } - } + input = socketClient.getInputStream(); + in = input.read(); + // here IAC is expected, since when the output stream in TelnetConsoleSession is + // created, several telnet + // commands are written to it, each of them starting with IAC + Assert.assertTrue("Client receive telnet responses from the server unexpected value [" + in + + "] instead of " + IAC + ".", in == IAC); + connection.telnetNegotiationFinished(); + Thread.sleep(5000); + EasyMock.verify(session, processor); + } + } + } + } } |