Path: blob/master/test/jdk/java/beans/Introspector/4058433/TestBeanProperty.java
41152 views
/*1* Copyright (c) 2014, 2015, 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*/2223import java.beans.BeanProperty;24import java.beans.PropertyChangeListener;25import java.beans.PropertyChangeSupport;26import java.beans.PropertyDescriptor;27import java.util.Arrays;2829/**30* @test31* @bug 405843332* @summary Tests the BeanProperty annotation33* @author Sergey Malenkov34* @library ..35*/36public class TestBeanProperty {37public static void main(String[] args) throws Exception {38Class<?>[] types =39{B.class, BL.class, BLF.class, E.class, H.class, P.class,40VU.class, D.class, EVD.class, EVE.class, EV.class, EVL.class,41EVX.class, R.class};42for (Class<?> type : types) {43PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, "value");44if (((B.class == type) || (BLF.class == type)) && pd.isBound()) {45BeanUtils.reportPropertyDescriptor(pd);46throw new Error("not bound");47}48if ((BL.class == type) == !pd.isBound()) {49BeanUtils.reportPropertyDescriptor(pd);50throw new Error("bound");51}52if ((E.class == type) == !pd.isExpert()) {53BeanUtils.reportPropertyDescriptor(pd);54throw new Error("expert");55}56if ((H.class == type) == !pd.isHidden()) {57BeanUtils.reportPropertyDescriptor(pd);58throw new Error("hidden");59}60if ((P.class == type) == !pd.isPreferred()) {61BeanUtils.reportPropertyDescriptor(pd);62throw new Error("preferred");63}64if ((R.class == type) == !Boolean.TRUE.equals(pd.getValue("required"))) {65BeanUtils.reportPropertyDescriptor(pd);66throw new Error("required");67}68if ((D.class == type) == !"getter".equals(pd.getShortDescription())) {69BeanUtils.reportPropertyDescriptor(pd);70throw new Error("shortDescription");71}72if ((VU.class == type) == !Boolean.TRUE.equals(pd.getValue("visualUpdate"))) {73BeanUtils.reportPropertyDescriptor(pd);74throw new Error("visualUpdate");75}76if ((EV.class == type) == !isEV(pd, "LEFT", 2, "javax.swing.SwingConstants.LEFT", "RIGHT", 4, "javax.swing.SwingConstants.RIGHT")) {77BeanUtils.reportPropertyDescriptor(pd);78throw new Error("enumerationValues from another package");79}80if ((EVL.class == type) == !isEV(pd, "ZERO", 0, "ZERO", "ONE", 1, "ONE")) {81BeanUtils.reportPropertyDescriptor(pd);82throw new Error("enumerationValues from another package");83}84if ((EVX.class == type) == !isEV(pd, "ZERO", 0, "X.ZERO", "ONE", 1, "X.ONE")) {85BeanUtils.reportPropertyDescriptor(pd);86throw new Error("enumerationValues from another package");87}88if (EVD.class == type && !isEV(pd)) {89BeanUtils.reportPropertyDescriptor(pd);90throw new Error("EV:"+ pd.getValue("enumerationValues"));91}92if (EVE.class == type && !isEV(pd)) {93BeanUtils.reportPropertyDescriptor(pd);94throw new Error("EV:"+ pd.getValue("enumerationValues"));95}96}97}9899private static boolean isEV(PropertyDescriptor pd, Object... expected) {100Object value = pd.getValue("enumerationValues");101return value instanceof Object[] && Arrays.equals((Object[]) value, expected);102}103104public static class B {105private int value;106107public int getValue() {108return this.value;109}110111public void setValue(int value) {112this.value = value;113}114}115116public static class BL {117private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);118private int value;119120public int getValue() {121return this.value;122}123124public void setValue(int value) {125this.value = value;126}127128public void addPropertyChangeListener(PropertyChangeListener listener) {129this.pcs.addPropertyChangeListener(listener);130}131132public void removePropertyChangeListener(PropertyChangeListener listener) {133this.pcs.removePropertyChangeListener(listener);134}135}136137public static class BLF {138private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);139private int value;140141public int getValue() {142return this.value;143}144145@BeanProperty(bound = false)146public void setValue(int value) {147this.value = value;148}149150public void addPropertyChangeListener(PropertyChangeListener listener) {151this.pcs.addPropertyChangeListener(listener);152}153154public void removePropertyChangeListener(PropertyChangeListener listener) {155this.pcs.removePropertyChangeListener(listener);156}157}158159public static class E {160private int value;161162public int getValue() {163return this.value;164}165166@BeanProperty(expert = true)167public void setValue(int value) {168this.value = value;169}170}171172public static class H {173private int value;174175public int getValue() {176return this.value;177}178179@BeanProperty(hidden = true)180public void setValue(int value) {181this.value = value;182}183}184185public static class P {186private int value;187188public int getValue() {189return this.value;190}191192@BeanProperty(preferred = true)193public void setValue(int value) {194this.value = value;195}196}197198public static class R {199private int value;200201public int getValue() {202return this.value;203}204205@BeanProperty(required = true)206public void setValue(int value) {207this.value = value;208}209}210211public static class VU {212private int value;213214public int getValue() {215return this.value;216}217218@BeanProperty(visualUpdate = true)219public void setValue(int value) {220this.value = value;221}222}223224public static class D {225private int value;226227@BeanProperty(description = "getter")228public int getValue() {229return this.value;230}231232@BeanProperty(description = "setter")233public void setValue(int value) {234this.value = value;235}236}237238public static class EVD {239240private int value;241242public int getValue() {243return value;244}245246@BeanProperty()247public void setValue(int value) {248this.value = value;249}250}251252public static class EVE {253254private int value;255256public int getValue() {257return value;258}259260@BeanProperty(enumerationValues = {})261public void setValue(int value) {262this.value = value;263}264}265266public static class EV {267private int value;268269public int getValue() {270return this.value;271}272273@BeanProperty(enumerationValues = {274"javax.swing.SwingConstants.LEFT",275"javax.swing.SwingConstants.RIGHT"})276public void setValue(int value) {277this.value = value;278}279}280281public static class EVL {282private int value;283284public int getValue() {285return this.value;286}287288@BeanProperty(enumerationValues = {"ZERO", "ONE"})289public void setValue(int value) {290this.value = value;291}292293public static int ZERO = 0;294public static int ONE = 1;295}296297public static class EVX {298private int value;299300public int getValue() {301return this.value;302}303304@BeanProperty(enumerationValues = {305"X.ZERO",306"X.ONE"})307public void setValue(int value) {308this.value = value;309}310}311312public static interface X {313int ZERO = 0;314int ONE = 1;315}316}317318319