Path: blob/master/test/jdk/java/beans/Introspector/4058433/TestJavaBean.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.awt.event.ActionListener;24import java.beans.BeanDescriptor;25import java.beans.BeanInfo;26import java.beans.Introspector;27import java.beans.JavaBean;2829/*30* @test31* @bug 405843332* @summary Tests the JavaBean annotation33* @author Sergey Malenkov34*/35public class TestJavaBean {3637static final String DSCR = "description";38static final String PRP = "value";39static final String ACT = "action";4041public static void main(final String[] args) throws Exception {42test(X.class, "TestJavaBean$X", "TestJavaBean$X", null, null);43test(D.class, "TestJavaBean$D", DSCR, null, null);44test(DP.class, "TestJavaBean$DP", "TestJavaBean$DP", PRP, null);45test(DES.class, "TestJavaBean$DES", "TestJavaBean$DES", null, ACT);46test(DDP.class, "TestJavaBean$DDP", DSCR, PRP, null);47test(DDES.class, "TestJavaBean$DDES", DSCR, null, ACT);48test(DPDES.class, "TestJavaBean$DPDES", "TestJavaBean$DPDES", PRP, ACT);49test(DDPDES.class, "TestJavaBean$DDPDES", DSCR, PRP, ACT);50}5152private static void test(Class<?> type, String name, String descr,53String prop, String event) throws Exception {54BeanInfo info = Introspector.getBeanInfo(type);55BeanDescriptor bd = info.getBeanDescriptor();5657if (!bd.getName().equals(name)) {58throw new Error("unexpected name of the bean");59}6061if (!bd.getShortDescription().equals(descr)) {62throw new Error("unexpected description of the bean");63}6465int dp = info.getDefaultPropertyIndex();66if (dp < 0 && prop != null) {67throw new Error("unexpected index of the default property");68}69if (dp >= 0) {70if (!info.getPropertyDescriptors()[dp].getName().equals(prop)) {71throw new Error("unexpected default property");72}73}74int des = info.getDefaultEventIndex();75if (des < 0 && event != null) {76throw new Error("unexpected index of the default event set");77}78if (des >= 0) {79if (!info.getEventSetDescriptors()[des].getName().equals(event)) {80throw new Error("unexpected default event set");81}82}83}8485public static class X {86}8788@JavaBean(description = DSCR)89public static class D {90}9192@JavaBean(defaultProperty = PRP)93public static class DP {94private int value;9596public int getValue() {97return this.value;98}99100public void setValue(int value) {101this.value = value;102}103}104105@JavaBean(defaultEventSet = ACT)106public static class DES {107public void addActionListener(ActionListener listener) {108}109110public void removeActionListener(ActionListener listener) {111}112}113114@JavaBean(description = DSCR, defaultProperty = PRP)115public static class DDP {116private int value;117118public int getValue() {119return this.value;120}121122public void setValue(int value) {123this.value = value;124}125}126127@JavaBean(description = DSCR, defaultEventSet = ACT)128public static class DDES {129public void addActionListener(ActionListener listener) {130}131132public void removeActionListener(ActionListener listener) {133}134}135136@JavaBean(defaultProperty = PRP, defaultEventSet = ACT)137public static class DPDES {138private int value;139140public int getValue() {141return this.value;142}143144public void setValue(int value) {145this.value = value;146}147148public void addActionListener(ActionListener listener) {149}150151public void removeActionListener(ActionListener listener) {152}153}154155@JavaBean(description = DSCR, defaultProperty = PRP, defaultEventSet = ACT)156public static class DDPDES {157private int value;158159public int getValue() {160return this.value;161}162163public void setValue(int value) {164this.value = value;165}166167public void addActionListener(ActionListener listener) {168}169170public void removeActionListener(ActionListener listener) {171}172}173}174175176