Skip to content

Commit fcdfd31

Browse files
Refactor to use derived suite overrides per review feedback
Instead of adding ConcurrentMapSpliteratorTester to the map tester list, use the derived suite architecture as @chaoren suggested: - Add ConcurrentCollectionSpliteratorTester that tests any collection's spliterator for the CONCURRENT characteristic - Add ConcurrentSetTestSuiteBuilder and ConcurrentCollectionTestSuiteBuilder that include this tester - Override createDerivedEntrySetSuite/KeySetSuite/ValueCollectionSuite in ConcurrentMapTestSuiteBuilder to use the concurrent builders - Delete ConcurrentMapSpliteratorTester (no longer needed) This approach is more composable - the tester can be reused for any concurrent collection, not just ConcurrentMap views.
1 parent d9764e2 commit fcdfd31

File tree

5 files changed

+173
-83
lines changed

5 files changed

+173
-83
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2025 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.common.collect.testing;
18+
19+
import static com.google.common.collect.testing.Helpers.copyToList;
20+
21+
import com.google.common.annotations.GwtIncompatible;
22+
import com.google.common.collect.testing.testers.ConcurrentCollectionSpliteratorTester;
23+
import java.util.List;
24+
25+
/**
26+
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a concurrent
27+
* Collection implementation.
28+
*
29+
* <p>This builder adds {@link ConcurrentCollectionSpliteratorTester} to verify that the
30+
* collection's spliterator has the {@link java.util.Spliterator#CONCURRENT} characteristic.
31+
*
32+
* @author Guava Authors
33+
*/
34+
@GwtIncompatible
35+
public class ConcurrentCollectionTestSuiteBuilder<E> extends CollectionTestSuiteBuilder<E> {
36+
37+
public static <E> ConcurrentCollectionTestSuiteBuilder<E> using(
38+
TestCollectionGenerator<E> generator) {
39+
ConcurrentCollectionTestSuiteBuilder<E> result = new ConcurrentCollectionTestSuiteBuilder<>();
40+
result.usingGenerator(generator);
41+
return result;
42+
}
43+
44+
@SuppressWarnings("rawtypes") // class literals
45+
@Override
46+
protected List<Class<? extends AbstractTester>> getTesters() {
47+
List<Class<? extends AbstractTester>> testers = copyToList(super.getTesters());
48+
testers.add(ConcurrentCollectionSpliteratorTester.class);
49+
return testers;
50+
}
51+
}

guava-testlib/src/com/google/common/collect/testing/ConcurrentMapTestSuiteBuilder.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.google.common.collect.testing.testers.ConcurrentMapRemoveTester;
2323
import com.google.common.collect.testing.testers.ConcurrentMapReplaceEntryTester;
2424
import com.google.common.collect.testing.testers.ConcurrentMapReplaceTester;
25-
import com.google.common.collect.testing.testers.ConcurrentMapSpliteratorTester;
2625
import java.util.List;
26+
import java.util.Map.Entry;
2727

