Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/TestMethods.java
41152 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 500418826* @summary Tests sequence of methods27* @author Sergey Malenkov28*/2930import java.beans.PropertyChangeEvent;31import java.beans.PropertyChangeListener;32import java.beans.PropertyChangeSupport;3334enum Fire {35PropertyChangeEvent,36PropertyObject,37PropertyInteger,38PropertyBoolean,39IndexedPropertyObject,40IndexedPropertyInteger,41IndexedPropertyBoolean,42}4344public class TestMethods extends PropertyChangeSupport implements PropertyChangeListener {45private static final String NAME = "property";4647public static void main(String[] args) {48Object source = new Object();49new TestMethods(source).firePropertyChange(new PropertyChangeEvent(source, NAME, null, null));50new TestMethods(source).firePropertyChange(NAME, null, null);51new TestMethods(source).firePropertyChange(NAME, 0, 1);52new TestMethods(source).firePropertyChange(NAME, true, false);53new TestMethods(source).fireIndexedPropertyChange(NAME, 0, null, null);54new TestMethods(source).fireIndexedPropertyChange(NAME, 0, 0, 1);55new TestMethods(source).fireIndexedPropertyChange(NAME, 0, true, false);56}5758private Fire state;5960public TestMethods(Object source) {61super(source);62addPropertyChangeListener(this);63}6465public void propertyChange(PropertyChangeEvent event) {66if (this.state != Fire.PropertyChangeEvent)67throw new Error("Illegal state: " + this.state);68}6970@Override71public void firePropertyChange(String property, Object oldValue, Object newValue) {72if ((this.state != null) && (this.state != Fire.PropertyBoolean) && (this.state != Fire.PropertyInteger))73throw new Error("Illegal state: " + this.state);7475this.state = Fire.PropertyObject;76super.firePropertyChange(property, oldValue, newValue);77}7879@Override80public void firePropertyChange(String property, int oldValue, int newValue) {81if (this.state != null)82throw new Error("Illegal state: " + this.state);8384this.state = Fire.PropertyInteger;85super.firePropertyChange(property, oldValue, newValue);86}8788@Override89public void firePropertyChange(String property, boolean oldValue, boolean newValue) {90if (this.state != null)91throw new Error("Illegal state: " + this.state);9293this.state = Fire.PropertyBoolean;94super.firePropertyChange(property, oldValue, newValue);95}9697@Override98public void firePropertyChange(PropertyChangeEvent event) {99if ((this.state != null) && (this.state != Fire.PropertyObject) && (this.state != Fire.IndexedPropertyObject))100throw new Error("Illegal state: " + this.state);101102this.state = Fire.PropertyChangeEvent;103super.firePropertyChange(event);104}105106@Override107public void fireIndexedPropertyChange(String property, int index, Object oldValue, Object newValue) {108if ((this.state != null) && (this.state != Fire.IndexedPropertyBoolean) && (this.state != Fire.IndexedPropertyInteger))109throw new Error("Illegal state: " + this.state);110111this.state = Fire.IndexedPropertyObject;112super.fireIndexedPropertyChange(property, index, oldValue, newValue);113}114115@Override116public void fireIndexedPropertyChange(String property, int index, int oldValue, int newValue) {117if (this.state != null)118throw new Error("Illegal state: " + this.state);119120this.state = Fire.IndexedPropertyInteger;121super.fireIndexedPropertyChange(property, index, oldValue, newValue);122}123124@Override125public void fireIndexedPropertyChange(String property, int index, boolean oldValue, boolean newValue) {126if (this.state != null)127throw new Error("Illegal state: " + this.state);128129this.state = Fire.IndexedPropertyBoolean;130super.fireIndexedPropertyChange(property, index, oldValue, newValue);131}132}133134135