diff --git a/plugins/org.eclipse.reddeer.eclipse/src/org/eclipse/reddeer/eclipse/wst/server/ui/RuntimePreferencePage.java b/plugins/org.eclipse.reddeer.eclipse/src/org/eclipse/reddeer/eclipse/wst/server/ui/RuntimePreferencePage.java index 30167bbd51..15ec5b81a6 100644 --- a/plugins/org.eclipse.reddeer.eclipse/src/org/eclipse/reddeer/eclipse/wst/server/ui/RuntimePreferencePage.java +++ b/plugins/org.eclipse.reddeer.eclipse/src/org/eclipse/reddeer/eclipse/wst/server/ui/RuntimePreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017 Red Hat, Inc and others. + * Copyright (c) 2018 Red Hat, 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 @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.eclipse.reddeer.common.logging.Logger; import org.eclipse.reddeer.common.wait.TimePeriod; @@ -71,6 +72,47 @@ public List getServerRuntimes(){ return runtimes; } + /** + * Returns server runtime based on passed name and type + * @param name string parameters representing name of the runtime + * @param type string type of runtime + * @return runtime object fulfilling conditions of equality + */ + public Runtime getServerRuntime(String name, String type) { + return getServerRuntime(new Runtime(name, type)); + } + + /** + * Returns server runtime based on passed runtime + * @param runtime runtime object to find + * @return runtime object if equals to passed parameter, null if there is none or + * throws an exception if there are more than one runtime found + */ + public Runtime getServerRuntime(Runtime runtime) { + List resultRuntimes = getServerRuntimes() + .stream() + .filter((run) -> run.equals(runtime)) + .collect(Collectors.toList()); + return resultRuntimes.isEmpty() ? null : resultRuntimes.get(0); + } + + /** + * Returns server runtime based on its name. + * @param name string name parameter + * @return runtime object if equals to passed parameter, null if there is none or + * throws an exception if there are more than one runtime found + */ + public Runtime getServerRuntime(String name) { + List resultRuntimes = getServerRuntimes() + .stream() + .filter((runtime) -> runtime.getName().equals(name)) + .collect(Collectors.toList()); + if (resultRuntimes.size() > 1) { + throw new EclipseLayerException("There are more than one runtime fulfilling conditions. Result is ambiguous."); + } + return resultRuntimes.isEmpty() ? null : resultRuntimes.get(0); + } + /** * Removes a given runtime. * @@ -115,7 +157,7 @@ private RuntimePreferencePage selectRuntime(String name){ return this; } } - throw new EclipseLayerException("Unable to find runtime "+name); + throw new EclipseLayerException("Unable to find runtime " + name); } /** diff --git a/tests/org.eclipse.reddeer.eclipse.test/plugin.xml b/tests/org.eclipse.reddeer.eclipse.test/plugin.xml index 6fddb5feae..c1d351a5f8 100644 --- a/tests/org.eclipse.reddeer.eclipse.test/plugin.xml +++ b/tests/org.eclipse.reddeer.eclipse.test/plugin.xml @@ -17,6 +17,8 @@ + + runtimes = preferencePage.getServerRuntimes(); + assertThat(runtimes.size(), is(3)); + + Runtime runtime1 = preferencePage.getServerRuntime(SERVER_NAME, SERVER_NAME); + assertThat(runtime1, is(new Runtime(SERVER_NAME, SERVER_NAME))); + + Runtime runtime2 = preferencePage.getServerRuntime(new Runtime(SERVER_NAME_2, SERVER_NAME_2)); + assertThat(runtime2, is(new Runtime(SERVER_NAME_2, SERVER_NAME_2))); + + Runtime runtime3 = null; + try { + runtime3 = preferencePage.getServerRuntime(SERVER_NAME_2); + } catch (EclipseLayerException exc) { + fail(exc.getMessage()); + } + assertThat(runtime3, is(new Runtime(SERVER_NAME_2, SERVER_NAME_2))); + } } diff --git a/tests/org.eclipse.reddeer.eclipse.test/src/org/eclipse/reddeer/eclipse/test/wst/server/ui/TestServerRuntime2.java b/tests/org.eclipse.reddeer.eclipse.test/src/org/eclipse/reddeer/eclipse/test/wst/server/ui/TestServerRuntime2.java new file mode 100644 index 0000000000..e9d00b0473 --- /dev/null +++ b/tests/org.eclipse.reddeer.eclipse.test/src/org/eclipse/reddeer/eclipse/test/wst/server/ui/TestServerRuntime2.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat, 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: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.reddeer.eclipse.test.wst.server.ui; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.wst.server.core.model.RuntimeDelegate; + +public class TestServerRuntime2 extends RuntimeDelegate { + + public static final String ID = "org.eclipse.reddeer.eclipse.test.wst.server.testserverruntime"; + + public static final String CATEGORY = "Basic"; + + public static final String NAME = "Test runtime 2"; + + public static final String TYPE = "Test runtime 2"; + + public IStatus validate() { + return Status.OK_STATUS; + } + + public void setDefaults(IProgressMonitor monitor) { + getRuntimeWorkingCopy().setLocation(new Path("")); + } +} \ No newline at end of file