Skip to content

Commit

Permalink
[JSTEP-10] Migrate modules datatype and parameter-names to `JUnit…
Browse files Browse the repository at this point in the history
… 5` (#344)
  • Loading branch information
JooHyukKim authored Jan 26, 2025
1 parent cb5e00d commit 05875eb
Show file tree
Hide file tree
Showing 34 changed files with 241 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.util.Optional;
import java.util.TimeZone;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;

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

public class ContextualOptional17Test extends ModuleTestBase
{
// [datatypes-java8#17]
Expand All @@ -30,6 +34,7 @@ static class ContextualOptionals
/**********************************************************
*/

@Test
public void testContextualOptionals() throws Exception
{
final ObjectMapper mapper = mapperWithModule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import java.util.Optional;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

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

public class CreatorTest extends ModuleTestBase
{
static class CreatorWithOptionalStrings
Expand Down Expand Up @@ -35,6 +39,7 @@ public CreatorWithOptionalStrings(@JsonProperty("a") Optional<String> a,
* Test to ensure that creator parameters use defaulting
* (introduced in Jackson 2.6)
*/
@Test
public void testCreatorWithOptionalDefault() throws Exception
{
CreatorWithOptionalStrings bean = MAPPER.readValue(
Expand All @@ -47,6 +52,7 @@ public void testCreatorWithOptionalDefault() throws Exception
assertEquals("foo", bean.a.get());
}

@Test
public void testCreatorWithOptionalAbsentAsNull() throws Exception
{
Jdk8Module module = new Jdk8Module()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.fasterxml.jackson.datatype.jdk8;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.stream.DoubleStream;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

@SuppressWarnings({ "unqualified-field-access", "javadoc" })
public class DoubleStreamSerializerTest extends StreamTestBase {

Expand All @@ -21,7 +21,7 @@ public class DoubleStreamSerializerTest extends StreamTestBase {

final String exceptionMessage = "DoubleStream peek threw";

@Before
@BeforeEach
public void setUp() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.fasterxml.jackson.datatype.jdk8;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.stream.IntStream;

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

@SuppressWarnings({ "unqualified-field-access", "javadoc" })
public class IntStreamSerializerTest extends StreamTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.io.*;
import java.util.Optional;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.*;

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

public class JDKSerializabilityTest extends ModuleTestBase
{
static class BooleanBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -11,6 +13,8 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;

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

public class Java8OptionalUnwrappedTest extends ModuleTestBase
{
static class Child {
Expand Down Expand Up @@ -46,6 +50,7 @@ static class Bean2 {
public String name;
}

@Test
public void testUntypedWithOptionalsNotNulls() throws Exception
{
final ObjectMapper mapper = mapperWithModule(false);
Expand All @@ -55,6 +60,7 @@ public void testUntypedWithOptionalsNotNulls() throws Exception
}

// for [datatype-jdk8#20]
@Test
public void testShouldSerializeUnwrappedOptional() throws Exception {
final ObjectMapper mapper = mapperWithModule(false);

Expand All @@ -63,6 +69,7 @@ public void testShouldSerializeUnwrappedOptional() throws Exception {
}

// for [datatype-jdk8#26]
@Test
public void testPropogatePrefixToSchema() throws Exception {
final ObjectMapper mapper = mapperWithModule(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.fasterxml.jackson.datatype.jdk8;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.stream.LongStream;

import org.junit.Test;
import org.junit.jupiter.api.Test;

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

@SuppressWarnings({ "unqualified-field-access", "javadoc" })
public class LongStreamSerializerTest extends StreamTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;

public abstract class ModuleTestBase extends junit.framework.TestCase
import static org.junit.jupiter.api.Assertions.fail;

public abstract class ModuleTestBase
{
public static class NoCheckSubTypeValidator
extends PolymorphicTypeValidator.Base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

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

public class OptionalBasicTest extends ModuleTestBase
{
public static final class OptionalData {
Expand Down Expand Up @@ -59,6 +63,7 @@ public static class ContainedImpl implements Contained { }

private final ObjectMapper MAPPER = mapperWithModule();

@Test
public void testOptionalTypeResolution() throws Exception {
// With 2.6, we need to recognize it as ReferenceType
JavaType t = MAPPER.constructType(Optional.class);
Expand All @@ -67,13 +72,15 @@ public void testOptionalTypeResolution() throws Exception {
assertTrue(t.isReferenceType());
}

@Test
public void testDeserAbsent() throws Exception {
Optional<?> value = MAPPER.readValue("null",
new TypeReference<Optional<String>>() {
});
assertFalse(value.isPresent());
}

@Test
public void testDeserSimpleString() throws Exception {
Optional<?> value = MAPPER.readValue("\"simpleString\"",
new TypeReference<Optional<String>>() {
Expand All @@ -82,13 +89,15 @@ public void testDeserSimpleString() throws Exception {
assertEquals("simpleString", value.get());
}

@Test
public void testDeserInsideObject() throws Exception {
OptionalData data = MAPPER.readValue("{\"myString\":\"simpleString\"}",
OptionalData.class);
assertTrue(data.myString.isPresent());
assertEquals("simpleString", data.myString.get());
}

@Test
public void testDeserComplexObject() throws Exception {
TypeReference<Optional<OptionalData>> type = new TypeReference<Optional<OptionalData>>() {
};
Expand All @@ -99,6 +108,7 @@ public void testDeserComplexObject() throws Exception {
assertEquals("simpleString", data.get().myString.get());
}

@Test
public void testDeserGeneric() throws Exception {
TypeReference<Optional<OptionalGenericData<String>>> type = new TypeReference<Optional<OptionalGenericData<String>>>() {
};
Expand All @@ -109,37 +119,43 @@ public void testDeserGeneric() throws Exception {
assertEquals("simpleString", data.get().myData.get());
}

@Test
public void testSerAbsent() throws Exception {
String value = MAPPER.writeValueAsString(Optional.empty());
assertEquals("null", value);
}

@Test
public void testSerSimpleString() throws Exception {
String value = MAPPER.writeValueAsString(Optional.of("simpleString"));
assertEquals("\"simpleString\"", value);
}

@Test
public void testSerInsideObject() throws Exception {
OptionalData data = new OptionalData();
data.myString = Optional.of("simpleString");
String value = MAPPER.writeValueAsString(data);
assertEquals("{\"myString\":\"simpleString\"}", value);
}

@Test
public void testSerComplexObject() throws Exception {
OptionalData data = new OptionalData();
data.myString = Optional.of("simpleString");
String value = MAPPER.writeValueAsString(Optional.of(data));
assertEquals("{\"myString\":\"simpleString\"}", value);
}

@Test
public void testSerGeneric() throws Exception {
OptionalGenericData<String> data = new OptionalGenericData<String>();
data.myData = Optional.of("simpleString");
String value = MAPPER.writeValueAsString(Optional.of(data));
assertEquals("{\"myData\":\"simpleString\"}", value);
}

@Test
public void testSerNonNull() throws Exception {
OptionalData data = new OptionalData();
data.myString = Optional.empty();
Expand All @@ -149,6 +165,7 @@ public void testSerNonNull() throws Exception {
assertEquals("{}", value);
}

@Test
public void testSerOptDefault() throws Exception {
OptionalData data = new OptionalData();
data.myString = Optional.empty();
Expand All @@ -157,6 +174,7 @@ public void testSerOptDefault() throws Exception {
assertEquals("{\"myString\":null}", value);
}

@Test
public void testSerOptNull() throws Exception {
OptionalData data = new OptionalData();
data.myString = null;
Expand All @@ -166,6 +184,7 @@ public void testSerOptNull() throws Exception {
}

@SuppressWarnings("deprecation")
@Test
public void testSerOptDisableAsNull() throws Exception {
final OptionalData data = new OptionalData();
data.myString = Optional.empty();
Expand All @@ -187,6 +206,7 @@ public void testSerOptDisableAsNull() throws Exception {
assertEquals("{}", mapper.writeValueAsString(data));
}

@Test
public void testSerOptNonEmpty() throws Exception {
OptionalData data = new OptionalData();
data.myString = null;
Expand All @@ -195,6 +215,7 @@ public void testSerOptNonEmpty() throws Exception {
assertEquals("{}", value);
}

@Test
public void testWithTypingEnabled() throws Exception {
final ObjectMapper objectMapper = mapperWithModule();
// ENABLE TYPING
Expand All @@ -210,6 +231,7 @@ public void testWithTypingEnabled() throws Exception {
assertEquals(myData.myString, deserializedMyData.myString);
}

@Test
public void testObjectId() throws Exception {
final Unit input = new Unit();
input.link(input);
Expand All @@ -222,6 +244,7 @@ public void testObjectId() throws Exception {
assertSame(result, base);
}

@Test
public void testOptionalCollection() throws Exception {

TypeReference<List<Optional<String>>> typeReference = new TypeReference<List<Optional<String>>>() {
Expand All @@ -238,10 +261,11 @@ public void testOptionalCollection() throws Exception {
List<Optional<String>> result = MAPPER.readValue(str, typeReference);
assertEquals(list.size(), result.size());
for (int i = 0; i < list.size(); ++i) {
assertEquals("Entry #" + i, list.get(i), result.get(i));
assertEquals(list.get(i), result.get(i), "Entry #" + i);
}
}

@Test
public void testPolymorphic() throws Exception
{
final Container dto = new Container();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import java.util.Optional;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class OptionalBooleanTest extends ModuleTestBase
{
static class BooleanBean {
Expand All @@ -18,6 +22,7 @@ public BooleanBean(Boolean b) {
private final ObjectMapper MAPPER = mapperWithModule();

// for [datatype-jdk8#23]
@Test
public void testBoolean() throws Exception
{
// First, serialization
Expand Down
Loading

0 comments on commit 05875eb

Please sign in to comment.