Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08e7f6de0bda974c16a512e91751462e56ca0c3c (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
/*******************************************************************************
 * Copyright (c) 2004, 2016 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
 *     Patrik Suzzi <psuzzi@gmail.com> - Bug 489250
 *******************************************************************************/
package org.eclipse.ui.tests.commands;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.ExternalActionManager;
import org.eclipse.jface.util.Util;
import org.eclipse.ui.statushandlers.StatusAdapter;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.eclipse.ui.tests.statushandlers.TestStatusHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * A tests whether is active will log an exception if the command is not
 * defined.
 *
 * @since 3.1
 */
@RunWith(JUnit4.class)
public final class Bug73756Test extends UITestCase {

	private static String CMD_ID = "a command that is not defined";

	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle(ExternalActionManager.class.getName());

	private static int SEVERITY = IStatus.ERROR;

	private static String MESSAGE = MessageFormat.format(Util.translateString(
			RESOURCE_BUNDLE, "undefinedCommand.WarningMessage", null), //$NON-NLS-1$
			CMD_ID);

	private static String PLUGIN_ID = "org.eclipse.jface";

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

	@Override
	protected void doTearDown() throws Exception {
		super.doTearDown();
		TestStatusHandler.uninstall();
	}

	/**
	 * Tests that calling <code>isActive()</code> on an undefined command
	 * causes a log message to be written. This simple calls
	 * <code>isActive()</code> for a bogus command identifier. A log listener
	 * flips a boolean flag if a log message is written.
	 */
	@Test
	public final void testUndefinedCommandIsActiveLogged() throws Exception {
		TestStatusHandler.install();

		// Check if a bogus command is active.
		ExternalActionManager.getInstance().getCallback().isActive(CMD_ID);

		// Check if a correct status is logged
		assertEquals(TestStatusHandler.getLastHandledStyle(), StatusManager.LOG);
		assertStatusAdapter(TestStatusHandler.getLastHandledStatusAdapter());
	}

	/**
	 * Checks whether the last handled status is correct
	 */
	private void assertStatusAdapter(StatusAdapter statusAdapter) {
		assertNotNull("A warning should have been logged.", statusAdapter);
		IStatus status = statusAdapter.getStatus();
		assertEquals(status.getSeverity(), SEVERITY);
		assertEquals(status.getPlugin(), PLUGIN_ID);
		assertEquals(status.getMessage(), MESSAGE);
	}
}

Back to the top