From b46cc02560c8a52eba2e7bf0d03ab3d78d88558e Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Wed, 27 May 2015 15:13:00 -0400 Subject: [PATCH] launch bar: per target provider and test fixes - fixed tests and added tests for previous API changes - fixed unsafe call of provider methods without catching exceptions - added description of priority attribute in the extension point API - fixed default return values in DefaultLaunchConfigProvider, in case it is extended - removed unused import - fixed per target provider to support persistance - added test for per target provider Change-Id: If08b18b939e86757108a800d1092a62621a8c7d0 --- .../schema/launchBarContributions.exsd | 18 +- .../core/DefaultLaunchConfigProvider.java | 25 +- .../core/ILaunchConfigurationProvider.java | 3 +- .../core/PerTargetLaunchConfigProvider.java | 163 ++++++++--- .../ProjectPerTargetLaunchConfigProvider.java | 32 ++- .../core/internal/LaunchBarManager.java | 26 +- .../controls/LaunchBarListViewer.java | 1 - .../META-INF/MANIFEST.MF | 2 +- .../build.properties | 3 +- .../fragment.xml | 14 + .../PerTargetLaunchConfigProviderTest.java | 259 ++++++++++++++++++ .../core/internal/LaunchBarManager2Test.java | 56 ++-- .../core/internal/LaunchBarManagerTest.java | 3 +- 13 files changed, 515 insertions(+), 90 deletions(-) create mode 100644 tests/org.eclipse.launchbar.core.tests/fragment.xml create mode 100644 tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProviderTest.java diff --git a/bundles/org.eclipse.launchbar.core/schema/launchBarContributions.exsd b/bundles/org.eclipse.launchbar.core/schema/launchBarContributions.exsd index aa466cf7c57..328698bd179 100644 --- a/bundles/org.eclipse.launchbar.core/schema/launchBarContributions.exsd +++ b/bundles/org.eclipse.launchbar.core/schema/launchBarContributions.exsd @@ -59,21 +59,24 @@ - + Global descriptor type id. I.e. com.example.mytype - + Priority of the descriptor, values from 1 to 100. +Lower values represent lower priority. +I.e. descriptor with priority 10 will override +descriptor with priority 5. Priority 0 is reserved for default descriptor. - + Class that implements ILaunchDescriptorType @@ -93,7 +96,7 @@ - + Id of the descriptor type defined in descriptorType element (can be from another extension) @@ -103,14 +106,17 @@ - + Priority of the provider, values from 1 to 100. +Lower values represent lower priority. +I.e. provider with priority 10 will be checked before +provider with priority 5. Priority 0 is reserved for default provider. - + Class that implements ILaunchConfigurationProvider diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java index 5022250ea84..36ae2dbd50c 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java @@ -12,10 +12,17 @@ import org.eclipse.remote.core.IRemoteConnection; */ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider { + /** + * Only support local connection. Override to support different types of connection. + */ @Override public boolean supports(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException { // Only supports Local connection - return target.getConnectionType().getId().equals("org.eclipse.remote.LocalServices"); //$NON-NLS-1$ + if (target != null && target.getConnectionType().getId().equals("org.eclipse.remote.LocalServices")) { //$NON-NLS-1$ + return true; + } else { + return false; + } } @Override @@ -30,6 +37,10 @@ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider return descriptor.getAdapter(ILaunchConfiguration.class); } + /** + * If you do override this method and return true you would have to make sure you add launch object which matches + * this configuration, otherwise it will not be visible + */ @Override public boolean ownsLaunchConfiguration(ILaunchConfiguration configuration) throws CoreException { // return false so that the config is added as a launch object @@ -38,8 +49,8 @@ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider @Override public boolean launchConfigurationRemoved(ILaunchConfiguration configuration) throws CoreException { - // catch any left over configs - return true; + // by contract we return true if we own or use to own configuration + return ownsLaunchConfiguration(configuration); } @Override @@ -54,13 +65,13 @@ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider @Override public boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException { - // catch any left over configs - return true; + // by contract we return true if we own configuration + return ownsLaunchConfiguration(configuration); } @Override public boolean launchConfigurationChanged(ILaunchConfiguration configuration) throws CoreException { - // catch any left over configs - return true; + // by contract we return true if we own configuration + return ownsLaunchConfiguration(configuration); } } diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java index 8094d185ada..91520233b45 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java @@ -23,7 +23,6 @@ import org.eclipse.remote.core.IRemoteConnection; * subclasses instead of implementing this directly. */ public interface ILaunchConfigurationProvider { - /** * Does this config provider provide launch configurations for the combination * of descriptor and target. @@ -33,7 +32,7 @@ public interface ILaunchConfigurationProvider { * * @param descriptor * @param target - * @return + * @return true if target is supported, false otherwise. */ boolean supports(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException; diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProvider.java index b8d7a6fc363..8652d880a66 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProvider.java @@ -1,6 +1,9 @@ package org.eclipse.launchbar.core; +import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Map.Entry; @@ -13,30 +16,90 @@ import org.eclipse.remote.core.IRemoteConnectionType; import org.eclipse.remote.core.IRemoteServicesManager; public abstract class PerTargetLaunchConfigProvider extends AbstractLaunchConfigProvider { - - public static final String ATTR_CONNECTION_TYPE = "connectionType"; //$NON-NLS-1$ - public static final String ATTR_CONNECTION_NAME = "connectionName"; //$NON-NLS-1$ + public final String ATTR_CONNECTION_TYPE = getConnectionTypeAttribute(); + public final String ATTR_CONNECTION_NAME = getConnectionNameAttribute(); private final Map> configMap = new HashMap<>(); + private final Collection ownedConfigs = new LinkedHashSet<>(); + + protected String getConnectionNameAttribute() { + return "org.eclipse.launchbar.core.connectionName";//$NON-NLS-1$ + } + + protected String getConnectionTypeAttribute() { + return "org.eclipse.launchbar.core.connectionType";//$NON-NLS-1$ + } @Override public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException { - Map targetMap = configMap.get(descriptor); - if (targetMap != null) { - ILaunchConfiguration config = targetMap.get(target); - if (config != null) { - return config; - } + Map targetMap = getTargetMap(descriptor); + ILaunchConfiguration config = targetMap.get(target); + if (config != null) { + return config; } + // first search for owned configurations, to see if any match to descriptor + config = findLaunchConfiguration(descriptor, target); + if (config == null) { + config = createLaunchConfiguration(descriptor, target); + launchConfigurationAdded(config); + } + targetMap.put(target, config); + return config; + } - ILaunchConfiguration config = createLaunchConfiguration(descriptor, target); + protected Map getTargetMap(ILaunchDescriptor descriptor) { + Map targetMap = configMap.get(descriptor); if (targetMap == null) { targetMap = new HashMap<>(); configMap.put(descriptor, targetMap); } - targetMap.put(target, config); - return config; + return targetMap; + } + + protected ILaunchConfiguration findLaunchConfiguration(ILaunchDescriptor descriptor, IRemoteConnection target) + throws CoreException { + for (ILaunchConfiguration configuration : ownedConfigs) { + if (descriptorAndTargetMatchesConfiguration(descriptor, target, configuration)) { + return configuration; + } + } + return null; + } + + protected boolean descriptorAndTargetMatchesConfiguration(ILaunchDescriptor descriptor, IRemoteConnection target, + ILaunchConfiguration configuration) { + if (targetMatchesConfiguration(target, configuration) == false) + return false; + if (descriptorMatchesConfiguration(descriptor, configuration) == false) + return false; + return true; + } + + /** + * This method should be overridden to check that configuration does actually represent the descriptor. + * You don't need to check ownership since this method will be only called on owned configurations + */ + protected boolean descriptorMatchesConfiguration(ILaunchDescriptor descriptor, ILaunchConfiguration configuration) { + // we using startsWith instead of equals because new configuration using "generateLaunchConfigurationName" method which + // means only prefix guaranteed to be matching, and the prefix is the descriptor name + return configuration.getName().startsWith(descriptor.getName()); + } + + protected boolean targetMatchesConfiguration(IRemoteConnection target, ILaunchConfiguration configuration) { + String targetName; + try { + targetName = configuration.getAttribute(ATTR_CONNECTION_NAME, ""); + } catch (CoreException e) { + return false; + } + if (target != null && target.getName().equals(targetName)) { + return true; + } else if (target == null && (targetName == null || targetName.isEmpty())) { + return true; + } else { + return false; + } } @Override @@ -47,66 +110,104 @@ public abstract class PerTargetLaunchConfigProvider extends AbstractLaunchConfig workingCopy.setAttribute(ATTR_CONNECTION_NAME, target.getName()); } - public static IRemoteConnection getTarget(ILaunchConfiguration configuration) throws CoreException { + public IRemoteConnection getTarget(ILaunchConfiguration configuration) throws CoreException { IRemoteServicesManager remoteManager = Activator.getService(IRemoteServicesManager.class); String connectionTypeId = configuration.getAttribute(ATTR_CONNECTION_TYPE, ""); //$NON-NLS-1$ if (connectionTypeId.isEmpty()) { return null; } - IRemoteConnectionType connectionType = remoteManager.getConnectionType(connectionTypeId); if (connectionType == null) { return null; } - String connectionName = configuration.getAttribute(ATTR_CONNECTION_NAME, ""); //$NON-NLS-1$ if (connectionName.isEmpty()) { return null; } - return connectionType.getConnection(connectionName); } + @Override + public boolean ownsLaunchConfiguration(ILaunchConfiguration configuration) throws CoreException { + return ownedConfigs.contains(configuration); + } + + public boolean ownsLaunchConfigurationByAttributes(ILaunchConfiguration configuration) { + try { + return super.ownsLaunchConfiguration(configuration); + } catch (CoreException e) { + // will happened if called after LC is deleted + return false; + } + } + @Override public boolean launchConfigurationRemoved(ILaunchConfiguration configuration) throws CoreException { - for (Entry> descEntry : configMap.entrySet()) { - for (Entry targetEntry : descEntry.getValue().entrySet()) { - if (targetEntry.getValue().equals(configuration)) { - descEntry.getValue().remove(targetEntry.getKey()); - if (descEntry.getValue().isEmpty()) { - configMap.remove(descEntry.getKey()); + boolean owned = ownsLaunchConfiguration(configuration); + if (owned) { + ownedConfigs.remove(configuration); + for (Entry> descEntry : configMap.entrySet()) { + for (Entry targetEntry : descEntry.getValue().entrySet()) { + if (targetEntry.getValue().equals(configuration)) { + descEntry.getValue().remove(targetEntry.getKey()); + if (descEntry.getValue().isEmpty()) { + configMap.remove(descEntry.getKey()); + } + return true; } - return true; } } } - return false; + return owned; } @Override public boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException { - // TODO re-create map + if (ownsLaunchConfigurationByAttributes(configuration)) { + ownedConfigs.add(configuration); + return true; + } return false; } @Override public boolean launchConfigurationChanged(ILaunchConfiguration configuration) throws CoreException { + if (ownsLaunchConfigurationByAttributes(configuration)) { + // clear cache, target could have changed + launchConfigurationRemoved(configuration); + ownedConfigs.add(configuration); + return true; + } else if (ownedConfigs.contains(configuration)) { + // user did something that will cause us to loose ownership of this configuration + launchConfigurationRemoved(configuration); + } return false; } @Override public void launchDescriptorRemoved(ILaunchDescriptor descriptor) throws CoreException { - configMap.remove(descriptor); + Map map = configMap.remove(descriptor); + if (map == null) + return; + for (ILaunchConfiguration config : map.values()) { + ownedConfigs.remove(config); + config.delete(); // remove all auto-configs associated with descriptor + } } @Override public void launchTargetRemoved(IRemoteConnection target) throws CoreException { - for (Entry> descEntry : configMap.entrySet()) { - descEntry.getValue().remove(target); - if (descEntry.getValue().isEmpty()) { - configMap.remove(descEntry.getKey()); + for (Iterator>> iterator = configMap.entrySet() + .iterator(); iterator.hasNext();) { + Entry> descEntry = iterator.next(); + Map map = descEntry.getValue(); + ILaunchConfiguration config = map.remove(target); + if (config != null) { + config.delete(); // remove all auto-configs associated with target + } + if (map.isEmpty()) { + iterator.remove(); } } } - } diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ProjectPerTargetLaunchConfigProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ProjectPerTargetLaunchConfigProvider.java index 0e0a5eeaffc..fbde803bf02 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ProjectPerTargetLaunchConfigProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ProjectPerTargetLaunchConfigProvider.java @@ -13,21 +13,44 @@ package org.eclipse.launchbar.core; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.remote.core.IRemoteConnection; public abstract class ProjectPerTargetLaunchConfigProvider extends PerTargetLaunchConfigProvider { - @Override public boolean supports(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException { - return descriptor.getAdapter(IProject.class) != null; + return (descriptor.getAdapter(IProject.class) != null); } @Override - protected void populateLaunchConfiguration(ILaunchDescriptor descriptor, IRemoteConnection target, + protected boolean descriptorMatchesConfiguration(ILaunchDescriptor descriptor, ILaunchConfiguration configuration) { + IProject project = descriptor.getAdapter(IProject.class); + if (project == null || configuration == null) + return false; + return (project.equals(getProject(configuration))); + } + + protected IProject getProject(ILaunchConfiguration configuration) { + IResource[] mappedResources = null; + try { + mappedResources = configuration.getMappedResources(); + } catch (CoreException e) { + return null; + } + if (mappedResources == null) + return null; + for (IResource resource : mappedResources) { + if (resource instanceof IProject) + return (IProject) resource; + } + return null; + } + + @Override + protected void populateLaunchConfiguration(ILaunchDescriptor descriptor, IRemoteConnection target, ILaunchConfigurationWorkingCopy workingCopy) throws CoreException { super.populateLaunchConfiguration(descriptor, target, workingCopy); - // Add our project to the mapped resources IProject project = descriptor.getAdapter(IProject.class); IResource[] mappedResources = workingCopy.getMappedResources(); @@ -40,5 +63,4 @@ public abstract class ProjectPerTargetLaunchConfigProvider extends PerTargetLaun workingCopy.setMappedResources(newResources); } } - } diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java index 5887f548f44..c1bd9c35178 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java @@ -37,6 +37,7 @@ import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.core.ILaunchMode; import org.eclipse.launchbar.core.ILaunchBarManager; +import org.eclipse.launchbar.core.ILaunchConfigurationProvider; import org.eclipse.launchbar.core.ILaunchDescriptor; import org.eclipse.launchbar.core.ILaunchDescriptorType; import org.eclipse.launchbar.core.ILaunchObjectProvider; @@ -648,8 +649,12 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchConfiguration boolean supportsTarget(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException { String descriptorTypeId = getDescriptorTypeId(descriptor.getType()); for (LaunchConfigProviderInfo provider : configProviders.get(descriptorTypeId)) { - if (provider.getProvider().supports(descriptor, target)) { - return true; + try { + if (provider.getProvider().supports(descriptor, target)) { + return true; + } + } catch (Throwable e) { + Activator.log(e); } } return false; @@ -718,10 +723,19 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchConfiguration } String descTypeId = getDescriptorTypeId(descriptor.getType()); - for (LaunchConfigProviderInfo provider : configProviders.get(descTypeId)) { - ILaunchConfiguration config = provider.getProvider().getLaunchConfiguration(descriptor, target); - if (config != null) { - return config; + for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeId)) { + try { + ILaunchConfigurationProvider provider = providerInfo.getProvider(); + // between multiple provider who support this descriptor we need to find one + // that supports this target + if (provider.supports(descriptor, target)) { + ILaunchConfiguration config = provider.getLaunchConfiguration(descriptor, target); + if (config != null) { + return config; + } + } + } catch (Throwable e) { + Activator.log(e); } } diff --git a/bundles/org.eclipse.launchbar.ui/src/org/eclipse/launchbar/ui/internal/controls/LaunchBarListViewer.java b/bundles/org.eclipse.launchbar.ui/src/org/eclipse/launchbar/ui/internal/controls/LaunchBarListViewer.java index c02e69ec206..46fbd2d1c33 100644 --- a/bundles/org.eclipse.launchbar.ui/src/org/eclipse/launchbar/ui/internal/controls/LaunchBarListViewer.java +++ b/bundles/org.eclipse.launchbar.ui/src/org/eclipse/launchbar/ui/internal/controls/LaunchBarListViewer.java @@ -27,7 +27,6 @@ import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; -import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.events.MouseTrackAdapter; import org.eclipse.swt.events.MouseTrackListener; import org.eclipse.swt.events.PaintEvent; diff --git a/tests/org.eclipse.launchbar.core.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.launchbar.core.tests/META-INF/MANIFEST.MF index c3a97cb2e66..7cedb534a9e 100644 --- a/tests/org.eclipse.launchbar.core.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.launchbar.core.tests/META-INF/MANIFEST.MF @@ -1,7 +1,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Launch Bar Core Tests -Bundle-SymbolicName: org.eclipse.launchbar.core.tests +Bundle-SymbolicName: org.eclipse.launchbar.core.tests;singleton:=true Bundle-Version: 1.0.0.qualifier Fragment-Host: org.eclipse.launchbar.core;bundle-version="1.0.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.7 diff --git a/tests/org.eclipse.launchbar.core.tests/build.properties b/tests/org.eclipse.launchbar.core.tests/build.properties index 34d2e4d2dad..e3023e14e99 100644 --- a/tests/org.eclipse.launchbar.core.tests/build.properties +++ b/tests/org.eclipse.launchbar.core.tests/build.properties @@ -1,4 +1,5 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + fragment.xml diff --git a/tests/org.eclipse.launchbar.core.tests/fragment.xml b/tests/org.eclipse.launchbar.core.tests/fragment.xml new file mode 100644 index 00000000000..25d5965ecbc --- /dev/null +++ b/tests/org.eclipse.launchbar.core.tests/fragment.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProviderTest.java b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProviderTest.java new file mode 100644 index 00000000000..a2fe64af600 --- /dev/null +++ b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/PerTargetLaunchConfigProviderTest.java @@ -0,0 +1,259 @@ +/******************************************************************************* + * Copyright (c) 2015 QNX Software Systems 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: + * Elena Laskavaia + *******************************************************************************/ +package org.eclipse.launchbar.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.launchbar.core.internal.Activator; +import org.eclipse.launchbar.core.internal.LaunchBarManager2Test; +import org.eclipse.remote.core.IRemoteConnection; +import org.eclipse.remote.core.IRemoteConnectionType; +import org.eclipse.remote.core.IRemoteServicesManager; +import org.junit.After; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.JVM) +public class PerTargetLaunchConfigProviderTest { + private IRemoteServicesManager remoteServiceManager; + private IRemoteConnection localTarget; + private String launchName; + private IRemoteConnection otherTarget; + private ILaunchConfigurationType launchConfigType; + private ILaunchDescriptorType descriptorType; + private ILaunchDescriptor descriptor; + private PerTargetLaunchConfigProvider1 provider; + + @Before + public void basicSetupOnly() throws CoreException { + remoteServiceManager = spy(Activator.getService(IRemoteServicesManager.class)); + localTarget = remoteServiceManager.getLocalConnectionType().getConnections().get(0); + // other mocked remote connections + otherTarget = mock(IRemoteConnection.class); + IRemoteConnectionType rtype = mock(IRemoteConnectionType.class); + doReturn(rtype).when(otherTarget).getConnectionType(); + doReturn("otherTargetType").when(rtype).getId(); + doReturn("otherTarget").when(otherTarget).getName(); + // launch stuff + launchName = "test"; + // launch config type + launchConfigType = getLaunchManager().getLaunchConfigurationType("org.eclipse.launchbar.core.tests.lctype1"); + // launch descriptor and type + descriptorType = mock(ILaunchDescriptorType.class); + descriptor = mock(ILaunchDescriptor.class); + doReturn(descriptorType).when(descriptor).getType(); + doReturn(launchName).when(descriptor).getName(); + // configProvider + provider = new PerTargetLaunchConfigProvider1(); + } + + private ILaunchManager getLaunchManager() { + return DebugPlugin.getDefault().getLaunchManager(); + } + + @After + public void after() throws CoreException { + ILaunchConfiguration[] launchConfigurations = getLaunchManager().getLaunchConfigurations(); + for (ILaunchConfiguration lc : launchConfigurations) { + lc.delete(); + } + } + + public class PerTargetLaunchConfigProvider1 extends PerTargetLaunchConfigProvider { + @Override + public boolean supports(ILaunchDescriptor descriptor, IRemoteConnection target) throws CoreException { + return true; + } + + @Override + public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor, IRemoteConnection target) + throws CoreException { + return launchConfigType; + } + }; + + @Test + public void testPopulateLaunchConfiguration() throws CoreException { + ILaunchConfiguration launchConfig = launchConfigType.newInstance(null, launchName).doSave(); + ILaunchConfigurationWorkingCopy launchConfigWC = launchConfig.getWorkingCopy(); + provider.populateLaunchConfiguration(descriptor, localTarget, launchConfigWC); + //assertEquals(launchConfig.getName(), launchConfigWC.getAttribute(LaunchBarManager2Test.ATTR_ORIGINAL_NAME, "")); + //assertEquals(provider.getClass().getName(), launchConfigWC.getAttribute(LaunchBarManager2Test.ATTR_PROVIDER_CLASS, "")); + assertTrue(provider.ownsLaunchConfigurationByAttributes(launchConfigWC)); + } + + @Test + public void testOwnsLaunchConfiguration() throws CoreException { + ILaunchConfiguration launchConfig = launchConfigType.newInstance(null, launchName).doSave(); + assertFalse(provider.ownsLaunchConfiguration(launchConfig)); + ILaunchConfiguration launchConfiguration = provider.getLaunchConfiguration(descriptor, localTarget); + assertTrue(provider.ownsLaunchConfiguration(launchConfiguration)); + } + + @Test + public void testGetLaunchConfiguration() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, otherTarget); + assertNotNull(launchConfiguration1); + assertNotNull(launchConfiguration2); + assertNotEquals(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testGetLaunchConfigurationReuse() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertSame(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testGetLaunchConfigurationPersistance() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + // reset provider + provider = new PerTargetLaunchConfigProvider1(); + provider.launchConfigurationAdded(launchConfiguration1); // simulate provider initialization on startup + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertEquals(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testGetTarget() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + assertSame(localTarget, provider.getTarget(launchConfiguration1)); + } + + @Test + public void testLaunchConfigurationRemoved() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + provider.launchConfigurationRemoved(launchConfiguration1); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertNotEquals(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testLaunchConfigurationChanged_NotReallyChanged() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + provider.launchConfigurationChanged(launchConfiguration1); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertSame(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testLaunchConfigurationChanged_Target() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + ILaunchConfigurationWorkingCopy wc = launchConfiguration1.getWorkingCopy(); + wc.setAttribute(provider.getConnectionNameAttribute(), otherTarget.getName()); + wc.doSave(); + provider.launchConfigurationChanged(launchConfiguration1); + //provider.launchConfigurationChanged(lc3); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertNotEquals(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testLaunchConfigurationChanged_OrgName() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + ILaunchConfigurationWorkingCopy wc = launchConfiguration1.getWorkingCopy(); + wc.setAttribute(LaunchBarManager2Test.ATTR_ORIGINAL_NAME, "bla"); + wc.doSave(); + provider.launchConfigurationChanged(launchConfiguration1); + // we should have lost ownership + assertFalse(provider.ownsLaunchConfiguration(launchConfiguration1)); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + assertNotEquals(launchConfiguration1, launchConfiguration2); + } + + @Test + public void testLaunchDescriptorRemoved() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration1); + provider.launchDescriptorRemoved(descriptor); + assertEquals(0, provider.getTargetMap(descriptor).size()); + assertFalse(provider.ownsLaunchConfiguration(launchConfiguration1)); + assertFalse(launchConfiguration1.exists()); + } + + @Test + public void testLaunchDescriptorRemoved2() throws CoreException { + provider.launchDescriptorRemoved(descriptor); + assertEquals(0, provider.getTargetMap(descriptor).size()); + } + + @Test + public void testLaunchTargetRemoved() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, otherTarget); + assertNotNull(launchConfiguration1); + provider.launchTargetRemoved(otherTarget); + assertEquals(0, provider.getTargetMap(descriptor).size()); + assertFalse(launchConfiguration1.exists()); + } + + @Test + public void testLaunchTargetRemoved2() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, otherTarget); + assertNotNull(launchConfiguration1); + ILaunchConfiguration launchConfiguration2 = provider.getLaunchConfiguration(descriptor, localTarget); + assertNotNull(launchConfiguration2); + provider.launchTargetRemoved(otherTarget); + assertEquals(1, provider.getTargetMap(descriptor).size()); + assertFalse(launchConfiguration1.exists()); + assertTrue(launchConfiguration2.exists()); + } + + @Test + public void testLaunchTargetRemoved3() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, otherTarget); + assertNotNull(launchConfiguration1); + provider.launchTargetRemoved(localTarget); + assertEquals(1, provider.getTargetMap(descriptor).size()); + } + + @Test + public void testLCRemoved() throws CoreException { + ILaunchConfiguration launchConfiguration1 = provider.getLaunchConfiguration(descriptor, otherTarget); + assertNotNull(launchConfiguration1); + assertTrue(provider.ownsLaunchConfiguration(launchConfiguration1)); + launchConfiguration1.delete(); + assertTrue(provider.ownsLaunchConfiguration(launchConfiguration1)); + provider.launchConfigurationRemoved(launchConfiguration1); + assertFalse(provider.ownsLaunchConfiguration(launchConfiguration1)); + } +} diff --git a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManager2Test.java b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManager2Test.java index 8761480f379..478d08f5c46 100644 --- a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManager2Test.java +++ b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManager2Test.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -45,7 +46,6 @@ import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.PlatformObject; -import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; @@ -66,7 +66,7 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; -@SuppressWarnings({"restriction", "nls"}) +@SuppressWarnings({ "restriction", "nls" }) @FixMethodOrder(MethodSorters.JVM) public class LaunchBarManager2Test { private LaunchBarManager manager; @@ -121,14 +121,16 @@ public class LaunchBarManager2Test { basicSetup(); } - protected void mockProviderElement(String descriptorTypeId, int priority, ILaunchConfigurationProvider provider) throws CoreException { + protected void mockProviderElement(String descriptorTypeId, int priority, ILaunchConfigurationProvider provider) + throws CoreException { IConfigurationElement element = mockElementAndAdd("configProvider"); doReturn(descriptorTypeId).when(element).getAttribute("descriptorType"); doReturn(Integer.toString(priority)).when(element).getAttribute("priority"); doReturn(provider).when(element).createExecutableExtension("class"); } - protected ILaunchConfigurationProvider mockProviderElement(String descriptorTypeId, int priority, ILaunchDescriptor descriptor, IRemoteConnection target, + protected ILaunchConfigurationProvider mockProviderElement(String descriptorTypeId, int priority, ILaunchDescriptor descriptor, + IRemoteConnection target, ILaunchConfiguration config, Object launchObj) throws CoreException { ILaunchConfigurationProvider provider = mock(ILaunchConfigurationProvider.class); mockProviderElement(descriptorTypeId, priority, provider); @@ -139,7 +141,8 @@ public class LaunchBarManager2Test { return provider; } - protected IConfigurationElement mockDescriptorTypeElement(String descriptorTypeId, int priority, ILaunchDescriptorType descriptorType) + protected IConfigurationElement mockDescriptorTypeElement(String descriptorTypeId, int priority, + ILaunchDescriptorType descriptorType) throws CoreException { IConfigurationElement element = mockElementAndAdd("descriptorType"); doReturn(descriptorTypeId).when(element).getAttribute("id"); @@ -171,7 +174,8 @@ public class LaunchBarManager2Test { ILaunchConfiguration lc = mock(ILaunchConfiguration.class); doReturn(string).when(lc).getName(); doReturn(lctype2).when(lc).getType(); - doReturn("").when(lc).getAttribute(eq(ORIGINAL_NAME), eq("")); + doReturn("").when(lc).getAttribute(eq(ATTR_ORIGINAL_NAME), eq("")); + doReturn("").when(lc).getAttribute(eq(ATTR_PROVIDER_CLASS), eq("")); return lc; } @@ -280,9 +284,7 @@ public class LaunchBarManager2Test { doReturn(descriptor).when(descriptorType).getDescriptor(launchObject); // configProvider provider = mockProviderElement(descriptorTypeId, 10, descriptor, otherTarget, launchConfig, launchObject); - mockLaunchObjectOnDescriptor(launchObject); - // default descriptor String defaultDescTypeId = "defaultDescType"; mockDescriptorTypeElement(defaultDescTypeId, 0, new DefaultLaunchDescriptorType()); @@ -543,9 +545,9 @@ public class LaunchBarManager2Test { return "pbtype"; } } + public static final String ATTR_ORIGINAL_NAME = org.eclipse.launchbar.core.internal.Activator.PLUGIN_ID + ".originalName"; //$NON-NLS-1$ + public static final String ATTR_PROVIDER_CLASS = org.eclipse.launchbar.core.internal.Activator.PLUGIN_ID + ".providerClass"; //$NON-NLS-1$ - String ORIGINAL_NAME = org.eclipse.launchbar.core.internal.Activator.PLUGIN_ID + ".originalName"; - String PROJECT_CONFIG = org.eclipse.launchbar.core.internal.Activator.PLUGIN_ID + ".projectConfig"; protected void projectMappingSetup() throws CoreException { descriptorType = new ProjectBasedLaunchDescriptorType(); descriptorTypeId = ((ProjectBasedLaunchDescriptorType) descriptorType).getId(); @@ -555,8 +557,7 @@ public class LaunchBarManager2Test { mockDescriptorTypeElement(descriptorTypeId, 10, descriptorType); //lc = provider.createLaunchConfiguration(lman, descType.getDescriptor(aaa)); mockLCProject(launchConfig, aaa); - mockLCAttribute(launchConfig, ORIGINAL_NAME, aaa.getName()); - mockLCAttribute(launchConfig, PROJECT_CONFIG, true); + mockLCAttribute(launchConfig, ATTR_ORIGINAL_NAME, aaa.getName()); assertEquals(0, manager.getLaunchDescriptors().length); provider = new ProjectPerTargetLaunchConfigProvider() { @Override @@ -571,6 +572,7 @@ public class LaunchBarManager2Test { } }; mockProviderElement(descriptorTypeId, 10, provider); + mockLCAttribute(launchConfig, ATTR_PROVIDER_CLASS, provider.getClass().getName()); init(); } @@ -793,7 +795,7 @@ public class LaunchBarManager2Test { @Test public void testGetActiveLaunchModeFromDescActive() throws CoreException { globalmodes.clear(); - ILaunchMode mode = mockLaunchModes(launchConfigType, "foo","run")[0]; + ILaunchMode mode = mockLaunchModes(launchConfigType, "foo", "run")[0]; manager.launchObjectAdded(launchObject); manager.setActiveLaunchDescriptor(descriptor); manager.setActiveLaunchMode(mode); @@ -902,30 +904,26 @@ public class LaunchBarManager2Test { public void testLaunchConfigurationAddedBad() throws CoreException { doThrow(new NullPointerException()).when(provider).ownsLaunchConfiguration(any(ILaunchConfiguration.class)); manager.launchConfigurationAdded(launchConfig); + verify(provider).launchConfigurationAdded(launchConfig); verify(provider).ownsLaunchConfiguration(launchConfig); } @Test - public void testLaunchConfigurationRemoved_fails() throws CoreException { - manager.launchConfigurationRemoved(launchConfig); - try { - verify(provider).launchConfigurationRemoved(launchConfig); - fail(); - } catch (Throwable e) { - // temp fail test - } + public void testLaunchConfigurationChanged() throws CoreException { + manager.launchConfigurationChanged(launchConfig); + verify(provider).launchConfigurationChanged(launchConfig); } @Test - public void testLaunchConfigurationRemovedBad_fails() throws CoreException { - doThrow(new NullPointerException()).when(provider).launchConfigurationRemoved(any(ILaunchConfiguration.class)); + public void testLaunchConfigurationRemoved() throws CoreException { manager.launchConfigurationRemoved(launchConfig); - try { - verify(provider).launchConfigurationRemoved(launchConfig); - fail(); - } catch (Throwable e) { - // temp fail test - } + verify(provider).launchConfigurationRemoved(launchConfig); } + @Test + public void testLaunchConfigurationRemovedBad() throws CoreException { + doThrow(new NullPointerException()).when(provider).launchConfigurationRemoved(any(ILaunchConfiguration.class)); + manager.launchConfigurationRemoved(launchConfig); + verify(provider).launchConfigurationRemoved(launchConfig); + } } diff --git a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManagerTest.java b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManagerTest.java index d5a43b01274..81bce296aa4 100644 --- a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManagerTest.java +++ b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchBarManagerTest.java @@ -3,6 +3,7 @@ package org.eclipse.launchbar.core.internal; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; + import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -33,7 +34,6 @@ import org.mockito.stubbing.Answer; @SuppressWarnings("nls") public class LaunchBarManagerTest { - @Test public void startupTest() throws Exception { // Make sure the manager starts up and defaults everything to null @@ -55,6 +55,7 @@ public class LaunchBarManagerTest { String launchConfigName = "launchConfig"; doReturn(launchConfigName).when(launchConfig).getName(); doReturn(launchConfigName).when(launchConfig).getAttribute(eq("org.eclipse.launchbar.core.originalName"), anyString()); + doReturn("").when(launchConfig).getAttribute(eq("org.eclipse.launchbar.core.providerClass"), anyString()); doReturn(true).when(launchConfigType).isPublic(); doReturn(launchConfigType).when(launchConfig).getType(); doReturn("launchConfigType").when(launchConfigType).getIdentifier();