Skip to content

Commit

Permalink
Add more tests for JsonParser.hasNext()
Browse files Browse the repository at this point in the history
  • Loading branch information
leadpony committed Jul 20, 2019
1 parent 3989ece commit c54f857
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Tests for `JsonBuilderFactory`.
- More tests for `JsonGenerator`.
- More tests for `JsonPatch` and `JsonMergePatch`.

## 0.10.0 - 2019-07-19
### Added
- Tests for `JsonGenerator.write()` which takes a double parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.leadpony.jsonp.testsuite.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -69,10 +70,14 @@ enum CollectionTestCase {
@ParameterizedTest
@EnumSource(CollectionTestCase.class)
public void createArrayBuilderShouldCreateBuilderFilledWithCollection(CollectionTestCase test) {
JsonArrayBuilder builder = factory.createArrayBuilder(test.collection);
JsonArray actual = builder.build();
try {
JsonArrayBuilder builder = factory.createArrayBuilder(test.collection);
JsonArray actual = builder.build();

assertThat(actual).isEqualTo(test.expected);
assertThat(actual).isEqualTo(test.expected);
} catch (Exception e) {
fail(e);
}
}

/**
Expand Down
101 changes: 65 additions & 36 deletions src/main/java/org/leadpony/jsonp/testsuite/tests/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.json.Json;
import javax.json.JsonValue;
import javax.json.stream.JsonParserFactory;
import javax.json.stream.JsonParsingException;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -70,18 +71,16 @@ public static void setUpOnce() {
* @author leadpony
*/
enum TerminationTestCase {
LITERAL("365", 1, false),
ARRAY("[1,2,3]", 5, false),
OBJECT("{\"a\":1}", 4, false);
LITERAL("365", 1),
ARRAY("[1,2,3]", 5),
OBJECT("{\"a\":1}", 4);

final String json;
final int iterations;
final boolean result;

TerminationTestCase(String json, int iterations, boolean result) {
TerminationTestCase(String json, int iterations) {
this.json = json;
this.iterations = iterations;
this.result = result;
}
}

Expand All @@ -95,57 +94,87 @@ public void hasNextShouldReturnResult(TerminationTestCase test) {
assertThat(parser.hasNext()).isTrue();
parser.next();
}
assertThat(parser.hasNext()).isEqualTo(test.result);
assertThat(parser.hasNext()).isEqualTo(false);
parser.close();
}

@ParameterizedTest
@ValueSource(strings = {"", " "})
@Ambiguous
public void hasNextShouldReturnFalseIfInputIsBlank(String json) {
JsonParser parser = createJsonParser(json);
assertThat(parser.hasNext()).isEqualTo(false);
parser.close();
}

/**
* Test cases for {@code JsonParser#hasNext()} when unexpected end of JSON has
* occurred.
*
* @author leadpony
*/
enum UnexpectedTerminationTestCase {
ARRAY_MISSING_END("[1,2,3", 4, false),
ARRAY_MISSING_ITEM("[1,2,", 3, true),
OBJECT_MISSING_END("{\"a\":1", 3, false),
OBJECT_MISSING_VALUE("{\"a\":", 2, true),
OBJECT_MISSING_COLON("{\"a\"", 2, false),
OBJECT_MISSING_KEY("{", 1, false);
enum HasNextExceptionTestCase {
VALUE_AFTER_ARRAY("[1,2] 3", 4),
VALUE_AFTER_OBJECT("{\"a\":1} 2}", 4),
VALUE_AFTER_VALUE("1 2", 1),
END_AFTER_ARRAY_START("[", 1),
END_AFTER_FIRST_ITEM("[1", 2),
END_AFTER_SECOND_ITEM("[1,2", 3),
EOI_AFTER_OBJECT_START("{", 1),
EOI_AFTER_FIRST_PROPERTY_KEY("{\"a\"", 2),
EOI_AFTER_FIRST_PROPERTY_VALUE("{\"a\":1", 3),
EOI_AFTER_SECOND_PROPERTY_KEY("{\"a\":1,\"b\"", 4),
EOI_AFTER_SECOND_PROPERTY_VALUE("{\"a\":1,\"b\":2", 5),

END_AFTER_ITEM_SEPARATOR("[1,", 2, true),
EOI_AFTER_COLON("{\"a\":", 2, true),
EOI_AFTER_PROPERTY_SEPARATOR("{\"a\":1,", 3, true),

EMPTY("", 0, false),
BLANK(" ", 0, false);

final String json;
final int iterations;
final boolean result;
final boolean throwing;
final boolean expected;

UnexpectedTerminationTestCase(String json, int iterations, boolean result) {
// separator
HasNextExceptionTestCase(String json, int iterations) {
this.json = json;
this.iterations = iterations;
this.result = result;
this.throwing = true;
this.expected = true;
}

HasNextExceptionTestCase(String json, int iterations, boolean expected) {
this.json = json;
this.iterations = iterations;
this.throwing = false;
this.expected = expected;
}
}

@ParameterizedTest
@EnumSource(UnexpectedTerminationTestCase.class)
@Ambiguous
public void hasNextShouldReturnFalseWhenTerminated(UnexpectedTerminationTestCase test) {
@EnumSource(HasNextExceptionTestCase.class)
public void hasNextShouldThrowJsonParsingException(HasNextExceptionTestCase test) {
JsonParser parser = createJsonParser(test.json);

int remaining = test.iterations;
while (remaining-- > 0) {
assertThat(parser.hasNext()).isTrue();
int iterations = test.iterations;
while (iterations-- > 0) {
parser.next();
}
assertThat(parser.hasNext()).isEqualTo(test.result);
parser.close();
}

@ParameterizedTest
@ValueSource(strings = {"", " "})
@Ambiguous
public void hasNextShouldReturnFalseIfInputIsBlank(String json) {
JsonParser parser = createJsonParser(json);
assertThat(parser.hasNext()).isEqualTo(false);
Throwable thrown = catchThrowable(() -> {
boolean actual = parser.hasNext();
assertThat(actual).isEqualTo(test.expected);
});
parser.close();

if (thrown != null) {
LOG.info(thrown.getMessage());
}

if (test.throwing) {
assertThat(thrown).isNotNull().isInstanceOf(JsonParsingException.class);
} else {
assertThat(thrown).isNull();
}
}

/**
Expand Down

0 comments on commit c54f857

Please sign in to comment.