]> source.dussan.org Git - jgit.git/commitdiff
Throw InvalidObjectIdException from ObjectId.fromString("tooshort") 44/51144/3
authorJonathan Nieder <jrn@google.com>
Tue, 30 Jun 2015 21:42:39 +0000 (14:42 -0700)
committerJonathan Nieder <jrn@google.com>
Mon, 6 Jul 2015 19:41:06 +0000 (12:41 -0700)
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 <jrn@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectIdTest.java
org.eclipse.jgit/src/org/eclipse/jgit/errors/InvalidObjectIdException.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java

index abf57d6cd3e8c375fa8fa7aba71ea48a1cb4dee8..2198b87aa5b9f7e46b43112355b7577deb946520 100644 (file)
@@ -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];
index ca1c26ae37c99f2e5694c8763a60f5305beb84f1..390545ffafc5ece076fa7f31636c99e1162952b8 100644 (file)
@@ -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(
index bdbffee490d96ef9b74d967f5211c1b98ed93ca3..de7e207ac4d447ee02704975ca0a4228111c42d7 100644 (file)
@@ -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);
        }