Skip to content

Commit

Permalink
Add test cases for collection/map including builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
leadpony committed Nov 23, 2019
1 parent 71db3c3 commit 8e762d7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>1.4.0</version>
<version>1.5.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JSON-P Test Suite</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.leadpony.jsonp.testsuite.helper.Ambiguous;

/**
* @author leadpony
Expand Down Expand Up @@ -97,10 +98,6 @@ enum CollectionTestCase {
this.collection = collection;
this.expected = expected;
}

private static Collection<?> collection(Object... objects) {
return Arrays.asList(objects);
}
}

@ParameterizedTest
Expand All @@ -117,6 +114,42 @@ public void createArrayBuilderShouldCreateBuilderFilledWithCollection(Collection
}
}

/**
* @author leadpony
*/
enum AmbiguousCollectionTestCase {
ARRAY_BUILDER(
collection(
"hello",
createFactory().createArrayBuilder().add(365)
),
array(b -> b.add("hello").add(array(b2 -> b2.add(365))))
);

final Collection<?> collection;
final JsonArray expected;

AmbiguousCollectionTestCase(Collection<?> collection, JsonArray expected) {
this.collection = collection;
this.expected = expected;
}
}

@Ambiguous
@ParameterizedTest
@EnumSource(AmbiguousCollectionTestCase.class)
public void createArrayBuilderShouldCreateBuilderFilledWithCollection(AmbiguousCollectionTestCase test) {
JsonBuilderFactory factory = createFactory();
try {
JsonArrayBuilder builder = factory.createArrayBuilder(test.collection);
JsonArray actual = builder.build();

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

/**
* @author leadpony
*/
Expand Down Expand Up @@ -183,12 +216,6 @@ enum MapTestCase {
this.map = map;
this.expected = expected;
}

private static Map<String, Object> map(Consumer<Map<String, Object>> consumer) {
Map<String, Object> map = new LinkedHashMap<>();
consumer.accept(map);
return map;
}
}

@ParameterizedTest
Expand All @@ -205,6 +232,42 @@ public void createObjectBuilderShouldCreateBuilderFilledWithMap(MapTestCase test
}
}

/**
* @author leadpony
*/
enum AmbiguousMapTestCase {
OBJECT_BUILDER(
map(m -> {
m.put("a", "hello");
m.put("b", createFactory().createObjectBuilder().add("x", 365));
}),
object(b -> b.add("a", "hello").add("b", object(b2 -> b2.add("x", 365))))
);

final Map<String, Object> map;
final JsonObject expected;

AmbiguousMapTestCase(Map<String, Object> map, JsonObject expected) {
this.map = map;
this.expected = expected;
}
}

@Ambiguous
@ParameterizedTest
@EnumSource(AmbiguousMapTestCase.class)
public void createObjectBuilderShouldCreateBuilderFilledWithMap(AmbiguousMapTestCase test) {
JsonBuilderFactory factory = createFactory();
try {
JsonObjectBuilder builder = factory.createObjectBuilder(test.map);
JsonObject actual = builder.build();

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

@Test
public void getConfigInUseShouldReturnEmptyMap() {
Map<String, Object> config = new HashMap<>();
Expand Down Expand Up @@ -234,6 +297,16 @@ private static JsonBuilderFactory createFactory(Map<String, ?> config) {
return Json.createBuilderFactory(config);
}

private static Collection<?> collection(Object... objects) {
return Arrays.asList(objects);
}

private static Map<String, Object> map(Consumer<Map<String, Object>> consumer) {
Map<String, Object> map = new LinkedHashMap<>();
consumer.accept(map);
return map;
}

private static JsonArray array(Consumer<JsonArrayBuilder> consumer) {
JsonArrayBuilder builder = Json.createArrayBuilder();
consumer.accept(builder);
Expand Down

0 comments on commit 8e762d7

Please sign in to comment.