Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-802

ReferenceMap iterator remove violates contract

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 4.4
    • 4.5.0-M1
    • Map
    • None

    Description

      Out of curiosity I ran Guava's testlib Map tests against the Apache types. This uncovered a contract bug where Iterator.remove() is invalidated by hasNext(), causing its call to no-op due to currentKey becoming null. The isolates case is,

      @Test
      public void iterator_remove() {
        var map = new ReferenceMap<>();
        map.put(1, 2);
        var iter = map.entrySet().iterator();
        assertTrue(iter.hasNext());
        assertTrue(iter.hasNext());
        assertEquals(iter.next(), 1);
        assertFalse(iter.hasNext());
        iter.remove();
        assertEquals(map, Map.of());
      }

      Guava's testlib has good coverage for the Collections Framework and might be worth integrating. The simple test case that I wrote is attached.

      Attachments

        1. ApacheMapTest.java
          2 kB
          Ben Manes
        2. ApacheMapTest-1.java
          2 kB
          Ben Manes

        Activity

          People

            kinow Bruno P. Kinoshita
            ben.manes Ben Manes
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 50m
                1h 50m