Skip to content

Commit

Permalink
TCK test UntypedMappingTest.testArrayMapping fixed (#275)
Browse files Browse the repository at this point in the history
TCK test UntypedMappingTest.testArrayMapping fixed

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed May 31, 2021
1 parent b249b4a commit e9ae296
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
9 changes: 6 additions & 3 deletions api/src/main/java/jakarta/json/bind/Jsonb.java
Expand Up @@ -260,7 +260,8 @@ public interface Jsonb extends AutoCloseable {
* The root object of the object content tree to be serialized. Must not be null.
*
* @param runtimeType
* Runtime type of the content tree's root object.
* Runtime type of the content tree's root object. Provided type needs to be
* related to the type of the instance.
*
* @return String instance with serialized JSON data.
*
Expand Down Expand Up @@ -298,7 +299,8 @@ public interface Jsonb extends AutoCloseable {
* The object content tree to be serialized.
*
* @param runtimeType
* Runtime type of the content tree's root object.
* Runtime type of the content tree's root object. Provided type needs to be
* related to the type of the instance.
*
* @param writer
* The JSON will be sent as a character stream to the given
Expand Down Expand Up @@ -339,7 +341,8 @@ public interface Jsonb extends AutoCloseable {
* The object content tree to be serialized.
*
* @param runtimeType
* Runtime type of the content tree's root object.
* Runtime type of the content tree's root object. Provided type needs to be
* related to the type of the instance.
*
* @param stream
* The JSON will be sent as a byte stream to the given
Expand Down
15 changes: 9 additions & 6 deletions tck/src/main/java/jakarta/json/bind/tck/SimpleMappingTester.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021 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.OutputStreamWriter;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
Expand All @@ -33,9 +34,11 @@ public class SimpleMappingTester<T> {
private final Jsonb jsonb = JsonbBuilder.create();

private final Class<T> typeClass;
private final Class<? super T> serializationType;

public SimpleMappingTester(Class<T> typeClass) {
this.typeClass = typeClass;
public SimpleMappingTester(Class<T> typeClass, Class<? super T> serializationType) {
this.typeClass = Objects.requireNonNull(typeClass);
this.serializationType = Objects.requireNonNull(serializationType);
}

public void test(T value, String expectedRepresentationPattern,
Expand Down Expand Up @@ -104,7 +107,7 @@ private void testMarshallingToWriter(T value,
}

private void testMarshallingByType(T value, String expectedRepresentation) {
String jsonString = jsonb.toJson(value, TypeContainer.class);
String jsonString = jsonb.toJson(value, serializationType);
if (jsonString.matches(expectedRepresentation)) {
return; // passed
} else {
Expand All @@ -116,7 +119,7 @@ private void testMarshallingByType(T value, String expectedRepresentation) {
private void testMarshallingByTypeToStream(T value,
String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(value, TypeContainer.class, stream);
jsonb.toJson(value, serializationType, stream);
String jsonString = new String(stream.toByteArray(),
StandardCharsets.UTF_8);
if (jsonString.matches(expectedRepresentation)) {
Expand All @@ -136,7 +139,7 @@ private void testMarshallingByTypeToWriter(T value,
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {

jsonb.toJson(value, TypeContainer.class, writer);
jsonb.toJson(value, serializationType, writer);
String jsonString = new String(stream.toByteArray(),
StandardCharsets.UTF_8);
if (jsonString.matches(expectedRepresentation)) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021 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 @@ -23,6 +23,7 @@
import java.lang.invoke.MethodHandles;
import java.math.BigDecimal;

import jakarta.json.bind.tck.TypeContainer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand Down Expand Up @@ -250,7 +251,7 @@ public void testBooleanMapping() {
*/
@Test
public void testNumberMapping() {
new SimpleMappingTester<>(NumberContainer.class).test(
new SimpleMappingTester<>(NumberContainer.class, TypeContainer.class).test(
new NumberContainer(), "\\{\\s*\"instance\"\\s*:\\s*0[\\.0]?+\\s*}",
"{ \"instance\" : 0 }", new NumberContainer() {
{
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021 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 @@ -31,6 +31,7 @@
import java.util.OptionalInt;
import java.util.OptionalLong;

import jakarta.json.bind.tck.TypeContainer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void testOptionalObjectMapping() {
simpleContainer.setStringInstance("String Value");
container.setInstance(Optional.of(simpleContainer));

new SimpleMappingTester<>(OptionalTypeContainer.class).test(
new SimpleMappingTester<>(OptionalTypeContainer.class, TypeContainer.class).test(
container,
"\\{\\s*\"instance\"\\s*:\\s*\\{\\s*\"stringInstance\"\\s*:\\s*\"String Value\"\\s*}\\s*}",
"{ \"instance\" : { \"stringInstance\" : \"String Value\" } }",
Expand All @@ -182,7 +183,7 @@ public void testOptionalObjectMapping() {
public void testEmptyOptionalMapping() {
OptionalContainer optionalContainer = new OptionalContainer();
optionalContainer.setInstance(Optional.empty());
new SimpleMappingTester<>(OptionalContainer.class).test(
new SimpleMappingTester<>(OptionalContainer.class, TypeContainer.class).test(
optionalContainer, "\\{\\s*}", "{ \"instance\" : null }",
optionalContainer);
}
Expand All @@ -202,7 +203,7 @@ public void testEmptyOptionalMapping() {
public void testEmptyOptionalArrayMapping() {
OptionalArrayContainer optionalContainer = new OptionalArrayContainer();
optionalContainer.setInstance(new Optional[] { Optional.empty() });
new SimpleMappingTester<>(OptionalArrayContainer.class).test(
new SimpleMappingTester<>(OptionalArrayContainer.class, TypeContainer.class).test(
optionalContainer, "\\{\\s*\"instance\"\\s*:\\s*\\[\\s*null\\s*]\\s*}",
"{ \"instance\" : [ null ] }", optionalContainer);
}
Expand Down Expand Up @@ -236,7 +237,7 @@ public void testOptionalIntMapping() {
public void testEmptyOptionalIntMapping() {
OptionalIntContainer optionalContainer = new OptionalIntContainer();
optionalContainer.setInstance(OptionalInt.empty());
new SimpleMappingTester<>(OptionalIntContainer.class).test(
new SimpleMappingTester<>(OptionalIntContainer.class, TypeContainer.class).test(
optionalContainer, "\\{\\s*}", "{ \"instance\" : null }",
optionalContainer);
}
Expand Down Expand Up @@ -270,7 +271,7 @@ public void testOptionalLongMapping() {
public void testEmptyOptionalLongMapping() {
OptionalLongContainer optionalContainer = new OptionalLongContainer();
optionalContainer.setInstance(OptionalLong.empty());
new SimpleMappingTester<>(OptionalLongContainer.class).test(
new SimpleMappingTester<>(OptionalLongContainer.class, TypeContainer.class).test(
optionalContainer, "\\{\\s*}", "{ \"instance\" : null }",
optionalContainer);
}
Expand Down Expand Up @@ -304,7 +305,7 @@ public void testOptionalDoubleMapping() {
public void testEmptyOptionalDoubleMapping() {
OptionalDoubleContainer optionalContainer = new OptionalDoubleContainer();
optionalContainer.setInstance(OptionalDouble.empty());
new SimpleMappingTester<>(OptionalDoubleContainer.class).test(
new SimpleMappingTester<>(OptionalDoubleContainer.class, TypeContainer.class).test(
optionalContainer, "\\{\\s*}", "{ \"instance\" : null }",
optionalContainer);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021 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 @@ -126,7 +126,7 @@ public Object getNullProperty() {
*/
@Test
public void testArrayMapping() {
new SimpleMappingTester<>(List.class).test(
new SimpleMappingTester<>(List.class, List.class).test(
Arrays.asList("Test String"), "\\[\\s*\"Test String\"s*\\]",
"[ \"Test String\" ]", Arrays.asList("Test String"));
}
Expand Down

0 comments on commit e9ae296

Please sign in to comment.