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 MemoryNotificationInfo.from to return a valid
  28  *          MemoryNotificationInfo object. Or throw exception if
  29  *          the input CompositeData is invalid.
  30  * @author  Mandy Chung
  31  *
  32  * @compile OpenTypeConverter.java
  33  * @build MemoryNotifInfoCompositeData
  34  * @run main MemoryNotifInfoCompositeData
  35  */
  36 
  37 import javax.management.openmbean.*;
  38 import java.lang.management.MemoryNotificationInfo;
  39 import java.lang.management.MemoryUsage;
  40 import sun.management.MemoryUsageCompositeData;
  41 
  42 public class MemoryNotifInfoCompositeData {
  43     public static void main(String[] argv) throws Exception {
  44         createGoodCompositeData();
  45         badNameCompositeData();
  46         badTypeCompositeData();
  47         System.out.println("Test passed");
  48     }
  49 
  50     private static final int POOL_NAME = 1;
  51     private static final int COUNT     = 2;
  52     private static final int USAGE     = 3;
  53     private static final String[] validItemNames = {
  54         "dummy1",
  55         "poolName",
  56         "count",
  57         "usage",
  58         "dummy2",
  59     };
  60 
  61     // these values are synchronized with the item names
  62     private static final Object[] values = {
  63         "Dummy",
  64         "Foo",
  65         new Long(100),
  66         MemoryUsageCompositeData.
  67             toCompositeData(new MemoryUsage(0, 100, 1000, 5000)),
  68         "Dummy",
  69     };
  70 
  71     public static void createGoodCompositeData() throws Exception {
  72 
  73         // get the CompositeType for MemoryUsage
  74         validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class);
  75         CompositeType ct =
  76             new CompositeType("MyCompositeType",
  77                               "CompositeType for MemoryNotificationInfo",
  78                               validItemNames,
  79                               validItemNames,
  80                               validItemTypes);
  81         CompositeData cd =
  82             new CompositeDataSupport(ct,
  83                                      validItemNames,
  84                                      values);
  85 
  86         MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
  87         if (!info.getPoolName().equals(values[POOL_NAME])) {
  88             throw new RuntimeException("pool name = " + info.getPoolName() +
  89                " expected = " + values[POOL_NAME]);
  90         }
  91         if (info.getCount() != ((Long) values[COUNT]).longValue()) {
  92             throw new RuntimeException("count = " + info.getCount() +
  93                " expected = " + values[COUNT]);
  94         }
  95         if (info.getUsage().getInit() != 0) {
  96             throw new RuntimeException("usage init = " +
  97                info.getUsage().getInit() +
  98                " expected = 0");
  99         }
 100         if (info.getUsage().getUsed() != 100) {
 101             throw new RuntimeException("usage used = " +
 102                info.getUsage().getUsed() +
 103                " expected = 100");
 104         }
 105         if (info.getUsage().getCommitted () != 1000) {
 106             throw new RuntimeException("usage committed = " +
 107                info.getUsage().getCommitted() +
 108                " expected = 1000");
 109         }
 110         if (info.getUsage().getMax() != 5000) {
 111             throw new RuntimeException("usage max = " +
 112                info.getUsage().getMax() +
 113                " expected = 5000");
 114         }
 115         System.out.print("Pool name = " + info.getPoolName());
 116         System.out.println(" Count = " + info.getCount());
 117         System.out.println("Usage = " + info.getUsage());
 118     }
 119 
 120     public static void badNameCompositeData() throws Exception {
 121         CompositeType ct =
 122             new CompositeType("MyCompositeType",
 123                               "CompositeType for MemoryNotificationInfo",
 124                               badItemNames,
 125                               badItemNames,
 126                               validItemTypes);
 127         CompositeData cd =
 128             new CompositeDataSupport(ct,
 129                                      badItemNames,
 130                                      values);
 131 
 132         try {
 133             MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
 134         } catch (IllegalArgumentException e) {
 135             System.out.println("Expected exception: " +
 136                 e.getMessage());
 137             return;
 138         }
 139         throw new RuntimeException(
 140             "IllegalArgumentException not thrown");
 141     }
 142 
 143     public static void badTypeCompositeData() throws Exception {
 144         CompositeType ct =
 145             new CompositeType("MyCompositeType",
 146                               "CompositeType for MemoryNotificationInfo",
 147                               validItemNames,
 148                               validItemNames,
 149                               badItemTypes);
 150 
 151         final Object[] values1 = {
 152             "Dummy",
 153             "Foo",
 154             new Long(100),
 155             "Bad memory usage object",
 156             "Dummy",
 157         };
 158 
 159         CompositeData cd =
 160             new CompositeDataSupport(ct,
 161                                      validItemNames,
 162                                      values1);
 163 
 164         try {
 165             MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
 166         } catch (IllegalArgumentException e) {
 167             System.out.println("Expected exception: " +
 168                 e.getMessage());
 169             return;
 170         }
 171         throw new RuntimeException(
 172             "IllegalArgumentException not thrown");
 173     }
 174 
 175     private static OpenType[] validItemTypes = {
 176         SimpleType.STRING,
 177         SimpleType.STRING,
 178         SimpleType.LONG,
 179         null, // OpenTypeConverter.toOpenType(MemoryUsage.class)
 180         SimpleType.STRING,
 181     };
 182     private static final String[] badItemNames = {
 183         "poolName",
 184         "BadCountName",
 185         "usage",
 186         "dummy1",
 187         "dummy2",
 188     };
 189     private static final OpenType[] badItemTypes = {
 190         SimpleType.STRING,
 191         SimpleType.STRING,
 192         SimpleType.LONG,
 193         SimpleType.STRING, // Bad type
 194         SimpleType.STRING,
 195     };
 196 }