mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-10 18:45:26 +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;
|
import org.osgi.service.prefs.Preferences;
|
||||||
|
|
||||||
public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
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 typeId;
|
||||||
private final String id;
|
private final String id;
|
||||||
final Preferences attributes;
|
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
|
* This should only be used to create the null target. There are no attributes supported on the
|
||||||
* null target.
|
* null target.
|
||||||
*/
|
*/
|
||||||
public LaunchTarget(String typeId, String id) {
|
private LaunchTarget(String typeId, String id) {
|
||||||
this.typeId = typeId;
|
this.typeId = typeId;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.attributes = null;
|
this.attributes = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LaunchTarget(String typeId, String id, Preferences attributes) {
|
public LaunchTarget(String typeId, String id, Preferences attributes) {
|
||||||
|
if (typeId == null || id == null || attributes == null)
|
||||||
|
throw new NullPointerException();
|
||||||
this.typeId = typeId;
|
this.typeId = typeId;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.attributes = attributes;
|
this.attributes = attributes;
|
||||||
|
@ -62,8 +69,8 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
result = prime * result + id.hashCode();
|
||||||
result = prime * result + ((typeId == null) ? 0 : typeId.hashCode());
|
result = prime * result + typeId.hashCode();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,17 +83,10 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
LaunchTarget other = (LaunchTarget) obj;
|
LaunchTarget other = (LaunchTarget) obj;
|
||||||
if (id == null) {
|
if (!id.equals(other.id))
|
||||||
if (other.id != null)
|
|
||||||
return false;
|
|
||||||
} else if (!id.equals(other.id))
|
|
||||||
return false;
|
return false;
|
||||||
if (typeId == null) {
|
if (!typeId.equals(other.typeId))
|
||||||
if (other.typeId != null)
|
|
||||||
return false;
|
|
||||||
} else if (!typeId.equals(other.typeId))
|
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,18 +26,18 @@ public interface ILaunchTarget extends IAdaptable {
|
||||||
/**
|
/**
|
||||||
* The null target, which is the default when no other target is available.
|
* 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.
|
* The id for the target. It is unique for each type.
|
||||||
*
|
*
|
||||||
* @return id for the target.
|
* @return id for the target.
|
||||||
*/
|
*/
|
||||||
String getId();
|
String getId();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The user consumable name of the target.
|
* The user consumable name of the target.
|
||||||
*
|
*
|
||||||
* @deprecated this will be the same as the id
|
* @deprecated this will be the same as the id
|
||||||
* @return name of the target
|
* @return name of the target
|
||||||
*/
|
*/
|
||||||
|
@ -55,7 +55,7 @@ public interface ILaunchTarget extends IAdaptable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a string attribute of this target
|
* Return a string attribute of this target
|
||||||
*
|
*
|
||||||
* @param key
|
* @param key
|
||||||
* key
|
* key
|
||||||
* @param defValue
|
* @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
|
* 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.
|
* changing the id, which results in a new launch target when saved.
|
||||||
*
|
*
|
||||||
* @return launch target working copy
|
* @return launch target working copy
|
||||||
*/
|
*/
|
||||||
ILaunchTargetWorkingCopy getWorkingCopy();
|
ILaunchTargetWorkingCopy getWorkingCopy();
|
||||||
|
|
|
@ -5,23 +5,31 @@
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* 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.*;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.CoreException;
|
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.ILaunchTarget;
|
||||||
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.osgi.service.prefs.Preferences;
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
public class LaunchTargetTest {
|
public class LaunchTargetTest {
|
||||||
|
private org.osgi.service.prefs.Preferences pref;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
pref = Mockito.mock(Preferences.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoveLaunchTarget() throws CoreException {
|
public void testRemoveLaunchTarget() throws CoreException {
|
||||||
|
@ -46,4 +54,56 @@ public class LaunchTargetTest {
|
||||||
assertFalse(targetSet.contains(target2));
|
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