Skip to content

Commit

Permalink
Modifications to split TCK out from jakartaee-tck repo, use JUnit, an…
Browse files Browse the repository at this point in the history
…d use jakarta namespace
  • Loading branch information
aguibert committed Jan 28, 2020
1 parent 2561bea commit 62d17c9
Show file tree
Hide file tree
Showing 273 changed files with 2,489 additions and 2,847 deletions.
17 changes: 15 additions & 2 deletions tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,27 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jakarta.json.version>2.0.0-RC1</jakarta.json.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${jakarta.json.version}</version>
<version>2.0.0-RC1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
150 changes: 61 additions & 89 deletions tck/src/main/java/jakarta/json/bind/MappingTester.java

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions tck/src/main/java/jakarta/json/bind/RegexMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020 IBM 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.json.bind;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class RegexMatcher extends TypeSafeMatcher<String> {

private final String regex;

public RegexMatcher(final String regex) {
this.regex = regex;
}

@Override
public void describeTo(final Description description) {
description.appendText("matches regex=`" + regex + "`");
}

@Override
public boolean matchesSafely(final String string) {
return string.matches(regex);
}


public static RegexMatcher matches(final String regex) {
return new RegexMatcher(regex);
}
}
135 changes: 60 additions & 75 deletions tck/src/main/java/jakarta/json/bind/SimpleMappingTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

/*
* $Id$
*/
package jakarta.json.bind;

package com.sun.ts.tests.jsonb;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -28,14 +26,6 @@
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;

import com.sun.javatest.Status;
import com.sun.ts.lib.harness.EETest.Fault;

import static com.sun.ts.tests.jsonb.MappingTester.combine;

public class SimpleMappingTester<T> {
private final Jsonb jsonb = JsonbBuilder.create();

Expand All @@ -45,103 +35,100 @@ public SimpleMappingTester(Class<T> typeClass) {
this.typeClass = typeClass;
}

public Status test(T value, String expectedRepresentationPattern,
String expectedRepresentation, Object expectedOutput) throws Fault {
return combine(testMarshalling(value, expectedRepresentationPattern),
testMarshallingToStream(value, expectedRepresentationPattern),
testMarshallingToWriter(value, expectedRepresentationPattern),
testMarshallingByType(value, expectedRepresentationPattern),
testMarshallingByTypeToStream(value, expectedRepresentationPattern),
testMarshallingByTypeToWriter(value, expectedRepresentationPattern),
testUnmarshallingByClass(expectedRepresentation, expectedOutput),
public void test(T value, String expectedRepresentationPattern,
String expectedRepresentation, Object expectedOutput) {
testMarshalling(value, expectedRepresentationPattern);
testMarshallingToStream(value, expectedRepresentationPattern);
testMarshallingToWriter(value, expectedRepresentationPattern);
testMarshallingByType(value, expectedRepresentationPattern);
testMarshallingByTypeToStream(value, expectedRepresentationPattern);
testMarshallingByTypeToWriter(value, expectedRepresentationPattern);
testUnmarshallingByClass(expectedRepresentation, expectedOutput);
testUnmarshallingByClassFromStream(expectedRepresentation,
expectedOutput),
expectedOutput);
testUnmarshallingByClassFromReader(expectedRepresentation,
expectedOutput),
testUnmarshallingByType(expectedRepresentation, expectedOutput),
expectedOutput);
testUnmarshallingByType(expectedRepresentation, expectedOutput);
testUnmarshallingByTypeFromStream(expectedRepresentation,
expectedOutput),
expectedOutput);
testUnmarshallingByTypeFromReader(expectedRepresentation,
expectedOutput));
expectedOutput);
}

