dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

SequenceNumberComparator throwing assertion error on JDK7 #320

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
On running unit tests using JDK7, the 'Inorder' feature
usage is always throwing an exception : "sequence number has to be
globally unique"

On debugging we see that the 'AllInvocationsFinder' class has a
comparator (SequenceNumberComparator) which is passed to a TreeSet.

    private final class SequenceNumberComparator implements
Comparator<Invocation> {
        public int compare(Invocation o1, Invocation o2) {
            int comparison =
o1.getSequenceNumber().compareTo(o2.getSequenceNumber());
            assert comparison != 0 : "sequence number has to be
globally unique";
            return comparison;
        }
    }

In JDK7, TreeSet/TreeMap have been changed to call the comparator when
the first element is inserted.

JDK7
    public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }

JDK6
  public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
          // TBD:
          // 5045147: (coll) Adding null to an empty TreeSet should
          // throw NullPointerException
          //
          // compare(key, key); // type check
            root = new Entry<K,V>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }

So on the first element’s insertion the comparator is called with the
same objects and the assert statement blows up.

To make use of mockito on JDK7, I think that assert statement should
be removed.

For now as a workaround, suppressing assertions for the
AllInvocationsFinder class should work.
Thread.currentThread().getContextClassLoader().setClassAssertionStatus(AllInvoca
tionsFinder.class.getName(),
false); 

Original issue reported on code.google.com by shakib.u...@gmail.com on 24 Feb 2012 at 11:05

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 24 Feb 2012 at 3:54

GoogleCodeExporter commented 8 years ago
The jdk behavior is bizarre. Can we leave the assertion (important for mockito 
integrity) but tweak it so that it does not throw when 2 objects are identical 
(==)?

Original comment by szcze...@gmail.com on 24 Feb 2012 at 10:03

GoogleCodeExporter commented 8 years ago
Yeah, I think that should be good enough.

Original comment by shakib.u...@gmail.com on 27 Feb 2012 at 11:20

GoogleCodeExporter commented 8 years ago
Actually the assertion was removed a long time ago, around the last 2009 
quarter.

The current code is working properly on the JDK7.

Original comment by brice.du...@gmail.com on 9 Mar 2012 at 11:12