Skip to content

Commit

Permalink
fix cp rights, access of JsonParserImpl, static imports removal
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Feb 26, 2024
1 parent 5d2083a commit d5b4a4c
Show file tree
Hide file tree
Showing 48 changed files with 663 additions and 757 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

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.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -27,7 +26,7 @@ public class TestProviderTest {
@Test
void hello() {
try (JsonGenerator generator = Json.createGenerator(System.out)) {
assertInstanceOf(TestGenerator.class, generator, "TestGenerator is not picked up");
Assertions.assertInstanceOf(TestGenerator.class, generator, "TestGenerator is not picked up");
generator.writeStartArray().writeEnd();
}
System.out.println();
Expand Down
2 changes: 1 addition & 1 deletion impl/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2024 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
2 changes: 1 addition & 1 deletion impl/src/main/java/org/eclipse/parsson/JsonMessages.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 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
35 changes: 13 additions & 22 deletions impl/src/main/java/org/eclipse/parsson/JsonParserImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 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 @@ -40,23 +40,14 @@

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.
*
* @author Jitendra Kotamraju
* @author Kin-man Chung
*/
class JsonParserImpl implements JsonParser {
public class JsonParserImpl implements JsonParser {

private Context currentContext = new NoneContext();
private Event currentEvent;
Expand Down Expand Up @@ -225,7 +216,7 @@ private JsonArray getArray(JsonArrayBuilder builder) {
}
builder.add(getValue());
}
throw parsingException(EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
}

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

@Override
Expand All @@ -262,7 +253,7 @@ public JsonLocation getLastCharLocation() {
public boolean hasNext() {
if (stack.isEmpty() && (currentEvent != null && currentEvent.compareTo(Event.KEY_NAME) > 0)) {
JsonToken token = tokenizer.nextToken();
if (token != EOF) {
if (token != JsonToken.EOF) {
throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token),
getLastCharLocation());
}
Expand Down Expand Up @@ -349,7 +340,7 @@ protected Event nextEventIfValueOrObjectOrArrayStart(JsonToken token) {
stack.push(currentContext);
currentContext = new ObjectContext();
return Event.START_OBJECT;
} else if (token == SQUAREOPEN) {
} else if (token == JsonToken.SQUAREOPEN) {
stack.push(currentContext);
currentContext = new ArrayContext();
return Event.START_ARRAY;
Expand Down Expand Up @@ -412,7 +403,7 @@ JsonToken firstValueOrJsonToken(JsonToken token) {
if (firstValue) {
firstValue = false;
} else {
if (token != COMMA) {
if (token != JsonToken.COMMA) {
throw parsingException(token, "[COMMA]");
}
token = tokenizer.nextToken();
Expand All @@ -437,7 +428,7 @@ private ObjectContext() {
public Event getNextEvent() {
// Handle 1. } 2. name:value 3. ,name:value
JsonToken token = tokenizer.nextToken();
if (token == EOF) {
if (token == JsonToken.EOF) {
switch (currentEvent) {
case START_OBJECT:
throw parsingException(token, "[STRING, CURLYCLOSE]");
Expand All @@ -448,7 +439,7 @@ public Event getNextEvent() {
}
} else if (currentEvent == Event.KEY_NAME) {
// Handle 1. :value
if (token != COLON) {
if (token != JsonToken.COLON) {
throw parsingException(token, "[COLON]");
}
token = tokenizer.nextToken();
Expand All @@ -459,13 +450,13 @@ public Event getNextEvent() {
throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]");
} else {
// Handle 1. } 2. name 3. ,name
if (token == CURLYCLOSE) {
if (token == JsonToken.CURLYCLOSE) {
currentContext = stack.pop();
return Event.END_OBJECT;
}

token = firstValueOrJsonToken(token);
if (token == STRING) {
if (token == JsonToken.STRING) {
return Event.KEY_NAME;
}
throw parsingException(token, "[STRING]");
Expand All @@ -483,11 +474,11 @@ private ArrayContext() {
@Override
public Event getNextEvent() {
JsonToken token = tokenizer.nextToken();
if (token == EOF) {
if (token == JsonToken.EOF) {
throw parsingException(token, (Objects.requireNonNull(currentEvent) == Event.START_ARRAY) ?
"[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]" : "[COMMA, CURLYCLOSE]");
}
if (token == SQUARECLOSE) {
if (token == JsonToken.SQUARECLOSE) {
currentContext = stack.pop();
return Event.END_ARRAY;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2013, 2024 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 @@ -16,6 +16,7 @@




internal.error=Internal Error

parser.getString.err=JsonParser#getString() is valid only for KEY_NAME, VALUE_STRING, VALUE_NUMBER parser states. \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024 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
9 changes: 4 additions & 5 deletions impl/src/test/java/org/eclipse/parsson/tests/Issue25Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024 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 @@ -16,13 +16,12 @@

package org.eclipse.parsson.tests;

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.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import jakarta.json.JsonObject;
Expand All @@ -39,7 +38,7 @@ void doubleClose() throws IOException {
try (InputStream in = new ByteArrayInputStream(content)) {
try (JsonParser parser = json.createParser(in)) {
JsonParser.Event firstEvent = parser.next();
assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
Assertions.assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
if (event == JsonParser.Event.START_OBJECT) {
Expand All @@ -61,7 +60,7 @@ void doubleCloseWithMoreContent() throws IOException {
try (InputStream in = new ByteArrayInputStream(content)) {
try (JsonParser parser = json.createParser(in)) {
JsonParser.Event firstEvent = parser.next();
assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
Assertions.assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
if (event == JsonParser.Event.START_OBJECT) {
Expand Down
33 changes: 15 additions & 18 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 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 @@ -16,10 +16,6 @@

package org.eclipse.parsson.tests;

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 java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
Expand All @@ -32,6 +28,7 @@
import jakarta.json.JsonValue;
import jakarta.json.JsonWriter;

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

/**
Expand Down Expand Up @@ -63,7 +60,7 @@ void testArrayEquals() {
JsonArray actual = reader.readArray();
reader.close();

assertEquals(expected, actual);
Assertions.assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -92,31 +89,31 @@ void testArrayEqualsUsingCollection() {
JsonArray actual = reader.readArray();
reader.close();

assertEquals(expected, actual);
Assertions.assertEquals(expected, actual);
}

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

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

@Test
void testAdd() {
JsonArray array = Json.createArrayBuilder().build();
try {
array.add(JsonValue.FALSE);
fail("JsonArray#add() should throw UnsupportedOperationException");
Assertions.fail("JsonArray#add() should throw UnsupportedOperationException");
} catch(UnsupportedOperationException e) {
// Expected
}
Expand All @@ -127,7 +124,7 @@ void testRemove() {
JsonArray array = Json.createArrayBuilder().build();
try {
array.remove(0);
fail("JsonArray#remove() should throw UnsupportedOperationException");
Assertions.fail("JsonArray#remove() should throw UnsupportedOperationException");
} catch(UnsupportedOperationException e) {
// Expected
}
Expand All @@ -142,15 +139,15 @@ void testNumberView() {
num.intValue();
}

assertEquals(20, array.getInt(0));
assertEquals(10, array.getInt(1));
Assertions.assertEquals(20, array.getInt(0));
Assertions.assertEquals(10, array.getInt(1));
}

@Test
void testArrayBuilderNpe() {
try {
Json.createArrayBuilder().add((JsonValue)null).build();
fail("JsonArrayBuilder#add(null) should throw NullPointerException");
Assertions.fail("JsonArrayBuilder#add(null) should throw NullPointerException");
} catch(NullPointerException e) {
// Expected
}
Expand All @@ -159,16 +156,16 @@ void testArrayBuilderNpe() {
@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
Assertions.assertTrue(array1.hashCode() == array1.hashCode()); //1st call compute hashCode, 2nd call returns cached value

JsonArray array2 = Json.createArrayBuilder().add(1).add(2).add(3).build();
assertTrue(array1.hashCode() == array2.hashCode());
Assertions.assertTrue(array1.hashCode() == array2.hashCode());

JsonArray array3 = Json.createArrayBuilder().build(); //org.eclipse.parsson.JsonArrayBuilderImpl.JsonArrayImpl
JsonArray array4 = JsonValue.EMPTY_JSON_ARRAY; //jakarta.json.EmptyArray

assertTrue(array3.equals(array4));
assertTrue(array3.hashCode() == array4.hashCode()); //equal instances have same hashCode
Assertions.assertTrue(array3.equals(array4));
Assertions.assertTrue(array3.hashCode() == array4.hashCode()); //equal instances have same hashCode
}

}
Loading

0 comments on commit d5b4a4c

Please sign in to comment.