Skip to content

Commit

Permalink
Fix collection tests for latest guava-testlib update (#2786)
Browse files Browse the repository at this point in the history
With the fix in the latest guava-testlib version null support is now properly
checked. But it expects that if a collection does not permit null, then
creating it with null should fail as well, see guava-testlib's:
- MapCreationTester.testCreateWithNullValueUnsupported()
- CollectionCreationTester.testCreateWithNull_unsupported()

However, the previous implementation for the JsonArray and JsonObject test
was using methods which implicitly converted null to JsonNull.
  • Loading branch information
Marcono1234 authored Jan 2, 2025
1 parent 287fd33 commit 3bb7685
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
10 changes: 6 additions & 4 deletions gson/src/test/java/com/google/gson/JsonArrayAsListSuiteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public Iterable<JsonElement> order(List<JsonElement> insertionOrder) {
@Override
public List<JsonElement> create(Object... elements) {
JsonArray array = new JsonArray();
// This is not completely accurate: Because there is no way to directly construct JsonArray or
// its List view with existing elements, this has to add the elements individually with
// `List#add`
var list = array.asList();
for (Object element : elements) {
array.add((JsonElement) element);
list.add((JsonElement) element);
}
return array.asList();
return list;
}
}

Expand All @@ -54,8 +58,6 @@ public static Test suite() {
return ListTestSuiteBuilder.using(new ListGenerator())
.withFeatures(
CollectionSize.ANY,
// Note: There is current a Guava bug which causes 'null additions' to not be tested if
// 'null queries' is enabled, see https://github.com/google/guava/issues/7401
CollectionFeature.ALLOWS_NULL_QUERIES,
CollectionFeature.RESTRICTS_ELEMENTS, // List only allows JsonElement
CollectionFeature.SUPPORTS_ADD,
Expand Down
10 changes: 6 additions & 4 deletions gson/src/test/java/com/google/gson/JsonObjectAsMapSuiteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public SampleElements<Entry<String, JsonElement>> samples() {
@Override
public Map<String, JsonElement> create(Object... elements) {
JsonObject object = new JsonObject();
// This is not completely accurate: Because there is no way to directly construct JsonObject
// or its Map view with existing entries, this has to add the entries individually with
// `Map#put`
var map = object.asMap();
for (Object element : elements) {
var entry = (Entry<?, ?>) element;
object.add((String) entry.getKey(), (JsonElement) entry.getValue());
map.put((String) entry.getKey(), (JsonElement) entry.getValue());
}
return object.asMap();
return map;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -70,8 +74,6 @@ public static Test suite() {
return MapTestSuiteBuilder.using(new MapGenerator())
.withFeatures(
CollectionSize.ANY,
// Note: There is current a Guava bug which causes 'null additions' to not be tested if
// 'null queries' is enabled, see https://github.com/google/guava/issues/7401
MapFeature.ALLOWS_ANY_NULL_QUERIES,
MapFeature.RESTRICTS_KEYS, // Map only allows String keys
MapFeature.RESTRICTS_VALUES, // Map only allows JsonElement values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public MapGenerator(boolean allowNullValues) {

@Override
protected Map<String, String> create(Entry<String, String>[] entries) {
// This is not completely accurate: Because LinkedTreeMap has no constructor which accepts
// existing entries, this has to add the entries individually with `Map#put`
var map = new LinkedTreeMap<String, String>(allowNullValues);
for (var entry : entries) {
map.put(entry.getKey(), entry.getValue());
Expand All @@ -47,8 +49,6 @@ private static Feature<?>[] createFeatures(Feature<?>... additionalFeatures) {
new ArrayList<Feature<?>>(
List.of(
CollectionSize.ANY,
// Note: There is current a Guava bug which causes 'null additions' to not be tested
// if 'null queries' is enabled, see https://github.com/google/guava/issues/7401
MapFeature.ALLOWS_ANY_NULL_QUERIES,
MapFeature.RESTRICTS_KEYS, // Map only allows comparable keys
MapFeature.SUPPORTS_PUT,
Expand All @@ -74,7 +74,8 @@ public static Test suite() {
.named("nullValues=false")
.createTestSuite();

TestSuite testSuite = new TestSuite("LinkedTreeMap");
// Use qualified class name to make it easier to find this test class in the IDE
TestSuite testSuite = new TestSuite(LinkedTreeMapSuiteTest.class.getName());
testSuite.addTest(nullValuesSuite);
testSuite.addTest(nonNullValuesSuite);

Expand Down

0 comments on commit 3bb7685

Please sign in to comment.