You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/05/01 00:22:11 UTC

[GitHub] [commons-collections] kinow commented on a diff in pull request #301: [COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections

kinow commented on code in PR #301:
URL: https://github.com/apache/commons-collections/pull/301#discussion_r862407940


##########
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 <a href="https://github.com/google/guava/tree/master/guava-testlib">https://github.com/google/guava/tree/master/guava-testlib</a>
+ * @see <a href="https://issues.apache.org/jira/browse/COLLECTIONS-802">https://issues.apache.org/jira/browse/COLLECTIONS-802</a>
+ */
+public final class GuavaTestlibTest extends TestCase {
+
+    public static Test suite() {

Review Comment:
   Thanks @ben-manes !
   
   I will leave the List, Set, etc., for follow-up issues. I had some 5 minutes to test it, and managed to write some tests for Lists, but these tests found issues in some of the Lists. I'd have to confirm if the features I selected are actually pertinent to the List implementations I used, or if we have other bugs :slightly_smiling_face: 
   
   So it's definitely useful, but I prefer to get this merged first for Maps, and later add other Map, List, Set implementations too :+1: 
   
   Here's the diff of what I had FWIW, thanks!!!
   Bruno
   
   ```diff
   diff --git a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
   index 88108c6a..7d068e17 100644
   --- a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
   +++ b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
   @@ -17,18 +17,25 @@
    
    package org.apache.commons.collections4;
    
   +import java.util.List;
    import java.util.Map;
    import java.util.function.Supplier;
    
   +import org.apache.commons.collections4.list.CursorableLinkedList;
   +import org.apache.commons.collections4.list.GrowthList;
   +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.ListFeature;
    import com.google.common.collect.testing.features.MapFeature;
    
    import junit.framework.Test;
   @@ -49,14 +56,17 @@ 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));
   +        test.addTest(suiteMap("HashedMap", HashedMap::new));
   +        test.addTest(suiteMap("LinkedMap", LinkedMap::new));
   +        test.addTest(suiteMap("LRUMap", LRUMap::new));
   +        test.addTest(suiteMap("ReferenceMap", ReferenceMap::new));
   +        test.addTest(suiteList("List", TreeList::new));
   +        test.addTest(suiteList("GrowthList", GrowthList::new));
   +        test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new));
            return test;
        }
    
   -    public static Test suite(String name, Supplier<Map<String, String>> factory) {
   +    public 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) {
   @@ -73,4 +83,23 @@ public final class GuavaTestlibTest extends TestCase {
                            MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
                    .createTestSuite();
        }
   +
   +    public static Test suiteList(String name, Supplier<List<String>> factory) {
   +        return 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, ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
   +                        CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
   +                .createTestSuite();
   +    }
    }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org