1 /*
   2  * Copyright (c) 2004, 2016, 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     5024531
  27  * @summary Test type mapping of the platform MXBean proxy
  28  *          returned from Management.newPlatformMXBeanProxy().
  29  * @author  Mandy Chung
  30  */
  31 import java.lang.management.*;
  32 import javax.management.*;
  33 import static java.lang.management.ManagementFactory.*;
  34 import java.util.List;
  35 import java.util.Map;
  36 import java.util.Properties;
  37 import com.sun.management.GcInfo;
  38 
  39 public class ProxyExceptions {
  40     private static MBeanServer server =
  41         ManagementFactory.getPlatformMBeanServer();
  42     private static MemoryPoolMXBean heapPool = null;
  43     private static MemoryPoolMXBean nonHeapPool = null;
  44     public static void main(String[] argv) throws Exception {
  45         List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans();
  46         for (MemoryPoolMXBean p : pools) {
  47             MemoryPoolMXBean proxy = newPlatformMXBeanProxy(server,
  48                 MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName(),
  49                 MemoryPoolMXBean.class);
  50             boolean uoeCaught;
  51             if (!p.isUsageThresholdSupported()) {
  52                 try {
  53                     proxy.getUsageThreshold();
  54                     uoeCaught = false;
  55                 } catch (UnsupportedOperationException e) {
  56                     uoeCaught = true;
  57                 }
  58                 if (!uoeCaught) {
  59                     throw new RuntimeException("TEST FAILED: " +
  60                         "UnsupportedOperationException not thrown " +
  61                         "when calling getUsageThreshold on " + p.getName());
  62                 }
  63                 try {
  64                     proxy.setUsageThreshold(100);
  65                     uoeCaught = false;
  66                 } catch (UnsupportedOperationException e) {
  67                     uoeCaught = true;
  68                 }
  69                 if (!uoeCaught) {
  70                     throw new RuntimeException("TEST FAILED: " +
  71                         "UnsupportedOperationException not thrown " +
  72                         "when calling setUsageThreshold on " + p.getName());
  73                 }
  74             }
  75             if (!p.isCollectionUsageThresholdSupported()) {
  76                 try {
  77                     proxy.getCollectionUsageThreshold();
  78                     uoeCaught = false;
  79                 } catch (UnsupportedOperationException e) {
  80                     uoeCaught = true;
  81                 }
  82                 if (!uoeCaught) {
  83                     throw new RuntimeException("TEST FAILED: " +
  84                         "UnsupportedOperationException not thrown " +
  85                         "when calling getCollectionUsageThreshold on " +
  86                         p.getName());
  87                 }
  88                 try {
  89                     proxy.setCollectionUsageThreshold(100);
  90                     uoeCaught = false;
  91                 } catch (UnsupportedOperationException e) {
  92                     uoeCaught = true;
  93                 }
  94                 if (!uoeCaught) {
  95                     throw new RuntimeException("TEST FAILED: " +
  96                         "UnsupportedOperationException not thrown " +
  97                         "when calling getCollectionUsageThreshold on " +
  98                         p.getName());
  99                 }
 100             }
 101         }
 102 
 103         ThreadMXBean thread = newPlatformMXBeanProxy(server,
 104                                                   THREAD_MXBEAN_NAME,
 105                                                   ThreadMXBean.class);
 106         boolean iaeCaught = false;
 107         try {
 108             thread.getThreadInfo(-1);
 109         } catch (IllegalArgumentException e) {
 110             iaeCaught = true;
 111         }
 112         if (!iaeCaught) {
 113             throw new RuntimeException("TEST FAILED: " +
 114                 "IllegalArgumentException not thrown " +
 115                 "when calling getThreadInfo(-1)");
 116         }
 117 
 118         System.out.println("Test passed.");
 119     }
 120 }