Skip to content

Commit

Permalink
[JSTEP-10] Migrate tests to JUnit 5 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Feb 2, 2025
1 parent e773f33 commit eec3990
Show file tree
Hide file tree
Showing 49 changed files with 290 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.jaxrs.base;

public abstract class BaseTestBase
extends junit.framework.TestCase
{
// for now just placeholder
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import java.util.Arrays;
import java.util.HashSet;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.jaxrs.base.BaseTestBase;
import com.fasterxml.jackson.jaxrs.cfg.AnnotationBundleKey;

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

// for [jaxrs-providers#111]
public class AnnotationBundleKeyTest
extends BaseTestBase
Expand Down Expand Up @@ -39,6 +43,7 @@ public Helper(@JsonProperty("x") int x) { }
public void setX(@JsonProperty("x") int x) { }
}

@Test
public void testWithClassAnnotations() throws Exception
{
Annotation[] annotation1 = Helper.class.getAnnotations();
Expand All @@ -48,6 +53,7 @@ public void testWithClassAnnotations() throws Exception
_checkWith(annotation1, annotation2);
}

@Test
public void testWithMethodAnnotationEquals() throws Exception
{
// First, same method parameters definitely should match
Expand All @@ -65,25 +71,29 @@ public void testWithMethodAnnotationEquals() throws Exception
_checkWith(annotation3, annotation4);
}

@Test
public void testWithMethodAnnotationDifferent() throws Exception
{
// However: not so with actually differing annotations
_checkNotEqual(Helper.class.getDeclaredMethod("getX").getAnnotations(),
Helper.class.getDeclaredMethod("notX").getAnnotations());
}

@Test
public void testWithMethodParameterAnnotation() throws Exception
{
_checkWith(Helper.class.getDeclaredMethod("setX", Integer.TYPE).getParameterAnnotations()[0],
Helper.class.getDeclaredMethod("setX", Integer.TYPE).getParameterAnnotations()[0]);
}

@Test
public void testWithConstructorAnnotation() throws Exception
{
_checkWith(Helper.class.getConstructor(Integer.TYPE).getAnnotations(),
Helper.class.getConstructor(Integer.TYPE).getAnnotations());
}


