jdk/test/javax/management/modelmbean/DefaultDescriptorFieldTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6252592 4967755
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that default fields are correctly added to Descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and that input Descriptors are never modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Lars Westergren
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanConstructorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.modelmbean.DescriptorSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.modelmbean.ModelMBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.modelmbean.ModelMBeanConstructorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.modelmbean.ModelMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.modelmbean.ModelMBeanInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.modelmbean.ModelMBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.modelmbean.ModelMBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Junit tests for bugs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 6252592 Provide for the user mandatory fields missing in Descriptor given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         to Model*Info constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 4967755 ModelMBeanAttributeInfo constructor modifies its Descriptor argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Lars Westergren
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class DefaultDescriptorFieldTest /*extends TestCase*/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        boolean fail = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Object test = new DefaultDescriptorFieldTest("Test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (Method m : DefaultDescriptorFieldTest.class.getMethods()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (m.getName().startsWith("test") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    m.getParameterTypes().length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                System.out.println("Testing " + m.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    m.invoke(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    fail = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    Throwable t = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    System.out.println("FAILED: exception: " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    t.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (fail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    //No check WHICH constructor is reflected, at least when the classes tested are constructed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    //so I just use first one that came to mind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    final Constructor[] constArr = String.class.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    final Constructor dummyConstructor = constArr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    Method getMethod = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Creates a new instance of MBeanTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public DefaultDescriptorFieldTest(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            getMethod = String.class.getMethod("toString");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Test instantiating the 5 different classes with a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Descriptor, and also setting a null descriptor after.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Expected: Default Descriptors created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void testNullDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    throws IntrospectionException, MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        final ModelMBeanConstructorInfo constInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                new ModelMBeanConstructorInfo("Dummy", dummyConstructor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        constInfo.setDescriptor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ModelMBeanAttributeInfo attInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new ModelMBeanAttributeInfo("dummyAttInfoName", "dummyAttInfoDesc", getMethod, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        attInfo.setDescriptor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ModelMBeanNotificationInfo notInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                new ModelMBeanNotificationInfo(null, "notificationClassName", "daName", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        notInfo.setDescriptor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ModelMBeanOperationInfo opInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                new ModelMBeanOperationInfo("test", getMethod, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        opInfo.setDescriptor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ModelMBeanInfoSupport infoSupport =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                new ModelMBeanInfoSupport(new DummyModelMBeanInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        infoSupport.setDescriptor(null,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        infoSupport.setDescriptor(null, "mbean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Test instantiating and setting a Descriptor without the "name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * "descriptorType" or "role" fields. This also tests whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Descriptor is cloned before default values are set, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * default values for one class will be incorrect for the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Expected: Descriptor should be cloned, missing default values should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void testFieldlessDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    throws IntrospectionException, MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Descriptor theNamelessOne = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        final ModelMBeanConstructorInfo constInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                new ModelMBeanConstructorInfo("Dummy", dummyConstructor, theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        constInfo.setDescriptor(theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ModelMBeanAttributeInfo attInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                new ModelMBeanAttributeInfo("dummyAttInfoName", "dummyAttInfoDesc", getMethod, null, theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        attInfo.setDescriptor(theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ModelMBeanNotificationInfo notInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                new ModelMBeanNotificationInfo(null, "notificationClassName", "daName", theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        notInfo.setDescriptor(theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ModelMBeanOperationInfo opInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                new ModelMBeanOperationInfo("test", getMethod, theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        opInfo.setDescriptor(theNamelessOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ModelMBeanInfoSupport infoSupport = new ModelMBeanInfoSupport(new DummyModelMBeanInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        infoSupport.setDescriptor(theNamelessOne, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        infoSupport.setDescriptor(theNamelessOne, "mbean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Creates an empty DescriptorSupport and then test that ModelMBeanConstructorInfo accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      * the correct fields in validation one by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void testCorrectConstructorDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Descriptor cDesc = new DescriptorSupport(new String[0],new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        final ModelMBeanConstructorInfo constInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                new ModelMBeanConstructorInfo("Dummy", dummyConstructor, cDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        assertFalse(fieldRefuted(constInfo, cDesc, "name" , "java.lang.String"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        assertFalse(fieldRefuted(constInfo, cDesc, "role" , "constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        assertFalse(fieldRefuted(constInfo, cDesc, "descriptorType" , "operation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Test that ModelMBeanConstructorInfo refutes incorrect fields in validation one by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void testIncorrectConstructorDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    throws NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Descriptor cDesc = new DescriptorSupport(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
           new String[] {"getMethod", "setMethod", "role", "Class", "name", "descriptorType"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
           new String[] {"dummyGetMethod", "dummySetMethod", "constructor", "dummyClass", "java.lang.String", "operation"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        final ModelMBeanConstructorInfo constInfo = new ModelMBeanConstructorInfo("Dummy", dummyConstructor, cDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        assertTrue(fieldRefuted(constInfo, cDesc, "name" , "blah"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        assertTrue(fieldRefuted(constInfo, cDesc, "descriptorType", "mbean"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        assertTrue(fieldRefuted(constInfo, cDesc, "descriptorType", "notification"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        assertTrue(fieldRefuted(constInfo, cDesc, "descriptorType", "constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        assertTrue(fieldRefuted(constInfo, cDesc, "role", "operation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void testCorrectAttributeDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    throws IntrospectionException, NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Descriptor aDesc = new DescriptorSupport(new String[0],new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        final ModelMBeanAttributeInfo attInfo = new ModelMBeanAttributeInfo("dummyAttInfoName", "dummyAttInfoDesc", getMethod, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        assertFalse(fieldRefuted(attInfo, aDesc, "name" , "dummyAttInfoName"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        assertFalse(fieldRefuted(attInfo, aDesc, "descriptorType" , "attribute"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void testIncorrectAttributeDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    throws IntrospectionException, NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Descriptor aDesc = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        final ModelMBeanAttributeInfo attInfo = new ModelMBeanAttributeInfo("dummyAttInfoName", "dummyAttInfoDesc", getMethod, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        assertTrue(fieldRefuted(attInfo, aDesc, "name" , "blah"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        assertTrue(fieldRefuted(attInfo, aDesc, "descriptorType" , "constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        assertTrue(fieldRefuted(attInfo, aDesc, "descriptorType" , "notification"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public void testCorrectNotificationDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    throws NoSuchMethodException, IllegalAccessException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Descriptor nDesc = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final ModelMBeanNotificationInfo nInfo = new ModelMBeanNotificationInfo(null, "notificationClassName", "daName", nDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        assertFalse(fieldRefuted(nInfo, nDesc, "name" , "notificationClassName"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        assertFalse(fieldRefuted(nInfo, nDesc, "descriptorType" , "notification"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void testIncorrectNotificationDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    throws NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Descriptor nDesc = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        final ModelMBeanNotificationInfo nInfo = new ModelMBeanNotificationInfo(null, "notificationClassName", "daName", nDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        assertTrue(fieldRefuted(nInfo, nDesc, "name" , "blah"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        assertTrue(fieldRefuted(nInfo, nDesc, "descriptorType" , "constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        assertTrue(fieldRefuted(nInfo, nDesc, "descriptorType" , "operation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void testCorrectOperationDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    throws NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Descriptor opDesc = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        final ModelMBeanOperationInfo opInfo = new ModelMBeanOperationInfo("test", "readable description", null, "type", 1, opDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        assertFalse(fieldRefuted(opInfo, opDesc, "name" , "test"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        assertFalse(fieldRefuted(opInfo, opDesc, "descriptorType" , "operation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        assertFalse(fieldRefuted(opInfo, opDesc, "role" , "operation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        assertFalse(fieldRefuted(opInfo, opDesc, "role" , "getter"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        assertFalse(fieldRefuted(opInfo, opDesc, "role" , "setter"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void testIncorrectOperationDescriptors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    throws NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Descriptor opDesc = new DescriptorSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        final ModelMBeanOperationInfo opInfo = new ModelMBeanOperationInfo("test", "readable description", null, "type", 1, opDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        assertTrue(fieldRefuted(opInfo, opDesc, "name" , "DENIED!!"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        assertTrue(fieldRefuted(opInfo, opDesc, "descriptorType" , "constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        assertTrue(fieldRefuted(opInfo, opDesc, "descriptorType" , "attribute"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        assertTrue(fieldRefuted(opInfo, opDesc, "descriptorType" , "notification"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        assertTrue(fieldRefuted(opInfo, opDesc, "descriptorType" , "x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    //TODO also test ModelMBeanInfoSupport perhaps. Slightly more difficult to set up and test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Clones descriptor, sets a new value in the clone and tries to apply clone to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * reflected Model*Info object method. The new field should be refuted if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * is a bad value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    //I like java, but this is one case where duck typing would have been slightly easier I think. :)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private boolean fieldRefuted(Object mbeanInfo, Descriptor desc, String fieldName, String newValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    throws NoSuchMethodException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Method setDescMethod = mbeanInfo.getClass().getMethod("setDescriptor", Descriptor.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Descriptor newDesc = (Descriptor)desc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        boolean refused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        newDesc.setField(fieldName, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            setDescMethod.invoke(mbeanInfo, newDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            //If we get classcast exception, someone passed in a bad object to reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            //Perhaps an unnecessary check, this cast?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            RuntimeOperationsException rex = (RuntimeOperationsException)ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            refused = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return refused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Dummy class used to create test objects. May not be needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
   private class DummyModelMBeanInfo implements ModelMBeanInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            public Descriptor[] getDescriptors(String inDescriptorType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            public void setDescriptors(Descriptor[] inDescriptors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            public Descriptor getDescriptor(String inDescriptorName, String inDescriptorType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            public void setDescriptor(Descriptor inDescriptor, String inDescriptorType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            public Descriptor getMBeanDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            public void setMBeanDescriptor(Descriptor inDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            public ModelMBeanAttributeInfo getAttribute(String inName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            public ModelMBeanOperationInfo getOperation(String inName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            public ModelMBeanNotificationInfo getNotification(String inName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            public MBeanAttributeInfo[] getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            public String getClassName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return "AnonMBeanInfoImpl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            public MBeanConstructorInfo[] getConstructors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            public MBeanNotificationInfo[] getNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            public MBeanOperationInfo[] getOperations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static void assertFalse(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        assertTrue(!x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private static void assertTrue(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (!x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new AssertionError("Assertion failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}