Skip to content

Commit

Permalink
Merge some more parts of #4759
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 24, 2024
1 parent 77d71c7 commit 760cf8f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

Expand Down Expand Up @@ -188,12 +187,7 @@ public void testWithCtorAndDelegate() throws Exception
mapper.setInjectableValues(new InjectableValues.Std()
.addValue(String.class, "Pooka")
);
CtorBean711 bean = null;
try {
bean = mapper.readValue("38", CtorBean711.class);
} catch (JacksonException e) {
fail("Did not expect problems, got: "+e.getMessage());
}
CtorBean711 bean = mapper.readValue("38", CtorBean711.class);
assertEquals(38, bean.age);
assertEquals("Pooka", bean.name);
}
Expand All @@ -205,12 +199,7 @@ public void testWithFactoryAndDelegate() throws Exception
mapper.setInjectableValues(new InjectableValues.Std()
.addValue(String.class, "Fygar")
);
FactoryBean711 bean = null;
try {
bean = mapper.readValue("38", FactoryBean711.class);
} catch (JacksonException e) {
fail("Did not expect problems, got: "+e.getMessage());
}
FactoryBean711 bean = mapper.readValue("38", FactoryBean711.class);
assertEquals(38, bean.age);
assertEquals("Fygar", bean.name1);
assertEquals("Fygar", bean.name2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
Expand Down Expand Up @@ -212,12 +212,7 @@ public void testUnknownHandlingIgnoreWithFeature() throws Exception
{
ObjectMapper mapper = newJsonMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TestBean result = null;
try {
result = mapper.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class);
} catch (JacksonException jex) {
fail("Did not expect a problem, got: "+jex.getMessage());
}
TestBean result = mapper.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class);
assertNotNull(result);
assertEquals(1, result._a);
assertNull(result._unknown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

// Tests for [databind#4584]: extension point for discovering "Default"
// Creator (primary Creator, usually constructor, used in case no creator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

// Tests for [databind#4620]:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ public void testClassIsMissing()
public void testDeserialize() throws Exception
{
ObjectMapper m = new ObjectMapper();
Parent result = null;

try {
result = m.readValue(" { } ", Parent.class);
} catch (Exception e) {
fail("Should not have had issues, got: "+e);
}
Parent result = m.readValue(" { } ", Parent.class);
assertNotNull(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public MultipleTheoreticalGetters(@JsonProperty("a") int foo) {
@Test
public void testIssue193() throws Exception
{
String json = objectWriter().writeValueAsString(new Bean193(1, 2));
String json = MAPPER.writeValueAsString(new Bean193(1, 2));
assertNotNull(json);
}

Expand All @@ -110,25 +110,19 @@ public void testNonConflict() throws Exception
@Test
public void testHypotheticalGetters() throws Exception
{
String json = objectWriter().writeValueAsString(new MultipleTheoreticalGetters());
String json = MAPPER.writeValueAsString(new MultipleTheoreticalGetters());
assertEquals(a2q("{'a':3}"), json);
}

// for [jackson-core#158]
@Test
public void testOverrideName() throws Exception
{
final ObjectMapper mapper = newJsonMapper();
String json = mapper.writeValueAsString(new CoreBean158());
String json = MAPPER.writeValueAsString(new CoreBean158());
assertEquals(a2q("{'bar':'x'}"), json);

// and back
CoreBean158 result = null;
try {
result = mapper.readValue(a2q("{'bar':'y'}"), CoreBean158.class);
} catch (Exception e) {
fail("Unexpected failure when reading CoreBean158: "+e);
}
CoreBean158 result = MAPPER.readValue(a2q("{'bar':'y'}"), CoreBean158.class);
assertNotNull(result);
assertEquals("y", result.bar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

// Tests for [databind#3505] causing a NPE when setting a DefaultTypeResolverBuilder
Expand Down Expand Up @@ -59,24 +61,21 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java
}
}

public void testDeductionWithDefaultTypeResolverBuilder() {
@Test
public void testDeductionWithDefaultTypeResolverBuilder() throws Exception {
final ObjectMapper mapper = jsonMapperBuilder()
.registerSubtypes(Parent.ChildOne.class, Parent.ChildTwo.class)
.setDefaultTyping(new AssertingTypeResolverBuilder()
.init(JsonTypeInfo.Id.DEDUCTION, null))
.build();

try {
final Parent firstRead = mapper.readValue("{ \"one\": \"Hello World\" }", Parent.class);
assertNotNull(firstRead);
assertTrue(firstRead instanceof Parent.ChildOne);
assertEquals("Hello World", ((Parent.ChildOne) firstRead).one);
final Parent secondRead = mapper.readValue("{ \"two\": \"Hello World\" }", Parent.class);
assertNotNull(secondRead);
assertTrue(secondRead instanceof Parent.ChildTwo);
assertEquals("Hello World", ((Parent.ChildTwo) secondRead).two);
} catch (Exception e) {
fail("This call should not throw");
}
final Parent firstRead = mapper.readValue("{ \"one\": \"Hello World\" }", Parent.class);
assertNotNull(firstRead);
assertTrue(firstRead instanceof Parent.ChildOne);
assertEquals("Hello World", ((Parent.ChildOne) firstRead).one);
final Parent secondRead = mapper.readValue("{ \"two\": \"Hello World\" }", Parent.class);
assertNotNull(secondRead);
assertTrue(secondRead instanceof Parent.ChildTwo);
assertEquals("Hello World", ((Parent.ChildTwo) secondRead).two);
}
}

0 comments on commit 760cf8f

Please sign in to comment.