Skip to content

Commit

Permalink
Convert Afterburner tests to JUnit5 (part of #268) (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 18, 2025
1 parent 0f590b9 commit 1b7a759
Show file tree
Hide file tree
Showing 83 changed files with 787 additions and 114 deletions.
9 changes: 7 additions & 2 deletions afterburner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ field access and method calls
</dependency>

<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 @@ -4,6 +4,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class AfterburnerModuleJDKSerializabilityTest extends AfterburnerTestBase
{
static class Point {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

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

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

Expand Down Expand Up @@ -195,12 +198,6 @@ protected static JsonMapper newVanillaJSONMapper() {
return new JsonMapper();
}

@Deprecated
protected ObjectMapper mapperWithModule()
{
return newObjectMapper();
}

/*
/**********************************************************
/* Helper methods; assertions
Expand Down Expand Up @@ -236,7 +233,7 @@ protected void assertType(Object ob, Class<?> expType)
* returning them
*/
protected String getAndVerifyText(JsonParser jp)
throws IOException, JsonParseException
throws IOException
{
// Ok, let's verify other accessors
int actLen = jp.getTextLength();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.fasterxml.jackson.module.afterburner;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class TestAccessFallback extends AfterburnerTestBase
{
@SuppressWarnings("serial")
Expand Down Expand Up @@ -50,6 +54,7 @@ public String getE()

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

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

@Test
public void testDeserializeAccess() throws Exception
{
ObjectMapper abMapper = newObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.fasterxml.jackson.module.afterburner;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

/**
* Tests for [Issue#21]
*/
public class TestSealedPackages extends AfterburnerTestBase
{
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.afterburner;

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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class TestUnwrapped extends AfterburnerTestBase
{
@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 All @@ -53,5 +61,4 @@ public void testUnwrappedDeserialize() throws Exception
assertNotNull(out.b);
assertEquals(9, out.b.value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

import java.io.*;

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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* Tests to verify that version information is properly accessible
*/
public class TestVersions extends AfterburnerTestBase
{
@Test
public void testMapperVersions() throws IOException
{
AfterburnerModule module = new AfterburnerModule();
assertVersion(module);
}

/*
/**********************************************************
/* Helper methods
/**********************************************************
*/

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,7 +1,8 @@
package com.fasterxml.jackson.module.afterburner.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.afterburner.AfterburnerTestBase;

Expand Down Expand Up @@ -45,12 +46,13 @@ public void setField3(byte[] field3) {
}
}

public abstract class IgnoreField3MixIn {
@JsonIgnore
public abstract byte[] getField3();
public abstract static class IgnoreField3MixIn {
@JsonIgnore
public abstract byte[] getField3();
}

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

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.afterburner.AfterburnerTestBase;

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

public class BasicDeserializeTest extends AfterburnerTestBase
{
public enum MyEnum {
Expand Down Expand Up @@ -169,12 +173,14 @@ public Model123 setValue(int value) {
*/

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 @@ -184,16 +190,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 @@ -205,16 +214,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 @@ -224,12 +236,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 @@ -242,6 +256,7 @@ public void testStringAsObjectField() throws Exception {
/**********************************************************************
*/

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

@Test
public void testMixed() throws Exception
{
MixedBean bean = MAPPER.readValue("{"
Expand All @@ -278,6 +294,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 @@ -293,6 +310,7 @@ public void testNonVoidProperty() throws Exception
}

// Test for [module-afterburner#16]
@Test
public void testBigNonVoidProperty() throws Exception
{
final String json = "{ \"stringField\" : \"zoobar\" }";
Expand All @@ -307,12 +325,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 @@ -323,6 +343,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 All @@ -345,6 +366,7 @@ public void testProblemWithIndentation() throws Exception {
}

// [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
@@ -1,8 +1,12 @@
package com.fasterxml.jackson.module.afterburner.deser;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;

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

public class GenericPropertyDeserializationTest extends AfterburnerTestBase
{
static abstract class AbstractMyClass<ID> {
Expand Down Expand Up @@ -31,6 +35,7 @@ public MyClass() { }

final private ObjectMapper MAPPER = newObjectMapper();

@Test
public void testGenericIssue4() throws Exception
{
MyClass result = MAPPER.readValue("{\"id\":\"foo\"}", MyClass.class);
Expand Down
Loading

0 comments on commit 1b7a759

Please sign in to comment.