Skip to content

Commit

Permalink
update(tests): migrate JUnit 4 code to JUnit 5 in async tests (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-a committed Mar 21, 2024
1 parent 6c7daa0 commit 7d762cd
Show file tree
Hide file tree
Showing 32 changed files with 455 additions and 190 deletions.
4 changes: 2 additions & 2 deletions src/test/java/com/fasterxml/jackson/core/JUnit5TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ protected String q(String str) {
return '"'+str+'"';
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapperForByteArray;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapperForByteBuffer;

public abstract class AsyncTestBase extends BaseTest
public abstract class AsyncTestBase extends JUnit5TestBase
{
final static String SPACES = " ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import java.io.IOException;

import com.fasterxml.jackson.core.*;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

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

// [core#1047]: Add max-name-length constraints
public class LargeDocReadTest extends AsyncTestBase
{
Expand All @@ -17,20 +21,23 @@ public class LargeDocReadTest extends AsyncTestBase
.build();

// Test name that is below default max name
@Test
public void testLargeNameBytes() throws Exception {
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);
try (JsonParser p = createParserUsingStream(JSON_F_DEFAULT, doc, "UTF-8")) {
consumeTokens(p);
}
}

@Test
public void testLargeNameChars() throws Exception {
final String doc = generateJSON(StreamReadConstraints.defaults().getMaxNameLength() - 100);
try (JsonParser p = createParserUsingReader(JSON_F_DEFAULT, doc)) {
consumeTokens(p);
}
}

@Test
public void testLargeNameWithSmallLimitBytes() throws Exception
{
final String doc = generateJSON(12_000);
Expand All @@ -42,6 +49,7 @@ public void testLargeNameWithSmallLimitBytes() throws Exception
}
}

@Test
public void testLargeNameWithSmallLimitChars() throws Exception
{
final String doc = generateJSON(12_000);
Expand All @@ -53,6 +61,7 @@ public void testLargeNameWithSmallLimitChars() throws Exception
}
}

@Test
public void testLargeNameWithSmallLimitAsync() throws Exception
{
final byte[] doc = utf8Bytes(generateJSON(12_000));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.io.*;

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

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

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

public class AsyncBinaryParseTest extends AsyncTestBase
class AsyncBinaryParseTest extends AsyncTestBase
{
private final JsonFactory JSON_F = new JsonFactory();

Expand All @@ -18,15 +20,18 @@ public class AsyncBinaryParseTest extends AsyncTestBase
139000
};

public void testRawAsRootValue() throws IOException {
@Test
void rawAsRootValue() throws IOException {
_testBinaryAsRoot(JSON_F);
}

public void testRawAsArray() throws IOException {
@Test
void rawAsArray() throws IOException {
_testBinaryAsArray(JSON_F);
}

public void testRawAsObject() throws IOException {
@Test
void rawAsObject() throws IOException {
_testBinaryAsObject(JSON_F);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.fasterxml.jackson.core.json.async;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

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

/**
* Set of basic unit tests for verifying that the basic parser
* functionality works as expected.
*/
public class AsyncCharEscapingTest extends AsyncTestBase
class AsyncCharEscapingTest extends AsyncTestBase
{
private final JsonFactory JSON_F = new JsonFactory();

public void testMissingLinefeedEscaping() throws Exception
@Test
void missingLinefeedEscaping() throws Exception
{
byte[] doc = _jsonDoc(a2q("['Linefeed: \n.']"));
_testMissingLinefeedEscaping(doc, 0, 99);
Expand Down Expand Up @@ -42,7 +48,8 @@ private void _testMissingLinefeedEscaping(byte[] doc, int offset, int readSize)
r.close();
}

public void testSimpleEscaping() throws Exception
@Test
void simpleEscaping() throws Exception
{
_testSimpleEscaping(0, 99);
_testSimpleEscaping(0, 5);
Expand Down Expand Up @@ -91,7 +98,8 @@ private void _testSimpleEscaping(int offset, int readSize) throws Exception
r.close();
}

public void test8DigitSequence() throws Exception
@Test
void test8DigitSequence() throws Exception
{
String DOC = "[\"\\u00411234\"]";
AsyncReaderWrapper r = asyncForBytes(JSON_F, 1, _jsonDoc(DOC), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import java.io.*;

import com.fasterxml.jackson.core.*;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

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

/**
* Unit tests for verifying that support for (non-standard) comments
* works as expected.
*/
public class AsyncCommentParsingTest extends AsyncTestBase
class AsyncCommentParsingTest extends AsyncTestBase
{
final static String DOC_WITH_SLASHSTAR_COMMENT =
"[ /* comment:\n ends here */ 1 /* one more ok to have \"unquoted\" */ ]"
Expand All @@ -27,13 +31,15 @@ public class AsyncCommentParsingTest extends AsyncTestBase
/**********************************************************
*/

public void testCommentsDisabled() throws Exception
@Test
void commentsDisabled() throws Exception
{
_testDisabled(DOC_WITH_SLASHSTAR_COMMENT);
_testDisabled(DOC_WITH_SLASHSLASH_COMMENT);
}

public void testCommentsEnabled() throws Exception
@Test
void commentsEnabled() throws Exception
{
_testEnabled(DOC_WITH_SLASHSTAR_COMMENT, 99);
_testEnabled(DOC_WITH_SLASHSTAR_COMMENT, 3);
Expand All @@ -44,7 +50,8 @@ public void testCommentsEnabled() throws Exception
_testEnabled(DOC_WITH_SLASHSLASH_COMMENT, 1);
}

public void testCCommentsWithUTF8() throws Exception
@Test
void cCommentsWithUTF8() throws Exception
{
final String JSON = "/* \u00a9 2099 Yoyodyne Inc. */\n [ \"bar? \u00a9\" ]\n";

Expand All @@ -55,7 +62,8 @@ public void testCCommentsWithUTF8() throws Exception
_testWithUTF8Chars(JSON, 1);
}

public void testYAMLCommentsEnabled() throws Exception
@Test
void yamlCommentsEnabled() throws Exception
{
final JsonFactory f = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_YAML_COMMENTS)
Expand All @@ -73,7 +81,8 @@ public void testYAMLCommentsEnabled() throws Exception
_testCommentsBetweenArrayValues(f, "# foo\n", 1);
}

public void testCCommentsEnabled() throws Exception {
@Test
void cCommentsEnabled() throws Exception {
final JsonFactory f = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
.build();
Expand All @@ -83,7 +92,8 @@ public void testCCommentsEnabled() throws Exception {
_testCommentsBeforePropValue(f, COMMENT, 1);
}

public void testCppCommentsEnabled() throws Exception {
@Test
void cppCommentsEnabled() throws Exception {
final JsonFactory f = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.core.*;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

public class AsyncConcurrencyTest extends AsyncTestBase
import static org.junit.jupiter.api.Assertions.fail;

class AsyncConcurrencyTest extends AsyncTestBase
{
final static JsonFactory JSON_F = new JsonFactory();
static {
Expand Down Expand Up @@ -103,7 +107,8 @@ public void close() throws Exception {
}

// [jackson-core#476]
public void testConcurrentAsync() throws Exception
@Test
void concurrentAsync() throws Exception
{
final int MAX_ROUNDS = 30;
for (int i = 0; i < MAX_ROUNDS; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import java.io.*;

import com.fasterxml.jackson.core.*;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

public class AsyncFieldNamesTest extends AsyncTestBase
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class AsyncFieldNamesTest extends AsyncTestBase
{
private final JsonFactory JSON_F = new JsonFactory();

Expand All @@ -16,7 +21,8 @@ public class AsyncFieldNamesTest extends AsyncTestBase
.build();

// Mainly to test "fast" parse for shortish names
public void testSimpleFieldNames() throws IOException
@Test
void simpleFieldNames() throws IOException
{
for (String name : new String[] { "", "a", "ab", "abc", "abcd",
"abcd1", "abcd12", "abcd123", "abcd1234",
Expand Down Expand Up @@ -45,7 +51,8 @@ private void _testSimpleFieldName(String fieldName) throws IOException
assertEquals(1, loc.getColumnNr());
}

public void testEscapedFieldNames() throws IOException
@Test
void escapedFieldNames() throws IOException
{
_testEscapedFieldNames("\\'foo\\'", "'foo'");
_testEscapedFieldNames("\\'foobar\\'", "'foobar'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

import com.fasterxml.jackson.core.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.async.AsyncTestBase;

import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

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

// Tests for verifying things such as handling of invalid control characters;
// decoding of UTF-8 BOM.
public class AsyncInvalidCharsTest extends AsyncTestBase
class AsyncInvalidCharsTest extends AsyncTestBase
{
private final JsonFactory JSON_F = new JsonFactory();

public void testUtf8BOMHandling() throws Exception
@Test
void utf8BOMHandling() throws Exception
{
_testUtf8BOMHandling(0, 99);
_testUtf8BOMHandling(0, 5);
Expand Down Expand Up @@ -90,7 +96,8 @@ private void _testUTF8BomFail(int offset, int readSize,
}
}

public void testHandlingOfInvalidSpace() throws Exception
@Test
void handlingOfInvalidSpace() throws Exception
{
_testHandlingOfInvalidSpace(0, 99);
_testHandlingOfInvalidSpace(0, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

import java.nio.ByteBuffer;

public class AsyncLocationTest extends AsyncTestBase
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class AsyncLocationTest extends AsyncTestBase
{
private final JsonFactory DEFAULT_F = new JsonFactory();

// for [core#531]
public void testLocationOffsets() throws Exception
@Test
void locationOffsets() throws Exception
{
JsonParser parser = DEFAULT_F.createNonBlockingByteArrayParser();
ByteArrayFeeder feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
Expand All @@ -39,7 +44,8 @@ public void testLocationOffsets() throws Exception
parser.close();
}

public void testLocationOffsetsByteBuffer() throws Exception
@Test
void locationOffsetsByteBuffer() throws Exception
{
JsonParser parser = DEFAULT_F.createNonBlockingByteBufferParser();
ByteBufferFeeder feeder = (ByteBufferFeeder) parser.getNonBlockingInputFeeder();
Expand Down
Loading

0 comments on commit 7d762cd

Please sign in to comment.