Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@
<version>1.15</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>31.1-jre</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
<action dev="ggregory" type="add">
Add github/codeql-action.
</action>
<action issue="COLLECTIONS-811" dev="kinow" type="add" due-to="Ben Manes">
Integrate Guava testlib tests.
</action>
<!-- UPDATE -->
<action type="update" dev="kinow" due-to="Dependabot, Gary Gregory">
Bump actions/setup-java from 1.4.0 to 3 #174 #177 #186 #224 #298.
Expand Down
125 changes: 125 additions & 0 deletions src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.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;
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.
*
* @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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done in the JUnit 4 or 5 style?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too, but I don't know how to translate the test suite created programmatically to JUnit 5 😥 ping @ben-manes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collection tests are JUnit3-based, and JUnit 4/5 have runners for supporting their older versions. Since those versions are not backwards compatible otherwise, it's a framework limitation. I think it is unlikely for Guava to revise the tests as it works well enough, even if old code.

FYI there are more test cases you could add (List, Set, etc) and I only did a quick check for Maps. (I also borrowed your tests for my implementation as another sanity check)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

So it's definitely useful, but I prefer to get this merged first for Maps, and later add other Map, List, Set implementations too 👍

Here's the diff of what I had FWIW, thanks!!!
Bruno

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();
+    }
 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can get a little further using the following, where TreeList passes but the others don't. The remaining failures look like actual mistakes.

public static Test suite() {
  TestSuite test = new TestSuite();
  test.addTest(suiteList("TreeList", TreeList::new));
  test.addTest(suiteList("GrowthList", GrowthList::new, CollectionFeature.SERIALIZABLE));
  test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new, CollectionFeature.SERIALIZABLE));
  return test;
}

public static Test suiteList(String name, Supplier<List<String>> factory, Feature<?>... features) {
  var 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();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, same for me with your code. I'm updating the PR now with the suiteMap and suiteTest methods, but leaving only TreeList tests enabled for now. I left a TODO marker so we can revisit it later.

Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in the case of GrowthList some of the failures are expected since it modified the List contract,

 * Decorates another <code>List</code> to make it seamlessly grow when
 * indices larger than the list size are used on add and set,
 * avoiding most IndexOutOfBoundsExceptions.

Technically a violation of the contract, but expected.

 * @throws IndexOutOfBoundsException if the index is out of range
 *         ({@code index < 0 || index > size()})
 */
void add(int index, E element);

You can use .suppressing(Method...) to disable those cases.

TestSuite test = new TestSuite();
// 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;
}

/**
* 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) {
Map<String, String> map = factory.get();
for (Map.Entry<String, String> 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();
}

/**
* 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();
}
}