From 972666448f7c242e85ae8df0ee8b0a1ef307b373 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Wed, 1 Jan 2025 22:53:15 +0100 Subject: [PATCH] Fix collection tests for latest guava-testlib update 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. --- .../java/com/google/gson/JsonArrayAsListSuiteTest.java | 10 ++++++---- .../java/com/google/gson/JsonObjectAsMapSuiteTest.java | 10 ++++++---- .../google/gson/internal/LinkedTreeMapSuiteTest.java | 7 ++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/gson/src/test/java/com/google/gson/JsonArrayAsListSuiteTest.java b/gson/src/test/java/com/google/gson/JsonArrayAsListSuiteTest.java index baca3846b9..81c8ead7b9 100644 --- a/gson/src/test/java/com/google/gson/JsonArrayAsListSuiteTest.java +++ b/gson/src/test/java/com/google/gson/JsonArrayAsListSuiteTest.java @@ -42,10 +42,14 @@ public Iterable order(List insertionOrder) { @Override public List 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; } } @@ -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, diff --git a/gson/src/test/java/com/google/gson/JsonObjectAsMapSuiteTest.java b/gson/src/test/java/com/google/gson/JsonObjectAsMapSuiteTest.java index c2fc8c73f8..bb1625d487 100644 --- a/gson/src/test/java/com/google/gson/JsonObjectAsMapSuiteTest.java +++ b/gson/src/test/java/com/google/gson/JsonObjectAsMapSuiteTest.java @@ -34,11 +34,15 @@ public SampleElements> samples() { @Override public Map 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") @@ -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 diff --git a/gson/src/test/java/com/google/gson/internal/LinkedTreeMapSuiteTest.java b/gson/src/test/java/com/google/gson/internal/LinkedTreeMapSuiteTest.java index d81812fb1d..4335e7ca11 100644 --- a/gson/src/test/java/com/google/gson/internal/LinkedTreeMapSuiteTest.java +++ b/gson/src/test/java/com/google/gson/internal/LinkedTreeMapSuiteTest.java @@ -31,6 +31,8 @@ public MapGenerator(boolean allowNullValues) { @Override protected Map create(Entry[] 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(allowNullValues); for (var entry : entries) { map.put(entry.getKey(), entry.getValue()); @@ -47,8 +49,6 @@ private static Feature[] createFeatures(Feature... additionalFeatures) { new ArrayList>( 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, @@ -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);