From ec0ca1bfcbbc913b40f17af8c8754ae10d387d53 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Tue, 3 Oct 2023 11:21:03 -0700 Subject: [PATCH] Prepare some `common.collect` tests and testlib classes to build under J2CL. RELNOTES=n/a PiperOrigin-RevId: 570444724 --- .../google/common/collect/testing/AbstractTester.java | 5 ++++- .../common/collect/testing/IteratorFeature.java | 11 +++++++---- .../test/com/google/common/collect/ListsImplTest.java | 2 ++ .../google/common/collect/testing/AbstractTester.java | 5 ++++- .../common/collect/testing/IteratorFeature.java | 11 +++++++---- .../common/collect/testing/SpliteratorTester.java | 10 ++++++++-- .../common/collect/ImmutableBiMapFloodingTest.java | 11 +++++++++-- .../common/collect/ImmutableListMultimapTest.java | 3 ++- .../common/collect/ImmutableSetMultimapTest.java | 3 ++- .../test/com/google/common/collect/ListsImplTest.java | 2 ++ .../test/com/google/common/collect/MultimapsTest.java | 3 ++- 11 files changed, 49 insertions(+), 17 deletions(-) diff --git a/android/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java b/android/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java index adbc8dcc6f79..cd9cbfdea95e 100644 --- a/android/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java +++ b/android/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java @@ -17,6 +17,7 @@ package com.google.common.collect.testing; import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -31,7 +32,7 @@ * parameterize the test. * @author George van den Driessche */ -@GwtCompatible +@GwtCompatible(emulated = true) public class AbstractTester extends TestCase { private G subjectGenerator; private String suiteName; @@ -73,10 +74,12 @@ public G getSubjectGenerator() { } /** Returns the name of the test method invoked by this test instance. */ + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL public final String getTestMethodName() { return super.getName(); } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL @Override public String getName() { return Platform.format("%s[%s]", super.getName(), suiteName); diff --git a/android/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java b/android/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java index c447e2922e31..c1e502272dea 100644 --- a/android/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java +++ b/android/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java @@ -16,10 +16,13 @@ package com.google.common.collect.testing; +import static java.util.Arrays.asList; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; + import com.google.common.annotations.GwtCompatible; -import java.util.Collections; -import java.util.EnumSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.ListIterator; import java.util.Set; @@ -49,12 +52,12 @@ public enum IteratorFeature { * A set containing none of the optional features of the {@link Iterator} or {@link ListIterator} * interfaces. */ - public static final Set UNMODIFIABLE = Collections.emptySet(); + public static final Set UNMODIFIABLE = emptySet(); /** * A set containing all of the optional features of the {@link Iterator} and {@link ListIterator} * interfaces. */ public static final Set MODIFIABLE = - Collections.unmodifiableSet(EnumSet.allOf(IteratorFeature.class)); + unmodifiableSet(new LinkedHashSet<>(asList(values()))); } diff --git a/android/guava-tests/test/com/google/common/collect/ListsImplTest.java b/android/guava-tests/test/com/google/common/collect/ListsImplTest.java index 8ef599c99a4d..6ed0b66b351f 100644 --- a/android/guava-tests/test/com/google/common/collect/ListsImplTest.java +++ b/android/guava-tests/test/com/google/common/collect/ListsImplTest.java @@ -99,11 +99,13 @@ private ListExample getExample() { return example == null ? new ImmutableListExample("test") : example; } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL @Override public String getName() { return example == null ? super.getName() : buildTestName(); } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL private String buildTestName() { return super.getName() + ":" + example.getName(); } diff --git a/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java b/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java index adbc8dcc6f79..cd9cbfdea95e 100644 --- a/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java +++ b/guava-testlib/src/com/google/common/collect/testing/AbstractTester.java @@ -17,6 +17,7 @@ package com.google.common.collect.testing; import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; import junit.framework.TestCase; import org.checkerframework.checker.nullness.qual.Nullable; @@ -31,7 +32,7 @@ * parameterize the test. * @author George van den Driessche */ -@GwtCompatible +@GwtCompatible(emulated = true) public class AbstractTester extends TestCase { private G subjectGenerator; private String suiteName; @@ -73,10 +74,12 @@ public G getSubjectGenerator() { } /** Returns the name of the test method invoked by this test instance. */ + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL public final String getTestMethodName() { return super.getName(); } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL @Override public String getName() { return Platform.format("%s[%s]", super.getName(), suiteName); diff --git a/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java b/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java index c447e2922e31..c1e502272dea 100644 --- a/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java +++ b/guava-testlib/src/com/google/common/collect/testing/IteratorFeature.java @@ -16,10 +16,13 @@ package com.google.common.collect.testing; +import static java.util.Arrays.asList; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableSet; + import com.google.common.annotations.GwtCompatible; -import java.util.Collections; -import java.util.EnumSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.ListIterator; import java.util.Set; @@ -49,12 +52,12 @@ public enum IteratorFeature { * A set containing none of the optional features of the {@link Iterator} or {@link ListIterator} * interfaces. */ - public static final Set UNMODIFIABLE = Collections.emptySet(); + public static final Set UNMODIFIABLE = emptySet(); /** * A set containing all of the optional features of the {@link Iterator} and {@link ListIterator} * interfaces. */ public static final Set MODIFIABLE = - Collections.unmodifiableSet(EnumSet.allOf(IteratorFeature.class)); + unmodifiableSet(new LinkedHashSet<>(asList(values()))); } diff --git a/guava-testlib/src/com/google/common/collect/testing/SpliteratorTester.java b/guava-testlib/src/com/google/common/collect/testing/SpliteratorTester.java index e0c2ebfea2ba..6197e86a64a4 100644 --- a/guava-testlib/src/com/google/common/collect/testing/SpliteratorTester.java +++ b/guava-testlib/src/com/google/common/collect/testing/SpliteratorTester.java @@ -20,6 +20,8 @@ import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; import static com.google.common.collect.testing.Helpers.assertEqualInOrder; import static com.google.common.collect.testing.Platform.format; +import static java.util.Arrays.asList; +import static java.util.Collections.unmodifiableSet; import static java.util.Comparator.naturalOrder; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; @@ -34,8 +36,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; -import java.util.EnumSet; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import java.util.Spliterator; import java.util.function.Consumer; import java.util.function.Function; @@ -199,6 +202,9 @@ void forEach(GeneralSpliterator spliterator, Consumer consumer }; abstract void forEach(GeneralSpliterator spliterator, Consumer consumer); + + static final Set ALL_STRATEGIES = + unmodifiableSet(new LinkedHashSet<>(asList(values()))); } private static @Nullable GeneralSpliterator trySplitTestingSize( @@ -289,7 +295,7 @@ public final Ordered expect(Iterable elements) { int characteristics = spliterator.characteristics(); long estimatedSize = spliterator.estimateSize(); for (SpliteratorDecompositionStrategy strategy : - EnumSet.allOf(SpliteratorDecompositionStrategy.class)) { + SpliteratorDecompositionStrategy.ALL_STRATEGIES) { List resultsForStrategy = new ArrayList<>(); strategy.forEach(spliteratorSupplier.get(), resultsForStrategy::add); diff --git a/guava-tests/test/com/google/common/collect/ImmutableBiMapFloodingTest.java b/guava-tests/test/com/google/common/collect/ImmutableBiMapFloodingTest.java index 722e3189a325..80b3aa262e08 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableBiMapFloodingTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableBiMapFloodingTest.java @@ -16,20 +16,24 @@ package com.google.common.collect; +import static java.util.Arrays.asList; +import static java.util.Collections.unmodifiableSet; + import com.google.common.annotations.GwtCompatible; import com.google.errorprone.annotations.CanIgnoreReturnValue; -import java.util.EnumSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.stream.Stream; @GwtCompatible public class ImmutableBiMapFloodingTest extends AbstractHashFloodingTest> { public ImmutableBiMapFloodingTest() { super( - EnumSet.allOf(ConstructionPathway.class).stream() + ConstructionPathway.ALL_PATHWAYS.stream() .flatMap( path -> Stream.>>of( @@ -110,5 +114,8 @@ public ImmutableBiMap create(List> entries) { @CanIgnoreReturnValue public abstract ImmutableBiMap create(List> entries); + + static final Set ALL_PATHWAYS = + unmodifiableSet(new LinkedHashSet<>(asList(values()))); } } diff --git a/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java b/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java index 4e0bdda13070..609e4545a87f 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableListMultimapTest.java @@ -30,6 +30,7 @@ import com.google.common.collect.testing.google.ListMultimapTestSuiteBuilder; import com.google.common.collect.testing.google.TestStringListMultimapGenerator; import com.google.common.collect.testing.google.UnmodifiableCollectionTests; +import com.google.common.primitives.Chars; import com.google.common.testing.CollectorTester; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; @@ -413,7 +414,7 @@ public void testToImmutableListMultimap() { public void testFlatteningToImmutableListMultimap() { Collector> collector = ImmutableListMultimap.flatteningToImmutableListMultimap( - str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); + str -> str.charAt(0), str -> Chars.asList(str.substring(1).toCharArray()).stream()); BiPredicate, Multimap> equivalence = Equivalence.equals() .onResultOf((Multimap mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) diff --git a/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java b/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java index 842ba92865d8..37b9672f0bd2 100644 --- a/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java +++ b/guava-tests/test/com/google/common/collect/ImmutableSetMultimapTest.java @@ -30,6 +30,7 @@ import com.google.common.collect.testing.google.SetMultimapTestSuiteBuilder; import com.google.common.collect.testing.google.TestStringSetMultimapGenerator; import com.google.common.collect.testing.google.UnmodifiableCollectionTests; +import com.google.common.primitives.Chars; import com.google.common.testing.CollectorTester; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; @@ -428,7 +429,7 @@ public void testToImmutableSetMultimap() { public void testFlatteningToImmutableSetMultimap() { Collector> collector = ImmutableSetMultimap.flatteningToImmutableSetMultimap( - str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); + str -> str.charAt(0), str -> Chars.asList(str.substring(1).toCharArray()).stream()); BiPredicate, Multimap> equivalence = Equivalence.equals() .onResultOf((Multimap mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) diff --git a/guava-tests/test/com/google/common/collect/ListsImplTest.java b/guava-tests/test/com/google/common/collect/ListsImplTest.java index 237bcc2c97bd..73071de1253e 100644 --- a/guava-tests/test/com/google/common/collect/ListsImplTest.java +++ b/guava-tests/test/com/google/common/collect/ListsImplTest.java @@ -99,11 +99,13 @@ private ListExample getExample() { return example == null ? new ImmutableListExample("test") : example; } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL @Override public String getName() { return example == null ? super.getName() : buildTestName(); } + @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL private String buildTestName() { return super.getName() + ":" + example.getName(); } diff --git a/guava-tests/test/com/google/common/collect/MultimapsTest.java b/guava-tests/test/com/google/common/collect/MultimapsTest.java index 738ba10ddecd..288a8a812a6d 100644 --- a/guava-tests/test/com/google/common/collect/MultimapsTest.java +++ b/guava-tests/test/com/google/common/collect/MultimapsTest.java @@ -35,6 +35,7 @@ import com.google.common.collect.Maps.EntryTransformer; import com.google.common.collect.testing.IteratorTester; import com.google.common.collect.testing.google.UnmodifiableCollectionTests; +import com.google.common.primitives.Chars; import com.google.common.testing.CollectorTester; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; @@ -106,7 +107,7 @@ public void testFlatteningToMultimap() { Collector> collector = Multimaps.flatteningToMultimap( str -> str.charAt(0), - str -> str.substring(1).chars().mapToObj(c -> (char) c), + str -> Chars.asList(str.substring(1).toCharArray()).stream(), MultimapBuilder.linkedHashKeys().arrayListValues()::build); BiPredicate, Multimap> equivalence = Equivalence.equals()