ExceptionsDescriptorTest.java 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test %M% %I%
 * @bug 6250014
 * @summary Test that Exceptions are added to the MbeanInfo
 * @author Jean-Francois Denise
 * @run main/othervm ExceptionsDescriptorTest
 */
import java.lang.management.ManagementFactory;
import java.util.HashSet;
import java.util.Set;
import javax.management.Descriptor;
import javax.management.JMX;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ObjectName;

public class ExceptionsDescriptorTest {

    private static final ObjectName OBJECT_NAME = ObjectName.valueOf(":type=Foo");
    final static String EXCEPTION_NAME = Exception.class.getName();
    final static String ILLEGAL_ARGUMENT_EXCEPTION_NAME =
            IllegalArgumentException.class.getName();
    final static Set<String> ONE_EXCEPTION = new HashSet<String>();
    final static Set<String> TWO_EXCEPTION = new HashSet<String>();
    static {
        ONE_EXCEPTION.add(EXCEPTION_NAME);
        TWO_EXCEPTION.add(EXCEPTION_NAME);
        TWO_EXCEPTION.add(ILLEGAL_ARGUMENT_EXCEPTION_NAME);
    }
    public interface TestMBean {

        public void doIt();

        public void doIt(String str) throws Exception;

        public void doIt(String str, boolean b) throws Exception,
                IllegalArgumentException;

        public String getThat();

        public void setThat(String that);

        public String getThe() throws Exception;

        public void setThe(String the);

        public String getThese();

        public void setThese(String the) throws Exception;

        public String getIt() throws Exception;

        public void setIt(String str) throws Exception;

        public String getThis() throws Exception, IllegalArgumentException;

        public void setThose(String str) throws Exception,
                IllegalArgumentException;
    }

    public static class Test implements TestMBean {

        public Test() {
        }

        public Test(int i) throws Exception {
        }

        public Test(int i, int j) throws Exception, IllegalArgumentException {
        }

        public void doIt() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void doIt(String str) throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void doIt(String str, boolean b) throws Exception, IllegalArgumentException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getThat() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setThat(String that) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getThe() throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setThe(String the) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getThese() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setThese(String the) throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getIt() throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setIt(String str) throws Exception {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getThis() throws Exception, IllegalArgumentException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setThose(String str) throws Exception, IllegalArgumentException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    private static void check(Descriptor d,
            Set<String> exceptionsExpectedValue,
            boolean exceptionsExpected,
            Set<String> setExceptionsExpectedValue,
            boolean setExceptionsExpected) throws Exception {
        String[] exceptionsValues = (String[]) d.getFieldValue(JMX.EXCEPTIONS_FIELD);
        String[] setExceptionsValues = (String[]) d.getFieldValue(JMX.SET_EXCEPTIONS_FIELD);

        if (exceptionsExpected && exceptionsValues == null) {
            throw new Exception("exceptions is expected but null value");
        }
        if (!exceptionsExpected && exceptionsValues != null) {
            throw new Exception("exceptions is not expected but non null value");
        }
        if (setExceptionsExpected && setExceptionsValues == null) {
            throw new Exception("setExceptions is expected but null value");
        }
        if (!setExceptionsExpected && setExceptionsValues != null) {
            throw new Exception("setExceptions is not expected but non null value");
        }

        if (exceptionsExpected) {
            checkValues(exceptionsExpectedValue, exceptionsValues);
        }
        if (setExceptionsExpected) {
            checkValues(setExceptionsExpectedValue, setExceptionsValues);
        }
    }

    private static void checkValues(Set<String> expectedValuesSet,
            String[] realValues) throws Exception {

        Set<String> realValuesSet = new HashSet<String>();
        for (String ex : realValues) {
            realValuesSet.add(ex);
        }
        if (!realValuesSet.equals(expectedValuesSet)) {
            throw new Exception("Invalid content for exceptions. Was expecting " +
                    expectedValuesSet + ". Found " + realValuesSet);
        }
    }

    public static void main(String[] args) throws Exception {
        Test t = new Test();
        ManagementFactory.getPlatformMBeanServer().registerMBean(t, OBJECT_NAME);
        MBeanInfo info = ManagementFactory.getPlatformMBeanServer().
                getMBeanInfo(OBJECT_NAME);
        //Constructors
        for (MBeanConstructorInfo ctr : info.getConstructors()) {
            if (ctr.getSignature().length == 0) {
                check(ctr.getDescriptor(), null, false, null, false);
            }
            if (ctr.getSignature().length == 1) {
                check(ctr.getDescriptor(), ONE_EXCEPTION, true, null, false);
            }
            if (ctr.getSignature().length == 2) {
                check(ctr.getDescriptor(),TWO_EXCEPTION,true, null, false);
            }
        }
        //Attributes
        for (MBeanAttributeInfo attr : info.getAttributes()) {
            if (attr.getName().equals("That")) {
                check(attr.getDescriptor(), null, false, null, false);
            }
            if (attr.getName().equals("The")) {
                check(attr.getDescriptor(), ONE_EXCEPTION,true,null, false);
            }
            if (attr.getName().equals("These")) {
                check(attr.getDescriptor(), null, false, ONE_EXCEPTION,true);
            }
            if (attr.getName().equals("It")) {
                check(attr.getDescriptor(), ONE_EXCEPTION,true,ONE_EXCEPTION,
                        true);
            }
            if (attr.getName().equals("This")) {
                check(attr.getDescriptor(), TWO_EXCEPTION,true,null,false);
            }
            if (attr.getName().equals("Those")) {
                check(attr.getDescriptor(), null,false,TWO_EXCEPTION,true);
            }
        }
        //Operations
        for (MBeanOperationInfo oper : info.getOperations()) {
            if (oper.getSignature().length == 0) {
                check(oper.getDescriptor(), null, false, null, false);
            }
            if (oper.getSignature().length == 1) {
                check(oper.getDescriptor(), ONE_EXCEPTION, true, null, false);
            }
            if (oper.getSignature().length == 2) {
                check(oper.getDescriptor(), TWO_EXCEPTION,true, null, false);
            }
        }
        System.out.println("Test passed");
    }
}