2828
/**
2929
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a ConcurrentMap
@@ -45,8 +45,7 @@ public static <K, V> ConcurrentMapTestSuiteBuilder<K, V> using(TestMapGenerator<
4545
ConcurrentMapPutIfAbsentTester.class,
4646
ConcurrentMapRemoveTester.class,
4747
ConcurrentMapReplaceTester.class,
48-
ConcurrentMapReplaceEntryTester.class,
49-
ConcurrentMapSpliteratorTester.class);
48+
ConcurrentMapReplaceEntryTester.class);
5049

5150
@SuppressWarnings("rawtypes") // class literals
5251
@Override
@@ -55,4 +54,21 @@ protected List<Class<? extends AbstractTester>> getTesters() {
5554
testers.addAll(TESTERS);
5655
return testers;
5756
}
57+
58+
@Override
59+
protected SetTestSuiteBuilder<Entry<K, V>> createDerivedEntrySetSuite(
60+
TestSetGenerator<Entry<K, V>> entrySetGenerator) {
61+
return ConcurrentSetTestSuiteBuilder.using(entrySetGenerator);
62+
}
63+
64+
@Override
65+
protected SetTestSuiteBuilder<K> createDerivedKeySetSuite(TestSetGenerator<K> keySetGenerator) {
66+
return ConcurrentSetTestSuiteBuilder.using(keySetGenerator);
67+
}
68+
69+
@Override
70+
protected CollectionTestSuiteBuilder<V> createDerivedValueCollectionSuite(
71+
TestCollectionGenerator<V> valueCollectionGenerator) {
72+
return ConcurrentCollectionTestSuiteBuilder.using(valueCollectionGenerator);
73+
}
5874
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2025 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.common.collect.testing;
18+
19+
import static com.google.common.collect.testing.Helpers.copyToList;
20+
21+
import com.google.common.annotations.GwtIncompatible;
22+
import com.google.common.collect.testing.testers.ConcurrentCollectionSpliteratorTester;
23+
import java.util.List;
24+
25+
/**
26+
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a concurrent Set
27+
* implementation.
28+
*
29+
* <p>This builder adds {@link ConcurrentCollectionSpliteratorTester} to verify that the set's
30+
* spliterator has the {@link java.util.Spliterator#CONCURRENT} characteristic.
31+
*
32+
* @author Guava Authors
33+
*/
34+
@GwtIncompatible
35+
public class ConcurrentSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> {
36+
37+
public static <E> ConcurrentSetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) {
38+
ConcurrentSetTestSuiteBuilder<E> result = new ConcurrentSetTestSuiteBuilder<>();
39+
result.usingGenerator(generator);
40+
return result;
41+
}
42+
43+
@SuppressWarnings("rawtypes") // class literals
44+
@Override
45+
protected List<Class<? extends AbstractTester>> getTesters() {
46+
List<Class<? extends AbstractTester>> testers = copyToList(super.getTesters());
47+
testers.add(ConcurrentCollectionSpliteratorTester.class);
48+
return testers;
49+
}
50+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2025 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.common.collect.testing.testers;
18+
19+
import com.google.common.annotations.GwtCompatible;
20+
import com.google.common.collect.testing.AbstractCollectionTester;
21+
import java.util.Spliterator;
22+
import org.jspecify.annotations.NullMarked;
23+
import org.junit.Ignore;
24+
25+
/**
26+
* A generic JUnit test which tests that a collection's spliterator has the {@link
27+
* Spliterator#CONCURRENT} characteristic. This tester is intended for use with concurrent
28+
* collections such as views of {@link java.util.concurrent.ConcurrentMap}.
29+
*
30+
* <p>Can't be invoked directly; please see {@link
31+
* com.google.common.collect.testing.ConcurrentCollectionTestSuiteBuilder} or {@link
32+
* com.google.common.collect.testing.ConcurrentSetTestSuiteBuilder}.
33+
*
34+
* @author Guava Authors
35+
*/
36+
@GwtCompatible
37+
@Ignore("test runners must not instantiate and run this directly, only via suites we build")
38+
// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
39+
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
40+
@NullMarked
41+
public class ConcurrentCollectionSpliteratorTester<E> extends AbstractCollectionTester<E> {
42+
43+
/**
44+
* Tests that the spliterator returned by {@code collection.spliterator()} has the {@code
45+
* CONCURRENT} characteristic.
46+
*/
47+
public void testSpliteratorHasConcurrent() {
48+
Spliterator<E> spliterator = collection.spliterator();
49+
assertTrue(
50+
"spliterator() should have CONCURRENT characteristic",
51+
spliterator.hasCharacteristics(Spliterator.CONCURRENT));
52+
}
53+
}

guava-testlib/src/com/google/common/collect/testing/testers/ConcurrentMapSpliteratorTester.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)