Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 1, 2023
2 parents 883a2a4 + 3155455 commit 7869b6b
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tools/jackson/core/JsonLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class JsonLocation
implements java.io.Serializable
{
private static final long serialVersionUID = 2L; // in 2.13
private static final long serialVersionUID = 2L;

/**
* Shared immutable "N/A location" that can be returned to indicate
Expand Down
85 changes: 59 additions & 26 deletions src/main/java/tools/jackson/core/io/ContentReference.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package tools.jackson.core.io;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import tools.jackson.core.ErrorReportConfiguration;

/**
* Abstraction that encloses information about content being processed --
* input source or output target, streaming or
Expand All @@ -31,7 +30,7 @@ public class ContentReference
* content snippets will be included.
*/
protected final static ContentReference UNKNOWN_CONTENT =
new ContentReference(false, null);
new ContentReference(false, null, ErrorReportConfiguration.defaults());

/**
* As content will be redacted by default in Jackson 2.16 and later,
Expand All @@ -42,16 +41,7 @@ public class ContentReference
* @since 2.16
*/
protected final static ContentReference REDACTED_CONTENT =
new ContentReference(false, null);

/**
* Include at most first 500 characters/bytes from contents; should be enough
* to give context, but not cause unfortunate side effects in things like
* logs.
*
* @since 2.9
*/
public static final int DEFAULT_MAX_CONTENT_SNIPPET = 500;
new ContentReference(false, null, ErrorReportConfiguration.defaults());

/**
* Reference to the actual underlying content.
Expand Down Expand Up @@ -79,23 +69,37 @@ public class ContentReference
*/
protected final boolean _isContentTextual;

/**
* max raw content to return as configured
*
* @since 2.16
*/
protected final int _maxRawContentLength;

/*
/**********************************************************************
/* Life-cycle
/**********************************************************************
*/

protected ContentReference(boolean isContentTextual, Object rawContent) {
this(isContentTextual, rawContent, -1, -1);
/**
* @since 2.16
*/
protected ContentReference(boolean isContentTextual, Object rawContent, ErrorReportConfiguration errorReportConfiguration)
{
this(isContentTextual, rawContent, -1, -1, errorReportConfiguration);
}

/**
* @since 2.16
*/
protected ContentReference(boolean isContentTextual, Object rawContent,
int offset, int length)
int offset, int length, ErrorReportConfiguration errorReportConfiguration)
{
_isContentTextual = isContentTextual;
_rawContent = rawContent;
_offset = offset;
_length = length;
_maxRawContentLength = errorReportConfiguration.getMaxRawContentLength();
}

/**
Expand All @@ -122,14 +126,41 @@ public static ContentReference unknown() {
public static ContentReference redacted() {
return REDACTED_CONTENT;
}



/**
* @deprecated Since 2.16. Use {@link #construct(boolean, Object, ErrorReportConfiguration)} instead.
*/
@Deprecated
public static ContentReference construct(boolean isContentTextual, Object rawContent) {
return new ContentReference(isContentTextual, rawContent);
return new ContentReference(isContentTextual, rawContent, ErrorReportConfiguration.defaults());
}

/**
* @deprecated Since 2.16. Use {@link #construct(boolean, Object, int, int, ErrorReportConfiguration)} instead.
*/
@Deprecated
public static ContentReference construct(boolean isContentTextual, Object rawContent,
int offset, int length) {
return new ContentReference(isContentTextual, rawContent, offset, length);
return new ContentReference(isContentTextual, rawContent, offset, length, ErrorReportConfiguration.defaults());
}

/**
* @since 2.16
*/
public static ContentReference construct(boolean isContentTextual, Object rawContent,
int offset, int length, ErrorReportConfiguration errorReportConfiguration)
{
return new ContentReference(isContentTextual, rawContent, offset, length, errorReportConfiguration);
}

/**
* @since 2.16
*/
public static ContentReference construct(boolean isContentTextual, Object rawContent,
ErrorReportConfiguration errorReportConfiguration)
{
return new ContentReference(isContentTextual, rawContent, errorReportConfiguration);
}

/**
Expand All @@ -151,7 +182,8 @@ public static ContentReference rawReference(boolean isContentTextual,
if (rawContent instanceof ContentReference) {
return (ContentReference) rawContent;
}
return new ContentReference(isContentTextual, rawContent);
return new ContentReference(isContentTextual, rawContent,
ErrorReportConfiguration.defaults());
}

public static ContentReference rawReference(Object rawContent) {
Expand Down Expand Up @@ -201,10 +233,11 @@ public Object getRawContent() {
* which content is counted, either bytes or chars) to use for truncation
* (so as not to include full content for humongous sources or targets)
*
* @see ErrorReportConfiguration#getMaxRawContentLength()
* @return Maximum content snippet to include before truncating
*/
protected int maxContentSnippetLength() {
return DEFAULT_MAX_CONTENT_SNIPPET;
protected int maxRawContentLength() {
return _maxRawContentLength;
}

/*
Expand Down Expand Up @@ -264,7 +297,7 @@ public StringBuilder appendSourceDescription(StringBuilder sb)
String trimmed;

// poor man's tuple...
final int maxLen = maxContentSnippetLength();
final int maxLen = maxRawContentLength();
int[] offsets = new int[] { contentOffset(), contentLength() };

if (srcRef instanceof CharSequence) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tools.jackson.core;

import tools.jackson.core.io.ContentReference;

/**
* Unit tests for class {@link ErrorReportConfiguration#getMaxRawContentLength()}.
*/
public class ErrorReportConfigurationMaxRawContentLengthTest extends BaseTest
{
/*
/**********************************************************
/* Unit Tests
/**********************************************************
*/

public void testBasicToStringErrorConfig() throws Exception {
// Truncated result
_verifyToString("abc", 2,
"[Source: (String)\"ab\"[truncated 1 chars]; line: 1, column: 1]");
// Exact length
_verifyToString("abc", 3,
"[Source: (String)\"abc\"; line: 1, column: 1]");
// Enough length
_verifyToString("abc", 4,
"[Source: (String)\"abc\"; line: 1, column: 1]");
}

/*
/**********************************************************
/* Internal helper methods
/**********************************************************
*/

private void _verifyToString(String rawSrc, int rawContentLength, String expectedMessage) {
ContentReference reference = _sourceRefWithErrorReportConfig(rawSrc, rawContentLength);
String location = new JsonLocation(reference, 10L, 10L, 1, 1).toString();
assertEquals(expectedMessage, location);
}

private ContentReference _sourceRefWithErrorReportConfig(String rawSrc, int rawContentLength) {
return _sourceRef(rawSrc,
ErrorReportConfiguration.builder().maxRawContentLength(rawContentLength).build());
}

private ContentReference _sourceRef(String rawSrc, ErrorReportConfiguration errorReportConfiguration) {
return ContentReference.construct(true, rawSrc, 0, rawSrc.length(),errorReportConfiguration);
}

}
92 changes: 33 additions & 59 deletions src/test/java/tools/jackson/core/ErrorReportConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package tools.jackson.core;

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

import tools.jackson.core.ErrorReportConfiguration;

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

private final ErrorReportConfiguration DEFAULTS = ErrorReportConfiguration.defaults();

@Test
public void testNormalBuild()
{
ErrorReportConfiguration config = ErrorReportConfiguration.builder()
Expand All @@ -28,40 +19,37 @@ public void testNormalBuild()
assertEquals(2008, config.getMaxRawContentLength());
}

@Test
public void testZeroLengths()
{
// boundary tests, because we throw error on negative values
ErrorReportConfiguration config = ErrorReportConfiguration.builder()
.maxErrorTokenLength(0)
.maxRawContentLength(0)
.build();

assertEquals(0, config.getMaxErrorTokenLength());
assertEquals(0, config.getMaxRawContentLength());
}

@Test
public void testInvalidMaxErrorTokenLength()
{
ErrorReportConfiguration.Builder builder = ErrorReportConfiguration.builder();

try {
builder.maxErrorTokenLength(-1);
fail("Should not reach here as exception is expected");
} catch (IllegalArgumentException ex) {
// expected
verifyException(ex, "Value of maxErrorTokenLength");
verifyException(ex, "cannot be negative");
}

try {
builder.maxRawContentLength(-1);
fail("Should not reach here as exception is expected");
} catch (IllegalArgumentException ex) {
// expected
verifyException(ex, "Value of maxRawContentLength");
verifyException(ex, "cannot be negative");
}
}

@Test
public void testDefaults()
{
// default value
Expand All @@ -72,68 +60,54 @@ public void testDefaults()
assertEquals(ErrorReportConfiguration.defaults(), ErrorReportConfiguration.defaults());
}

@Test
public void testOverrideDefaultErrorReportConfiguration()
{
// (1) override with null, will be no change
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(null);

ErrorReportConfiguration nullDefaults = ErrorReportConfiguration.defaults();

assertEquals(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH, nullDefaults.getMaxErrorTokenLength());
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH, nullDefaults.getMaxRawContentLength());

// (2) override wiht other value that actually changes default values
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(ErrorReportConfiguration.builder()
.maxErrorTokenLength(10101)
.maxRawContentLength(20202)
.build());

ErrorReportConfiguration overrideDefaults = ErrorReportConfiguration.defaults();

assertEquals(10101, overrideDefaults.getMaxErrorTokenLength());
assertEquals(20202, overrideDefaults.getMaxRawContentLength());

// (3) revert back to default values
// IMPORTANT : make sure to revert back, otherwise other tests will be affected
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(ErrorReportConfiguration.builder()
.maxErrorTokenLength(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH)
.maxRawContentLength(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH)
.build());
try {
ErrorReportConfiguration nullDefaults = ErrorReportConfiguration.defaults();

assertEquals(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH, nullDefaults.getMaxErrorTokenLength());
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH, nullDefaults.getMaxRawContentLength());

// (2) override with other value that actually changes default values
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(ErrorReportConfiguration.builder()
.maxErrorTokenLength(10101)
.maxRawContentLength(20202)
.build());

ErrorReportConfiguration overrideDefaults = ErrorReportConfiguration.defaults();

assertEquals(10101, overrideDefaults.getMaxErrorTokenLength());
assertEquals(20202, overrideDefaults.getMaxRawContentLength());
} finally {
// (3) revert back to default values
// IMPORTANT : make sure to revert back, otherwise other tests will be affected
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(ErrorReportConfiguration.builder()
.maxErrorTokenLength(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH)
.maxRawContentLength(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH)
.build());
}
}

