diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java index c83bda332..872b97af0 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java +++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java @@ -158,7 +158,7 @@ public byte asByte() @Override public short asShort() { - if (!isInByteRange()) { + if (!isInShortRange()) { throw new MessageIntegerOverflowException(value); } return (short) value; diff --git a/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala b/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala index e21c387fb..6cb7af603 100644 --- a/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala @@ -16,7 +16,6 @@ package org.msgpack.value import java.math.BigInteger - import org.msgpack.core._ import scala.util.parsing.json.JSON @@ -97,5 +96,35 @@ class ValueTest extends MessagePackSpec } + "check appropriate range for integers" in { + import ValueFactory._ + import java.lang.Byte + import java.lang.Short + + newInteger(Byte.MAX_VALUE).asByte() shouldBe Byte.MAX_VALUE + newInteger(Byte.MIN_VALUE).asByte() shouldBe Byte.MIN_VALUE + newInteger(Short.MAX_VALUE).asShort() shouldBe Short.MAX_VALUE + newInteger(Short.MIN_VALUE).asShort() shouldBe Short.MIN_VALUE + newInteger(Integer.MAX_VALUE).asInt() shouldBe Integer.MAX_VALUE + newInteger(Integer.MIN_VALUE).asInt() shouldBe Integer.MIN_VALUE + intercept[MessageIntegerOverflowException] { + newInteger(Byte.MAX_VALUE+1).asByte() + } + intercept[MessageIntegerOverflowException] { + newInteger(Byte.MIN_VALUE-1).asByte() + } + intercept[MessageIntegerOverflowException] { + newInteger(Short.MAX_VALUE+1).asShort() + } + intercept[MessageIntegerOverflowException] { + newInteger(Short.MIN_VALUE-1).asShort() + } + intercept[MessageIntegerOverflowException] { + newInteger(Integer.MAX_VALUE+1.toLong).asInt() + } + intercept[MessageIntegerOverflowException] { + newInteger(Integer.MIN_VALUE-1.toLong).asInt() + } + } } }