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