aboutsummaryrefslogtreecommitdiff
path: root/test/javax/management/ObjectName/ValueOfTest.java
blob: 4a68a3d4c5ce55d73ae344490c40468f15ce693a (plain)
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
/*
 * 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
 * @bug 6734813
 * @summary Test the ObjectName.valueOf methods
 * @author Eamonn McManus
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Hashtable;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

public class ValueOfTest {
    public static void main(String[] args) throws Exception {
        // Calls that should work
        testPositive("d:foo=bar,baz=buh");
        testPositive("foo", "bar", "baz");
        Hashtable<String, String> h = new Hashtable<String, String>();
        h.put("foo", "bar");
        h.put("baz", "buh");
        testPositive("domain", h);

        // Calls that should not work
        testNegative("d");
        testNegative("d:");
        testNegative("d::foo=bar");
        testNegative("d:", "foo", "bar");
        testNegative("d", "foo=", "bar");
        testNegative("d:", h);
        testNegative("d", new Hashtable<String, String>());
    }

    private static void testPositive(Object... args) throws Exception {
        Method valueOf = valueOfMethod(args);
        Method getInstance = getInstanceMethod(args);
        Constructor<?> constructor = constructor(args);

        Object valueOfValue = valueOf.invoke(null, args);
        Object getInstanceValue = getInstance.invoke(null, args);
        Object constructorValue = constructor.newInstance(args);

        String argString =
                Arrays.toString(args).replace('[', '(').replace(']', ')');

        if (!valueOfValue.equals(getInstanceValue)) {
            throw new Exception(
                    "valueOf" + argString + " differs from getInstance" +
                    argString);
        }

        if (!valueOfValue.equals(constructorValue)) {
            throw new Exception(
                    "valueOf" + argString + " differs from new ObjectName " +
                    argString);
        }

        System.out.println("OK: valueOf" + argString);
    }

    private static void testNegative(Object... args) throws Exception {
        Method valueOf = valueOfMethod(args);
        Method getInstance = getInstanceMethod(args);

        String argString =
                Arrays.toString(args).replace('[', '(').replace(']', ')');

        final Throwable valueOfException;
        try {
            valueOf.invoke(null, args);
            throw new Exception("valueOf" + argString + " did not fail but should");
        } catch (InvocationTargetException e) {
            valueOfException = e.getCause();
        }
        if (!(valueOfException instanceof IllegalArgumentException)) {
            throw new Exception(
                    "valueOf" + argString + " threw " +
                    valueOfException.getClass().getName() + " instead of " +
                    "IllegalArgumentException", valueOfException);
        }

        final Throwable valueOfCause = valueOfException.getCause();
        if (!(valueOfCause instanceof MalformedObjectNameException)) {
            throw new Exception(
                    "valueOf" + argString + " threw exception with wrong " +
                    "type of cause", valueOfCause);
        }

        if (!valueOfException.getMessage().equals(valueOfCause.getMessage())) {
            // The IllegalArgumentException should have the same message as
            // the MalformedObjectNameException it wraps.
            // This isn't specified but is desirable.
            throw new Exception(
                    "valueOf" + argString + ": message in wrapping " +
                    "IllegalArgumentException (" + valueOfException.getMessage() +
                    ") differs from message in wrapped " +
                    "MalformedObjectNameException (" + valueOfCause.getMessage() +
                    ")");
        }

        final Throwable getInstanceException;
        try {
            getInstance.invoke(null, args);
            throw new Exception("getInstance" + argString + " did not fail but should");
        } catch (InvocationTargetException e) {
            getInstanceException = e.getCause();
        }
        if (!(getInstanceException instanceof MalformedObjectNameException)) {
            throw new Exception(
                    "getInstance" + argString + " threw wrong exception",
                    getInstanceException);
        }

        if (!valueOfException.getMessage().equals(getInstanceException.getMessage())) {
            // Again this is not specified.
            throw new Exception(
                    "Exception message from valueOf" + argString + " (" +
                    valueOfException.getMessage() + ") differs from message " +
                    "from getInstance" + argString + " (" +
                    getInstanceException.getMessage() + ")");
        }

        System.out.println("OK (correct exception): valueOf" + argString);
    }

    private static Method valueOfMethod(Object[] args) throws Exception {
        return method("valueOf", args);
    }

    private static Method getInstanceMethod(Object[] args) throws Exception {
        return method("getInstance", args);
    }

    private static Method method(String name, Object[] args) throws Exception {
        Class<?>[] argTypes = argTypes(args);
        return ObjectName.class.getMethod(name, argTypes);
    }

    private static Constructor<?> constructor(Object[] args) throws Exception {
        Class<?>[] argTypes = argTypes(args);
        return ObjectName.class.getConstructor(argTypes);
    }

    private static Class<?>[] argTypes(Object[] args) {
        Class<?>[] argTypes = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++)
            argTypes[i] = args[i].getClass();
        return argTypes;
    }
}