diff options
author | Pavol Srna | 2017-02-10 11:33:51 +0000 |
---|---|---|
committer | Victor Rubezhny | 2017-02-21 00:02:18 +0000 |
commit | cd38ef11f21131450266998b959fd814f901f823 (patch) | |
tree | f334f6091269cc8625fddf36304262b645789d75 | |
parent | fb68015cb4880da731a991bad899af3b409fddd8 (diff) | |
download | webtools.jsdt-cd38ef11f21131450266998b959fd814f901f823.tar.gz webtools.jsdt-cd38ef11f21131450266998b959fd814f901f823.tar.xz webtools.jsdt-cd38ef11f21131450266998b959fd814f901f823.zip |
Bug 512025 - Enhance NodeJS Integration Tests - wait for nodejs process to be terminated
Change-Id: I5775fc23d7e1b611a3e86b07899e7192453b36e9
Signed-off-by: Pavol Srna <psrna@redhat.com>
3 files changed, 112 insertions, 22 deletions
diff --git a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/common/JSTTestBase.java b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/common/JSTTestBase.java index 7020ec69a..581960de1 100644 --- a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/common/JSTTestBase.java +++ b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/common/JSTTestBase.java @@ -14,9 +14,11 @@ package org.eclipse.wst.jsdt.integration.tests.common; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; +import java.net.Socket; import java.util.HashMap; import java.util.List; @@ -57,6 +59,10 @@ import org.eclipse.wst.jsdt.integration.tests.internal.wizard.js.NewJSFileWizard import org.eclipse.wst.jsdt.integration.tests.internal.condition.TreeContainsItem; import org.eclipse.wst.jsdt.integration.tests.internal.wizard.npm.NpmInitDialog; import org.junit.runner.RunWith; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchesListener; +import org.eclipse.debug.core.model.IProcess; /** * TestBase Class for JST tests @@ -234,7 +240,7 @@ public class JSTTestBase { } } - protected void resume(Tree tree, Matcher matcher){ + protected void resume(Tree tree, Matcher matcher) { List<TreeItem> items = tree.getAllItems(); for (TreeItem i : items) { if (matcher.matches(i.getText())) { @@ -243,18 +249,18 @@ public class JSTTestBase { } } } - + protected TreeItem getVariable(String name) { WorkbenchView variables = new WorkbenchView("Variables"); variables.activate(); DefaultTree variablesTree = new DefaultTree(); - + TreeItem var = null; - try{ + try { new WaitUntil(new TreeContainsItem(variablesTree, new WithTextMatcher(name), false)); - }catch (WaitTimeoutExpiredException e) { - //not found + } catch (WaitTimeoutExpiredException e) { + // not found return null; } List<TreeItem> vars = variablesTree.getItems(); @@ -265,4 +271,49 @@ public class JSTTestBase { } return var; } + + public void terminatePrcs(IProcess[] prcs) { + + for (IProcess p : prcs) { + try { + p.terminate(); + } catch (DebugException e) { + fail(e.getMessage()); + } + assertTrue("Proces:" + p.getLabel() + " not terminated!", p.isTerminated()); + } + } + + public static boolean portAvailable(int port) { + try (Socket ignored = new Socket("localhost", port)) { + return false; + } catch (IOException ignored) { + return true; + } + } + + public class NodeJSLaunchListener implements ILaunchesListener { + + private ILaunch launch; + + public ILaunch getNodeJSLaunch() { + return launch; + } + + @Override + public void launchesAdded(ILaunch[] arg0) { + this.launch = arg0[0]; + } + + @Override + public void launchesChanged(ILaunch[] arg0) { + + } + + @Override + public void launchesRemoved(ILaunch[] arg0) { + + } + + } } diff --git a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSDebuggerTest.java b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSDebuggerTest.java index 726827f6e..fa0399932 100644 --- a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSDebuggerTest.java +++ b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSDebuggerTest.java @@ -31,6 +31,8 @@ import org.jboss.reddeer.workbench.impl.editor.TextEditor; import org.jboss.reddeer.workbench.impl.view.WorkbenchView; import org.eclipse.wst.jsdt.integration.tests.internal.condition.CursorPositionIsOnLine; import org.eclipse.wst.jsdt.integration.tests.internal.condition.TreeContainsItem; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchManager; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -41,7 +43,6 @@ import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; - /** * Tests for NodeJS Debugger * @@ -63,6 +64,11 @@ public class NodeJSDebuggerTest extends JSTTestBase { private static String DEBUG_VARIABLE_AUTHOR = "author"; private static String DEBUG_VARIABLE_AUTHOR_VALUE = "\"George Orwell\""; + private static int APP_PORT = 3000; + private static int DEBUG_PORT = 5858; + + private static NodeJSLaunchListener launchListener = null; + @BeforeClass public static void prepare() { /* PE is closed in Debug perspective, open it */ @@ -81,14 +87,18 @@ public class NodeJSDebuggerTest extends JSTTestBase { @Before public void debugAs() { + assertTrue(portAvailable(APP_PORT)); + assertTrue(portAvailable(DEBUG_PORT)); - debugAsNodeJSAppMenu(new ProjectExplorer().getProject(TEST_APP_NAME).getProjectItem("index.js")) - .select(); + launchListener = new NodeJSLaunchListener(); + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + manager.addLaunchListener(launchListener); + + debugAsNodeJSAppMenu(new ProjectExplorer().getProject(TEST_APP_NAME).getProjectItem("index.js")).select(); new WorkbenchView("Debug").open(); DefaultTree debugTree = new DefaultTree(); - new WaitUntil( - new TreeContainsItem(debugTree, new RegexMatcher("\\(anonymous function\\)(.*)(index\\.js)(.*)")), + new WaitUntil(new TreeContainsItem(debugTree, new RegexMatcher("\\(anonymous function\\)(.*)(index\\.js)(.*)")), TimePeriod.LONG); RegexMatcher matcher = new RegexMatcher("\\(anonymous function\\)(.*)(index\\.js)(.*)"); @@ -97,13 +107,14 @@ public class NodeJSDebuggerTest extends JSTTestBase { ConsoleView console = new ConsoleView(); console.open(); new WaitUntil(new ConsoleHasText("Debugger listening"), TimePeriod.LONG); + } @After public void terminate() { - ConsoleView console = new ConsoleView(); - console.open(); - console.terminateConsole(); + terminatePrcs(launchListener.getNodeJSLaunch().getProcesses()); + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + manager.removeLaunchListener(launchListener); } @Test @@ -125,7 +136,6 @@ public class NodeJSDebuggerTest extends JSTTestBase { @Test public void testJSVariablesInitialized() throws CoreException { - new ProjectExplorer().getProject(TEST_APP_NAME).getProjectItem("book.js").open(); new WaitUntil(new EditorWithTitleIsActive("book.js")); TextEditor editor = new TextEditor("book.js"); @@ -135,18 +145,17 @@ public class NodeJSDebuggerTest extends JSTTestBase { new WorkbenchView("Debug").open(); DefaultTree debugTree = new DefaultTree(); resume(debugTree, new RegexMatcher("\\(anonymous function\\)(.*)(index\\.js)(.*)")); - + new WaitUntil(new EditorWithTitleIsActive("book.js"), TimePeriod.LONG); new WaitUntil(new CursorPositionIsOnLine(editor, BOOK_JS_BREAKPOINT_LINE)); TreeItem varTitle = getVariable(DEBUG_VARIABLE_TITLE); TreeItem varAuthor = getVariable(DEBUG_VARIABLE_AUTHOR); - assertTrue("Variable '"+ DEBUG_VARIABLE_TITLE + "' not found in view!", varTitle != null); + assertTrue("Variable '" + DEBUG_VARIABLE_TITLE + "' not found in view!", varTitle != null); assertThat(varTitle.getCell(0), is(DEBUG_VARIABLE_TITLE)); assertThat(varTitle.getCell(1), is(DEBUG_VARIABLE_TITLE_VALUE)); - - + assertTrue("Variable '" + DEBUG_VARIABLE_AUTHOR + "' not found in view!", varAuthor != null); assertThat(varAuthor.getCell(0), is(DEBUG_VARIABLE_AUTHOR)); assertThat(varAuthor.getCell(1), is(DEBUG_VARIABLE_AUTHOR_VALUE)); diff --git a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSLauncherTest.java b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSLauncherTest.java index d54cddc66..56f182251 100644 --- a/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSLauncherTest.java +++ b/tests/org.eclipse.wst.jsdt.integration.tests/src/org/eclipse/wst/jsdt/integration/tests/nodejs/NodeJSLauncherTest.java @@ -20,6 +20,7 @@ import org.jboss.reddeer.eclipse.jdt.ui.ProjectExplorer; import org.jboss.reddeer.eclipse.ui.console.ConsoleView; import org.jboss.reddeer.eclipse.ui.perspectives.DebugPerspective; import org.jboss.reddeer.requirements.openperspective.OpenPerspectiveRequirement.OpenPerspective; +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -28,6 +29,9 @@ import org.junit.Assert; import static org.junit.Assert.assertTrue; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchManager; import org.eclipse.wst.jsdt.integration.tests.common.JSTTestBase; /** @@ -42,6 +46,11 @@ public class NodeJSLauncherTest extends JSTTestBase { private static String TEST_APP_NAME = "jsdt-node-test-project"; private static String IMPORT_PATH = "resources/" + TEST_APP_NAME; + private static int APP_PORT = 3000; + private static int DEBUG_PORT = 5858; + + private NodeJSLaunchListener launchListener = null; + @BeforeClass public static void prepare() { /* PE is closed in Debug perspective, open it */ @@ -66,6 +75,12 @@ public class NodeJSLauncherTest extends JSTTestBase { @Test public void testNodeJSAppIsRunning() { + assertTrue(portAvailable(APP_PORT)); + + launchListener = new NodeJSLaunchListener(); + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + manager.addLaunchListener(launchListener); + ExplorerItem indexJS = new ProjectExplorer().getProject(TEST_APP_NAME).getProjectItem("index.js"); indexJS.select(); runAsNodeJSAppMenu().select(); @@ -77,7 +92,6 @@ public class NodeJSLauncherTest extends JSTTestBase { } catch (WaitTimeoutExpiredException e) { Assert.fail("Node.js App is not running!"); } - console.terminateConsole(); } @Test @@ -89,6 +103,13 @@ public class NodeJSLauncherTest extends JSTTestBase { @Test public void testNodeJSAppIsDebugging() { + assertTrue(portAvailable(APP_PORT)); + assertTrue(portAvailable(DEBUG_PORT)); + + launchListener = new NodeJSLaunchListener(); + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + manager.addLaunchListener(launchListener); + debugAsNodeJSAppMenu(new ProjectExplorer().getProject(TEST_APP_NAME).getProjectItem("index.js")).select(); ConsoleView console = new ConsoleView(); @@ -96,8 +117,17 @@ public class NodeJSLauncherTest extends JSTTestBase { new WaitUntil(new ConsoleHasText("Debugger listening"), TimePeriod.LONG); assertTrue("Node.js App is not debugging!", console.getConsoleText().contains("Debugger listening")); - - console.terminateConsole(); } + @After + public void terminate() { + if (launchListener != null) { + ILaunch launch = launchListener.getNodeJSLaunch(); + if (launch != null) { + terminatePrcs(launch.getProcesses()); + } + ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); + manager.removeLaunchListener(launchListener); + } + } } |