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

Start converting some unit tests to JUnit5; add new test base class #1212

Merged
merged 1 commit into from
Feb 11, 2024
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
4 changes: 4 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

import junit.framework.TestCase;

/**
* Base class for JUnit 4 based tests. To be deprecated from
* Jackson 2.18 or later.
*/
@SuppressWarnings("resource")
public abstract class BaseTest
extends TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.fasterxml.jackson.core;

import static org.assertj.core.api.Assertions.assertThat;
import com.fasterxml.jackson.core.io.ContentReference;

import static org.assertj.core.api.Assertions.assertThat;

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

import org.junit.jupiter.api.Test;

/**
* Unit tests for class {@link ErrorReportConfiguration}.
*
* @since 2.16
*/
public class ErrorReportConfigurationTest
extends BaseTest
extends JUnit5TestBase
{

/*
/**********************************************************
/* Unit Tests
Expand All @@ -24,6 +29,7 @@ public class ErrorReportConfigurationTest

private final ErrorReportConfiguration DEFAULTS = ErrorReportConfiguration.defaults();

@Test
public void testNormalBuild()
{
ErrorReportConfiguration config = ErrorReportConfiguration.builder()
Expand All @@ -35,6 +41,7 @@ public void testNormalBuild()
assertEquals(2008, config.getMaxRawContentLength());
}

@Test
public void testZeroLengths()
{
// boundary tests, because we throw error on negative values
Expand All @@ -47,6 +54,7 @@ public void testZeroLengths()
assertEquals(0, config.getMaxRawContentLength());
}

@Test
public void testInvalidMaxErrorTokenLength()
{
ErrorReportConfiguration.Builder builder = ErrorReportConfiguration.builder();
Expand All @@ -66,6 +74,7 @@ public void testInvalidMaxErrorTokenLength()
}
}

@Test
public void testDefaults()
{
// default value
Expand All @@ -76,6 +85,7 @@ public void testDefaults()
assertEquals(ErrorReportConfiguration.defaults(), ErrorReportConfiguration.defaults());
}

@Test
public void testOverrideDefaultErrorReportConfiguration()
{
// (1) override with null, will be no change
Expand Down Expand Up @@ -106,6 +116,7 @@ public void testOverrideDefaultErrorReportConfiguration()
}
}

@Test
public void testRebuild()
{
ErrorReportConfiguration config = ErrorReportConfiguration.builder().build();
Expand All @@ -115,6 +126,7 @@ public void testRebuild()
assertEquals(config.getMaxRawContentLength(), rebuiltConfig.getMaxRawContentLength());
}

@Test
public void testBuilderConstructorWithErrorReportConfiguration()
{
ErrorReportConfiguration configA = ErrorReportConfiguration.builder()
Expand All @@ -128,6 +140,7 @@ public void testBuilderConstructorWithErrorReportConfiguration()
assertEquals(configA.getMaxRawContentLength(), configB.getMaxRawContentLength());
}

@Test
public void testWithJsonLocation() throws Exception
{
// Truncated result
Expand All @@ -138,6 +151,7 @@ public void testWithJsonLocation() throws Exception
_verifyJsonLocationToString("abc", 4, "\"abc\"");
}

@Test
public void testWithJsonFactory() throws Exception
{
// default
Expand All @@ -160,6 +174,7 @@ public void testWithJsonFactory() throws Exception
.maxRawContentLength(0).build());
}

@Test
public void testExpectedTokenLengthWithConfigurations()
throws Exception
{
Expand Down Expand Up @@ -201,6 +216,7 @@ public void testExpectedTokenLengthWithConfigurations()
}
}

@Test
public void testNonPositiveErrorTokenConfig()
{
// Zero should be ok
Expand All @@ -217,6 +233,7 @@ public void testNonPositiveErrorTokenConfig()
}
}

@Test
public void testNullSetterThrowsException() {
try {
newStreamFactory().setErrorReportConfiguration(null);
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/JUnit5TestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.fasterxml.jackson.core;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

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

/**
* Intended replacement for {@link BaseTest}
*/
public class JUnit5TestBase
{
/*
/**********************************************************************
/* Factory methods
/**********************************************************************
*/

protected JsonFactory newStreamFactory() {
return new JsonFactory();
}

protected JsonFactoryBuilder streamFactoryBuilder() {
return (JsonFactoryBuilder) JsonFactory.builder();
}

/*
/**********************************************************************
/* Assertions
/**********************************************************************
*/

protected void assertToken(JsonToken expToken, JsonToken actToken)
{
if (actToken != expToken) {
fail("Expected token "+expToken+", current token "+actToken);
}
}

protected void assertToken(JsonToken expToken, JsonParser p)
{
assertToken(expToken, p.currentToken());
}

/**
* @param e Exception to check
* @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included
* in {@code e.getMessage()} -- using case-INSENSITIVE comparison
*/
public static void verifyException(Throwable e, String... anyMatches)
{
String msg = e.getMessage();
String lmsg = (msg == null) ? "" : msg.toLowerCase();
for (String match : anyMatches) {
String lmatch = match.toLowerCase();
if (lmsg.indexOf(lmatch) >= 0) {
return;
}
}
fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\"");
}

/*
/**********************************************************************
/* Escaping/quoting
/**********************************************************************
*/

protected String q(String str) {
return '"'+str+'"';
}

protected String a2q(String json) {
return json.replace("'", "\"");
}

protected byte[] utf8Bytes(String str) {
return str.getBytes(StandardCharsets.UTF_8);
}

protected String utf8String(ByteArrayOutputStream bytes) {
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
}

}