From ce6dc5459a883e68ec2263d609bb4158861bed81 Mon Sep 17 00:00:00 2001 From: Anna Dushistova Date: Tue, 30 Sep 2008 15:02:23 +0000 Subject: [PATCH] [249102] fixed unit tests. --- .../shells/ShellOutputListener.java | 47 ++++++++++++++ .../subsystems/shells/ShellServiceTest.java | 61 ++++++++++++------- 2 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellOutputListener.java diff --git a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellOutputListener.java b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellOutputListener.java new file mode 100644 index 00000000000..aab1be8d0d7 --- /dev/null +++ b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellOutputListener.java @@ -0,0 +1,47 @@ +/******************************************************************************** + * Copyright (c) 2008 MontaVista Software, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anna Dushistova (MontaVista) - [249102][testing] Improve ShellService Unittests + ********************************************************************************/ +package org.eclipse.rse.tests.subsystems.shells; + +import java.util.ArrayList; + +import org.eclipse.rse.services.shells.IHostOutput; +import org.eclipse.rse.services.shells.IHostShellChangeEvent; +import org.eclipse.rse.services.shells.IHostShellOutputListener; + +public class ShellOutputListener implements IHostShellOutputListener { + + private ArrayList outputs; + + public ShellOutputListener() { + outputs = new ArrayList(); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.rse.services.shells.IHostShellOutputListener#shellOutputChanged + * (org.eclipse.rse.services.shells.IHostShellChangeEvent) + */ + public void shellOutputChanged(IHostShellChangeEvent event) { + IHostOutput[] output = event.getLines(); + for (int i = 0; i < output.length; i++) + outputs.add(output[i]); + } + + /** + * @return + */ + public Object[] getAllOutput() { + return outputs.toArray(); + } + +} diff --git a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellServiceTest.java b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellServiceTest.java index 2ccdf65ac87..a8cb6353baa 100644 --- a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellServiceTest.java +++ b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/subsystems/shells/ShellServiceTest.java @@ -7,6 +7,7 @@ * * Contributors: * Anna Dushistova (MontaVista) - adapted from FileServiceTest + * Anna Dushistova (MontaVista) - [249102][testing] Improve ShellService Unittests *******************************************************************************/ package org.eclipse.rse.tests.subsystems.shells; @@ -22,15 +23,12 @@ import org.eclipse.rse.core.model.IHost; import org.eclipse.rse.core.subsystems.ISubSystem; import org.eclipse.rse.services.shells.IHostOutput; import org.eclipse.rse.services.shells.IHostShell; -import org.eclipse.rse.services.shells.IHostShellChangeEvent; -import org.eclipse.rse.services.shells.IHostShellOutputListener; import org.eclipse.rse.services.shells.IShellService; import org.eclipse.rse.subsystems.shells.core.subsystems.servicesubsystem.IShellServiceSubSystem; import org.eclipse.rse.subsystems.shells.core.subsystems.servicesubsystem.ShellServiceSubSystem; import org.eclipse.rse.tests.core.connection.RSEBaseConnectionTestCase; -public class ShellServiceTest extends RSEBaseConnectionTestCase implements - IHostShellOutputListener { +public class ShellServiceTest extends RSEBaseConnectionTestCase { private String fPropertiesFileName; // For testing the test: verify methods on Local @@ -42,7 +40,7 @@ public class ShellServiceTest extends RSEBaseConnectionTestCase implements /** * Constructor with specific test name. - * + * * @param name * test to execute */ @@ -52,7 +50,7 @@ public class ShellServiceTest extends RSEBaseConnectionTestCase implements /** * Constructor with connection type and specific test name. - * + * * @param name * test to execute * @param propertiesFileName @@ -130,27 +128,46 @@ public class ShellServiceTest extends RSEBaseConnectionTestCase implements mon); assertNotNull(hostShell); assertNotNull(hostShell.getStandardOutputReader()); - + ShellOutputListener outputListener = new ShellOutputListener(); + hostShell.addOutputListener(outputListener); + // run command + hostShell.writeToShell("echo test"); + hostShell.writeToShell("exit"); + while (hostShell.isActive()) { + Thread.sleep(200); + } + Object[] allOutput = outputListener.getAllOutput(); + boolean matchFound = false; + for (int i = 0; i < allOutput.length; i++) { + matchFound = ((IHostOutput) allOutput[i]).getString() + .equals("test"); + if (matchFound) + break; + } + assertTrue(matchFound); } public void testRunCommand() throws Exception { IHostShell hostShell = null; - if (!isWindows()) { - hostShell = shellService.runCommand("", "echo test", - new String[] {}, mon); - hostShell.addOutputListener(this); - assertNotNull(hostShell); - assertNotNull(hostShell.getStandardOutputReader()); - while (hostShell.isActive()) { - Thread.sleep(200); - } + hostShell = shellService.runCommand("", "echo test", new String[] {}, + mon); + ShellOutputListener outputListener = new ShellOutputListener(); + hostShell.addOutputListener(outputListener); + hostShell.writeToShell("exit"); + assertNotNull(hostShell); + assertNotNull(hostShell.getStandardOutputReader()); + while (hostShell.isActive()) { + Thread.sleep(200); } + Object[] allOutput = outputListener.getAllOutput(); + boolean matchFound = false; + for (int i = 0; i < allOutput.length; i++) { + matchFound = ((IHostOutput) allOutput[i]).getString() + .equals("test"); + if (matchFound) + break; + } + assertTrue(matchFound); } - public void shellOutputChanged(IHostShellChangeEvent event) { - IHostOutput[] output = event.getLines(); - if (output.length > 0) { - assertEquals(output[0].getString(), "test"); - } - } }