mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-01 14:15:23 +02:00
launchbar: added tests, cleaned up API
added some tests for LaunchTarget hide some API's for LaunchTarget added some asserts to prevent misuse Change-Id: I8b5b59ea8901133e79d03afe55e6c1fca2762bc1
This commit is contained in:
parent
83f6bf8deb
commit
a91b8a801a
3 changed files with 82 additions and 22 deletions
|
@ -13,7 +13,12 @@ import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
|
|||
import org.osgi.service.prefs.Preferences;
|
||||
|
||||
public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
||||
|
||||
public static final ILaunchTarget NULL_TARGET = new LaunchTarget("null", "---") {
|
||||
@Override
|
||||
public ILaunchTargetWorkingCopy getWorkingCopy() {
|
||||
throw new UnsupportedOperationException("getWorkingCopy is not supported for NULL_TARGET");
|
||||
};
|
||||
};
|
||||
private final String typeId;
|
||||
private final String id;
|
||||
final Preferences attributes;
|
||||
|
@ -22,13 +27,15 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
|||
* This should only be used to create the null target. There are no attributes supported on the
|
||||
* null target.
|
||||
*/
|
||||
public LaunchTarget(String typeId, String id) {
|
||||
private LaunchTarget(String typeId, String id) {
|
||||
this.typeId = typeId;
|
||||
this.id = id;
|
||||
this.attributes = null;
|
||||
}
|
||||
|
||||
public LaunchTarget(String typeId, String id, Preferences attributes) {
|
||||
if (typeId == null || id == null || attributes == null)
|
||||
throw new NullPointerException();
|
||||
this.typeId = typeId;
|
||||
this.id = id;
|
||||
this.attributes = attributes;
|
||||
|
@ -62,8 +69,8 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
|||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = prime * result + ((typeId == null) ? 0 : typeId.hashCode());
|
||||
result = prime * result + id.hashCode();
|
||||
result = prime * result + typeId.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -76,17 +83,10 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
|||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
LaunchTarget other = (LaunchTarget) obj;
|
||||
if (id == null) {
|
||||
if (other.id != null)
|
||||
return false;
|
||||
} else if (!id.equals(other.id))
|
||||
if (!id.equals(other.id))
|
||||
return false;
|
||||
if (typeId == null) {
|
||||
if (other.typeId != null)
|
||||
return false;
|
||||
} else if (!typeId.equals(other.typeId))
|
||||
if (!typeId.equals(other.typeId))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,18 +26,18 @@ public interface ILaunchTarget extends IAdaptable {
|
|||
/**
|
||||
* The null target, which is the default when no other target is available.
|
||||
*/
|
||||
public static final ILaunchTarget NULL_TARGET = new LaunchTarget("null", "---"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
public static final ILaunchTarget NULL_TARGET = LaunchTarget.NULL_TARGET;
|
||||
|
||||
/**
|
||||
* The id for the target. It is unique for each type.
|
||||
*
|
||||
*
|
||||
* @return id for the target.
|
||||
*/
|
||||
String getId();
|
||||
|
||||
/**
|
||||
* The user consumable name of the target.
|
||||
*
|
||||
*
|
||||
* @deprecated this will be the same as the id
|
||||
* @return name of the target
|
||||
*/
|
||||
|
@ -55,7 +55,7 @@ public interface ILaunchTarget extends IAdaptable {
|
|||
|
||||
/**
|
||||
* Return a string attribute of this target
|
||||
*
|
||||
*
|
||||
* @param key
|
||||
* key
|
||||
* @param defValue
|
||||
|
@ -67,7 +67,7 @@ public interface ILaunchTarget extends IAdaptable {
|
|||
/**
|
||||
* Create a working copy of this launch target to allow setting of attributes. It also allows
|
||||
* changing the id, which results in a new launch target when saved.
|
||||
*
|
||||
*
|
||||
* @return launch target working copy
|
||||
*/
|
||||
ILaunchTargetWorkingCopy getWorkingCopy();
|
||||
|
|
|
@ -5,23 +5,31 @@
|
|||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.launchbar.core.internal;
|
||||
package org.eclipse.launchbar.core.internal.target;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.launchbar.core.internal.Activator;
|
||||
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
||||
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.osgi.service.prefs.Preferences;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class LaunchTargetTest {
|
||||
private org.osgi.service.prefs.Preferences pref;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
pref = Mockito.mock(Preferences.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveLaunchTarget() throws CoreException {
|
||||
|
@ -46,4 +54,56 @@ public class LaunchTargetTest {
|
|||
assertFalse(targetSet.contains(target2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
LaunchTarget t1 = new LaunchTarget("a", "b", pref);
|
||||
LaunchTarget t2 = new LaunchTarget("a", "b", pref);
|
||||
assertEquals(t1, t2);
|
||||
LaunchTarget t3 = new LaunchTarget("a", "a", pref);
|
||||
assertNotEquals(t1, t3);
|
||||
LaunchTarget t4 = new LaunchTarget("b", "a", pref);
|
||||
assertNotEquals(t4, t3);
|
||||
assertNotEquals(t4, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsHashode() {
|
||||
LaunchTarget t1 = new LaunchTarget("a", "b", pref);
|
||||
LaunchTarget t2 = new LaunchTarget("a", "b", pref);
|
||||
assertEquals(t1.hashCode(), t2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasic() {
|
||||
LaunchTarget t1 = new LaunchTarget("a", "b", pref);
|
||||
ILaunchTarget save = t1.getWorkingCopy().save();
|
||||
assertEquals(t1, save);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullTarget() {
|
||||
ILaunchTarget nt = ILaunchTarget.NULL_TARGET;
|
||||
assertEquals("b", nt.getAttribute("a", "b"));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testNPEInConstrPref() {
|
||||
new LaunchTarget("a", "b", null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testNPEInConstrType() {
|
||||
new LaunchTarget(null, "b", pref);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testNPEInConstrId() {
|
||||
new LaunchTarget("type", null, pref);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testWConNULL() {
|
||||
ILaunchTarget nt = ILaunchTarget.NULL_TARGET;
|
||||
nt.getWorkingCopy();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue