From 283a317ce37ffb7cb65bf47333a7a9c9c1b62802 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Tue, 26 Apr 2022 22:35:18 +1200 Subject: [PATCH 1/2] [COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections --- pom.xml | 6 ++ src/changes/changes.xml | 3 + .../collections4/GuavaTestlibTest.java | 76 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java diff --git a/pom.xml b/pom.xml index 9b993a7e91..cd58814c9c 100644 --- a/pom.xml +++ b/pom.xml @@ -524,6 +524,12 @@ 1.15 true + + com.google.guava + guava-testlib + 31.1-jre + test + diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6d2dadae46..d7ff0711ea 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -143,6 +143,9 @@ Add github/codeql-action. + + Integrate Guava testlib tests. + Bump actions/setup-java from 1.4.0 to 3 #174 #177 #186 #224 #298. diff --git a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java new file mode 100644 index 0000000000..88108c6aec --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.collections4; + +import java.util.Map; +import java.util.function.Supplier; + +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.MapTestSuiteBuilder; +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.MapFeature; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * 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. + * + * @since 4.5.0 + * @see https://github.com/google/guava/tree/master/guava-testlib + * @see https://issues.apache.org/jira/browse/COLLECTIONS-802 + */ +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)); + return test; + } + + public static Test suite(String name, Supplier> factory) { + return MapTestSuiteBuilder.using(new TestStringMapGenerator() { + @Override + protected Map create(Map.Entry[] entries) { + Map map = factory.get(); + for (Map.Entry entry : entries) { + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + }) + .named(name) + .withFeatures( + CollectionSize.ANY, MapFeature.GENERAL_PURPOSE, + MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE) + .createTestSuite(); + } +} From eff4e6dc9f7cc1236ccdfaddf23f212b4d0a7b4d Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Sun, 1 May 2022 18:12:06 +1200 Subject: [PATCH 2/2] [COLLECTIONS-811] Add tests for Lists too, thanks to @ben-manes --- src/changes/changes.xml | 2 +- .../collections4/GuavaTestlibTest.java | 63 ++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d7ff0711ea..e8e1057c40 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -143,7 +143,7 @@ Add github/codeql-action. - + Integrate Guava testlib tests. diff --git a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java index 88108c6aec..78685ea50c 100644 --- a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java +++ b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java @@ -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; @@ -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 https://github.com/google/guava/tree/master/guava-testlib @@ -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> 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> factory) { return MapTestSuiteBuilder.using(new TestStringMapGenerator() { @Override protected Map create(Map.Entry[] entries) { @@ -73,4 +92,34 @@ protected Map create(Map.Entry[] 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> factory, Feature... features) { + final ListTestSuiteBuilder suite = ListTestSuiteBuilder.using(new TestStringListGenerator() { + @Override + protected List create(String[] elements) { + List 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(); + } }