private Status testMarshalling(T value, String expectedRepresentation) {
private void testMarshalling(T value, String expectedRepresentation) {
String jsonString = jsonb.toJson(value);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed("[testMarshalling] - Failed to correctly marshal "
fail("[testMarshalling] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
}

private Status testMarshallingToStream(T value,
private void testMarshallingToStream(T value,
String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(value, stream);
String jsonString = new String(stream.toByteArray(),
StandardCharsets.UTF_8);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status
.failed("[testMarshallingToStream] - Failed to correctly marshal "
fail("[testMarshallingToStream] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testMarshallingToWriter(T value,
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);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status
.failed("[testMarshallingToWriter] - Failed to correctly marshal "
fail("[testMarshallingToWriter] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testMarshallingByType(T value, String expectedRepresentation) {
private void testMarshallingByType(T value, String expectedRepresentation) {
String jsonString = jsonb.toJson(value, TypeContainer.class);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status
.failed("[testMarshallingByType] - Failed to correctly marshal "
fail("[testMarshallingByType] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
}

private Status testMarshallingByTypeToStream(T value,
private void testMarshallingByTypeToStream(T value,
String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(value, TypeContainer.class, stream);
String jsonString = new String(stream.toByteArray(),
StandardCharsets.UTF_8);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testMarshallingByTypeToStream] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testMarshallingByTypeToWriter(T value,
private void testMarshallingByTypeToWriter(T value,
String expectedRepresentation) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream)) {
Expand All @@ -150,48 +137,47 @@ private Status testMarshallingByTypeToWriter(T value,
String jsonString = new String(stream.toByteArray(),
StandardCharsets.UTF_8);
if (jsonString.matches(expectedRepresentation)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testMarshallingByTypeToWriter] - Failed to correctly marshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testUnmarshallingByClass(String expectedRepresentation,
private void testUnmarshallingByClass(String expectedRepresentation,
Object value) {
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation,
typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status
.failed("[testUnmarshallingByClass] - Failed to correctly unmarshal "
fail("[testUnmarshallingByClass] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
}

private Status testUnmarshallingByClassFromStream(
private void testUnmarshallingByClassFromStream(
String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(
expectedRepresentation.getBytes(StandardCharsets.UTF_8))) {
Object unmarshalledObject = jsonb.fromJson(stream, typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testUnmarshallingByClassFromStream] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testUnmarshallingByClassFromReader(
private void testUnmarshallingByClassFromReader(
String expectedRepresentation, Object value) {
try (
ByteArrayInputStream stream = new ByteArrayInputStream(
Expand All @@ -200,48 +186,47 @@ private Status testUnmarshallingByClassFromReader(

Object unmarshalledObject = jsonb.fromJson(reader, typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testUnmarshallingByClassFromReader] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testUnmarshallingByType(String expectedRepresentation,
private void testUnmarshallingByType(String expectedRepresentation,
Object value) {
Object unmarshalledObject = jsonb.fromJson(expectedRepresentation,
(Type) typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status
.failed("[testUnmarshallingByType] - Failed to correctly unmarshal "
fail("[testUnmarshallingByType] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
}

private Status testUnmarshallingByTypeFromStream(
private void testUnmarshallingByTypeFromStream(
String expectedRepresentation, Object value) {
try (ByteArrayInputStream stream = new ByteArrayInputStream(
expectedRepresentation.getBytes(StandardCharsets.UTF_8))) {
Object unmarshalledObject = jsonb.fromJson(stream, (Type) typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testUnmarshallingByTypeFromStream] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}

private Status testUnmarshallingByTypeFromReader(
private void testUnmarshallingByTypeFromReader(
String expectedRepresentation, Object value) {
try (
ByteArrayInputStream stream = new ByteArrayInputStream(
Expand All @@ -250,14 +235,14 @@ private Status testUnmarshallingByTypeFromReader(

Object unmarshalledObject = jsonb.fromJson(reader, (Type) typeClass);
if (value.equals(unmarshalledObject)) {
return Status.passed("OK");
return; // passed
} else {
return Status.failed(
fail(
"[testUnmarshallingByTypeFromReader] - Failed to correctly unmarshal "
+ value.getClass().getName() + " object");
}
} catch (IOException e) {
return Status.error(e.getMessage());
fail(e.getMessage());
}
}
}
2 changes: 1 addition & 1 deletion tck/src/main/java/jakarta/json/bind/TypeContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* $Id$
*/

package com.sun.ts.tests.jsonb;
package jakarta.json.bind;

public interface TypeContainer<T> {
T getInstance();
Expand Down
Loading

0 comments on commit 62d17c9

Please sign in to comment.