Path: blob/master/test/jdk/java/beans/XMLDecoder/spec/TestMethod.java
41155 views
/*1* Copyright (c) 2008, 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* @summary Tests <method> element26* @run main/othervm -Djava.security.manager=allow TestMethod27* @author Sergey Malenkov28*/2930import java.beans.XMLDecoder;3132public final class TestMethod extends AbstractTest {33public static final String XML34= "<java>\n"35+ " <new class=\"TestMethod$A\">\n"36+ " <method name=\"m\">\n"37+ " <new class=\"TestMethod$Y\"/>\n"38+ " <new class=\"TestMethod$Y\"/>\n"39+ " </method>\n"40+ " </new>\n"41+ " <new class=\"TestMethod$B\">\n"42+ " <method name=\"m\">\n"43+ " <new class=\"TestMethod$Y\"/>\n"44+ " <new class=\"TestMethod$Y\"/>\n"45+ " </method>\n"46+ " </new>\n"47+ " <new class=\"TestMethod$C\">\n"48+ " <method name=\"m\">\n"49+ " <new class=\"TestMethod$Z\"/>\n"50+ " <new class=\"TestMethod$Z\"/>\n"51+ " </method>\n"52+ " </new>\n"53+ " <new class=\"TestMethod$D\">\n"54+ " <method name=\"m\">\n"55+ " <new class=\"TestMethod$Z\"/>\n"56+ " <new class=\"TestMethod$Z\"/>\n"57+ " </method>\n"58+ " </new>\n"59+ " <new class=\"TestMethod$E\">\n"60+ " <method name=\"m\">\n"61+ " <new class=\"TestMethod$Z\"/>\n"62+ " <new class=\"TestMethod$Z\"/>\n"63+ " </method>\n"64+ " </new>\n"65+ "</java>";6667public static void main(String[] args) {68new TestMethod().test(true);69}7071private NoSuchMethodException exception;7273@Override74public void exceptionThrown(Exception exception) {75if (this.exception != null) {76// only one exception allowed77super.exceptionThrown(exception);78} else if (exception instanceof NoSuchMethodException) {79// expected exception: ambiguous methods are found80this.exception = (NoSuchMethodException) exception;81} else {82super.exceptionThrown(exception);83}84}8586@Override87protected void validate(XMLDecoder decoder) {88this.exception = null;89validate(decoder, A.class);90validate(decoder, B.class);91validate(decoder, C.class);92validate(decoder, D.class);93validate(decoder, E.class);94if (this.exception == null) {95throw new Error("NoSuchMethodException expected");96}97}9899private static void validate(XMLDecoder decoder, Class type) {100if (!type.equals(decoder.readObject().getClass())) {101throw new Error("unexpected class");102}103}104105/**106* All ambiguous method declarations should fail.107*/108public static class A {109public void m(X x1, X x2) {110throw new Error("A.m(X,X) should not be called");111}112113public void m(X x1, Y y2) {114throw new Error("A.m(X,Y) should not be called");115}116117public void m(Y y1, X x2) {118throw new Error("A.m(Y,X) should not be called");119}120}121122/**123* The most specific method in this case would be the second declaration.124*/125public static class B {126public void m(X x1, X x2) {127throw new Error("B.m(X,X) should not be called");128}129130public void m(X x1, Y y2) {131// expected: B.m(X,Y) should be called132}133}134135/**136* The most specific method in this case would be the first declaration.137*/138public static class C {139public void m(Y y1, Y y2) {140// expected: C.m(Y,Y) should be called141}142143public void m(X x1, X x2) {144throw new Error("C.m(X,X) should not be called");145}146}147148/**149* Same as the previous case but flip methods.150*/151public static class D {152public void m(X x1, X x2) {153throw new Error("D.m(X,X) should not be called");154}155156public void m(Y y1, Y y2) {157// expected: D.m(Y,Y) should be called158}159}160161/**162* The method should be called with (Z,Z).163*/164public static class E {165public void m(X x1, X x2) {166// expected: E.m(X,X) should be called167}168}169170public static class X {171}172173public static class Y extends X {174}175176public static class Z extends Y {177}178}179180181