Skip to content

Commit

Permalink
Migrate Blackbird module (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Jan 19, 2025
1 parent b327537 commit 6b5d24b
Show file tree
Hide file tree
Showing 89 changed files with 726 additions and 21 deletions.
10 changes: 8 additions & 2 deletions blackbird/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ that uses LambdaMetafactory based code generation to replace reflection calls.
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- Test dependencies: -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

public abstract class BlackbirdTestBase extends junit.framework.TestCase
import static org.junit.jupiter.api.Assertions.*;

public abstract class BlackbirdTestBase
{
// // // First some "shared" classes from databind's `BaseMapTest`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import java.lang.reflect.Proxy;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class TestAccessFallback extends BlackbirdTestBase
{
@SuppressWarnings("serial")
Expand Down Expand Up @@ -52,6 +56,7 @@ public String getE()

private static final String BEAN_JSON = "{\"e\":\"a\"}";

@Test
public void testSerializeAccess() throws Exception
{
ObjectMapper abMapper = newObjectMapper();
Expand All @@ -61,6 +66,7 @@ public void testSerializeAccess() throws Exception
assertEquals(BEAN_JSON, abMapper.writeValueAsString(new MyBean("a")));
}

@Test
public void testDeserializeAccess() throws Exception
{
ObjectMapper abMapper = newObjectMapper();
Expand All @@ -72,6 +78,7 @@ public void testDeserializeAccess() throws Exception
assertEquals("a", bean2.getE());
}

@Test
public void testProxyAccessIssue181() throws Exception {
ObjectMapper om = newObjectMapper();
String val = om.writeValueAsString(Proxy.newProxyInstance(TestAccessFallback.class.getClassLoader(), new Class<?>[] { Beany.class }, (p, m, a) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.fasterxml.jackson.module.blackbird;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

/**
* Tests for [Issue#21]
*/
public class TestSealedPackages extends BlackbirdTestBase
{
private final ObjectMapper MAPPER = newObjectMapper();

@Test
public void testJavaStdDeserialization() throws Exception
{
String json = "{}";
Exception e = MAPPER.readValue(json, Exception.class);
assertNotNull(e);
}

@Test
public void testJavaStdSerialization() throws Exception
{
String json = MAPPER.writeValueAsString(Thread.currentThread().getThreadGroup());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.fasterxml.jackson.module.blackbird;

import org.junit.jupiter.api.Test;

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

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

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

public class TestUnwrapped extends BlackbirdTestBase
{
@JsonPropertyOrder({ "a", "b" })
Expand Down Expand Up @@ -34,6 +40,7 @@ public Unwrapped() { }
/**********************************************************
*/

@Test
public void testSimpleSerialize() throws Exception
{
final ObjectMapper VANILLA = new ObjectMapper();
Expand All @@ -43,6 +50,7 @@ public void testSimpleSerialize() throws Exception
assertEquals(json, BURNER.writeValueAsString(input));
}

@Test
public void testUnwrappedDeserialize() throws Exception
{
final ObjectMapper VANILLA = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import java.io.*;
import java.lang.invoke.MethodHandles;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;

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

/**
* Tests to verify that version information is properly accessible
*/
public class TestVersions extends BlackbirdTestBase
{
@Test
public void testMapperVersions() throws IOException
{
BlackbirdModule module = new BlackbirdModule(c -> MethodHandles.lookup());
Expand All @@ -26,7 +31,7 @@ public void testMapperVersions() throws IOException
private void assertVersion(Versioned vers)
{
Version v = vers.version();
assertFalse("Should find version information (got "+v+")", v.isUnknownVersion());
assertFalse(v.isUnknownVersion(), "Should find version information (got "+v+")");
Version exp = PackageVersion.VERSION;
assertEquals(exp.toFullString(), v.toFullString());
assertEquals(exp, v);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.fasterxml.jackson.module.blackbird.codegen;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

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

// for [afterburner#51], where re-generation of classes does not work
// as expected
public class GenerateWithMixinsTest extends BlackbirdTestBase
Expand Down Expand Up @@ -50,7 +54,8 @@ public abstract class IgnoreField3MixIn {
public abstract byte[] getField3();
}

public void testIssue51() throws JsonProcessingException
@Test
public void testIssue51() throws JsonProcessingException
{
SampleObject sampleObject = new SampleObject("field1", 2, "field3".getBytes());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.fasterxml.jackson.module.blackbird.deser;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

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

// Failing test from "BasicDeserializeTest", see [modules-base#123]
public class BasicDeserialize123Test extends BlackbirdTestBase
{
Expand All @@ -27,6 +31,7 @@ public Model123 setValue(int value) {
private final ObjectMapper MAPPER = newObjectMapper();

// [modules-base#123]
@Test
public void testFluentMethod() throws Exception
{
String json = MAPPER.writeValueAsString(new Model123(28));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import java.util.List;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

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

public class BasicDeserializeTest extends BlackbirdTestBase
{
public enum MyEnum {
Expand Down Expand Up @@ -153,11 +157,13 @@ static class Issue60Pojo {

private final ObjectMapper MAPPER = newObjectMapper();

@Test
public void testIntMethod() throws Exception {
IntBean bean = MAPPER.readValue("{\"x\":13}", IntBean.class);
assertEquals(13, bean._x);
}

@Test
public void testMultiIntMethod() throws Exception {
IntsBean bean = MAPPER.readValue("{\"c\":3,\"a\":9,\"b\":111,\"e\":-9999,\"d\":1}", IntsBean.class);
assertEquals(9, bean._a);
Expand All @@ -167,16 +173,19 @@ public void testMultiIntMethod() throws Exception {
assertEquals(-9999, bean._e);
}

@Test
public void testLongMethod() throws Exception {
LongBean bean = MAPPER.readValue("{\"x\":-1}", LongBean.class);
assertEquals(-1, bean._x);
}

@Test
public void testStringMethod() throws Exception {
StringBean bean = MAPPER.readValue("{\"x\":\"zoobar\"}", StringBean.class);
assertEquals("zoobar", bean._x);
}

@Test
public void testObjectMethod() throws Exception {
EnumBean bean = MAPPER.readValue("{\"x\":\"A\"}", EnumBean.class);
assertEquals(MyEnum.A, bean._x);
Expand All @@ -188,16 +197,19 @@ public void testObjectMethod() throws Exception {
/**********************************************************************
*/

@Test
public void testIntField() throws Exception {
IntFieldBean bean = MAPPER.readValue("{\"value\":-92}", IntFieldBean.class);
assertEquals(-92, bean.x);
}

@Test
public void testLongField() throws Exception {
LongFieldBean bean = MAPPER.readValue("{\"value\":-92}", LongFieldBean.class);
assertEquals(-92, bean.value);
}

@Test
public void testStringField() throws Exception {
StringFieldBean bean = MAPPER.readValue("{\"x\":\"\"}", StringFieldBean.class);
assertEquals("", bean.x);
Expand All @@ -207,12 +219,14 @@ public void testStringField() throws Exception {
assertNull(bean.x);
}

@Test
public void testEnumField() throws Exception {
EnumFieldBean bean = MAPPER.readValue("{\"x\":\"C\"}", EnumFieldBean.class);
assertEquals(MyEnum.C, bean.x);
}

// Verify [Issue#10], so that nulls do not get coerced to String "null"
@Test
public void testStringAsObjectField() throws Exception {
StringAsObject bean = MAPPER.readValue("{\"value\":null}", StringAsObject.class);
assertNotNull(bean);
Expand All @@ -225,6 +239,7 @@ public void testStringAsObjectField() throws Exception {
/**********************************************************************
*/

@Test
public void testFiveMinuteDoc() throws Exception
{
FiveMinuteUser input = new FiveMinuteUser("First", "Name", true,
Expand All @@ -237,6 +252,7 @@ public void testFiveMinuteDoc() throws Exception
}
}

@Test
public void testMixed() throws Exception
{
MixedBean bean = MAPPER.readValue("{"
Expand All @@ -261,6 +277,7 @@ public void testMixed() throws Exception
}

// Test for [Issue-5]
@Test
public void testNonVoidProperty() throws Exception
{
final String json = "{ \"stringField\" : \"zoobar\", \"stringField2\" : \"barzoo\" }";
Expand All @@ -276,6 +293,7 @@ public void testNonVoidProperty() throws Exception
}

// Test for [module-afterburner#16]
@Test
public void testBigNonVoidProperty() throws Exception
{
final String json = "{ \"stringField\" : \"zoobar\" }";
Expand All @@ -290,12 +308,14 @@ public void testBigNonVoidProperty() throws Exception
}

// NOTE: failed with databind-2.5.0; fixed for 2.5.1
@Test
public void testStringBuilder() throws Exception
{
StringBuilder sb = MAPPER.readValue(quote("foobar"), StringBuilder.class);
assertEquals("foobar", sb.toString());
}

@Test
public void testBooleans() throws Exception
{
BooleansBean bean = MAPPER.readValue(aposToQuotes("{'a':true, 'b':true}"),
Expand All @@ -306,6 +326,7 @@ public void testBooleans() throws Exception
}

// for [module-afterburner#60] (caused by a bug in jackson-core up to 2.6.2, fixed in 2.6.3)
@Test
public void testProblemWithIndentation() throws Exception {
final String JSON = "{\n"
+" \"foos\" :\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.fasterxml.jackson.module.blackbird.deser;

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.module.blackbird.BlackbirdTestBase;

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

public class DoubleBoxedArrayConstructorDeser141Test extends BlackbirdTestBase
{
static class Foo141 {
Expand All @@ -19,6 +23,7 @@ public Foo141(@JsonProperty("bar") Double[] bar) {

private final ObjectMapper MAPPER = newObjectMapper();

@Test
public void testBoxedDoubleArrayCreator() throws Exception
{
Foo141 foo = new Foo141(new Double[] { 2.0, 0.25 });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.fasterxml.jackson.module.blackbird.deser;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

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

public class DoubleBoxedArraySetterDeser141Test extends BlackbirdTestBase
{
static class Foo141 {
Expand All @@ -21,6 +25,7 @@ public Foo141 setBar(@JsonProperty("bar") Double[] bar) {

private final ObjectMapper MAPPER = newObjectMapper();

@Test
public void testBoxedDoubleArraySetter() throws Exception
{
Foo141 foo = new Foo141().setBar(new Double[] { 2.0, 0.25 });
Expand Down
Loading

0 comments on commit 6b5d24b

Please sign in to comment.