Skip to content

Commit

Permalink
Minor clean up of new test wrt #4453
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 10, 2024
1 parent 884af2e commit 03458e9
Showing 1 changed file with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -15,8 +16,19 @@

// [databind#2978]
public class StdValueInstantiatorTest
extends DatabindTestUtil
{
private final ObjectMapper MAPPER = newJsonMapper();

private static final long LONG_TEST_VALUE = 12345678901L;

static class Stuff {
final double value;

Stuff(double value) {
this.value = value;
}
}

@Test
public void testDoubleValidation_valid() {
Expand All @@ -34,31 +46,20 @@ public void testDoubleValidation_invalid() {

@Test
public void testJsonIntegerToDouble() throws Exception {
ObjectMapper m = new ObjectMapper();
Stuff a = m.readValue("5", Stuff.class);
Stuff a = MAPPER.readValue("5", Stuff.class);
assertEquals(5, a.value);
}

@Test
public void testJsonLongToDouble() throws Exception {
ObjectMapper m = new ObjectMapper();
assertTrue(LONG_TEST_VALUE > Integer.MAX_VALUE);
Stuff a = m.readValue(String.valueOf(LONG_TEST_VALUE), Stuff.class);
Stuff a = MAPPER.readValue(String.valueOf(LONG_TEST_VALUE), Stuff.class);
assertEquals(LONG_TEST_VALUE, a.value);
}

static class Stuff {
final double value;

Stuff(double value) {
this.value = value;
}
}

@Test
public void testJsonIntegerDeserializationPrefersInt() throws Exception {
ObjectMapper m = new ObjectMapper();
A a = m.readValue("5", A.class);
A a = MAPPER.readValue("5", A.class);
assertEquals(1, a.creatorType);
}

Expand All @@ -84,8 +85,7 @@ static class A {

@Test
public void testJsonIntegerDeserializationPrefersLong() throws Exception {
ObjectMapper m = new ObjectMapper();
B a = m.readValue("5", B.class);
B a = MAPPER.readValue("5", B.class);
assertEquals(2, a.creatorType);
}

Expand All @@ -107,8 +107,7 @@ static class B {

@Test
public void testJsonIntegerDeserializationPrefersBigInteger() throws Exception {
ObjectMapper m = new ObjectMapper();
C a = m.readValue("5", C.class);
C a = MAPPER.readValue("5", C.class);
assertEquals(3, a.creatorType);
}

Expand All @@ -126,8 +125,7 @@ static class C {

@Test
public void testJsonLongDeserializationPrefersLong() throws Exception {
ObjectMapper m = new ObjectMapper();
A2 a = m.readValue(String.valueOf(LONG_TEST_VALUE), A2.class);
A2 a = MAPPER.readValue(String.valueOf(LONG_TEST_VALUE), A2.class);
assertEquals(2, a.creatorType);
}

Expand All @@ -153,8 +151,7 @@ static class A2 {

@Test
public void testJsonLongDeserializationPrefersBigInteger() throws Exception {
ObjectMapper m = new ObjectMapper();
B2 a = m.readValue(String.valueOf(LONG_TEST_VALUE), B2.class);
B2 a = MAPPER.readValue(String.valueOf(LONG_TEST_VALUE), B2.class);
assertEquals(3, a.creatorType);
}

Expand All @@ -172,10 +169,9 @@ static class B2 {

@Test
public void testJsonIntegerIntoDoubleConstructorThrows() throws Exception {
ObjectMapper m = new ObjectMapper();
try {
m.readValue("5", D.class);
fail();
MAPPER.readValue("5", D.class);
fail("Should not pass");
} catch (ValueInstantiationException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
assertEquals("boo", e.getCause().getMessage());
Expand All @@ -191,14 +187,12 @@ static final class D {

@Test
public void testJsonLongIntoDoubleConstructorThrows() throws Exception {
ObjectMapper m = new ObjectMapper();
try {
m.readValue(String.valueOf(LONG_TEST_VALUE), D.class);
fail();
MAPPER.readValue(String.valueOf(LONG_TEST_VALUE), D.class);
fail("Should not pass");
} catch (ValueInstantiationException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
assertEquals("boo", e.getCause().getMessage());
}
}

}

0 comments on commit 03458e9

Please sign in to comment.