From 761f070866f224b4c9f4abed6e1231518677e848 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Tue, 30 Jun 2015 14:42:39 -0700 Subject: [PATCH] Throw InvalidObjectIdException from ObjectId.fromString("tooshort") ObjectId.fromString already throws InvalidObjectIdException for most malformed object ids, but for this kind it previously threw IllegalArgumentException. Since InvalidObjectIdException is a child of IllegalArgumentException, callers that catch IllegalArgumentException will continue to work. Change-Id: I24e1422d51607c86a1cb816a495703279e461f01 Signed-off-by: Jonathan Nieder --- .../tst/org/eclipse/jgit/lib/ObjectIdTest.java | 17 +++++++++++++++++ .../jgit/errors/InvalidObjectIdException.java | 9 +++++++++ .../src/org/eclipse/jgit/lib/ObjectId.java | 8 +++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java index abf57d6cd..2198b87aa 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java @@ -49,6 +49,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.eclipse.jgit.errors.InvalidObjectIdException; + import org.junit.Test; public class ObjectIdTest { @@ -124,6 +126,21 @@ public class ObjectIdTest { assertEquals(x.toLowerCase(), oid.name()); } + @Test(expected = InvalidObjectIdException.class) + public void testFromString_short() { + ObjectId.fromString("cafe1234"); + } + + @Test(expected = InvalidObjectIdException.class) + public void testFromString_nonHex() { + ObjectId.fromString("0123456789abcdefghij0123456789abcdefghij"); + } + + @Test(expected = InvalidObjectIdException.class) + public void testFromString_shortNonHex() { + ObjectId.fromString("6789ghij"); + } + @Test public void testGetByte() { byte[] raw = new byte[20]; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/errors/InvalidObjectIdException.java b/org.eclipse.jgit/src/org/eclipse/jgit/errors/InvalidObjectIdException.java index ca1c26ae3..390545ffa 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/errors/InvalidObjectIdException.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/errors/InvalidObjectIdException.java @@ -68,6 +68,15 @@ public class InvalidObjectIdException extends IllegalArgumentException { super(msg(bytes, offset, length)); } + /** + * @param id the invalid id. + * + * @since 4.1 + */ + public InvalidObjectIdException(String id) { + super(MessageFormat.format(JGitText.get().invalidId, id)); + } + private static String msg(byte[] bytes, int offset, int length) { try { return MessageFormat.format( diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java index bdbffee49..de7e207ac 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java @@ -45,7 +45,6 @@ package org.eclipse.jgit.lib; import org.eclipse.jgit.errors.InvalidObjectIdException; -import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.RawParseUtils; @@ -53,7 +52,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.text.MessageFormat; /** * A SHA-1 abstraction. @@ -230,9 +228,9 @@ public class ObjectId extends AnyObjectId implements Serializable { * @return the converted object id. */ public static ObjectId fromString(final String str) { - if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) - throw new IllegalArgumentException( - MessageFormat.format(JGitText.get().invalidId, str)); + if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) { + throw new InvalidObjectIdException(str); + } return fromHexString(Constants.encodeASCII(str), 0); }