Skip to content

Commit

Permalink
JUnit5 (#115)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Pinsky <anton.pinsky@ionos.com>
Co-authored-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
api-from-the-ion and lukasj authored Feb 26, 2024
1 parent dfbe33a commit a38b7d1
Show file tree
Hide file tree
Showing 45 changed files with 1,036 additions and 807 deletions.
4 changes: 2 additions & 2 deletions demos/customprovider-jdk9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
<artifactId>jakarta.json-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

package customprovider.test;

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

import jakarta.json.Json;
import jakarta.json.stream.JsonGenerator;

import org.eclipse.parsson.demos.customprovider.TestGenerator;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
*
Expand All @@ -23,9 +25,9 @@
public class TestProviderTest {

@Test
public void hello() {
void hello() {
try (JsonGenerator generator = Json.createGenerator(System.out)) {
Assert.assertTrue("TestGenerator is not picked up", generator instanceof TestGenerator);
assertInstanceOf(TestGenerator.class, generator, "TestGenerator is not picked up");
generator.writeStartArray().writeEnd();
}
System.out.println();
Expand Down
10 changes: 8 additions & 2 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@
<artifactId>jakarta.json-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
33 changes: 21 additions & 12 deletions impl/src/main/java/org/eclipse/parsson/JsonParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@

import org.eclipse.parsson.JsonTokenizer.JsonToken;

import static org.eclipse.parsson.JsonTokenizer.JsonToken.COLON;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.COMMA;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.CURLYCLOSE;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.CURLYOPEN;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.EOF;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.SQUARECLOSE;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.SQUAREOPEN;
import static org.eclipse.parsson.JsonTokenizer.JsonToken.STRING;

/**
* JSON parser implementation. NoneContext, ArrayContext, ObjectContext is used
* to go to next parser state.
Expand Down Expand Up @@ -310,7 +319,7 @@ private JsonArray getArray(JsonArrayBuilder builder) {
}
builder.add(getValue());
}
throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
throw parsingException(EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
}

private CharSequence getCharSequence() {
Expand All @@ -331,7 +340,7 @@ private JsonObject getObject(JsonObjectBuilder builder) {
next();
builder.add(key, getValue());
}
throw parsingException(JsonToken.EOF, "[STRING, CURLYCLOSE]");
throw parsingException(EOF, "[STRING, CURLYCLOSE]");
}

@Override
Expand All @@ -347,7 +356,7 @@ public JsonLocation getLastCharLocation() {
public boolean hasNext() {
if (stack.isEmpty() && (currentEvent != null && currentEvent.compareTo(Event.KEY_NAME) > 0)) {
JsonToken token = tokenizer.nextToken();
if (token != JsonToken.EOF) {
if (token != EOF) {
throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token),
getLastCharLocation());
}
Expand Down Expand Up @@ -434,7 +443,7 @@ protected Event nextEventIfValueOrObjectOrArrayStart(JsonToken token) {
stack.push(currentContext);
currentContext = new ObjectContext();
return Event.START_OBJECT;
} else if (token == JsonToken.SQUAREOPEN) {
} else if (token == SQUAREOPEN) {
stack.push(currentContext);
currentContext = new ArrayContext();
return Event.START_ARRAY;
Expand Down Expand Up @@ -510,7 +519,7 @@ private ObjectContext() {
public Event getNextEvent() {
// Handle 1. } 2. name:value 3. ,name:value
JsonToken token = tokenizer.nextToken();
if (token == JsonToken.EOF) {
if (token == EOF) {
switch (currentEvent) {
case START_OBJECT:
throw parsingException(token, "[STRING, CURLYCLOSE]");
Expand All @@ -521,7 +530,7 @@ public Event getNextEvent() {
}
} else if (currentEvent == Event.KEY_NAME) {
// Handle 1. :value
if (token != JsonToken.COLON) {
if (token != COLON) {
throw parsingException(token, "[COLON]");
}
token = tokenizer.nextToken();
Expand All @@ -532,19 +541,19 @@ public Event getNextEvent() {
throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]");
} else {
// Handle 1. } 2. name 3. ,name
if (token == JsonToken.CURLYCLOSE) {
if (token == CURLYCLOSE) {
currentContext = stack.pop();
return Event.END_OBJECT;
}
if (firstValue) {
firstValue = false;
} else {
if (token != JsonToken.COMMA) {
if (token != COMMA) {
throw parsingException(token, "[COMMA]");
}
token = tokenizer.nextToken();
}
if (token == JsonToken.STRING) {
if (token == STRING) {
return Event.KEY_NAME;
}
throw parsingException(token, "[STRING]");
Expand All @@ -563,22 +572,22 @@ private ArrayContext() {
@Override
public Event getNextEvent() {
JsonToken token = tokenizer.nextToken();
if (token == JsonToken.EOF) {
if (token == EOF) {
switch (currentEvent) {
case START_ARRAY:
throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]");
default:
throw parsingException(token, "[COMMA, CURLYCLOSE]");
}
}
if (token == JsonToken.SQUARECLOSE) {
if (token == SQUARECLOSE) {
currentContext = stack.pop();
return Event.END_ARRAY;
}
if (firstValue) {
firstValue = false;
} else {
if (token != JsonToken.COMMA) {
if (token != COMMA) {
throw parsingException(token, "[COMMA]");
}
token = tokenizer.nextToken();
Expand Down
8 changes: 4 additions & 4 deletions impl/src/test/java/org/eclipse/parsson/tests/Issue25Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.eclipse.parsson.tests;

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

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import jakarta.json.JsonObject;
import jakarta.json.spi.JsonProvider;
Expand All @@ -32,7 +32,7 @@
public class Issue25Test {

@Test
public void doubleClose() throws IOException {
void doubleClose() throws IOException {
byte[] content = "[\"test\"]".getBytes();
JsonProvider json = JsonProvider.provider();
for (int i = 0; i < 3; i++) {
Expand All @@ -54,7 +54,7 @@ public void doubleClose() throws IOException {
}

@Test
public void doubleCloseWithMoreContent() throws IOException {
void doubleCloseWithMoreContent() throws IOException {
byte[] content = loadResource("/comments.json");
JsonProvider json = JsonProvider.provider();
for (int i = 0; i < 3; i++) {
Expand Down
47 changes: 31 additions & 16 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@

package org.eclipse.parsson.tests;

import junit.framework.TestCase;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import jakarta.json.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonNumber;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.json.JsonWriter;

import org.junit.jupiter.api.Test;

/**
* @author Jitendra Kotamraju
*/
public class JsonArrayTest extends TestCase {
public JsonArrayTest(String testName) {
super(testName);
}

public void testArrayEquals() throws Exception {
public class JsonArrayTest {
@Test
void testArrayEquals() throws Exception {
JsonArray expected = Json.createArrayBuilder()
.add(JsonValue.TRUE)
.add(JsonValue.FALSE)
Expand All @@ -59,7 +66,8 @@ public void testArrayEquals() throws Exception {
assertEquals(expected, actual);
}

public void testArrayEqualsUsingCollection() {
@Test
void testArrayEqualsUsingCollection() {
List<Object> list = new ArrayList<>();
list.add(JsonValue.TRUE);
list.add(JsonValue.FALSE);
Expand Down Expand Up @@ -87,21 +95,24 @@ public void testArrayEqualsUsingCollection() {
assertEquals(expected, actual);
}

public void testStringValue() throws Exception {
@Test
void testStringValue() throws Exception {
JsonArray array = Json.createArrayBuilder()
.add("John")
.build();
assertEquals("John", array.getString(0));
}

public void testIntValue() throws Exception {
@Test
void testIntValue() throws Exception {
JsonArray array = Json.createArrayBuilder()
.add(20)
.build();
assertEquals(20, array.getInt(0));
}

public void testAdd() {
@Test
void testAdd() {
JsonArray array = Json.createArrayBuilder().build();
try {
array.add(JsonValue.FALSE);
Expand All @@ -111,7 +122,8 @@ public void testAdd() {
}
}

public void testRemove() {
@Test
void testRemove() {
JsonArray array = Json.createArrayBuilder().build();
try {
array.remove(0);
Expand All @@ -121,7 +133,8 @@ public void testRemove() {
}
}

public void testNumberView() throws Exception {
@Test
void testNumberView() throws Exception {
JsonArray array = Json.createArrayBuilder().add(20).add(10).build();

List<JsonNumber> numberList = array.getValuesAs(JsonNumber.class);
Expand All @@ -133,7 +146,8 @@ public void testNumberView() throws Exception {
assertEquals(10, array.getInt(1));
}

public void testArrayBuilderNpe() {
@Test
void testArrayBuilderNpe() {
try {
JsonArray array = Json.createArrayBuilder().add((JsonValue)null).build();
fail("JsonArrayBuilder#add(null) should throw NullPointerException");
Expand All @@ -142,7 +156,8 @@ public void testArrayBuilderNpe() {
}
}

public void testHashCode() {
@Test
void testHashCode() {
JsonArray array1 = Json.createArrayBuilder().add(1).add(2).add(3).build();
assertTrue(array1.hashCode() == array1.hashCode()); //1st call compute hashCode, 2nd call returns cached value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,41 @@

package org.eclipse.parsson.tests;

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

import java.io.StringReader;
import java.math.BigDecimal;

import jakarta.json.Json;
import jakarta.json.JsonNumber;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import junit.framework.TestCase;

import org.eclipse.parsson.api.JsonConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test maxBigDecimalLength limit set from System property.
*/
public class JsonBigDecimalLengthLimitTest extends TestCase {

public JsonBigDecimalLengthLimitTest(String testName) {
super(testName);
}
public class JsonBigDecimalLengthLimitTest {

@Override
protected void setUp() {
@BeforeEach
void setUp() {
System.setProperty(JsonConfig.MAX_BIGDECIMAL_LEN, "500");
}

@Override
protected void tearDown() {
@AfterEach
void tearDown() {
System.clearProperty(JsonConfig.MAX_BIGDECIMAL_LEN);
}

// Test BigDecimal max source characters array length using length equal to system property limit of 500.
// Parsing shall pass and return value equal to source String.
public void testLargeBigDecimalBellowLimit() {
@Test
void testLargeBigDecimalBellowLimit() {
JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_500));
JsonNumber check = Json.createValue(new BigDecimal(JsonNumberTest.Π_500));
JsonValue value = reader.readValue();
Expand All @@ -57,7 +60,8 @@ public void testLargeBigDecimalBellowLimit() {

// Test BigDecimal max source characters array length using length above system property limit of 500.
// Parsing shall pass and return value equal to source String.
public void testLargeBigDecimalAboveLimit() {
@Test
void testLargeBigDecimalAboveLimit() {
JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_501));
try {
reader.readValue();
Expand Down
Loading

0 comments on commit a38b7d1

Please sign in to comment.