jdk/test/java/lang/management/CompositeData/MemoryUsageCompositeData.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2004 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  * @bug     4982289
       
    27  * @summary Test MemoryUsage.from() method to return a valid MemoryUsage
       
    28  *          or throw exception if the input CompositeData is invalid.
       
    29  * @author  Mandy Chung
       
    30  *
       
    31  * @build MemoryUsageCompositeData
       
    32  * @run main MemoryUsageCompositeData
       
    33  */
       
    34 
       
    35 import javax.management.openmbean.*;
       
    36 import java.lang.management.MemoryUsage;
       
    37 
       
    38 public class MemoryUsageCompositeData {
       
    39     public static void main(String[] argv) throws Exception {
       
    40         createGoodCompositeData();
       
    41         badTypeCompositeData();
       
    42         badNameCompositeData();
       
    43         System.out.println("Test passed");
       
    44     }
       
    45 
       
    46     public static void createGoodCompositeData() throws Exception {
       
    47         final int K = 1024;
       
    48         // these values are synchronized with the item names
       
    49         final Object[] values = {
       
    50             new Long(5 * K),  // committed
       
    51             new Long(1 * K),  // init
       
    52             new Long(10 * K), // max
       
    53             new Long(2 * K),  // used
       
    54             "Dummy",
       
    55             "Dummy",
       
    56         };
       
    57 
       
    58         CompositeType muct =
       
    59             new CompositeType("MyMemoryUsageCompositeType",
       
    60                               "CompositeType for MemoryUsage",
       
    61                               memoryUsageItemNames,
       
    62                               memoryUsageItemNames,
       
    63                               memoryUsageItemTypes);
       
    64         CompositeData cd =
       
    65             new CompositeDataSupport(muct,
       
    66                                      memoryUsageItemNames,
       
    67                                      values);
       
    68         MemoryUsage u = MemoryUsage.from(cd);
       
    69         if (u.getInit() != ((Long) values[INIT]).longValue()) {
       
    70             throw new RuntimeException("init = " + u.getInit() +
       
    71                " expected = " + values[INIT]);
       
    72         }
       
    73         if (u.getUsed() != ((Long) values[USED]).longValue()) {
       
    74             throw new RuntimeException("used = " + u.getUsed() +
       
    75                " expected = " + values[USED]);
       
    76         }
       
    77         if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
       
    78             throw new RuntimeException("committed = " + u.getCommitted() +
       
    79                " expected = " + values[COMMITTED]);
       
    80         }
       
    81         if (u.getMax() != ((Long) values[MAX]).longValue()) {
       
    82             throw new RuntimeException("max = " + u.getMax() +
       
    83                " expected = " + values[MAX]);
       
    84         }
       
    85         System.out.println(u);
       
    86     }
       
    87 
       
    88     public static void badTypeCompositeData() throws Exception {
       
    89         final int K = 1024;
       
    90         final Object[] values = {
       
    91             new Integer(5 * K),
       
    92             new Long(1 * K),
       
    93             new Long(10 * K),
       
    94             new Long(2 * K),
       
    95             "Dummy",
       
    96             "Dummy",
       
    97         };
       
    98 
       
    99         CompositeType muct =
       
   100             new CompositeType("MyMemoryUsageCompositeType",
       
   101                               "CompositeType for MemoryUsage",
       
   102                               memoryUsageItemNames,
       
   103                               memoryUsageItemNames,
       
   104                               badMUItemTypes);
       
   105         CompositeData cd =
       
   106            new CompositeDataSupport(muct,
       
   107                                     memoryUsageItemNames,
       
   108                                     values);
       
   109         try {
       
   110             MemoryUsage u = MemoryUsage.from(cd);
       
   111         } catch (IllegalArgumentException e) {
       
   112             System.out.println("Expected exception: " +
       
   113                 e.getMessage());
       
   114             return;
       
   115         }
       
   116         throw new RuntimeException(
       
   117             "IllegalArgumentException not thrown");
       
   118     }
       
   119 
       
   120     public static void badNameCompositeData() throws Exception {
       
   121         final int K = 1024;
       
   122         final Object[] values = {
       
   123             new Long(5 * K),
       
   124             new Long(1 * K),
       
   125             new Long(10 * K),
       
   126             new Long(2 * K),
       
   127             "Dummy",
       
   128             "Dummy",
       
   129         };
       
   130 
       
   131         CompositeType muct =
       
   132             new CompositeType("MyMemoryUsageCompositeType",
       
   133                               "CompositeType for MemoryUsage",
       
   134                                badMUItemNames,
       
   135                                badMUItemNames,
       
   136                                memoryUsageItemTypes);
       
   137         CompositeData cd =
       
   138             new CompositeDataSupport(muct,
       
   139                                      badMUItemNames,
       
   140                                      values);
       
   141         try {
       
   142             MemoryUsage u = MemoryUsage.from(cd);
       
   143         } catch (IllegalArgumentException e) {
       
   144             System.out.println("Expected exception: " +
       
   145                 e.getMessage());
       
   146             return;
       
   147         }
       
   148         throw new RuntimeException(
       
   149             "IllegalArgumentException not thrown");
       
   150     }
       
   151 
       
   152     private static final int COMMITTED = 0;
       
   153     private static final int INIT      = 1;
       
   154     private static final int MAX       = 2;
       
   155     private static final int USED      = 3;
       
   156     private static final String[] memoryUsageItemNames = {
       
   157         "committed",
       
   158         "init",
       
   159         "max",
       
   160         "used",
       
   161         "dummy1",
       
   162         "dummy2",
       
   163     };
       
   164     private static final OpenType[] memoryUsageItemTypes = {
       
   165         SimpleType.LONG,
       
   166         SimpleType.LONG,
       
   167         SimpleType.LONG,
       
   168         SimpleType.LONG,
       
   169         SimpleType.STRING,
       
   170         SimpleType.STRING,
       
   171     };
       
   172     private static final String[] badMUItemNames = {
       
   173         "Committed",
       
   174         "Init",
       
   175         "max",
       
   176         "used",
       
   177         "dummy1",
       
   178         "dummy2",
       
   179     };
       
   180     private static final OpenType[] badMUItemTypes = {
       
   181         SimpleType.INTEGER,
       
   182         SimpleType.LONG,
       
   183         SimpleType.LONG,
       
   184         SimpleType.LONG,
       
   185         SimpleType.STRING,
       
   186         SimpleType.STRING,
       
   187     };
       
   188 }