jdk/test/java/util/logging/Listeners.java
changeset 22864 f476f1ae4bd3
parent 22863 6a578e0f65ea
parent 22173 f130ca87de66
child 22865 3b8857d7b3cc
equal deleted inserted replaced
22863:6a578e0f65ea 22864:f476f1ae4bd3
     1 /*
       
     2  * Copyright (c) 2012, 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 7192275
       
    27  * @summary Basic test of addPropertyListener/removePropertyListener methods
       
    28  * @run main/othervm Listeners
       
    29  */
       
    30 
       
    31 import java.util.logging.LogManager;
       
    32 import java.beans.PropertyChangeListener;
       
    33 import java.beans.PropertyChangeEvent;
       
    34 
       
    35 public class Listeners {
       
    36 
       
    37     static void assertTrue(boolean result, String msg) {
       
    38         if (!result)
       
    39             throw new RuntimeException(msg);
       
    40     }
       
    41 
       
    42     /**
       
    43      * A {@code PropertyChangeListener} that counts the number of times that
       
    44      * the {@code propertyChange} method is fired, and also checks that the
       
    45      * event source is the expected (fixed) object.
       
    46      */
       
    47     static class Listener implements PropertyChangeListener {
       
    48         private final Object expectedSource;
       
    49         private int fireCount;
       
    50 
       
    51         Listener(Object expectedSource) {
       
    52             this.expectedSource = expectedSource;
       
    53         }
       
    54 
       
    55         int fireCount() {
       
    56             return fireCount;
       
    57         }
       
    58 
       
    59         Listener reset() {
       
    60             fireCount = 0;
       
    61             return this;
       
    62         }
       
    63 
       
    64         @Override
       
    65         public void propertyChange(PropertyChangeEvent evt) {
       
    66             assertTrue(evt.getSource() == expectedSource, "Unexpected source");
       
    67             fireCount++;
       
    68         }
       
    69     }
       
    70 
       
    71     /**
       
    72      * Tests that the given listeners are invoked the expected number of
       
    73      * times.
       
    74      */
       
    75     static void test(Listener[] listeners, int... expected) throws Exception {
       
    76         // reset counts
       
    77         for (Listener listener : listeners) {
       
    78             listener.reset();
       
    79         }
       
    80 
       
    81         // re-reading configuration causes events to be fired
       
    82         LogManager.getLogManager().readConfiguration();
       
    83 
       
    84         // check event listeners invoked as expected
       
    85         for (int i = 0; i < expected.length; i++) {
       
    86             assertTrue(listeners[i].fireCount() == expected[i],
       
    87                 "Unexpected event count");
       
    88         }
       
    89     }
       
    90 
       
    91     @SuppressWarnings("deprecation")
       
    92     public static void main(String[] args) throws Exception {
       
    93         LogManager logman = LogManager.getLogManager();
       
    94 
       
    95         Listener[] listeners = new Listener[2];
       
    96         Listener listener1 = listeners[0] = new Listener(LogManager.class);
       
    97         Listener listener2 = listeners[1] = new Listener(LogManager.class);
       
    98 
       
    99         // add listeners
       
   100         logman.addPropertyChangeListener(listener1);
       
   101         test(listeners, 1, 0);
       
   102 
       
   103         logman.addPropertyChangeListener(listener1);
       
   104         test(listeners, 2, 0);
       
   105 
       
   106         logman.addPropertyChangeListener(listener2);
       
   107         test(listeners, 2, 1);
       
   108 
       
   109         // null handling to check for impact on the existing registrations
       
   110         try {
       
   111             logman.addPropertyChangeListener(null);
       
   112             assertTrue(false, "NullPointerException expected");
       
   113         } catch (NullPointerException expected) { }
       
   114         test(listeners, 2, 1);
       
   115 
       
   116         logman.removePropertyChangeListener(null);  // no-op
       
   117         test(listeners, 2, 1);
       
   118 
       
   119         // remove listeners
       
   120         logman.removePropertyChangeListener(listener1);
       
   121         test(listeners, 1, 1);
       
   122 
       
   123         logman.removePropertyChangeListener(listener1);
       
   124         test(listeners, 0, 1);
       
   125 
       
   126         logman.removePropertyChangeListener(listener1);  // no-op
       
   127         test(listeners, 0, 1);
       
   128 
       
   129         logman.removePropertyChangeListener(listener2);
       
   130         test(listeners, 0, 0);
       
   131 
       
   132         logman.removePropertyChangeListener(listener2);  // no-op
       
   133         test(listeners, 0, 0);
       
   134     }
       
   135 }