jdk/test/java/beans/XMLDecoder/spec/TestMethod.java
changeset 1844 ac2cf8242428
child 5506 202f599c92aa
equal deleted inserted replaced
1843:267cc4de4221 1844:ac2cf8242428
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @summary Tests <method> element
       
    27  * @author Sergey Malenkov
       
    28  */
       
    29 
       
    30 import java.beans.XMLDecoder;
       
    31 
       
    32 public final class TestMethod extends AbstractTest {
       
    33     public static final String XML
       
    34             = "<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>";
       
    66 
       
    67     public static void main(String[] args) {
       
    68         new TestMethod().test(true);
       
    69     }
       
    70 
       
    71     private NoSuchMethodException exception;
       
    72 
       
    73     @Override
       
    74     public void exceptionThrown(Exception exception) {
       
    75         if (this.exception != null) {
       
    76             // only one exception allowed
       
    77             super.exceptionThrown(exception);
       
    78         } else if (exception instanceof NoSuchMethodException) {
       
    79             // expected exception: ambiguous methods are found
       
    80             this.exception = (NoSuchMethodException) exception;
       
    81         } else {
       
    82             super.exceptionThrown(exception);
       
    83         }
       
    84     }
       
    85 
       
    86     @Override
       
    87     protected void validate(XMLDecoder decoder) {
       
    88         this.exception = null;
       
    89         validate(decoder, A.class);
       
    90         validate(decoder, B.class);
       
    91         validate(decoder, C.class);
       
    92         validate(decoder, D.class);
       
    93         validate(decoder, E.class);
       
    94         if (this.exception == null) {
       
    95             throw new Error("NoSuchMethodException expected");
       
    96         }
       
    97     }
       
    98 
       
    99     private static void validate(XMLDecoder decoder, Class type) {
       
   100         if (!type.equals(decoder.readObject().getClass())) {
       
   101             throw new Error("unexpected class");
       
   102         }
       
   103     }
       
   104 
       
   105     /**
       
   106      * All ambiguous method declarations should fail.
       
   107      */
       
   108     public static class A {
       
   109         public void m(X x1, X x2) {
       
   110             throw new Error("A.m(X,X) should not be called");
       
   111         }
       
   112 
       
   113         public void m(X x1, Y y2) {
       
   114             throw new Error("A.m(X,Y) should not be called");
       
   115         }
       
   116 
       
   117         public void m(Y y1, X x2) {
       
   118             throw new Error("A.m(Y,X) should not be called");
       
   119         }
       
   120     }
       
   121 
       
   122     /**
       
   123      * The most specific method in this case would be the second declaration.
       
   124      */
       
   125     public static class B {
       
   126         public void m(X x1, X x2) {
       
   127             throw new Error("B.m(X,X) should not be called");
       
   128         }
       
   129 
       
   130         public void m(X x1, Y y2) {
       
   131             // expected: B.m(X,Y) should be called
       
   132         }
       
   133     }
       
   134 
       
   135     /**
       
   136      * The most specific method in this case would be the first declaration.
       
   137      */
       
   138     public static class C {
       
   139         public void m(Y y1, Y y2) {
       
   140             // expected: C.m(Y,Y) should be called
       
   141         }
       
   142 
       
   143         public void m(X x1, X x2) {
       
   144             throw new Error("C.m(X,X) should not be called");
       
   145         }
       
   146     }
       
   147 
       
   148     /**
       
   149      * Same as the previous case but flip methods.
       
   150      */
       
   151     public static class D {
       
   152         public void m(X x1, X x2) {
       
   153             throw new Error("D.m(X,X) should not be called");
       
   154         }
       
   155 
       
   156         public void m(Y y1, Y y2) {
       
   157             // expected: D.m(Y,Y) should be called
       
   158         }
       
   159     }
       
   160 
       
   161     /**
       
   162      * The method should be called with (Z,Z).
       
   163      */
       
   164     public static class E {
       
   165         public void m(X x1, X x2) {
       
   166             // expected: E.m(X,X) should be called
       
   167         }
       
   168     }
       
   169 
       
   170     public static class X {
       
   171     }
       
   172 
       
   173     public static class Y extends X {
       
   174     }
       
   175 
       
   176     public static class Z extends Y {
       
   177     }
       
   178 }