1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6175517 6278707
  27  * @summary Test that ambiguous ConstructorProperties annotations are detected.
  28  * @author Eamonn McManus
  29  * @modules java.desktop
  30  *          java.management
  31  * @run clean AmbiguousConstructorTest
  32  * @run build AmbiguousConstructorTest
  33  * @run main AmbiguousConstructorTest
  34  */
  35 
  36 import java.beans.ConstructorProperties;
  37 import java.io.InvalidObjectException;
  38 import javax.management.*;
  39 
  40 public class AmbiguousConstructorTest {
  41     public static void main(String[] args) throws Exception {
  42         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  43 
  44         System.out.println("Unambiguous case:");
  45         ObjectName unambigName = new ObjectName("d:type=Unambiguous");
  46         mbs.registerMBean(new UnambiguousImpl(), unambigName);
  47         System.out.println("...OK");
  48 
  49         System.out.println("Ambiguous case:");
  50         ObjectName ambigName = new ObjectName("d:type=Ambiguous");
  51         boolean exception = false;
  52         try {
  53             mbs.registerMBean(new AmbiguousImpl(), ambigName);
  54         } catch (Exception e) {
  55             // TODO - check for the specific exception we should get.
  56             // Currently exception happens in preRegister so we have
  57             // RuntimeMBeanException -> IllegalArgumentException ->
  58             // InvalidObjectException, where the IllegalArgumentException
  59             // is thrown by MXSupport.MBeanDispatcher.visitAttribute when
  60             // it calls ConvertingMethod.checkCallFromOpen
  61             System.out.println("...OK, got expected exception:");
  62             e.printStackTrace(System.out);
  63             exception = true;
  64         }
  65         if (!exception) {
  66             System.out.println("TEST FAILED: expected exception, got none");
  67             throw new Exception("Did not get expected exception");
  68         }
  69 
  70         System.out.println("TEST PASSED");
  71     }
  72 
  73     public static class Unambiguous {
  74         public byte getA() {return 0;}
  75         public short getB() {return 0;}
  76         public int getC() {return 0;}
  77         public long getD() {return 0;}
  78 
  79         @ConstructorProperties({"a", "b"})
  80         public Unambiguous(byte a, short b) {}
  81 
  82         @ConstructorProperties({"b", "c"})
  83         public Unambiguous(short b, int c) {}
  84 
  85         @ConstructorProperties({"a", "b", "c"})
  86         public Unambiguous(byte a, short b, int c) {}
  87     }
  88 
  89     public static class Ambiguous {
  90         public byte getA() {return 0;}
  91         public short getB() {return 0;}
  92         public int getC() {return 0;}
  93         public long getD() {return 0;}
  94 
  95         @ConstructorProperties({"a", "b"})
  96         public Ambiguous(byte a, short b) {}
  97 
  98         @ConstructorProperties({"b", "c"})
  99         public Ambiguous(short b, int c) {}
 100 
 101         @ConstructorProperties({"a", "b", "c", "d"})
 102         public Ambiguous(byte a, short b, int c, long d) {}
 103     }
 104 
 105     public static interface UnambiguousMXBean {
 106         public void setUnambiguous(Unambiguous x);
 107     }
 108 
 109     public static class UnambiguousImpl implements UnambiguousMXBean {
 110         public void setUnambiguous(Unambiguous x) {}
 111     }
 112 
 113     public static interface AmbiguousMXBean {
 114         public void setAmbiguous(Ambiguous x);
 115     }
 116 
 117     public static class AmbiguousImpl implements AmbiguousMXBean {
 118         public void setAmbiguous(Ambiguous x) {}
 119     }
 120 }