Skip to content

Commit

Permalink
[COLLECTIONS-811] Add tests for Lists too, thanks to @ben-manes
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jun 6, 2022
1 parent 283a317 commit eff4e6d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<action dev="ggregory" type="add">
Add github/codeql-action.
</action>
<action issue="COLLECTIONS-811" dev="kinow" type="add">
<action issue="COLLECTIONS-811" dev="kinow" type="add" due-to="Ben Manes">
Integrate Guava testlib tests.
</action>
<!-- UPDATE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@

package org.apache.commons.collections4;

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.commons.collections4.list.TreeList;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.collections4.map.LRUMap;
import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.collections4.map.ReferenceMap;

import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.MapTestSuiteBuilder;
import com.google.common.collect.testing.TestStringListGenerator;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.Feature;
import com.google.common.collect.testing.features.ListFeature;
import com.google.common.collect.testing.features.MapFeature;

import junit.framework.Test;
Expand All @@ -38,8 +44,7 @@
/**
* This test uses Google's Guava Testlib testing libraries to validate the
* contract of collection classes in Commons Collections. This was introduced
* after COLLECTIONS-802, where the issue reported was found with Testlib,
* with thanks to Ben Manes.
* after COLLECTIONS-802, where the issue reported was found with Testlib.
*
* @since 4.5.0
* @see <a href="https://github.com/google/guava/tree/master/guava-testlib">https://github.com/google/guava/tree/master/guava-testlib</a>
Expand All @@ -49,14 +54,28 @@ public final class GuavaTestlibTest extends TestCase {

public static Test suite() {
TestSuite test = new TestSuite();
test.addTest(suite("HashedMap", HashedMap::new));
test.addTest(suite("LinkedMap", LinkedMap::new));
test.addTest(suite("LRUMap", LRUMap::new));
test.addTest(suite("ReferenceMap", ReferenceMap::new));
// Map
test.addTest(suiteMap("HashedMap", HashedMap::new));
test.addTest(suiteMap("LinkedMap", LinkedMap::new));
test.addTest(suiteMap("LRUMap", LRUMap::new));
test.addTest(suiteMap("ReferenceMap", ReferenceMap::new));
// List
test.addTest(suiteList("TreeList", TreeList::new));
// TODO: In COLLECTIONS-811 we enabled the list tests for TreeList, but these other two types did not
// pass the tests. Someone needs to confirm if it is a bug in the code, or we need to change the
// test features.
// test.addTest(suiteList("GrowthList", GrowthList::new, CollectionFeature.SERIALIZABLE));
// test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new, CollectionFeature.SERIALIZABLE));
return test;
}

public static Test suite(String name, Supplier<Map<String, String>> factory) {
/**
* Programmatically create a JUnit (3, 4) Test Suite for Guava testlib tests with Maps.
* @param name name of the test
* @param factory factory to create new Maps
* @return a JUnit 3, 4 Test Suite
*/
private static Test suiteMap(String name, Supplier<Map<String, String>> factory) {
return MapTestSuiteBuilder.using(new TestStringMapGenerator() {
@Override
protected Map<String, String> create(Map.Entry<String, String>[] entries) {
Expand All @@ -73,4 +92,34 @@ protected Map<String, String> create(Map.Entry<String, String>[] entries) {
MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
.createTestSuite();
}

/**
* Programmatically create a JUnit (3, 4) Test Suite for Guava testlib tests with Lists.
* @param name name of the test
* @param factory factory to create new Lists
* @param features test features used in the tests
* @return a JUnit 3, 4 Test Suite
*/
private static Test suiteList(String name, Supplier<List<String>> factory, Feature<?>... features) {
final ListTestSuiteBuilder<String> suite = ListTestSuiteBuilder.using(new TestStringListGenerator() {
@Override
protected List<String> create(String[] elements) {
List<String> list = factory.get();
for (String element : elements) {
list.add(element);
}
return list;
}
})
.named(name)
.withFeatures(
CollectionSize.ANY,
ListFeature.GENERAL_PURPOSE,
ListFeature.REMOVE_OPERATIONS,
CollectionFeature.ALLOWS_NULL_VALUES,
CollectionFeature.DESCENDING_VIEW,
CollectionFeature.SUBSET_VIEW);
suite.withFeatures(features);
return suite.createTestSuite();
}
}

0 comments on commit eff4e6d

Please sign in to comment.