Skip to content

Commit

Permalink
update(tests): migrate JUnit 4 code to JUnit 5 in core.base64 to core…
Browse files Browse the repository at this point in the history
….sym (#1247)
  • Loading branch information
timo-a authored Mar 22, 2024
1 parent 4e5f779 commit fff79ea
Show file tree
Hide file tree
Showing 35 changed files with 643 additions and 288 deletions.
56 changes: 56 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/JUnit5TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,38 @@ public static IOContext testIOContext() {
return TestSupport.testIOContext();
}

protected void writeJsonDoc(JsonFactory f, String doc, JsonGenerator g) throws IOException
{
JsonParser p = f.createParser(a2q(doc));

while (p.nextToken() != null) {
g.copyCurrentStructure(p);
}
p.close();
g.close();
}

protected String readAndWrite(JsonFactory f, JsonParser p) throws IOException
{
StringWriter sw = new StringWriter(100);
JsonGenerator g = f.createGenerator(sw);
g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
while (p.nextToken() != null) {
g.copyCurrentEvent(p);
}
} catch (IOException e) {
g.flush();
throw new AssertionError(
"Unexpected problem during `readAndWrite`. Output so far: '" +
sw + "'; problem: " + e.getMessage(),
e);
}
p.close();
g.close();
return sw.toString();
}

/*
/**********************************************************************
/* Assertions
Expand Down Expand Up @@ -364,6 +396,30 @@ private static void fieldNameFor(StringBuilder sb, int index)
}
}

protected int[] calcQuads(String word) {
return calcQuads(utf8Bytes(word));
}

protected int[] calcQuads(byte[] wordBytes) {
int blen = wordBytes.length;
int[] result = new int[(blen + 3) / 4];
for (int i = 0; i < blen; ++i) {
int x = wordBytes[i] & 0xFF;

if (++i < blen) {
x = (x << 8) | (wordBytes[i] & 0xFF);
if (++i < blen) {
x = (x << 8) | (wordBytes[i] & 0xFF);
if (++i < blen) {
x = (x << 8) | (wordBytes[i] & 0xFF);
}
}
}
result[i >> 2] = x;
}
return result;
}

public static byte[] readResource(String ref)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@

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

public class Base64BinaryParsingTest
extends JUnit5TestBase
class Base64BinaryParsingTest
extends JUnit5TestBase
{
private final JsonFactory JSON_F = newStreamFactory();

@Test
public void testBase64UsingInputStream() throws Exception
void base64UsingInputStream() throws Exception
{
_testBase64Text(MODE_INPUT_STREAM);
_testBase64Text(MODE_INPUT_STREAM_THROTTLED);
_testBase64Text(MODE_DATA_INPUT);
}

@Test
public void testBase64UsingReader() throws Exception
void base64UsingReader() throws Exception
{
_testBase64Text(MODE_READER);
}

@Test
public void testStreaming() throws IOException
void streaming() throws IOException
{
_testStreaming(MODE_INPUT_STREAM);
_testStreaming(MODE_INPUT_STREAM_THROTTLED);
Expand All @@ -37,7 +37,7 @@ public void testStreaming() throws IOException
}

@Test
public void testSimple() throws IOException
void simple() throws IOException
{
for (int mode : ALL_MODES) {
// [core#414]: Allow leading/trailign white-space, ensure it is accepted
Expand All @@ -49,29 +49,29 @@ public void testSimple() throws IOException
}

@Test
public void testInArray() throws IOException
void inArray() throws IOException
{
for (int mode : ALL_MODES) {
_testInArray(mode);
}
}

@Test
public void testWithEscaped() throws IOException {
void withEscaped() throws IOException {
for (int mode : ALL_MODES) {
_testEscaped(mode);
}
}

@Test
public void testWithEscapedPadding() throws IOException {
void withEscapedPadding() throws IOException {
for (int mode : ALL_MODES) {
_testEscapedPadding(mode);
}
}

@Test
public void testInvalidTokenForBase64() throws IOException
void invalidTokenForBase64() throws IOException
{
for (int mode : ALL_MODES) {

Expand All @@ -90,7 +90,7 @@ public void testInvalidTokenForBase64() throws IOException
}

@Test
public void testInvalidChar() throws IOException
void invalidChar() throws IOException
{
for (int mode : ALL_MODES) {

Expand Down Expand Up @@ -130,7 +130,7 @@ public void testInvalidChar() throws IOException
}

@Test
public void testOkMissingPadding() throws IOException {
void okMissingPadding() throws IOException {
final byte[] DOC1 = new byte[] { (byte) 0xAD };
_testOkMissingPadding(DOC1, MODE_INPUT_STREAM);
_testOkMissingPadding(DOC1, MODE_INPUT_STREAM_THROTTLED);
Expand Down Expand Up @@ -158,7 +158,7 @@ private void _testOkMissingPadding(byte[] input, int mode) throws IOException
}

@Test
public void testFailDueToMissingPadding() throws IOException {
void failDueToMissingPadding() throws IOException {
final String DOC1 = q("fQ"); // 1 bytes, no padding
_testFailDueToMissingPadding(DOC1, MODE_INPUT_STREAM);
_testFailDueToMissingPadding(DOC1, MODE_INPUT_STREAM_THROTTLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

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

public class Base64CodecTest
extends JUnit5TestBase
class Base64CodecTest
extends JUnit5TestBase
{
@Test
public void testVariantAccess()
void variantAccess()
{
for (Base64Variant var : new Base64Variant[] {
Base64Variants.MIME,
Expand All @@ -32,7 +32,7 @@ public void testVariantAccess()
}

@Test
public void testProps()
void props()
{
Base64Variant std = Base64Variants.MIME;
// let's verify basic props of std cocec
Expand All @@ -47,7 +47,7 @@ public void testProps()
}

@Test
public void testCharEncoding() throws Exception
void charEncoding() throws Exception
{
Base64Variant std = Base64Variants.MIME;
assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Char('?'));
Expand Down Expand Up @@ -82,7 +82,7 @@ public void testCharEncoding() throws Exception
}

@Test
public void testConvenienceMethods() throws Exception
void convenienceMethods() throws Exception
{
final Base64Variant std = Base64Variants.MIME;

Expand All @@ -105,7 +105,7 @@ public void testConvenienceMethods() throws Exception
}

@Test
public void testConvenienceMethodWithLFs() throws Exception
void convenienceMethodWithLFs() throws Exception
{
final Base64Variant std = Base64Variants.MIME;

Expand Down Expand Up @@ -133,7 +133,7 @@ public void testConvenienceMethodWithLFs() throws Exception

@SuppressWarnings("unused")
@Test
public void testErrors() throws Exception
void errors() throws Exception
{
try {
Base64Variant b = new Base64Variant("foobar", "xyz", false, '!', 24);
Expand All @@ -158,7 +158,7 @@ public void testErrors() throws Exception
}

@Test
public void testPaddingReadBehaviour() throws Exception {
void paddingReadBehaviour() throws Exception {

for (Base64Variant variant: Arrays.asList(Base64Variants.MIME, Base64Variants.MIME_NO_LINEFEEDS, Base64Variants.PEM)) {

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

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

public class Base64GenerationTest
extends JUnit5TestBase
class Base64GenerationTest
extends JUnit5TestBase
{
/* The usual sample input string, from Thomas Hobbes's "Leviathan"
* (via Wikipedia)
Expand Down Expand Up @@ -50,15 +50,15 @@ public class Base64GenerationTest
private final JsonFactory JSON_F = new JsonFactory();

@Test
public void testStreamingBinaryWrites() throws Exception
void streamingBinaryWrites() throws Exception
{
_testStreamingWrites(JSON_F, true);
_testStreamingWrites(JSON_F, false);
}

// For [core#55]
@Test
public void testIssue55() throws Exception
void issue55() throws Exception
{
final JsonFactory f = new JsonFactory();

Expand Down Expand Up @@ -91,15 +91,15 @@ public void testIssue55() throws Exception
* test things, as it does need writers to construct the data first.
*/
@Test
public void testSimpleBinaryWrite() throws Exception
void simpleBinaryWrite() throws Exception
{
_testSimpleBinaryWrite(false);
_testSimpleBinaryWrite(true);
}

