diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTarget.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTarget.java index 987414558bc..5e1656d9c87 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTarget.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTarget.java @@ -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; } - } diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/ILaunchTarget.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/ILaunchTarget.java index 6cb14a1fb4d..4a4f32ff876 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/ILaunchTarget.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/target/ILaunchTarget.java @@ -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(); diff --git a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchTargetTest.java b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/target/LaunchTargetTest.java similarity index 51% rename from tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchTargetTest.java rename to tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/target/LaunchTargetTest.java index 65c7209fa65..f9fca4c1f6e 100644 --- a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/LaunchTargetTest.java +++ b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/internal/target/LaunchTargetTest.java @@ -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(); + } }