1 /* 2 * Copyright (c) 2007, 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 5004188 27 * @summary Tests sequence of listeners 28 * @author Sergey Malenkov 29 */ 30 31 import java.beans.PropertyChangeEvent; 32 import java.beans.PropertyVetoException; 33 import java.beans.VetoableChangeListener; 34 import java.beans.VetoableChangeListenerProxy; 35 import java.beans.VetoableChangeSupport; 36 37 public final class TestListeners implements VetoableChangeListener { 38 private static final String NAME = "property"; 39 private static final String NONE = "broken"; 40 41 private static int current; 42 main(String[] args)43 public static void main(String[] args) throws PropertyVetoException { 44 VetoableChangeSupport vcs = new VetoableChangeSupport(TestListeners.class); 45 vcs.addVetoableChangeListener(new TestListeners(0)); 46 vcs.addVetoableChangeListener(NAME, new TestListeners(2)); 47 vcs.addVetoableChangeListener(new TestListeners(1)); 48 vcs.addVetoableChangeListener(NAME, new VetoableChangeListenerProxy(NAME, new TestListeners(3))); 49 50 51 current = 0; 52 vcs.fireVetoableChange(NAME, 0, 1); 53 if (current != 4) 54 throw new Error("Expected 4 listeners, but called " + current); 55 56 current = 0; 57 vcs.fireVetoableChange(NONE, 1, 0); 58 if (current != 2) 59 throw new Error("Expected 2 listeners, but called " + current); 60 61 62 VetoableChangeListener[] all = vcs.getVetoableChangeListeners(); 63 if (all.length != 4) 64 throw new Error("Expected 4 listeners, but contained " + all.length); 65 66 VetoableChangeListener[] named = vcs.getVetoableChangeListeners(NAME); 67 if (named.length != 2) 68 throw new Error("Expected 2 named listeners, but contained " + named.length); 69 70 VetoableChangeListener[] other = vcs.getVetoableChangeListeners(NONE); 71 if (other.length != 0) 72 throw new Error("Expected 0 other listeners, but contained " + other.length); 73 74 vcs.removeVetoableChangeListener(new TestListeners(0)); 75 vcs.removeVetoableChangeListener(new TestListeners(1)); 76 vcs.removeVetoableChangeListener(NAME, new TestListeners(2)); 77 vcs.removeVetoableChangeListener(NAME, new VetoableChangeListenerProxy(NAME, new TestListeners(3))); 78 79 all = vcs.getVetoableChangeListeners(); 80 if (all.length != 0) 81 throw new Error("Expected 4 listeners, but contained " + all.length); 82 83 named = vcs.getVetoableChangeListeners(NAME); 84 if (named.length != 0) 85 throw new Error("Expected 2 named listeners, but contained " + named.length); 86 87 other = vcs.getVetoableChangeListeners(NONE); 88 if (other.length != 0) 89 throw new Error("Expected 0 other listeners, but contained " + other.length); 90 } 91 92 private final int index; 93 TestListeners(int index)94 public TestListeners(int index) { 95 this.index = index; 96 } 97 vetoableChange(PropertyChangeEvent event)98 public void vetoableChange(PropertyChangeEvent event) { 99 if (this.index < 0) 100 throw new Error("Unexpected listener: " + this.index); 101 102 System.out.println("index = " + this.index); 103 if (this.index != current++) 104 throw new Error("Unexpected listener: " + this.index); 105 } 106 107 @Override equals(Object object)108 public boolean equals(Object object) { 109 if (object instanceof TestListeners) { 110 TestListeners test = (TestListeners)object; 111 return test.index == this.index; 112 } 113 return false; 114 } 115 116 @Override hashCode()117 public int hashCode() { 118 return this.index; 119 } 120 } 121