<commit_before>package org.jboss.reddeer.eclipse.wst.server.ui.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.ui.IServerModule;
import org.jboss.reddeer.eclipse.condition.ServerExists;
import org.jboss.reddeer.eclipse.exception.EclipseLayerException;
import org.jboss.reddeer.eclipse.wst.server.ui.editor.ServerEditor;
import org.jboss.reddeer.eclipse.wst.server.ui.view.ServersViewEnums.ServerPublishState;
import org.jboss.reddeer.eclipse.wst.server.ui.view.ServersViewEnums.ServerState;
import org.jboss.reddeer.eclipse.wst.server.ui.wizard.ModifyModulesDialog;
import org.jboss.reddeer.junit.logging.Logger;
import org.jboss.reddeer.swt.api.TreeItem;
import org.jboss.reddeer.swt.condition.JobIsRunning;
import org.jboss.reddeer.swt.condition.ShellWithTextIsActive;
import org.jboss.reddeer.swt.condition.WaitCondition;
import org.jboss.reddeer.swt.impl.button.CheckBox;
import org.jboss.reddeer.swt.impl.button.PushButton;
import org.jboss.reddeer.swt.impl.menu.ContextMenu;
import org.jboss.reddeer.swt.impl.shell.DefaultShell;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.wait.TimePeriod;
import org.jboss.reddeer.swt.wait.WaitUntil;
import org.jboss.reddeer.swt.wait.WaitWhile;

/**
 * Represents a server on {@link ServersView}. Contains both, the server data
 * (name, state, status) and operations that can be invoked on server (Start,
 * Stop, Delete etc.). For operations that can be invoked on project added to
 * server see {@link ServerModule}
 * 
 * @author <NAME>
 * 
 */
public class Server {

	private static final TimePeriod TIMEOUT = TimePeriod.VERY_LONG;

	private static final String ADD_AND_REMOVE = "Add and Remove...";

	private static final Logger log = Logger.getLogger(Server.class);

	protected TreeItem treeItem;

	public Server(TreeItem treeItem) {
		this.treeItem = treeItem;
	}

	public ServerLabel getLabel(){
		return new ServerLabel(treeItem);
	}

	public List<ServerModule> getModules() {
		final List<ServerModule> modules = new ArrayList<ServerModule>();

		Display.syncExec(new Runnable() {

			@Override
			public void run() {
				for (TreeItem item : treeItem.getItems()){
					org.eclipse.swt.widgets.TreeItem swtItem = item.getSWTWidget();
					Object data = swtItem.getData();
					if (data instanceof IModule || data instanceof IServerModule){
						modules.add(createServerModule(item));
					}
				}				
			}
		});

		return modules;
	}

	public ServerModule getModule(String name) {
		for (ServerModule module : getModules()){
			if (module.getLabel().getName().equals(name)){
				return module;
			}
		}
		throw new EclipseLayerException("There is no module with name " + name + " on server " + getLabel().getName());
	}

	public ModifyModulesDialog addAndRemoveModules() {
		select();
		new ContextMenu(ADD_AND_REMOVE).select();
		return new ModifyModulesDialog();
	}
	
	public ServerEditor open() {
		select();
		new ContextMenu("Open").select();
		return createServerEditor(getLabel().getName());
	}

	public void start() {
		log.info("Starting server " + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot start server because it is not stopped");
		}
		operateServerState("Start", ServerState.STARTED);
	}

	public void debug() {
		log.info("Starting server in debug" + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot debug server because it is not stopped");
		}
		operateServerState("Debug", ServerState.DEBUGGING);
	}

	public void profile() {
		log.info("Starting server in profiling mode" + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot profile server because it is not stopped");
		}
		operateServerState("Profile", ServerState.PROFILING);
	}

	public void restart() {
		log.info("Restarting server " + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server because it is not running");
		}
		operateServerState("Restart", ServerState.STARTED);
	}