@Test
public void testWithConstructorParameterAnnotation() throws Exception
{
_checkWith(Helper.class.getConstructor(Integer.TYPE).getParameterAnnotations()[0],
Expand All @@ -97,18 +107,18 @@ protected void _checkWith(Annotation[] anns1, Annotation[] anns2) {
}
HashSet<Annotation> annsSet1 = new HashSet<Annotation>(Arrays.asList(anns1));
HashSet<Annotation> annsSet2 = new HashSet<Annotation>(Arrays.asList(anns2));
assertTrue("Internal error: should never differ", annsSet1.equals(annsSet2));
assertEquals(annsSet1, annsSet2, "Internal error: should never differ");

AnnotationBundleKey b1 = new AnnotationBundleKey(anns1, Object.class);
AnnotationBundleKey b2 = new AnnotationBundleKey(anns2, Object.class);

assertTrue(String.format("Implementations over %s backed annotations differ", anns1[0].getClass()), (b1.equals(b2) && b2.equals(b1)));
assertTrue((b1.equals(b2) && b2.equals(b1)), String.format("Implementations over %s backed annotations differ", anns1[0].getClass()));
}

protected void _checkNotEqual(Annotation[] anns1, Annotation[] anns2) {
AnnotationBundleKey b1 = new AnnotationBundleKey(anns1, Object.class);
AnnotationBundleKey b2 = new AnnotationBundleKey(anns2, Object.class);

assertFalse(String.format("Implementations over %s backed annotations SHOULD differ but won't", anns1[0].getClass()), (b1.equals(b2) || b2.equals(b1)));
assertFalse((b1.equals(b2) || b2.equals(b1)), String.format("Implementations over %s backed annotations SHOULD differ but won't", anns1[0].getClass()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import java.io.InputStream;
import java.util.Arrays;

import org.junit.Assert;

import com.fasterxml.jackson.core.*;

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

public abstract class JaxrsTestBase
extends junit.framework.TestCase
{
/*
/**********************************************************
Expand Down Expand Up @@ -56,7 +55,7 @@ protected void verifyException(Throwable e, String... matches)

protected void _verifyBytes(byte[] actBytes, byte... expBytes)
{
Assert.assertArrayEquals(expBytes, actBytes);
assertArrayEquals(expBytes, actBytes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.fasterxml.jackson.jaxrs.cbor;

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.*;

public class TestCBORVersions extends JaxrsTestBase
{
@Test
public void testMapperVersions()
{
assertVersion(new JacksonCBORProvider());
Expand All @@ -19,7 +24,7 @@ public void testMapperVersions()
private void assertVersion(Versioned vers)
{
final 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
Expand Up @@ -5,6 +5,10 @@

import javax.ws.rs.core.MediaType;

import org.junit.jupiter.api.Test;

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

/**
* Unit test to check [JACKSON-540]
*/
Expand All @@ -15,6 +19,7 @@ static class Bean {
}

// [Issue#1]: exception for no content
@Test
public void testCanSerializeEmpty() throws IOException
{
JacksonCBORProvider prov = new JacksonCBORProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import javax.ws.rs.Produces;

import org.eclipse.jetty.server.Server;
import org.junit.Assert;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.jaxrs.cbor.CBORMediaTypes;

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

public abstract class SimpleEndpointTestBase extends ResourceTestBase
{
final static int TEST_PORT = 6011;
Expand Down Expand Up @@ -72,6 +75,7 @@ public static class SimpleRawApp extends CBORApplicationWithJackson {
/**********************************************************
*/

@Test
public void testSimpleObject() throws Exception
{
final ObjectMapper mapper = new ObjectMapper(new CBORFactory());
Expand All @@ -91,6 +95,7 @@ public void testSimpleObject() throws Exception
assertEquals(2, p.y);
}

@Test
public void testCustomMediaTypeWithCborExtension() throws Exception
{
final ObjectMapper mapper = new ObjectMapper(new CBORFactory());
Expand All @@ -115,12 +120,13 @@ public void testCustomMediaTypeWithCborExtension() throws Exception

// [Issue#34] Verify that Untouchables act the way as they should
@SuppressWarnings("resource")
@Test
public void testUntouchables() throws Exception
{
Server server = startServer(TEST_PORT, SimpleRawApp.class);
try {
InputStream in = new URL("http://localhost:"+TEST_PORT+"/raw/bytes").openStream();
Assert.assertArrayEquals(UNTOUCHABLE_RESPONSE, readAll(in));
assertArrayEquals(UNTOUCHABLE_RESPONSE, readAll(in));
} finally {
server.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import javax.ws.rs.core.Link;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class LinkTest extends ModuleTestBase
{
private final ObjectMapper MAPPER = mapperWithModule();


@Test
public void testLink() throws Exception
{
Link input = Link.fromUri("http://dot.com?foo=bar")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.ObjectMapper;

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

public abstract class ModuleTestBase
extends junit.framework.TestCase
{
protected ObjectMapper mapperWithModule() {
return new ObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import java.io.IOException;
import java.util.*;

import org.junit.Assert;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;

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

public abstract class JaxrsTestBase
extends junit.framework.TestCase
{
protected static class NoCheckSubTypeValidator
extends PolymorphicTypeValidator.Base
Expand Down Expand Up @@ -68,7 +67,7 @@ protected void verifyException(Throwable e, String... matches)

protected void _verifyBytes(byte[] actBytes, byte... expBytes)
{
Assert.assertArrayEquals(expBytes, actBytes);
assertArrayEquals(expBytes, actBytes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

import javax.ws.rs.core.MediaType;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.jaxrs.cfg.JaxRSFeature;

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

/**
* Unit test to check [JACKSON-540]
*/
public class TestCanDeserialize extends JaxrsTestBase {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCanDeserialize() throws IOException {
Map<String, Object> object = new LinkedHashMap<String, Object>();
JacksonJsonProvider prov = new JacksonJsonProvider();
Expand All @@ -31,6 +36,7 @@ public void testCanDeserialize() throws IOException {
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCanDeserializeEmpty() throws IOException {
JacksonJsonProvider prov = new JacksonJsonProvider();

Expand All @@ -46,7 +52,8 @@ public void testCanDeserializeEmpty() throws IOException {
/**
* Unit test for verifying functioning of {@link JaxRSFeature#ALLOW_EMPTY_INPUT}.
*/
public void testFailingDeserializeEmpty() throws IOException {
@Test
public void testFailingDeserializeEmpty() throws IOException {
JacksonJsonProvider prov = new JacksonJsonProvider();
prov.disable(JaxRSFeature.ALLOW_EMPTY_INPUT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.io.*;
import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

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

/**
* Unit test to check [JACKSON-540]
*/
Expand All @@ -20,6 +24,7 @@ static class Simple {
public void setList(List<String> l) { list = l; }
}

@Test
public void testCanSerialize() throws IOException
{
ObjectMapper mapper = JsonMapper.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.fasterxml.jackson.jaxrs.json;

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.*;

public class TestJSONVersions extends JaxrsTestBase
{
@Test
public void testMapperVersions()
{
assertVersion(new JacksonJsonProvider());
Expand All @@ -19,7 +24,7 @@ public void testMapperVersions()
private void assertVersion(Versioned vers)
{
final 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
Expand Up @@ -6,13 +6,17 @@

import javax.ws.rs.core.MediaType;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.annotation.JacksonFeatures;

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

/**
* Tests for [Issue-2], Addition of {@link JacksonFeatures}.
*/
Expand Down Expand Up @@ -45,6 +49,7 @@ public void writeConfig2() { }
*/

// [Issue-2], serialization
@Test
public void testWriteConfigs() throws Exception
{
JacksonJsonProvider prov = new JacksonJsonProvider();
Expand All @@ -66,6 +71,7 @@ public void testWriteConfigs() throws Exception
assertEquals("{\"a\":3}", out.toString("UTF-8"));
}

@Test
public void testWriteConfigsViaBundle() throws Exception
{
JacksonJsonProvider prov = new JacksonJsonProvider();
Expand All @@ -79,6 +85,7 @@ public void testWriteConfigsViaBundle() throws Exception
}

// [Issue-2], deserialization
@Test
public void testReadConfigs() throws Exception
{
JacksonJsonProvider prov = new JacksonJsonProvider();
Expand Down
Loading

0 comments on commit eec3990

Please sign in to comment.