Path: blob/master/test/jdk/java/beans/Introspector/Test4274639.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 4274639 430528026* @summary Tests PropertyDescriptor/PropertyEditorSupport improvements27* @author Mark Davidson28*/2930import java.beans.IntrospectionException;31import java.beans.PropertyDescriptor;32import java.beans.PropertyEditor;33import java.beans.PropertyEditorSupport;34import java.beans.SimpleBeanInfo;3536public class Test4274639 {37private static String STRING_PROPERTY = "string";38private static String INTEGER_PROPERTY = "integer";3940private static String STRING_VALUE = "Test Text";41private static Integer INTEGER_VALUE = 261074;4243public static void main(String[] args) {44TestBean bean = new TestBean(STRING_VALUE);45if (!STRING_VALUE.equals(bean.getString()))46throw new Error("unexpected string property: " + bean.getString());4748boolean string = false;49boolean integer = false;50for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(bean.getClass())) {51String name = pd.getName();52System.out.println(" - " + name);53if (name.equals(STRING_PROPERTY)) {54// This tests createPropertyEditor such that the PropertyEditor55// returned will have the bean as the source object.56Class type = pd.getPropertyEditorClass();57if (!StringEditor.class.equals(type))58throw new Error("unexpected property editor type: " + type);5960PropertyEditor editor = pd.createPropertyEditor(bean);61if (editor == null)62throw new Error("property editor cannot be created");6364if (STRING_VALUE != editor.getValue())65throw new Error("unexpected value: " + editor.getValue());6667Object source = ((PropertyEditorSupport) editor).getSource();68if (source != bean)69throw new Error("unexpected source: " + source);7071string = true;72}73if (name.equals(INTEGER_PROPERTY)) {74// This tests createPropertyEditor such that the PropertyEditor75// returned will be just a new instance76Class type = pd.getPropertyEditorClass();77if (!IntegerEditor.class.equals(type))78throw new Error("unexpected property editor type: " + type);7980PropertyEditor editor = pd.createPropertyEditor(bean);81if (editor == null)82throw new Error("property editor cannot be created");8384if (INTEGER_VALUE != editor.getValue())85throw new Error("unexpected value: " + editor.getValue());8687Object source = ((PropertyEditorSupport) editor).getSource();88if (source != editor)89throw new Error("unexpected source: " + source);9091integer = true;92}93}94if (!string)95throw new Error("string property is not tested");9697if (!integer)98throw new Error("integer property is not tested");99}100101public static final class TestBean {102private String string;103private int integer;104105public TestBean() {106this.string = "default";107this.integer = 0;108}109110public TestBean(String string) {111setString(string);112}113114public String getString() {115return this.string;116}117118public void setString(String string) {119this.string = string;120}121122public int getInteger() {123return this.integer;124}125126public void setInteger(int integer) {127this.integer = integer;128}129}130131public static final class TestBeanBeanInfo extends SimpleBeanInfo {132public PropertyDescriptor[] getPropertyDescriptors() {133PropertyDescriptor[] pds = new PropertyDescriptor[2];134try {135pds[0] = new PropertyDescriptor(STRING_PROPERTY, TestBean.class);136pds[0].setPropertyEditorClass(StringEditor.class);137138pds[1] = new PropertyDescriptor(INTEGER_PROPERTY, TestBean.class);139pds[1].setPropertyEditorClass(IntegerEditor.class);140141}142catch (IntrospectionException exception) {143throw new Error("unexpected error", exception);144}145return pds;146}147}148149// Public constructor was added for 4305280150public static final class StringEditor extends PropertyEditorSupport {151public StringEditor(Object source) {152super(source);153154if (source instanceof TestBean) {155TestBean test = (TestBean) source;156setValue(test.getString());157}158}159}160161// Will use the default public constructor162// that uses this property editor as the source.163public static final class IntegerEditor extends PropertyEditorSupport {164public Object getValue() {165return INTEGER_VALUE; // default value is hard coded166}167}168}169170171