	public void restartInDebug() {
		log.info("Restarting server in debug" + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server in debug because it is not running");
		}
		operateServerState("Restart in Debug", ServerState.DEBUGGING);
	}

	public void restartInProfile() {
		log.info("Restarting server in profile" + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server in profile because it is not running");
		}
		operateServerState("Restart in Profile", ServerState.PROFILING);
	}

	public void stop() {
		log.info("Stopping server " + getLabel().getName());
		ServerState state = getLabel().getState();
		if (!ServerState.STARTING.equals(state) && !state.isRunningState()){
			throw new ServersViewException("Cannot stop server because it not running");
		}
		operateServerState("Stop", ServerState.STOPPED);
	}

	public void publish() {
		log.info("Publishing server " + getLabel().getName());
		select();
		new ContextMenu("Publish").select();
		waitForPublish();
	}

	public void clean() {
		log.info("Cleaning server " + getLabel().getName());
		select();
		new ContextMenu("Clean...").select();
		new DefaultShell("Server");
		new PushButton("OK").click();
		waitForPublish();
	}

	public void delete() {
		delete(false);
	}

	public void delete(boolean stopFirst) {
		final String name = getLabel().getName();
		log.info("Deleting server " + name + ". Stopping server first: " + stopFirst);
		select();
		ServerState state = getLabel().getState();

		new ContextMenu("Delete").select();	
		new WaitUntil(new ShellWithTextIsActive("Delete Server"),TimePeriod.NORMAL);
		if (!ServerState.STOPPED.equals(state) && !ServerState.NONE.equals(state)){
			new CheckBox().toggle(stopFirst);
		}
		new PushButton("OK").click();
		new WaitWhile(new ServerExists(name), TIMEOUT);
		new WaitWhile(new JobIsRunning(), TIMEOUT);
	}

	protected void select() {
		treeItem.select();
	}

	protected void operateServerState(String menuItem, ServerState resultState){
		select();
		new ContextMenu(menuItem).select();
		new WaitUntil(new JobIsRunning(), TIMEOUT);
		new WaitUntil(new ServerStateCondition(resultState), TIMEOUT);
		new WaitWhile(new JobIsRunning(), TIMEOUT);

		//check if the server has expected state after jobs are done
		new WaitUntil(new ServerStateCondition(resultState), TIMEOUT);
	}

	protected void waitForPublish(){
		new WaitUntil(new JobIsRunning(), TIMEOUT);
		new WaitWhile(new ServerPublishStateCondition(ServerPublishState.PUBLISHING), TIMEOUT);
		new WaitUntil(new ServerPublishStateCondition(ServerPublishState.SYNCHRONIZED), TIMEOUT);
	}
	
	protected ServerModule createServerModule(TreeItem item){
		return new ServerModule(item);
	}
	
	protected ServerEditor createServerEditor(String title){
		return new ServerEditor(title);
	}

	private class ServerStateCondition implements WaitCondition {

		private ServerState expectedState;

		private ServerStateCondition(ServerState expectedState) {
			this.expectedState = expectedState;
		}

		@Override
		public boolean test() {
			return expectedState.equals(getLabel().getState());
		}

		@Override
		public String description() {
			return "server's state is: " + expectedState.getText();
		}
	}

	private class ServerPublishStateCondition implements WaitCondition {

		private ServerPublishState expectedState;

		private ServerPublishStateCondition(ServerPublishState expectedState) {
			this.expectedState = expectedState;
		}

		@Override
		public boolean test() {
			return expectedState.equals(getLabel().getPublishState());
		}

		@Override
		public String description() {
			return "server's publish state is " + expectedState.getText();
		}
	}

}

