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

Fix tests that make wrong assumptions about character encoding #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 18 additions & 17 deletions tck/src/main/java/ee/jakarta/tck/json/bind/SimpleMappingTester.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -22,6 +22,7 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

Expand Down Expand Up @@ -65,14 +66,14 @@ public void test(T value, String expectedRepresentationPattern,
}

private void testMarshalling(T value, String expectedRepresentation) {
String jsonString = jsonb.toJson(value);
String jsonString = jsonb.toJson(value); // Assumes character encoding is Default when creating Writer
assertThat("[testMarshalling] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
}

private void testMarshallingToStream(T value, String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(value, stream);
jsonb.toJson(value, stream); // Assumes character encoding is UTF-8 when creating Writer
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
assertThat("[testMarshallingToStream] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
Expand All @@ -84,8 +85,8 @@ private void testMarshallingToStream(T value, String expectedRepresentation) {
private void testMarshallingToWriter(T value, String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {
jsonb.toJson(value, writer);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
jsonb.toJson(value, writer); // Writer determins character encoding (Default)
String jsonString = new String(stream.toByteArray(), Charset.defaultCharset());
assertThat("[testMarshallingToWriter] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
} catch (IOException e) {
Expand All @@ -94,14 +95,14 @@ private void testMarshallingToWriter(T value, String expectedRepresentation) {
}

private void testMarshallingByType(T value, String expectedRepresentation) {
String jsonString = jsonb.toJson(value, serializationType);
String jsonString = jsonb.toJson(value, serializationType); // Assumes character encoding is Default when creating Writer
assertThat("[testMarshallingByType] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
}

private void testMarshallingByTypeToStream(T value, String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(value, serializationType, stream);
jsonb.toJson(value, serializationType, stream); // Assumes character encoding is UTF-8 when creating Writer
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
assertThat("[testMarshallingByTypeToStream] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
Expand All @@ -113,8 +114,8 @@ private void testMarshallingByTypeToStream(T value, String expectedRepresentatio
private void testMarshallingByTypeToWriter(T value, String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {
jsonb.toJson(value, serializationType, writer);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
jsonb.toJson(value, serializationType, writer); // Writer determins character encoding (Default)
String jsonString = new String(stream.toByteArray(), Charset.defaultCharset());
assertThat("[testMarshallingByTypeToWriter] - Failed to correctly marshal " + value.getClass().getName() + " object",
jsonString, matchesPattern(expectedRepresentation));
} catch (IOException e) {
Expand All @@ -123,14 +124,14 @@ private void testMarshallingByTypeToWriter(T value, String expectedRepresentatio
}

private void testUnmarshallingByClass(String expectedRepresentation, Object value) {
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation, typeClass);
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation, typeClass); // Assumes character encoding is Default when creating Reader
assertThat("[testUnmarshallingByClass] - Failed to correctly unmarshal " + value.getClass().getName() + " object",
unmarshalledObject, is(value));
}

private void testUnmarshallingByClassFromStream(String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes(StandardCharsets.UTF_8))) {
Object unmarshalledObject = jsonb.fromJson(stream, typeClass);
Object unmarshalledObject = jsonb.fromJson(stream, typeClass); // Assumes character encoding is UTF-8 when creating Reader
assertThat("[testUnmarshallingByClassFromStream] - Failed to correctly unmarshal " + value.getClass()
.getName() + " object",
unmarshalledObject, is(value));
Expand All @@ -140,9 +141,9 @@ private void testUnmarshallingByClassFromStream(String expectedRepresentation, O
}

private void testUnmarshallingByClassFromReader(String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes(StandardCharsets.UTF_8));
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes());
InputStreamReader reader = new InputStreamReader(stream)) {
Object unmarshalledObject = jsonb.fromJson(reader, typeClass);
Object unmarshalledObject = jsonb.fromJson(reader, typeClass); // Reader determins character encoding (Default)
assertThat("[testUnmarshallingByClassFromReader] - Failed to correctly unmarshal " + value.getClass()
.getName() + " object",
unmarshalledObject, is(value));
Expand All @@ -152,14 +153,14 @@ private void testUnmarshallingByClassFromReader(String expectedRepresentation, O
}

private void testUnmarshallingByType(String expectedRepresentation, Object value) {
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation, (Type) typeClass);
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation, (Type) typeClass); // Assumes character encoding is Default when creating Writer
assertThat("[testUnmarshallingByType] - Failed to correctly unmarshal " + value.getClass().getName() + " object",
unmarshalledObject, is(value));
}

private void testUnmarshallingByTypeFromStream(String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes(StandardCharsets.UTF_8))) {
Object unmarshalledObject = jsonb.fromJson(stream, (Type) typeClass);
Object unmarshalledObject = jsonb.fromJson(stream, (Type) typeClass); // Assumes character encoding is UTF-8 when creating Writer
assertThat("[testUnmarshallingByTypeFromStream] - Failed to correctly unmarshal " + value.getClass()
.getName() + " object",
unmarshalledObject, is(value));
Expand All @@ -169,9 +170,9 @@ private void testUnmarshallingByTypeFromStream(String expectedRepresentation, Ob
}

private void testUnmarshallingByTypeFromReader(String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes(StandardCharsets.UTF_8));
try (ByteArrayInputStream stream = new ByteArrayInputStream(expectedRepresentation.getBytes());
InputStreamReader reader = new InputStreamReader(stream)) {
Object unmarshalledObject = jsonb.fromJson(reader, (Type) typeClass);
Object unmarshalledObject = jsonb.fromJson(reader, (Type) typeClass); // Reader determins character encoding (Default)
assertThat("[testUnmarshallingByTypeFromReader] - Failed to correctly unmarshal " + value.getClass()
.getName() + " object",
unmarshalledObject, is(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import jakarta.json.bind.Jsonb;
Expand Down Expand Up @@ -94,7 +95,7 @@ public void testFromJsonStringType() {
@Test
public void testFromJsonReaderClass() throws IOException {
try (ByteArrayInputStream stream = new ByteArrayInputStream(TEST_JSON_BYTE);
InputStreamReader reader = new InputStreamReader(stream)) {
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { //TEST_JSON uses UTF-8
SimpleContainer unmarshalledObject = jsonb.fromJson(reader, SimpleContainer.class);
assertThat("Failed to unmarshal using Jsonb.fromJson method with Reader and Class arguments.",
unmarshalledObject.getInstance(), is(TEST_STRING));
Expand All @@ -112,7 +113,7 @@ public void testFromJsonReaderClass() throws IOException {
@Test
public void testFromJsonReaderType() throws IOException {
try (ByteArrayInputStream stream = new ByteArrayInputStream(TEST_JSON_BYTE);
InputStreamReader reader = new InputStreamReader(stream)) {
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { //TEST_JSON uses UTF-8
SimpleContainer unmarshalledObject = jsonb
.fromJson(reader, new SimpleContainer() { }.getClass().getGenericSuperclass());
assertThat("Failed to unmarshal using Jsonb.fromJson method with Reader and Type arguments.",
Expand Down Expand Up @@ -198,7 +199,7 @@ public void testToJsonObjectWriter() throws IOException {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {
jsonb.toJson(new SimpleContainer(), writer);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
String jsonString = new String(stream.toByteArray(), Charset.defaultCharset()); //Writer uses Default
assertThat("Failed to marshal using Jsonb.toJson method with Object and Writer arguments.",
jsonString, matchesPattern(MATCHING_PATTERN));
}
Expand All @@ -217,7 +218,7 @@ public void testToJsonObjectTypeWriter() throws IOException {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {
jsonb.toJson(new SimpleContainer(), new SimpleContainer() { }.getClass().getGenericSuperclass(), writer);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
String jsonString = new String(stream.toByteArray(), Charset.defaultCharset()); //Writer uses Default
assertThat("Failed to marshal using Jsonb.toJson method with Object, Type and Writer arguments.",
jsonString, matchesPattern(MATCHING_PATTERN));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -32,6 +32,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;

import java.nio.charset.StandardCharsets;

/**
* @test
* @sources BinaryDataCustomizationTest.java
Expand Down Expand Up @@ -60,7 +62,7 @@ public void testByteBinaryDataEncoding() {
BinaryDataContainer container = jsonb.fromJson("{ \"data\" : [ 84, 101, 115, 116, 32, 83, 116, 114, 105, 110, 103 ] }",
BinaryDataContainer.class);
assertThat("Failed to correctly unmarshal binary data using BYTE binary data encoding.",
new String(container.getData()), is("Test String"));
new String(container.getData(), StandardCharsets.UTF_8), is("Test String")); //Data was encoded using UTF-8
}

/*
Expand All @@ -81,7 +83,7 @@ public void testBase64BinaryDataEncoding() {

BinaryDataContainer unmarshalledObject = jsonb.fromJson("{ \"data\" : \"VGVzdCBTdHJpbmc\" }", BinaryDataContainer.class);
assertThat("Failed to correctly unmarshal binary data using BASE_64 binary data encoding.",
new String(unmarshalledObject.getData()), is("Test String"));
new String(unmarshalledObject.getData(), StandardCharsets.UTF_8), is("Test String")); //Data was encoded using UTF-8
}

/*
Expand All @@ -102,6 +104,6 @@ public void testBase64UrlBinaryDataEncoding() {

BinaryDataContainer unmarshalledObject = jsonb.fromJson("{ \"data\" : \"VGVzdCBTdHJpbmc=\" }", BinaryDataContainer.class);
assertThat("Failed to correctly unmarshal binary data using BASE_64_URL binary data encoding.",
new String(unmarshalledObject.getData()), is("Test String"));
new String(unmarshalledObject.getData(), StandardCharsets.UTF_8), is("Test String")); //Data was encoded using UTF-8
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -20,8 +20,10 @@

package ee.jakarta.tck.json.bind.customizedmapping.binarydata.model;

import java.nio.charset.StandardCharsets;

public class BinaryDataContainer {
private byte[] data = "Test String".getBytes();
private byte[] data = "Test String".getBytes(StandardCharsets.UTF_8); // Data must be UTF-8 for assertions

public byte[] getData() {
return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -20,8 +20,10 @@

package ee.jakarta.tck.json.bind.customizedmapping.ijson.model;

import java.nio.charset.StandardCharsets;

public class BinaryDataContainer {
private byte[] data = "Test String".getBytes();
private byte[] data = "Test String".getBytes(StandardCharsets.UTF_8); // Data must be UTF-8 for assertions

public byte[] getData() {
return data;
Expand Down