@Test
public void testRebuild()
{
ErrorReportConfiguration config = ErrorReportConfiguration.builder().build();

ErrorReportConfiguration rebuiltConfig = config.rebuild().build();

assertEquals(config.getMaxErrorTokenLength(), rebuiltConfig.getMaxErrorTokenLength());
assertEquals(config.getMaxRawContentLength(), rebuiltConfig.getMaxRawContentLength());
}


@Test
public void testBuilderConstructorWithTwoParams()
{
ErrorReportConfiguration config = new ErrorReportConfiguration.Builder(1313, 2424)
.build();

assertEquals(1313, config.getMaxErrorTokenLength());
assertEquals(2424, config.getMaxRawContentLength());
}

@Test
public void testBuilderConstructorWithErrorReportConfiguration()
{
ErrorReportConfiguration configA = ErrorReportConfiguration.builder()
.maxErrorTokenLength(1234)
.maxRawContentLength(5678)
.build();

ErrorReportConfiguration configB = new ErrorReportConfiguration.Builder(configA).build();

assertEquals(configA.getMaxErrorTokenLength(), configB.getMaxErrorTokenLength());
assertEquals(configA.getMaxRawContentLength(), configB.getMaxRawContentLength());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/tools/jackson/core/JsonLocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testBasicToString()
public void testTruncatedSource()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ContentReference.DEFAULT_MAX_CONTENT_SNIPPET; ++i) {
for (int i = 0; i < ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH; ++i) {
sb.append("x");
}
String main = sb.toString();
Expand Down

0 comments on commit 7869b6b

Please sign in to comment.