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>tags/v4.1.0.201509280440-r
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||
import org.eclipse.jgit.errors.InvalidObjectIdException; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
public class ObjectIdTest { | public class ObjectIdTest { | ||||
assertEquals(x.toLowerCase(), oid.name()); | 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 | @Test | ||||
public void testGetByte() { | public void testGetByte() { | ||||
byte[] raw = new byte[20]; | byte[] raw = new byte[20]; |
super(msg(bytes, offset, length)); | 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) { | private static String msg(byte[] bytes, int offset, int length) { | ||||
try { | try { | ||||
return MessageFormat.format( | return MessageFormat.format( |
package org.eclipse.jgit.lib; | package org.eclipse.jgit.lib; | ||||
import org.eclipse.jgit.errors.InvalidObjectIdException; | import org.eclipse.jgit.errors.InvalidObjectIdException; | ||||
import org.eclipse.jgit.internal.JGitText; | |||||
import org.eclipse.jgit.util.NB; | import org.eclipse.jgit.util.NB; | ||||
import org.eclipse.jgit.util.RawParseUtils; | import org.eclipse.jgit.util.RawParseUtils; | ||||
import java.io.ObjectInputStream; | import java.io.ObjectInputStream; | ||||
import java.io.ObjectOutputStream; | import java.io.ObjectOutputStream; | ||||
import java.io.Serializable; | import java.io.Serializable; | ||||
import java.text.MessageFormat; | |||||
/** | /** | ||||
* A SHA-1 abstraction. | * A SHA-1 abstraction. | ||||
* @return the converted object id. | * @return the converted object id. | ||||
*/ | */ | ||||
public static ObjectId fromString(final String str) { | 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); | return fromHexString(Constants.encodeASCII(str), 0); | ||||
} | } | ||||