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