Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/Test4353056.java
41149 views
/*1* Copyright (c) 2003, 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 435305626* @summary Tests basic IndexPropertyChangeEvent functionality27* @author Mark Davidson28*/2930import java.awt.Color;3132import java.beans.IndexedPropertyChangeEvent;33import java.beans.PropertyChangeEvent;34import java.beans.PropertyChangeListener;35import java.beans.PropertyChangeSupport;3637/**38* Tests the basic functionality of IndexedPropertyChangeEvent and39* the fireIndexed... methods on PropertyChangeSupport.40*/41public class Test4353056 implements PropertyChangeListener {42private static final int COUNT = 100;43private static final String COLOR = "color";44private static final String BOOLEAN = "boolean";45private static final String INTEGER = "integer";4647public static void main(String[] args) throws Exception {48Test4353056 test = new Test4353056();49test.addPropertyChangeListener(test);50for (int i = 0; i < COUNT; i++) {51boolean even = i % 2 == 0;52test.setColor(i, i % 3 == 0 ? Color.RED : Color.BLUE);53test.setBoolean(i, even);54test.setInteger(i, i);55}56}5758private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);5960private Color color;61private boolean flag;62private int value;6364private String name;65private int index = -1;6667public void addPropertyChangeListener(PropertyChangeListener listener) {68this.pcs.addPropertyChangeListener(listener);69}7071public void removePropertyChangeListener(PropertyChangeListener listener) {72this.pcs.removePropertyChangeListener(listener);73}7475/**76* Setter for Object indexed property.77*78* @param index the property index79* @param color new value80*/81public void setColor(int index, Color color) {82Color oldColor = this.color;83this.color = color;8485this.index = index;86this.name = COLOR;8788this.pcs.fireIndexedPropertyChange(COLOR, index,89oldColor, color);90}9192/**93* Setter for boolean indexed property.94*95* @param index the property index96* @param flag new value97*/98public void setBoolean(int index, boolean flag) {99boolean oldBool = this.flag;100this.flag = flag;101102this.index = index;103this.name = BOOLEAN;104105this.pcs.fireIndexedPropertyChange(BOOLEAN, index,106oldBool, flag);107}108109/**110* Setter for integer indexed property.111*112* @param index the property index113* @param value new value114*/115public void setInteger(int index, int value) {116int oldInt = this.value;117this.value = value;118119this.index = index;120this.name = INTEGER;121122this.pcs.fireIndexedPropertyChange(INTEGER, index,123oldInt, value);124}125126public void propertyChange(PropertyChangeEvent event) {127Object value = event.getNewValue();128if (value.equals(event.getOldValue())) {129throw new Error("new value is equal to old one");130}131if (!this.name.equals(event.getPropertyName())) {132throw new Error("unexpected property name");133} else if (this.name.equals(COLOR)) {134if (!value.equals(this.color)) {135throw new Error("unexpected object value");136}137} else if (this.name.equals(BOOLEAN)) {138if (!value.equals(Boolean.valueOf(this.flag))) {139throw new Error("unexpected boolean value");140}141} else if (this.name.equals(INTEGER)) {142if (!value.equals(Integer.valueOf(this.value))) {143throw new Error("unexpected integer value");144}145} else {146throw new Error("unexpected property name");147}148if (event instanceof IndexedPropertyChangeEvent) {149IndexedPropertyChangeEvent ipce = (IndexedPropertyChangeEvent) event;150if (this.index != ipce.getIndex()) {151throw new Error("unexpected property index");152}153} else {154throw new Error("unexpected event type");155}156System.out.println(this.name + " at " + this.index + " is " + value);157158this.name = null;159this.index = -1;160}161}162163164