Skip to content

Commit 4e5f779

Browse files
authored
update(tests): migrate JUnit 4 code to JUnit 5 in core.io, core.json and failing (#1246)
1 parent 7d81ac8 commit 4e5f779

37 files changed

+621
-299
lines changed

src/test/java/com/fasterxml/jackson/core/JUnit5TestBase.java

+53-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import java.nio.charset.StandardCharsets;
55
import java.util.Arrays;
66

7+
import com.fasterxml.jackson.core.io.IOContext;
78
import com.fasterxml.jackson.core.testsupport.MockDataInput;
9+
import com.fasterxml.jackson.core.testsupport.TestSupport;
810
import com.fasterxml.jackson.core.testsupport.ThrottledInputStream;
911
import com.fasterxml.jackson.core.testsupport.ThrottledReader;
12+
import org.junit.jupiter.api.Test;
1013

1114
import static org.junit.jupiter.api.Assertions.assertEquals;
1215
import static org.junit.jupiter.api.Assertions.fail;
@@ -16,6 +19,8 @@
1619
*/
1720
public class JUnit5TestBase
1821
{
22+
protected final static String FIELD_BASENAME = "f";
23+
1924
protected final static int MODE_INPUT_STREAM = 0;
2025
protected final static int MODE_INPUT_STREAM_THROTTLED = 1;
2126
protected final static int MODE_READER = 2;
@@ -228,6 +233,16 @@ public static JsonGenerator createGenerator(TokenStreamFactory f, Writer w) thro
228233
return f.createGenerator(w);
229234
}
230235

236+
/*
237+
/**********************************************************************
238+
/* Helper type construction
239+
/**********************************************************************
240+
*/
241+
242+
public static IOContext testIOContext() {
243+
return TestSupport.testIOContext();
244+
}
245+
231246
/*
232247
/**********************************************************************
233248
/* Assertions
@@ -295,18 +310,10 @@ protected String q(String str) {
295310
return '"'+str+'"';
296311
}
297312

298-
protected static String a2q(String json) {
313+
public static String a2q(String json) {
299314
return json.replace('\'', '"');
300315
}
301316

302-
protected static byte[] utf8Bytes(String str) {
303-
return str.getBytes(StandardCharsets.UTF_8);
304-
}
305-
306-
protected String utf8String(ByteArrayOutputStream bytes) {
307-
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
308-
}
309-
310317
public static byte[] encodeInUTF32BE(String input)
311318
{
312319
int len = input.length();
@@ -321,12 +328,48 @@ public static byte[] encodeInUTF32BE(String input)
321328
return result;
322329
}
323330

331+
protected static byte[] utf8Bytes(String str) {
332+
return str.getBytes(StandardCharsets.UTF_8);
333+
}
334+
335+
protected String utf8String(ByteArrayOutputStream bytes) {
336+
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
337+
}
338+
339+
public static String fieldNameFor(int index)
340+
{
341+
StringBuilder sb = new StringBuilder(16);
342+
fieldNameFor(sb, index);
343+
return sb.toString();
344+
}
345+
346+
private static void fieldNameFor(StringBuilder sb, int index)
347+
{
348+
/* let's do something like "f1.1" to exercise different
349+
* field names (important for byte-based codec)
350+
* Other name shuffling done mostly just for fun... :)
351+
*/
352+
sb.append(FIELD_BASENAME);
353+
sb.append(index);
354+
if (index > 50) {
355+
sb.append('.');
356+
if (index > 200) {
357+
sb.append(index);
358+
if (index > 4000) { // and some even longer symbols...
359+
sb.append(".").append(index);
360+
}
361+
} else {
362+
sb.append(index >> 3); // divide by 8
363+
}
364+
}
365+
}
366+
324367
public static byte[] readResource(String ref)
325368
{
326369
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
327370
final byte[] buf = new byte[4000];
328371

329-
InputStream in = BaseTest.class.getResourceAsStream(ref);
372+
InputStream in = JUnit5TestBase.class.getResourceAsStream(ref);
330373
if (in != null) {
331374
try {
332375
int len;

src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@
22

33
import java.math.BigDecimal;
44

5-
public class BigDecimalParserTest extends com.fasterxml.jackson.core.BaseTest
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class BigDecimalParserTest extends com.fasterxml.jackson.core.JUnit5TestBase
610
{
7-
public void testLongInvalidStringParse() {
11+
@Test
12+
void longInvalidStringParse() {
813
try {
914
BigDecimalParser.parse(genLongInvalidString());
1015
fail("expected NumberFormatException");
1116
} catch (NumberFormatException nfe) {
12-
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
13-
assertTrue("exception message value contains truncated", nfe.getMessage().contains("truncated"));
17+
assertTrue(nfe.getMessage().startsWith("Value \"AAAAA"), "exception message starts as expected?");
18+
assertTrue(nfe.getMessage().contains("truncated"), "exception message value contains truncated");
1419
}
1520
}
1621

17-
public void testLongInvalidStringFastParse() {
22+
@Test
23+
void longInvalidStringFastParse() {
1824
try {
1925
BigDecimalParser.parseWithFastParser(genLongInvalidString());
2026
fail("expected NumberFormatException");
2127
} catch (NumberFormatException nfe) {
22-
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
23-
assertTrue("exception message value contains truncated", nfe.getMessage().contains("truncated"));
28+
assertTrue(nfe.getMessage().startsWith("Value \"AAAAA"), "exception message starts as expected?");
29+
assertTrue(nfe.getMessage().contains("truncated"), "exception message value contains truncated");
2430
}
2531
}
2632

27-
public void testLongValidStringParse() {
33+
@Test
34+
void longValidStringParse() {
2835
String num = genLongValidString(500);
2936
final BigDecimal EXP = new BigDecimal(num);
3037

@@ -34,7 +41,8 @@ public void testLongValidStringParse() {
3441
assertEquals(EXP, BigDecimalParser.parse(num.toCharArray(), 0, num.length()));
3542
}
3643

37-
public void testLongValidStringFastParse() {
44+
@Test
45+
void longValidStringFastParse() {
3846
String num = genLongValidString(500);
3947
final BigDecimal EXP = new BigDecimal(num);
4048

src/test/java/com/fasterxml/jackson/core/io/BigIntegerParserTest.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.fasterxml.jackson.core.io;
22

3-
public class BigIntegerParserTest extends com.fasterxml.jackson.core.BaseTest {
3+
import org.junit.jupiter.api.Test;
44

5-
public void testFastParseBigIntegerFailsWithENotation() {
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
class BigIntegerParserTest extends com.fasterxml.jackson.core.JUnit5TestBase {
9+
10+
@Test
11+
void fastParseBigIntegerFailsWithENotation() {
612
String num = "2e308";
713
try {
814
BigIntegerParser.parseWithFastParser(num);
@@ -12,26 +18,28 @@ public void testFastParseBigIntegerFailsWithENotation() {
1218
}
1319
}
1420

15-
public void testLongStringFastParseBigInteger() {
21+
@Test
22+
void longStringFastParseBigInteger() {
1623
try {
1724
BigIntegerParser.parseWithFastParser(genLongString());
1825
fail("expected NumberFormatException");
1926
} catch (NumberFormatException nfe) {
20-
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
21-
assertTrue("exception message value contains: truncated", nfe.getMessage().contains("truncated"));
22-
assertTrue("exception message value contains: BigInteger", nfe.getMessage().contains("BigInteger"));
27+
assertTrue(nfe.getMessage().startsWith("Value \"AAAAA"), "exception message starts as expected?");
28+
assertTrue(nfe.getMessage().contains("truncated"), "exception message value contains: truncated");
29+
assertTrue(nfe.getMessage().contains("BigInteger"), "exception message value contains: BigInteger");
2330
}
2431
}
2532

26-
public void testLongStringFastParseBigIntegerRadix() {
33+
@Test
34+
void longStringFastParseBigIntegerRadix() {
2735
try {
2836
BigIntegerParser.parseWithFastParser(genLongString(), 8);
2937
fail("expected NumberFormatException");
3038
} catch (NumberFormatException nfe) {
31-
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
32-
assertTrue("exception message value contains: truncated", nfe.getMessage().contains("truncated"));
33-
assertTrue("exception message value contains: radix 8", nfe.getMessage().contains("radix 8"));
34-
assertTrue("exception message value contains: BigInteger", nfe.getMessage().contains("BigInteger"));
39+
assertTrue(nfe.getMessage().startsWith("Value \"AAAAA"), "exception message starts as expected?");
40+
assertTrue(nfe.getMessage().contains("truncated"), "exception message value contains: truncated");
41+
assertTrue(nfe.getMessage().contains("radix 8"), "exception message value contains: radix 8");
42+
assertTrue(nfe.getMessage().contains("BigInteger"), "exception message value contains: BigInteger");
3543
}
3644
}
3745

src/test/java/com/fasterxml/jackson/core/io/BufferRecyclerPoolTest.java

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
package com.fasterxml.jackson.core.io;
22

3-
import java.io.IOException;
4-
import java.io.OutputStream;
5-
6-
import com.fasterxml.jackson.core.BaseTest;
3+
import com.fasterxml.jackson.core.JUnit5TestBase;
74
import com.fasterxml.jackson.core.JsonFactory;
85
import com.fasterxml.jackson.core.JsonGenerator;
96
import com.fasterxml.jackson.core.base.GeneratorBase;
107
import com.fasterxml.jackson.core.util.BufferRecycler;
11-
import com.fasterxml.jackson.core.util.RecyclerPool;
128
import com.fasterxml.jackson.core.util.JsonRecyclerPools;
9+
import com.fasterxml.jackson.core.util.RecyclerPool;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.io.IOException;
13+
import java.io.OutputStream;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
1316

1417
// Tests for [core#1064] wrt custom `BufferRecycler`
15-
public class BufferRecyclerPoolTest extends BaseTest
18+
class BufferRecyclerPoolTest extends JUnit5TestBase
1619
{
17-
public void testNoOp() throws Exception {
20+
@Test
21+
void noOp() throws Exception {
1822
// no-op pool doesn't actually pool anything, so avoid checking it
1923
checkBufferRecyclerPoolImpl(JsonRecyclerPools.nonRecyclingPool(), false, true);
2024
}
2125

22-
public void testThreadLocal() throws Exception {
26+
@Test
27+
void threadLocal() throws Exception {
2328
checkBufferRecyclerPoolImpl(JsonRecyclerPools.threadLocalPool(), true, false);
2429
}
2530

26-
public void testLockFree() throws Exception {
31+
@Test
32+
void lockFree() throws Exception {
2733
checkBufferRecyclerPoolImpl(JsonRecyclerPools.newLockFreePool(), true, true);
2834
}
2935

30-
public void testConcurrentDequeue() throws Exception {
36+
@Test
37+
void concurrentDequeue() throws Exception {
3138
checkBufferRecyclerPoolImpl(JsonRecyclerPools.newConcurrentDequePool(), true, true);
3239
}
3340

34-
public void testBounded() throws Exception {
41+
@Test
42+
void bounded() throws Exception {
3543
checkBufferRecyclerPoolImpl(JsonRecyclerPools.newBoundedPool(1), true, true);
3644
}
3745

38-
public void testPluggingPool() throws Exception {
46+
@Test
47+
void pluggingPool() throws Exception {
3948
checkBufferRecyclerPoolImpl(new TestPool(), true, true);
4049
}
4150

src/test/java/com/fasterxml/jackson/core/io/MergedStreamTest.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
import com.fasterxml.jackson.core.JsonEncoding;
66

7-
public class MergedStreamTest
8-
extends com.fasterxml.jackson.core.BaseTest
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class MergedStreamTest
12+
extends com.fasterxml.jackson.core.JUnit5TestBase
913
{
10-
public void testSimple() throws Exception
14+
@Test
15+
void simple() throws Exception
1116
{
1217
IOContext ctxt = testIOContext();
1318
// bit complicated; must use recyclable buffer...

src/test/java/com/fasterxml/jackson/core/io/NumberInputTest.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import java.math.BigInteger;
44

5-
public class NumberInputTest
6-
extends com.fasterxml.jackson.core.BaseTest
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class NumberInputTest
10+
extends com.fasterxml.jackson.core.JUnit5TestBase
711
{
8-
public void testNastySmallDouble()
12+
@Test
13+
void nastySmallDouble()
914
{
1015
//relates to https://github.com/FasterXML/jackson-core/issues/750
1116
//prior to jackson v2.14, this value used to be returned as Double.MIN_VALUE
@@ -14,7 +19,8 @@ public void testNastySmallDouble()
1419
assertEquals(Double.parseDouble(nastySmallDouble), NumberInput.parseDouble(nastySmallDouble, true));
1520
}
1621

17-
public void testParseFloat()
22+
@Test
23+
void parseFloat()
1824
{
1925
final String exampleFloat = "1.199999988079071";
2026
assertEquals(1.1999999f, NumberInput.parseFloat(exampleFloat, false));
@@ -27,7 +33,8 @@ public void testParseFloat()
2733
assertEquals("1.4E-45", Float.toString(NumberInput.parseFloat(exampleFloat2, true)));
2834
}
2935

30-
public void testParseLongBigInteger()
36+
@Test
37+
void parseLongBigInteger()
3138
{
3239
StringBuilder stringBuilder = new StringBuilder();
3340
for (int i = 0; i < 1000; i++) {
@@ -44,7 +51,8 @@ public void testParseLongBigInteger()
4451
assertEquals(new BigInteger(test2000), NumberInput.parseBigInteger(test2000, true));
4552
}
4653

47-
public void testBigIntegerWithRadix()
54+
@Test
55+
void bigIntegerWithRadix()
4856
{
4957
final String val = "1ABCDEF";
5058
final int radix = 16;
@@ -53,7 +61,8 @@ public void testBigIntegerWithRadix()
5361
assertEquals(expected, NumberInput.parseBigIntegerWithRadix(val, radix, false));
5462
}
5563

56-
public void testParseBigIntegerFailsWithENotation()
64+
@Test
65+
void parseBigIntegerFailsWithENotation()
5766
{
5867
try {
5968
NumberInput.parseBigInteger("1e10", false);
@@ -63,7 +72,8 @@ public void testParseBigIntegerFailsWithENotation()
6372
}
6473
}
6574

66-
public void testLooksLikeValidNumber()
75+
@Test
76+
void looksLikeValidNumber()
6777
{
6878
assertTrue(NumberInput.looksLikeValidNumber("0"));
6979
assertTrue(NumberInput.looksLikeValidNumber("1"));

0 commit comments

Comments
 (0)