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 public static void main(String[] args) throws Exception { 92 LogManager logman = LogManager.getLogManager(); 93 94 Listener[] listeners = new Listener[2]; 95 Listener listener1 = listeners[0] = new Listener(LogManager.class); 96 Listener listener2 = listeners[1] = new Listener(LogManager.class); 97 98 // add listeners 99 logman.addPropertyChangeListener(listener1); 100 test(listeners, 1, 0); 101 102 logman.addPropertyChangeListener(listener1); 103 test(listeners, 2, 0); 104 105 logman.addPropertyChangeListener(listener2); 106 test(listeners, 2, 1); 107 108 // null handling to check for impact on the existing registrations 109 try { 110 logman.addPropertyChangeListener(null); 111 assertTrue(false, "NullPointerException expected"); 112 } catch (NullPointerException expected) { } 113 test(listeners, 2, 1); 114 115 logman.removePropertyChangeListener(null); // no-op 116 test(listeners, 2, 1); 117 118 // remove listeners 119 logman.removePropertyChangeListener(listener1); 120 test(listeners, 1, 1); 121 122 logman.removePropertyChangeListener(listener1); 123 test(listeners, 0, 1); 124 125 logman.removePropertyChangeListener(listener1); // no-op 126 test(listeners, 0, 1); 127 128 logman.removePropertyChangeListener(listener2); 129 test(listeners, 0, 0); 130 131 logman.removePropertyChangeListener(listener2); // no-op 132 test(listeners, 0, 0); 133 } 134 }