Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bebc17e613e07fe6056b62534b2132a08b627565 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.tests.commands;

import org.eclipse.core.commands.Category;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests some of the API on command manager.
 *
 * @since 3.1
 */
@RunWith(JUnit4.class)
public final class CommandManagerTest extends UITestCase {

	/**
	 * An execution listener that can be attached to the command manager. It
	 * will track which events it last heard.
	 *
	 * @since 3.1
	 */
	private final class ExecutionListener implements IExecutionListener {

		/**
		 * The last command identifier to be not handled, or <code>null</code>
		 * if none.
		 */
		private String notHandledId = null;

		/**
		 * The last command identifier to be executed, or <code>null</code> if
		 * none.
		 */
		private String preExecuteId = null;

		/**
		 * The last not handled exception throw, or <code>null</code> if none.
		 */
		private NotHandledException notHandledException = null;

		/**
		 * The last execution event, or <code>null</code> if none.
		 */
		private ExecutionEvent preExecuteEvent = null;

		@Override
		public final void notHandled(final String commandId,
				final NotHandledException exception) {
			notHandledId = commandId;
			notHandledException = exception;
		}

		@Override
		public final void postExecuteFailure(final String commandId,
				final ExecutionException exception) {
			// Do nothing.
		}

		@Override
		public final void postExecuteSuccess(final String commandId,
				final Object returnValue) {
			// Do nothing
		}

		@Override
		public final void preExecute(final String commandId,
				final ExecutionEvent event) {
			preExecuteId = commandId;
			preExecuteEvent = event;
		}

	}

	/**
	 * Constructs a new instance of <code>CommandManagerTest</code>.
	 */
	public CommandManagerTest() {
		super(CommandManagerTest.class.getSimpleName());
	}

	@Test
	public final void testExecutionListener() {
		final String commandId = "myCommand";
		final CommandManager commandManager = new CommandManager();
		final Category category = commandManager.getCategory(commandId);
		category.define("name", null);
		final Command command = commandManager.getCommand(commandId);
		command.define("name", null, category, null);
		final ExecutionListener listener = new ExecutionListener();
		commandManager.addExecutionListener(listener);
		Exception exception = null;
		final ExecutionEvent event = new ExecutionEvent();
		try {
			command.execute(event);
		} catch (final ExecutionException | NotHandledException e) {
			exception = e;
		}

		assertSame(
				"Should have received a pre-execute event for the correct command",
				commandId, listener.preExecuteId);
		assertSame(
				"Should have received a pre-execute event with the correct event",
				event, listener.preExecuteEvent);
		assertSame(
				"Should have received a not-handled event for the correct command",
				commandId, listener.notHandledId);
		assertSame(
				"Should have received a not-handled event with the correct exception",
				exception, listener.notHandledException);

	}
}

Back to the top