Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLDecoder/spec/TestMethod.java
38821 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* @author Sergey Malenkov27*/2829import java.beans.XMLDecoder;3031public final class TestMethod extends AbstractTest {32public static final String XML33= "<java>\n"34+ " <new class=\"TestMethod$A\">\n"35+ " <method name=\"m\">\n"36+ " <new class=\"TestMethod$Y\"/>\n"37+ " <new class=\"TestMethod$Y\"/>\n"38+ " </method>\n"39+ " </new>\n"40+ " <new class=\"TestMethod$B\">\n"41+ " <method name=\"m\">\n"42+ " <new class=\"TestMethod$Y\"/>\n"43+ " <new class=\"TestMethod$Y\"/>\n"44+ " </method>\n"45+ " </new>\n"46+ " <new class=\"TestMethod$C\">\n"47+ " <method name=\"m\">\n"48+ " <new class=\"TestMethod$Z\"/>\n"49+ " <new class=\"TestMethod$Z\"/>\n"50+ " </method>\n"51+ " </new>\n"52+ " <new class=\"TestMethod$D\">\n"53+ " <method name=\"m\">\n"54+ " <new class=\"TestMethod$Z\"/>\n"55+ " <new class=\"TestMethod$Z\"/>\n"56+ " </method>\n"57+ " </new>\n"58+ " <new class=\"TestMethod$E\">\n"59+ " <method name=\"m\">\n"60+ " <new class=\"TestMethod$Z\"/>\n"61+ " <new class=\"TestMethod$Z\"/>\n"62+ " </method>\n"63+ " </new>\n"64+ "</java>";6566public static void main(String[] args) {67new TestMethod().test(true);68}6970private NoSuchMethodException exception;7172@Override73public void exceptionThrown(Exception exception) {74if (this.exception != null) {75// only one exception allowed76super.exceptionThrown(exception);77} else if (exception instanceof NoSuchMethodException) {78// expected exception: ambiguous methods are found79this.exception = (NoSuchMethodException) exception;80} else {81super.exceptionThrown(exception);82}83}8485@Override86protected void validate(XMLDecoder decoder) {87this.exception = null;88validate(decoder, A.class);89validate(decoder, B.class);90validate(decoder, C.class);91validate(decoder, D.class);92validate(decoder, E.class);93if (this.exception == null) {94throw new Error("NoSuchMethodException expected");95}96}9798private static void validate(XMLDecoder decoder, Class type) {99if (!type.equals(decoder.readObject().getClass())) {100throw new Error("unexpected class");101}102}103104/**105* All ambiguous method declarations should fail.106*/107public static class A {108public void m(X x1, X x2) {109throw new Error("A.m(X,X) should not be called");110}111112public void m(X x1, Y y2) {113throw new Error("A.m(X,Y) should not be called");114}115116public void m(Y y1, X x2) {117throw new Error("A.m(Y,X) should not be called");118}119}120121/**122* The most specific method in this case would be the second declaration.123*/124public static class B {125public void m(X x1, X x2) {126throw new Error("B.m(X,X) should not be called");127}128129public void m(X x1, Y y2) {130// expected: B.m(X,Y) should be called131}132}133134/**135* The most specific method in this case would be the first declaration.136*/137public static class C {138public void m(Y y1, Y y2) {139// expected: C.m(Y,Y) should be called140}141142public void m(X x1, X x2) {143throw new Error("C.m(X,X) should not be called");144}145}146147/**148* Same as the previous case but flip methods.149*/150public static class D {151public void m(X x1, X x2) {152throw new Error("D.m(X,X) should not be called");153}154155public void m(Y y1, Y y2) {156// expected: D.m(Y,Y) should be called157}158}159160/**161* The method should be called with (Z,Z).162*/163public static class E {164public void m(X x1, X x2) {165// expected: E.m(X,X) should be called166}167}168169public static class X {170}171172public static class Y extends X {173}174175public static class Z extends Y {176}177}178179180