// for [core#318]
@Test
public void testBinaryAsEmbeddedObject() throws Exception
void binaryAsEmbeddedObject() throws Exception
{
JsonGenerator g;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Base64Padding912Test
extends JUnit5TestBase
class Base64Padding912Test
extends JUnit5TestBase
{
private final JsonFactory JSON_F = newStreamFactory();

@Test
public void testPaddingUsingInputStream() throws Exception
void paddingUsingInputStream() throws Exception
{
_testPadding(MODE_INPUT_STREAM);
_testPadding(MODE_INPUT_STREAM_THROTTLED);
}

@Test
public void testPaddingUsingReader() throws Exception
void paddingUsingReader() throws Exception
{
_testPadding(MODE_READER);
}

@Test
public void testPaddingUsingDataInput() throws Exception
void paddingUsingDataInput() throws Exception
{
_testPadding(MODE_DATA_INPUT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package com.fasterxml.jackson.core.constraints;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;

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

/**
* Unit test(s) for verifying handling of new (in 2.15) StreamReadConstraints
* wrt maximum nesting depth.
*/
public class DeeplyNestedContentReadTest
extends com.fasterxml.jackson.core.BaseTest
class DeeplyNestedContentReadTest
extends com.fasterxml.jackson.core.JUnit5TestBase
{
private final JsonFactory JSON_F = newStreamFactory();

private final int MAX_NESTING = StreamReadConstraints.DEFAULT_MAX_DEPTH;

private final int TESTED_NESTING = MAX_NESTING + 50;

public void testDeepNestingStreaming() throws Exception

@Test
void deepNestingStreaming() throws Exception
{
final String DOC = createDeepNestedDoc(TESTED_NESTING);
for (int mode : ALL_STREAMING_MODES) {
Expand All @@ -37,7 +43,8 @@ private void _testDeepNesting(JsonParser p) throws Exception
}
}

public void testLegacyConstraintSettingTest() throws Exception
@Test
void legacyConstraintSettingTest() throws Exception
{
final int LOWER_MAX = 40;

Expand Down
Loading

0 comments on commit fff79ea

Please sign in to comment.