test/jdk/java/beans/Introspector/Test4274639.java
author fyang
Mon, 04 Nov 2019 16:10:39 +0800
changeset 59307 fc216dcef2bb
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233466: aarch64: remove unnecessary load of mdo when profiling return and parameters type Reviewed-by: adinn
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 4274639 4305280
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests PropertyDescriptor/PropertyEditorSupport improvements
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.beans.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.PropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.PropertyEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.PropertyEditorSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.SimpleBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Test4274639 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static String STRING_PROPERTY = "string";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static String INTEGER_PROPERTY = "integer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static String STRING_VALUE = "Test Text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static Integer INTEGER_VALUE = 261074;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        TestBean bean = new TestBean(STRING_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (!STRING_VALUE.equals(bean.getString()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            throw new Error("unexpected string property: " + bean.getString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        boolean string = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        boolean integer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(bean.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            String name = pd.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println(" - " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (name.equals(STRING_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                // This tests createPropertyEditor such that the PropertyEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                // returned will have the bean as the source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                Class type = pd.getPropertyEditorClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                if (!StringEditor.class.equals(type))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    throw new Error("unexpected property editor type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                PropertyEditor editor = pd.createPropertyEditor(bean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if (editor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    throw new Error("property editor cannot be created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                if (STRING_VALUE != editor.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    throw new Error("unexpected value: " + editor.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                Object source = ((PropertyEditorSupport) editor).getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (source != bean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    throw new Error("unexpected source: " + source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                string = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (name.equals(INTEGER_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                // This tests createPropertyEditor such that the PropertyEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                // returned will be just a new instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                Class type = pd.getPropertyEditorClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (!IntegerEditor.class.equals(type))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    throw new Error("unexpected property editor type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                PropertyEditor editor = pd.createPropertyEditor(bean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (editor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    throw new Error("property editor cannot be created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (INTEGER_VALUE != editor.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    throw new Error("unexpected value: " + editor.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                Object source = ((PropertyEditorSupport) editor).getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (source != editor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    throw new Error("unexpected source: " + source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                integer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new Error("string property is not tested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (!integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new Error("integer property is not tested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final class TestBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        private int integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public TestBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.string = "default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.integer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public TestBean(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            setString(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return this.string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public void setString(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            this.string = string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public int getInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return this.integer;
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 setInteger(int integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            this.integer = integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static final class TestBeanBeanInfo extends SimpleBeanInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public PropertyDescriptor[] getPropertyDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            PropertyDescriptor[] pds = new PropertyDescriptor[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                pds[0] = new PropertyDescriptor(STRING_PROPERTY, TestBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                pds[0].setPropertyEditorClass(StringEditor.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                pds[1] = new PropertyDescriptor(INTEGER_PROPERTY, TestBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                pds[1].setPropertyEditorClass(IntegerEditor.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            catch (IntrospectionException exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new Error("unexpected error", exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return pds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Public constructor was added for 4305280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static final class StringEditor extends PropertyEditorSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public StringEditor(Object source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (source instanceof TestBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                TestBean test = (TestBean) source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                setValue(test.getString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // Will use the default public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // that uses this property editor as the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static final class IntegerEditor extends PropertyEditorSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return INTEGER_VALUE; // default value is hard coded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}