1 /*
   2  * Copyright (c) 2017, 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 import java.awt.event.AWTEventListener;
  25 import java.util.EventListener;
  26 import java.util.concurrent.CountDownLatch;
  27 import java.util.concurrent.TimeUnit;
  28 
  29 import javax.swing.event.EventListenerList;
  30 
  31 /**
  32  * @test
  33  * @bug 5031664
  34  * @summary EventListenerList.getXXX should always return up to date data
  35  */
  36 public final class GetUpToDateData {
  37 
  38     static final EventListenerList listeners = new EventListenerList();
  39 
  40     static final EventListener o1 = new EventListener() {
  41     };
  42     static final AWTEventListener o2 = event -> {
  43     };
  44 
  45     public static void main(final String[] args) throws Exception {
  46         CountDownLatch go = new CountDownLatch(3);
  47 
  48         Thread t1 = new Thread(() -> {
  49             try {
  50                 // Time to warm-up t3, t4, t5
  51                 Thread.sleep(2000);
  52             } catch (InterruptedException e) {
  53             }
  54             listeners.add(EventListener.class, o1);
  55         });
  56         Thread t2 = new Thread(() -> {
  57             try {
  58                 // Time to warm-up t3, t4, t5
  59                 Thread.sleep(2000);
  60             } catch (InterruptedException e) {
  61             }
  62             listeners.add(AWTEventListener.class, o2);
  63         });
  64 
  65         Thread t3 = new Thread(() -> {
  66             while (listeners.getListenerCount() != 2) {
  67             }
  68             go.countDown();
  69         });
  70         Thread t4 = new Thread(() -> {
  71             while (listeners.getListeners(EventListener.class).length != 1
  72                     || listeners.getListeners(EventListener.class)[0] != o1) {
  73             }
  74             go.countDown();
  75         });
  76         Thread t5 = new Thread(() -> {
  77             while (listeners.getListeners(AWTEventListener.class).length != 1
  78                     || listeners.getListeners(AWTEventListener.class)[0] != o2) {
  79             }
  80             go.countDown();
  81         });
  82 
  83         t1.setDaemon(true);
  84         t2.setDaemon(true);
  85         t3.setDaemon(true);
  86         t4.setDaemon(true);
  87         t5.setDaemon(true);
  88 
  89         t1.start();
  90         t2.start();
  91         t3.start();
  92         t4.start();
  93         t5.start();
  94         if (!go.await(10, TimeUnit.SECONDS)) {
  95             throw new RuntimeException("The test hangs");
  96         }
  97     }
  98 }