jdk/test/java/beans/PropertyChangeSupport/Test4353056.java
author kvn
Thu, 05 Dec 2013 15:13:12 -0800
changeset 22858 f4a6f0eba875
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4353056
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests basic IndexPropertyChangeEvent functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.IndexedPropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Tests the basic functionality of IndexedPropertyChangeEvent and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the fireIndexed... methods on PropertyChangeSupport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class Test4353056 implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int COUNT = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final String COLOR = "color";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String BOOLEAN = "boolean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String INTEGER = "integer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Test4353056 test = new Test4353056();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        test.addPropertyChangeListener(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for (int i = 0; i < COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            boolean even = i % 2 == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            test.setColor(i, i % 3 == 0 ? Color.RED : Color.BLUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            test.setBoolean(i, even);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            test.setInteger(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.pcs.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void removePropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.pcs.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Setter for Object indexed property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param index  the property index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param color  new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void setColor(int index, Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Color oldColor = this.color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.color = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.name = COLOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.pcs.fireIndexedPropertyChange(COLOR, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                oldColor, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Setter for boolean indexed property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param index  the property index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param flag   new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void setBoolean(int index, boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        boolean oldBool = this.flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.flag = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.name = BOOLEAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.pcs.fireIndexedPropertyChange(BOOLEAN, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                oldBool, flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Setter for integer indexed property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param index  the property index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param value  new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void setInteger(int index, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int oldInt = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.name = INTEGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.pcs.fireIndexedPropertyChange(INTEGER, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                oldInt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object value = event.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (value.equals(event.getOldValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new Error("new value is equal to old one");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (!this.name.equals(event.getPropertyName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new Error("unexpected property name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else if (this.name.equals(COLOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!value.equals(this.color)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new Error("unexpected object value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else if (this.name.equals(BOOLEAN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (!value.equals(Boolean.valueOf(this.flag))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new Error("unexpected boolean value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else if (this.name.equals(INTEGER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (!value.equals(Integer.valueOf(this.value))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new Error("unexpected integer value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new Error("unexpected property name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (event instanceof IndexedPropertyChangeEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            IndexedPropertyChangeEvent ipce = (IndexedPropertyChangeEvent) event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (this.index != ipce.getIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new Error("unexpected property index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Error("unexpected event type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.println(this.name + " at " + this.index + " is " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}