1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6312056 4155650 4294891 4904074
  27  * @summary Reasonable things should happen if mutating while iterating.
  28  */
  29 
  30 import java.util.Iterator;
  31 import java.util.Map;
  32 import java.util.concurrent.ConcurrentHashMap;
  33 import java.util.concurrent.ConcurrentMap;
  34 import java.util.concurrent.ConcurrentSkipListMap;
  35 
  36 public class ConcurrentModification {
  37     static volatile int passed = 0, failed = 0;
  38 
  39     static void fail(String msg) {
  40         failed++;
  41         new AssertionError(msg).printStackTrace();
  42     }
  43 
  44     static void unexpected(Throwable t) {
  45         failed++;
  46         t.printStackTrace();
  47     }
  48 
  49     static void check(boolean condition, String msg) {
  50         if (condition)
  51             passed++;
  52         else
  53             fail(msg);
  54     }
  55 
  56     static void check(boolean condition) {
  57         check(condition, "Assertion failed");
  58     }
  59 
  60     private static void test(ConcurrentMap<Integer, Integer> m)
  61     {
  62         try {
  63             m.clear();
  64             check(m.isEmpty());
  65             m.put(1,2);
  66             Iterator<Map.Entry<Integer,Integer>> it = m.entrySet().iterator();
  67             if (it.hasNext()) {
  68                 m.remove(1); // sneaky
  69                 Map.Entry<Integer, Integer> e = it.next();
  70                 check(m.isEmpty());
  71                 check(e.getKey() == 1);
  72                 check(e.getValue() == 2);
  73             }
  74         } catch (Throwable t) {unexpected(t);}
  75 
  76         try {
  77             m.clear();
  78             check(m.isEmpty());
  79             m.put(1,2);
  80             Iterator<Map.Entry<Integer,Integer>> it = m.entrySet().iterator();
  81             if (it.hasNext()) {
  82                 m.put(1,3); // sneaky
  83                 Map.Entry<Integer, Integer> e = it.next();
  84                 check(e.getKey() == 1);
  85                 check(e.getValue() == 2 || e.getValue() == 3);
  86                 if (m instanceof ConcurrentHashMap) {
  87                     e.setValue(4);
  88                     check(m.get(1) == 4);
  89                 }
  90             }
  91         } catch (Throwable t) {unexpected(t);}
  92     }
  93 
  94     public static void main(String[] args) {
  95         test(new ConcurrentHashMap<Integer,Integer>());
  96         test(new ConcurrentSkipListMap<Integer,Integer>());
  97 
  98         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  99         if (failed > 0) throw new Error("Some tests failed");
 100     }
 101 }