<commit_msg>Fix server deletion<commit_after>package org.jboss.reddeer.eclipse.wst.server.ui.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.ui.IServerModule;
import org.jboss.reddeer.eclipse.condition.ServerExists;
import org.jboss.reddeer.eclipse.exception.EclipseLayerException;
import org.jboss.reddeer.eclipse.wst.server.ui.editor.ServerEditor;
import org.jboss.reddeer.eclipse.wst.server.ui.view.ServersViewEnums.ServerPublishState;
import org.jboss.reddeer.eclipse.wst.server.ui.view.ServersViewEnums.ServerState;
import org.jboss.reddeer.eclipse.wst.server.ui.wizard.ModifyModulesDialog;
import org.jboss.reddeer.junit.logging.Logger;
import org.jboss.reddeer.swt.api.TreeItem;
import org.jboss.reddeer.swt.condition.JobIsRunning;
import org.jboss.reddeer.swt.condition.ShellWithTextIsActive;
import org.jboss.reddeer.swt.condition.WaitCondition;
import org.jboss.reddeer.swt.impl.button.CheckBox;
import org.jboss.reddeer.swt.impl.button.PushButton;
import org.jboss.reddeer.swt.impl.menu.ContextMenu;
import org.jboss.reddeer.swt.impl.shell.DefaultShell;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.wait.TimePeriod;
import org.jboss.reddeer.swt.wait.WaitUntil;
import org.jboss.reddeer.swt.wait.WaitWhile;

/**
 * Represents a server on {@link ServersView}. Contains both, the server data
 * (name, state, status) and operations that can be invoked on server (Start,
 * Stop, Delete etc.). For operations that can be invoked on project added to
 * server see {@link ServerModule}
 * 
 * @author <NAME>
 * 
 */
public class Server {

	private static final TimePeriod TIMEOUT = TimePeriod.VERY_LONG;

	private static final String ADD_AND_REMOVE = "Add and Remove...";

	private static final Logger log = Logger.getLogger(Server.class);

	protected TreeItem treeItem;

	public Server(TreeItem treeItem) {
		this.treeItem = treeItem;
	}

	public ServerLabel getLabel(){
		return new ServerLabel(treeItem);
	}

	public List<ServerModule> getModules() {
		final List<ServerModule> modules = new ArrayList<ServerModule>();

		Display.syncExec(new Runnable() {

			@Override
			public void run() {
				for (TreeItem item : treeItem.getItems()){
					org.eclipse.swt.widgets.TreeItem swtItem = item.getSWTWidget();
					Object data = swtItem.getData();
					if (data instanceof IModule || data instanceof IServerModule){
						modules.add(createServerModule(item));
					}
				}				
			}
		});

		return modules;
	}

	public ServerModule getModule(String name) {
		for (ServerModule module : getModules()){
			if (module.getLabel().getName().equals(name)){
				return module;
			}
		}
		throw new EclipseLayerException("There is no module with name " + name + " on server " + getLabel().getName());
	}

	public ModifyModulesDialog addAndRemoveModules() {
		select();
		new ContextMenu(ADD_AND_REMOVE).select();
		return new ModifyModulesDialog();
	}
	
	public ServerEditor open() {
		select();
		new ContextMenu("Open").select();
		return createServerEditor(getLabel().getName());
	}

	public void start() {
		log.info("Starting server " + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot start server because it is not stopped");
		}
		operateServerState("Start", ServerState.STARTED);
	}

	public void debug() {
		log.info("Starting server in debug" + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot debug server because it is not stopped");
		}
		operateServerState("Debug", ServerState.DEBUGGING);
	}

	public void profile() {
		log.info("Starting server in profiling mode" + getLabel().getName());
		if (!ServerState.STOPPED.equals(getLabel().getState())){
			throw new ServersViewException("Cannot profile server because it is not stopped");
		}
		operateServerState("Profile", ServerState.PROFILING);
	}

	public void restart() {
		log.info("Restarting server " + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server because it is not running");
		}
		operateServerState("Restart", ServerState.STARTED);
	}

