Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections #301

Merged
merged 2 commits into from Jun 6, 2022

Conversation

kinow
Copy link
Member

@kinow kinow commented Apr 26, 2022

Adding the test class from @ben-manes, pending merge of #300

@ben-manes
Copy link

/cc @kevinb9n 🙂

@kinow kinow marked this pull request as ready for review April 28, 2022 10:40
@kinow
Copy link
Member Author

kinow commented Apr 28, 2022

Rebased. Confirmed the license is compatible (AL as well). Waiting for CI, and will update changes.xml in the meantime. Then it should be ready for review 👍

@kinow
Copy link
Member Author

kinow commented Apr 28, 2022

And didn't notice a difference in build time. Executed a few times locally as well, and can confirm it doesn't bring any brittleness to our tests. Looks like a good addition to our test code. Thanks for showing us this @ben-manes !

*/
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.

@kinow
Copy link
Member Author

kinow commented May 1, 2022

Rebased.

@codecov-commenter
Copy link

Codecov Report

Merging #301 (919b449) into master (1677dac) will increase coverage by 0.10%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##             master     #301      +/-   ##
============================================
+ Coverage     85.87%   85.97%   +0.10%     
- Complexity     4676     4678       +2     
============================================
  Files           292      292              
  Lines         13469    13469              
  Branches       1955     1955              
============================================
+ Hits          11566    11580      +14     
+ Misses         1326     1321       -5     
+ Partials        577      568       -9     
Impacted Files Coverage Δ
...he/commons/collections4/map/AbstractHashedMap.java 93.82% <0.00%> (+0.98%) ⬆️
...commons/collections4/map/AbstractReferenceMap.java 91.48% <0.00%> (+1.11%) ⬆️
...org/apache/commons/collections4/list/TreeList.java 93.56% <0.00%> (+1.87%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1677dac...919b449. Read the comment docs.

* 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.
Copy link
Member

Choose a reason for hiding this comment

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

We don't use @author tags, so would you please move attribution to changes.xml and/or pom.xml?

Choose a reason for hiding this comment

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

I am fine having it removed and appreciate the thanks.

Copy link
Member Author

@kinow kinow Jun 6, 2022

Choose a reason for hiding this comment

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

Done @garydgregory, and rebased! Thanks

@garydgregory garydgregory merged commit 06f7b6f into apache:master Jun 6, 2022
anantdamle pushed a commit to anantdamle/commons-collections that referenced this pull request Aug 14, 2023
…llections (apache#301)

* [COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections

* [COLLECTIONS-811] Add tests for Lists too, thanks to @ben-manes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants