jdk/test/javax/management/openmbean/CompositeDataToMapTest.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2008 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 6750472 6752563
       
    27  * @summary Test CompositeDataSupport.toMap.
       
    28  * @author Eamonn McManus
       
    29  * @run main/othervm -ea CompositeDataToMapTest
       
    30  */
       
    31 
       
    32 import java.lang.reflect.InvocationHandler;
       
    33 import java.lang.reflect.InvocationTargetException;
       
    34 import java.lang.reflect.Method;
       
    35 import java.lang.reflect.Proxy;
       
    36 import java.util.Collections;
       
    37 import java.util.HashMap;
       
    38 import java.util.Map;
       
    39 import javax.management.openmbean.CompositeData;
       
    40 import javax.management.openmbean.CompositeDataSupport;
       
    41 import javax.management.openmbean.CompositeType;
       
    42 import javax.management.openmbean.OpenType;
       
    43 import javax.management.openmbean.SimpleType;
       
    44 
       
    45 public class CompositeDataToMapTest {
       
    46     private static class IdentityInvocationHandler implements InvocationHandler {
       
    47         private final Object wrapped;
       
    48 
       
    49         public IdentityInvocationHandler(Object wrapped) {
       
    50             this.wrapped = wrapped;
       
    51         }
       
    52 
       
    53         public Object invoke(Object proxy, Method m, Object[] args)
       
    54                 throws Throwable {
       
    55             try {
       
    56                 return m.invoke(wrapped, args);
       
    57             } catch (InvocationTargetException e) {
       
    58                 throw e.getCause();
       
    59             }
       
    60         }
       
    61     }
       
    62 
       
    63     private static <T> T wrap(T x, Class<T> intf) {
       
    64         InvocationHandler ih = new IdentityInvocationHandler(x);
       
    65         return intf.cast(Proxy.newProxyInstance(
       
    66                 intf.getClassLoader(), new Class<?>[] {intf}, ih));
       
    67     }
       
    68 
       
    69     public static void main(String[] args) throws Exception {
       
    70         if (!CompositeDataToMapTest.class.desiredAssertionStatus())
       
    71             throw new AssertionError("Must be run with -ea");
       
    72 
       
    73         CompositeType emptyCT = new CompositeType(
       
    74                 "empty", "empty", new String[0], new String[0], new OpenType<?>[0]);
       
    75         CompositeData emptyCD = new CompositeDataSupport(
       
    76                 emptyCT, Collections.<String, Object>emptyMap());
       
    77         assert CompositeDataSupport.toMap(emptyCD).isEmpty() :
       
    78             "Empty CD produces empty Map";
       
    79 
       
    80         CompositeData emptyCD2 = new CompositeDataSupport(
       
    81                 emptyCT, new String[0], new Object[0]);
       
    82         assert emptyCD.equals(emptyCD2) : "Empty CD can be constructed two ways";
       
    83 
       
    84         CompositeType namedNumberCT = new CompositeType(
       
    85                 "NamedNumber", "NamedNumber",
       
    86                 new String[] {"name", "number"},
       
    87                 new String[] {"name", "number"},
       
    88                 new OpenType<?>[] {SimpleType.STRING, SimpleType.INTEGER});
       
    89         Map<String, Object> namedNumberMap = new HashMap<String, Object>();
       
    90         namedNumberMap.put("name", "Deich");
       
    91         namedNumberMap.put("number", 10);
       
    92         CompositeData namedNumberCD = new CompositeDataSupport(
       
    93                 namedNumberCT, namedNumberMap);
       
    94         assert CompositeDataSupport.toMap(namedNumberCD).equals(namedNumberMap) :
       
    95             "Map survives passage through CompositeData";
       
    96 
       
    97         namedNumberCD = wrap(namedNumberCD, CompositeData.class);
       
    98         assert CompositeDataSupport.toMap(namedNumberCD).equals(namedNumberMap) :
       
    99             "Map survives passage through wrapped CompositeData";
       
   100 
       
   101         namedNumberMap = CompositeDataSupport.toMap(namedNumberCD);
       
   102         namedNumberMap.put("name", "Ceathar");
       
   103         namedNumberMap.put("number", 4);
       
   104         namedNumberCD = new CompositeDataSupport(namedNumberCT, namedNumberMap);
       
   105         assert CompositeDataSupport.toMap(namedNumberCD).equals(namedNumberMap) :
       
   106             "Modified Map survives passage through CompositeData";
       
   107 
       
   108         try {
       
   109             namedNumberMap = CompositeDataSupport.toMap(null);
       
   110             assert false : "Null toMap arg provokes exception";
       
   111         } catch (Exception e) {
       
   112             assert e instanceof IllegalArgumentException :
       
   113                 "Exception for null toMap arg is IllegalArgumentException";
       
   114         }
       
   115     }
       
   116 }