1 /******************************************************************************* 2 * Copyright (c) 2004, 2016 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Patrik Suzzi <psuzzi@gmail.com> - Bug 489250 14 *******************************************************************************/ 15 package org.eclipse.ui.tests.commands; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertNotNull; 19 20 import java.text.MessageFormat; 21 import java.util.ResourceBundle; 22 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.jface.action.ExternalActionManager; 25 import org.eclipse.jface.util.Util; 26 import org.eclipse.ui.statushandlers.StatusAdapter; 27 import org.eclipse.ui.statushandlers.StatusManager; 28 import org.eclipse.ui.tests.statushandlers.TestStatusHandler; 29 import org.junit.Before; 30 import org.junit.Test; 31 32 /** 33 * A tests whether is active will log an exception if the command is not 34 * defined. 35 * 36 * @since 3.1 37 */ 38 public final class Bug73756Test { 39 40 private static String CMD_ID = "a command that is not defined"; 41 42 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle 43 .getBundle(ExternalActionManager.class.getName()); 44 45 private static int SEVERITY = IStatus.ERROR; 46 47 private static String MESSAGE = MessageFormat.format( 48 Util.translateString(RESOURCE_BUNDLE, "undefinedCommand.WarningMessage", null), //$NON-NLS-1$ 49 CMD_ID); 50 51 private static String PLUGIN_ID = "org.eclipse.jface"; 52 53 @Before doTearDown()54 public void doTearDown() throws Exception { 55 TestStatusHandler.uninstall(); 56 } 57 58 /** 59 * Tests that calling <code>isActive()</code> on an undefined command causes a 60 * log message to be written. This simple calls <code>isActive()</code> for a 61 * bogus command identifier. A log listener flips a boolean flag if a log 62 * message is written. 63 */ 64 @Test testUndefinedCommandIsActiveLogged()65 public final void testUndefinedCommandIsActiveLogged() throws Exception { 66 TestStatusHandler.install(); 67 68 // Check if a bogus command is active. 69 ExternalActionManager.getInstance().getCallback().isActive(CMD_ID); 70 71 // Check if a correct status is logged 72 assertEquals(TestStatusHandler.getLastHandledStyle(), StatusManager.LOG); 73 assertStatusAdapter(TestStatusHandler.getLastHandledStatusAdapter()); 74 } 75 76 /** 77 * Checks whether the last handled status is correct 78 */ assertStatusAdapter(StatusAdapter statusAdapter)79 private void assertStatusAdapter(StatusAdapter statusAdapter) { 80 assertNotNull("A warning should have been logged.", statusAdapter); 81 IStatus status = statusAdapter.getStatus(); 82 assertEquals(status.getSeverity(), SEVERITY); 83 assertEquals(status.getPlugin(), PLUGIN_ID); 84 assertEquals(status.getMessage(), MESSAGE); 85 } 86 } 87