Path: blob/master/test/jdk/java/beans/Introspector/Test4619536.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 461953626* @summary Tests resolving the ambiguities in the resolution of IndexedPropertyDescriptors27* @author Mark Davidson28*/2930import java.beans.IndexedPropertyDescriptor;31import java.beans.PropertyDescriptor;32import java.util.Date;3334public class Test4619536 {35public static void main(String[] args) throws Exception {36IndexedPropertyDescriptor ipd = BeanUtils.getIndexedPropertyDescriptor(A.class, "foo");37if (!ipd.getIndexedPropertyType().equals(String.class)) {38error(ipd, "A.foo should be String type");39}40PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(B.class, "foo");41if (pd instanceof IndexedPropertyDescriptor) {42error(pd, "B.foo should not be an indexed property");43}44if (!pd.getPropertyType().equals(Date.class)) {45error(pd, "B.foo should be Date type");46}47pd = BeanUtils.findPropertyDescriptor(Child.class, "foo");48if (pd instanceof IndexedPropertyDescriptor) {49error(pd, "Child.foo should not be an indexed property");50}51pd = BeanUtils.findPropertyDescriptor(Classic.class, "foo");52if (pd instanceof IndexedPropertyDescriptor) {53error(pd, "Classic.foo should not be an indexed property");54}55ipd = BeanUtils.getIndexedPropertyDescriptor(Index.class, "foo");56if (!hasIPD(ipd)) {57error(pd, "Index.foo should have ipd values");58}59if (hasPD(ipd)) {60error(ipd, "Index.foo should not have pd values");61}62ipd = BeanUtils.getIndexedPropertyDescriptor(All.class, "foo");63if (!hasPD(ipd) || !hasIPD(ipd)) {64error(ipd, "All.foo should have all pd/ipd values");65}66if (!isValidType(ipd)) {67error(ipd, "All.foo pdType should equal ipdType");68}69ipd = BeanUtils.getIndexedPropertyDescriptor(Getter.class, "foo");70if (ipd.getReadMethod() == null || ipd.getWriteMethod() != null) {71error(ipd, "Getter.foo classic methods incorrect");72}73if (!isValidType(ipd)) {74error(ipd, "Getter.foo pdType should equal ipdType");75}76ipd = BeanUtils.getIndexedPropertyDescriptor(BadGetter.class, "foo");77if (hasPD(ipd)) {78error(ipd, "BadGetter.foo should not have classic methods");79}80ipd = BeanUtils.getIndexedPropertyDescriptor(Setter.class, "foo");81if (ipd.getReadMethod() != null || ipd.getWriteMethod() == null) {82error(ipd, "Setter.foo classic methods incorrect");83}84if (!isValidType(ipd)) {85error(ipd, "Setter.foo pdType should equal ipdType");86}87ipd = BeanUtils.getIndexedPropertyDescriptor(BadSetter.class, "foo");88if (hasPD(ipd)) {89error(ipd, "BadSetter.foo should not have classic methods");90}91}9293public static boolean hasPD(PropertyDescriptor pd) {94if (null == pd.getPropertyType()) {95return false;96}97return (null != pd.getReadMethod())98|| (null != pd.getWriteMethod());99}100101public static boolean hasIPD(IndexedPropertyDescriptor ipd) {102if (null == ipd.getIndexedPropertyType()) {103return false;104}105return (null != ipd.getIndexedReadMethod())106|| (null != ipd.getIndexedWriteMethod());107}108109public static boolean isValidType(IndexedPropertyDescriptor ipd) {110Class type = ipd.getPropertyType();111return type.isArray() && type.getComponentType().equals(ipd.getIndexedPropertyType());112}113114public static void error(PropertyDescriptor pd, String message) {115BeanUtils.reportPropertyDescriptor(pd);116throw new Error(message);117}118119// Test case from 4619536120public static class A {121// prop foo on A should be indexed of type String122public String getFoo(int x) {123return null;124}125}126127public static class B extends A {128// prop foo on should be non-indexed of type Date129public Date getFoo() {130return null;131}132}133134// Test case from 4812428 (this works in 1.5.0)135public static class Parent {136public void setFoo(String foo) {137}138139public Child getFoo(int index) {140return null;141}142}143144public static class Child extends Parent {145public Child getFoo() {146return null;147}148}149150// This class has a complete set of pd151public static class Classic {152public String[] getFoo() {153return null;154}155156public void setFoo(String[] foo) {157}158}159160// This class has a complete set of ipd161public static class Index {162public String getFoo(int i) {163return null;164}165166public void setFoo(int i, String f) {167}168}169170// This class adds a proper getter and setter171public static class All extends Index {172public String[] getFoo() {173return null;174}175176public void setFoo(String[] foo) {177}178}179180// This class adds a classic getter181public static class Getter extends Index {182public String[] getFoo() {183return null;184}185}186187// This class has an alternate getter and should be merged188public static class BadGetter extends Index {189public String getFoo() {190return null;191}192}193194// This class adds a classic setter195public static class Setter extends Index {196public void setFoo(String[] f) {197}198}199200// This class has an alternate setter and should be merged201public static class BadSetter extends Index {202public void setFoo(String f) {203}204}205}206207208