diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java index fa937f84d615c..7b18eaa2bad38 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java @@ -97,7 +97,7 @@ public void testFromRubbish() throws Exception { } public void testVersionedMediaType() { - String version = String.valueOf(Math.abs(randomByte())); + String version = String.valueOf(randomNonNegativeByte()); assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+json;compatible-with=" + version), equalTo(XContentType.JSON)); assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+cbor;compatible-with=" + version), @@ -119,7 +119,7 @@ public void testVersionedMediaType() { } public void testVersionParsing() { - byte version = (byte) Math.abs(randomByte()); + byte version = randomNonNegativeByte(); assertThat(XContentType.parseVersion("application/vnd.elasticsearch+json;compatible-with=" + version), equalTo(version)); assertThat(XContentType.parseVersion("application/vnd.elasticsearch+cbor;compatible-with=" + version), @@ -146,12 +146,12 @@ public void testUnrecognizedParameter() { is(nullValue())); } public void testMediaTypeWithoutESSubtype() { - String version = String.valueOf(Math.abs(randomByte())); + String version = String.valueOf(randomNonNegativeByte()); assertThat(XContentType.fromMediaType("application/json;compatible-with=" + version), nullValue()); } public void testAnchoring() { - String version = String.valueOf(Math.abs(randomByte())); + String version = String.valueOf(randomNonNegativeByte()); assertThat(XContentType.fromMediaType("sth_application/json;compatible-with=" + version + ".0"), nullValue()); assertThat(XContentType.fromMediaType("sth_application/json;compatible-with=" + version + "_sth"), nullValue()); assertThat(XContentType.fromMediaType("application/json;compatible-with=" + version + "_sth"), nullValue()); diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index efeec57cd20d6..9e0504fb5e8ff 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -608,6 +608,11 @@ public static byte randomByte() { return (byte) random().nextInt(); } + public static byte randomNonNegativeByte() { + byte randomByte = randomByte(); + return (byte) (randomByte == Byte.MIN_VALUE ? 0 : Math.abs(randomByte)); + } + /** * Helper method to create a byte array of a given length populated with random byte values *