Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate /deser tests to JUnit 5 #4317

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ObjectNode;

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

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.a2q;
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;

// for [databind#3394]
public class AnySetter3394Test extends BaseMapTest
public class AnySetter3394Test
{
static class AnySetter3394Bean {
public int id;
Expand All @@ -23,6 +29,7 @@ static class AnySetter3394Bean {

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testAnySetterWithJsonNode() throws Exception
{
final String DOC = a2q("{'test':3,'nullable':null,'id':42,'value':true}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import java.util.*;

import com.fasterxml.jackson.annotation.*;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

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

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;

/**
* Unit tests for verifying that {@link JsonAnySetter} annotation
* works as expected.
*/
public class AnySetterTest
extends BaseMapTest
{
static class MapImitator
{
Expand Down Expand Up @@ -259,6 +263,7 @@ static class IdentityDTO349 {

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testSimpleMapImitation() throws Exception
{
MapImitator mapHolder = MAPPER.readValue
Expand All @@ -274,6 +279,7 @@ public void testSimpleMapImitation() throws Exception
assertEquals(Integer.valueOf(3), l.get(2));
}

@Test
public void testAnySetterDisable() throws Exception
{
try {
Expand All @@ -286,16 +292,18 @@ public void testAnySetterDisable() throws Exception

}

@Test
public void testSimpleTyped() throws Exception
{
MapImitatorWithValue mapHolder = MAPPER.readValue
("{ \"a\" : [ 3, -1 ], \"b\" : [ ] }", MapImitatorWithValue.class);
Map<String,int[]> result = mapHolder._map;
assertEquals(2, result.size());
assertEquals(new int[] { 3, -1 }, result.get("a"));
assertEquals(new int[0], result.get("b"));
assertArrayEquals(new int[] { 3, -1 }, result.get("a"));
assertArrayEquals(new int[0], result.get("b"));
}

@Test
public void testBrokenWithDoubleAnnotations() throws Exception
{
try {
Expand All @@ -307,20 +315,23 @@ public void testBrokenWithDoubleAnnotations() throws Exception
}
}

@Test
public void testIgnored() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = newJsonMapper();
mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
_testIgnorals(mapper);
}

@Test
public void testIgnoredPart2() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = newJsonMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
_testIgnorals(mapper);
}

@Test
public void testProblem744() throws Exception
{
Bean744 bean = MAPPER.readValue("{\"name\":\"Bob\"}", Bean744.class);
Expand All @@ -329,13 +340,15 @@ public void testProblem744() throws Exception
assertEquals("Bob", bean.additionalProperties.get("name"));
}

@Test
public void testIssue797() throws Exception
{
String json = MAPPER.writeValueAsString(new Bean797BaseImpl());
assertEquals("{}", json);
}

// [Issue#337]
@Test
public void testPolymorphic() throws Exception
{
PolyAnyBean input = new PolyAnyBean();
Expand All @@ -353,6 +366,7 @@ public void testPolymorphic() throws Exception
assertEquals("xyz", ((Impl) ob).value);
}

@Test
public void testJsonAnySetterOnMap() throws Exception {
JsonAnySetterOnMap result = MAPPER.readValue("{\"id\":2,\"name\":\"Joe\", \"city\":\"New Jersey\"}",
JsonAnySetterOnMap.class);
Expand All @@ -361,6 +375,7 @@ public void testJsonAnySetterOnMap() throws Exception {
assertEquals("New Jersey", result.other.get("city"));
}

@Test
public void testJsonAnySetterOnNullMap() throws Exception {
final String DOC = a2q("{'id':2,'name':'Joe', 'city':'New Jersey'}");
JsonAnySetterOnNullMap result = MAPPER.readValue(DOC,
Expand Down Expand Up @@ -391,9 +406,10 @@ public void testJsonAnySetterOnNullMap() throws Exception {
);

// [databind#349]
@Test
public void testUnwrappedWithAny() throws Exception
{
final ObjectMapper mapper = objectMapper();
final ObjectMapper mapper = newJsonMapper();
Bean349 value = mapper.readValue(UNWRAPPED_JSON_349, Bean349.class);
assertNotNull(value);
assertEquals(3, value.x);
Expand All @@ -402,9 +418,10 @@ public void testUnwrappedWithAny() throws Exception
}

// [databind#349]
@Test
public void testUnwrappedWithAnyAsUpdate() throws Exception
{
final ObjectMapper mapper = objectMapper();
final ObjectMapper mapper = newJsonMapper();
Bean349 bean = mapper.readerFor(Bean349.class)
.withValueToUpdate(new Bean349())
.readValue(UNWRAPPED_JSON_349);
Expand All @@ -414,9 +431,10 @@ public void testUnwrappedWithAnyAsUpdate() throws Exception
}

// [databind#1035]
@Test
public void testGenericAnySetter() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = newJsonMapper();

Map<String, Integer> stringGenericMap = new HashMap<String, Integer>();
stringGenericMap.put("testStringKey", 5);
Expand All @@ -432,18 +450,18 @@ public void testGenericAnySetter() throws Exception
assertNotNull(stringGeneric);
assertEquals(stringGeneric.getStaticallyMappedProperty(), "Test");
for(Map.Entry<String, Integer> entry : stringGeneric.getDynamicallyMappedProperties().entrySet()) {
assertTrue("A key in MyGeneric<String> is not an String.", entry.getKey() instanceof String);
assertTrue("A value in MyGeneric<Integer> is not an Integer.", entry.getValue() instanceof Integer);
assertTrue(entry.getKey() instanceof String, "A key in MyGeneric<String> is not an String.");
assertTrue(entry.getValue() instanceof Integer, "A value in MyGeneric<Integer> is not an Integer.");
}
assertEquals(stringGeneric.getDynamicallyMappedProperties(), stringGenericMap);

assertNotNull(integerGeneric);
assertEquals(integerGeneric.getStaticallyMappedProperty(), "Test2");
for(Map.Entry<Integer, Integer> entry : integerGeneric.getDynamicallyMappedProperties().entrySet()) {
Object key = entry.getKey();
assertEquals("A key in MyGeneric<Integer> is not an Integer.", Integer.class, key.getClass());
assertEquals(Integer.class, key.getClass(), "A key in MyGeneric<Integer> is not an Integer.");
Object value = entry.getValue();
assertEquals("A value in MyGeneric<Integer> is not an Integer.", Integer.class, value.getClass());
assertEquals(Integer.class, value.getClass(), "A value in MyGeneric<Integer> is not an Integer.");
}
assertEquals(integerGeneric.getDynamicallyMappedProperties(), integerGenericMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.fasterxml.jackson.databind.deser;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

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

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;

/**
* Simple unit tests to verify that it is possible to handle
* potentially cyclic structures, as long as object graph itself
* is not cyclic. This is the case for directed hierarchies like
* trees and DAGs.
*/
public class CyclicTypesDeserTest
extends BaseMapTest
{
static class Bean
{
Expand Down Expand Up @@ -62,6 +67,7 @@ static class Selfie405 {

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testLinked() throws Exception
{
Bean first = MAPPER.readValue
Expand All @@ -77,13 +83,15 @@ public void testLinked() throws Exception
assertNull(last._next);
}

@Test
public void testLinkedGeneric() throws Exception
{
StringLink link = MAPPER.readValue("{\"next\":null}", StringLink.class);
assertNotNull(link);
assertNull(link.next);
}

@Test
public void testCycleWith2Classes() throws Exception
{
LinkA a = MAPPER.readValue("{\"next\":{\"a\":null}}", LinkA.class);
Expand All @@ -93,6 +101,7 @@ public void testCycleWith2Classes() throws Exception
}

// [Issue#405]: Should be possible to ignore cyclic ref
@Test
public void testIgnoredCycle() throws Exception
{
Selfie405 self1 = new Selfie405(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.*;

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

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.sharedMapper;

// Tests for [databind#2539] for checking whether given (raw) type has explicit
// deserializer associated
public class DeserializerFactoryTest extends BaseMapTest
public class DeserializerFactoryTest
{
private final ObjectMapper MAPPER = sharedMapper();

static class POJO2539 { }

// [databind#2539]: check existence of deserializer for type
@Test
public void testJDKScalarDeserializerExistence() throws Exception
{
// First verify some basic types
Expand All @@ -34,6 +41,7 @@ public void testJDKScalarDeserializerExistence() throws Exception
// _verifyIsFound(java.util.Object.class);
}

@Test
public void testJDKContainerDeserializerExistence() throws Exception
{
// Both general and specific container types should be considered supported
Expand All @@ -48,6 +56,7 @@ public void testJDKContainerDeserializerExistence() throws Exception
_verifyIsFound(HashSet.class);
}

@Test
public void testJDKArraysOfExistence() throws Exception
{
// Similarly, array types of all supported types should be allowed
Expand All @@ -57,6 +66,7 @@ public void testJDKArraysOfExistence() throws Exception
_verifyIsFound(UUID[].class);
}

@Test
public void testNoDeserTypes() throws Exception
{
// Types for which we should NOT have explicit deserializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil.StringWrapper;

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

@SuppressWarnings("serial")
public class GenericCollectionDeserTest
extends BaseMapTest
{
static class ListSubClass extends ArrayList<StringWrapper> { }

Expand Down Expand Up @@ -41,6 +46,7 @@ protected static class BooleanElement {
/**
* Verifying that sub-classing works ok wrt generics information
*/
@Test
public void testListSubClass() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -59,6 +65,7 @@ public void testListSubClass() throws Exception
*/

// Verifying that sub-classing works ok wrt generics information
@Test
public void testAnnotatedLStringist() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -69,6 +76,7 @@ public void testAnnotatedLStringist() throws Exception
assertEquals("...", ((StringWrapper) ob).str);
}

@Test
public void testAnnotatedBooleanList() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Expand Down
Loading