	public void restartInDebug() {
		log.info("Restarting server in debug" + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server in debug because it is not running");
		}
		operateServerState("Restart in Debug", ServerState.DEBUGGING);
	}

	public void restartInProfile() {
		log.info("Restarting server in profile" + getLabel().getName());
		if (!getLabel().getState().isRunningState()){
			throw new ServersViewException("Cannot restart server in profile because it is not running");
		}
		operateServerState("Restart in Profile", ServerState.PROFILING);
	}

	public void stop() {
		log.info("Stopping server " + getLabel().getName());
		ServerState state = getLabel().getState();
		if (!ServerState.STARTING.equals(state) && !state.isRunningState()){
			throw new ServersViewException("Cannot stop server because it not running");
		}
		operateServerState("Stop", ServerState.STOPPED);
	}

	public void publish() {
		log.info("Publishing server " + getLabel().getName());
		select();
		new ContextMenu("Publish").select();
		waitForPublish();
	}

	public void clean() {
		log.info("Cleaning server " + getLabel().getName());
		select();
		new ContextMenu("Clean...").select();
		new DefaultShell("Server");
		new PushButton("OK").click();
		waitForPublish();
	}

	public void delete() {
		delete(false);
	}

	public void delete(boolean stopFirst) {
		final String name = getLabel().getName();
		log.info("Deleting server " + name + ". Stopping server first: " + stopFirst);
		select();
		ServerState state = getLabel().getState();

		new ContextMenu("Delete").select();	
		new DefaultShell("Delete Server");
		if (!ServerState.STOPPED.equals(state) && !ServerState.NONE.equals(state)){
			new CheckBox().toggle(stopFirst);
		}
		new PushButton("OK").click();
		new WaitWhile(new ServerExists(name), TIMEOUT);
		new WaitWhile(new JobIsRunning(), TIMEOUT);
	}

	protected void select() {
		treeItem.select();
	}

	protected void operateServerState(String menuItem, ServerState resultState){
		select();
		new ContextMenu(menuItem).select();
		new WaitUntil(new JobIsRunning(), TIMEOUT);
		new WaitUntil(new ServerStateCondition(resultState), TIMEOUT);
		new WaitWhile(new JobIsRunning(), TIMEOUT);

		//check if the server has expected state after jobs are done
		new WaitUntil(new ServerStateCondition(resultState), TIMEOUT);
	}

	protected void waitForPublish(){
		new WaitUntil(new JobIsRunning(), TIMEOUT);
		new WaitWhile(new ServerPublishStateCondition(ServerPublishState.PUBLISHING), TIMEOUT);
		new WaitUntil(new ServerPublishStateCondition(ServerPublishState.SYNCHRONIZED), TIMEOUT);
	}
	
	protected ServerModule createServerModule(TreeItem item){
		return new ServerModule(item);
	}
	
	protected ServerEditor createServerEditor(String title){
		return new ServerEditor(title);
	}

	private class ServerStateCondition implements WaitCondition {

		private ServerState expectedState;

		private ServerStateCondition(ServerState expectedState) {
			this.expectedState = expectedState;
		}

		@Override
		public boolean test() {
			return expectedState.equals(getLabel().getState());
		}

		@Override
		public String description() {
			return "server's state is: " + expectedState.getText();
		}
	}

	private class ServerPublishStateCondition implements WaitCondition {

		private ServerPublishState expectedState;

		private ServerPublishStateCondition(ServerPublishState expectedState) {
			this.expectedState = expectedState;
		}

		@Override
		public boolean test() {
			return expectedState.equals(getLabel().getPublishState());
		}

		@Override
		public String description() {
			return "server's publish state is " + expectedState.getText();
		}
	}

}


{'fraction_non_alphanumeric': 0.12523434570678665, 'fraction_numerical': 0.0, 'mean_word_length': 5.022958223560407, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '835512', 'n_tokens_mistral': 5266, 'n_tokens_neox': 4780, 'n_words': 1113}