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 4982668
27 * @summary Test that a map containing 'null' values is handled
28 *          properly when converting the map into a hashtable,
29 *          i.e. all 'null' values should be removed before creating
30 *          the hashtable in order to avoid a NullPointerException.
31 *          Check also that null values for keys are not allowed in
32 *          the maps passed to the JMXConnector[Server] factories.
33 * @author Luis-Miguel Alventosa
34 * @modules java.management.rmi
35 *          java.management/com.sun.jmx.remote.util
36 * @run clean MapNullValuesTest
37 * @run build MapNullValuesTest
38 * @run main MapNullValuesTest
39 */
40
41import java.rmi.RemoteException;
42import java.rmi.registry.Registry;
43import java.rmi.registry.LocateRegistry;
44import java.util.HashMap;
45import java.util.Hashtable;
46import java.util.Iterator;
47import java.util.Map;
48import java.util.Set;
49import javax.management.MBeanServer;
50import javax.management.MBeanServerFactory;
51import javax.management.remote.JMXConnector;
52import javax.management.remote.JMXConnectorFactory;
53import javax.management.remote.JMXConnectorServer;
54import javax.management.remote.JMXConnectorServerFactory;
55import javax.management.remote.JMXServiceURL;
56import com.sun.jmx.remote.util.EnvHelp;
57
58public class MapNullValuesTest {
59
60    private static int port;
61    private Map map0;
62    private Map map1;
63    private Map map2;
64    private Map map3;
65    private Map maps[];
66
67    public MapNullValuesTest() {
68        // Map 0
69        //
70        map0 = new HashMap();
71
72        // Map 1
73        //
74        map1 = new HashMap();
75        map1.put("key1", "value1");
76        map1.put("key2", "value2");
77        map1.put("key3", "value3");
78
79        // Map 2
80        //
81        map2 = new HashMap();
82        map2.put("key1", "value1");
83        map2.put("key2", null);
84        map2.put("key3", "value3");
85        map2.put("key4", null);
86        map2.put("key5", "value5");
87
88        // Map 3
89        //
90        map3 = new HashMap();
91        map3.put("key1", "value1");
92        map3.put(null, "value2");
93        map3.put("key3", "value3");
94
95        // Map Array
96        //
97        maps = new Map[] { map0, map1, map2, map3 };
98    }
99
100    private void checkContents(Map m, Hashtable t)
101        throws IllegalArgumentException {
102        int size = m.size();
103        Set s = m.entrySet();
104        for (Iterator i = s.iterator(); i.hasNext(); ) {
105            Map.Entry e = (Map.Entry) i.next();
106            Object key = e.getKey();
107            Object value = e.getValue();
108            if (key == null || value == null) { // Null value
109                size--;
110            } else { // Check for equality
111                if (t.get(key) == null)
112                    throw new IllegalArgumentException("Unknown key!");
113                else if (!t.get(key).equals(value))
114                    throw new IllegalArgumentException("Value mismatch!");
115            }
116        }
117        if (t.size() != size)
118            throw new IllegalArgumentException("Size mismatch!");
119    }
120
121    private int mapToHashtableTests() {
122        int errorCount = 0;
123        echo("");
124        echo(dashedMessage("Run MapToHashtable Tests"));
125        for (int i = 0; i < maps.length; i++) {
126            echo("\n>>> MapToHashtable Test [" + i + "]");
127            try {
128                echo("\tMap = " + maps[i]);
129                Hashtable t = EnvHelp.mapToHashtable(maps[i]);
130                echo("\tHashtable = " + t);
131                checkContents(maps[i], t);
132                echo("\tTest [" + i + "] PASSED!");
133            } catch (Exception e) {
134                errorCount++;
135                echo("\tTest [" + i + "] FAILED!");
136                e.printStackTrace(System.out);
137            }
138        }
139        if (errorCount == 0) {
140            echo("");
141            echo(dashedMessage("MapToHashtable Tests PASSED!"));
142        } else {
143            echo("");
144            echo(dashedMessage("MapToHashtable Tests FAILED!"));
145        }
146        return errorCount;
147    }
148
149    private int jmxConnectorServerFactoryTests() {
150        int errorCount = 0;
151        echo("");
152        echo(dashedMessage("Run JMXConnectorServerFactory Tests"));
153        for (int i = 0; i < maps.length - 1; i++) {
154            echo("\n>>> JMXConnectorServerFactory Test [" + i + "]");
155            try {
156                echo("\tMap = " + maps[i]);
157                echo("\tCreate the MBean server");
158                MBeanServer mbs = MBeanServerFactory.createMBeanServer();
159                echo("\tCreate the RMI connector server");
160                JMXServiceURL url =
161                    new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" +
162                                      port + "/JMXConnectorServerFactory" + i);
163                JMXConnectorServer jmxcs =
164                    JMXConnectorServerFactory.newJMXConnectorServer(url,
165                                                                    maps[i],
166                                                                    mbs);
167                echo("\tStart the RMI connector server");
168                jmxcs.start();
169                echo("\tCall RMIConnectorServer.toJMXConnector(Map)");
170                jmxcs.toJMXConnector(maps[i]);
171                echo("\tStop the RMI connector server");
172                jmxcs.stop();
173                echo("\tTest [" + i + "] PASSED!");
174            } catch (Exception e) {
175                errorCount++;
176                echo("\tTest [" + i + "] FAILED!");
177                e.printStackTrace(System.out);
178            }
179        }
180        if (errorCount == 0) {
181            echo("");
182            echo(dashedMessage("JMXConnectorServerFactory Tests PASSED!"));
183        } else {
184            echo("");
185            echo(dashedMessage("JMXConnectorServerFactory Tests FAILED!"));
186        }
187        return errorCount;
188    }
189
190    private int jmxConnectorFactoryTests() {
191        int errorCount = 0;
192        echo("");
193        echo(dashedMessage("Run JMXConnectorFactory Tests"));
194        for (int i = 0; i < maps.length - 1; i++) {
195            echo("\n>>> JMXConnectorFactory Test [" + i + "]");
196            try {
197                echo("\tMap = " + maps[i]);
198                echo("\tCreate the MBean server");
199                MBeanServer mbs = MBeanServerFactory.createMBeanServer();
200                echo("\tCreate the RMI connector server");
201                JMXServiceURL url =
202                    new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" +
203                                      port + "/JMXConnectorFactory" + i);
204                JMXConnectorServer jmxcs =
205                    JMXConnectorServerFactory.newJMXConnectorServer(url,
206                                                                    null,
207                                                                    mbs);
208                echo("\tStart the RMI connector server");
209                jmxcs.start();
210                echo("\tCreate and connect the RMI connector");
211                JMXConnector jmxc =
212                    JMXConnectorFactory.connect(jmxcs.getAddress(), maps[i]);
213                echo("\tClose the RMI connector");
214                jmxc.close();
215                echo("\tTest [" + i + "] PASSED!");
216            } catch (Exception e) {
217                errorCount++;
218                echo("\tTest [" + i + "] FAILED!");
219                e.printStackTrace(System.out);
220            }
221        }
222        if (errorCount == 0) {
223            echo("");
224            echo(dashedMessage("JMXConnectorFactory Tests PASSED!"));
225        } else {
226            echo("");
227            echo(dashedMessage("JMXConnectorFactory Tests FAILED!"));
228        }
229        return errorCount;
230    }
231
232    private int nullKeyFactoryTests() {
233        int errorCount = 0;
234        echo("");
235        echo(dashedMessage("Run Null Key Factory Tests"));
236        echo("\tMap = " + map3);
237        try {
238            String urlStr =
239                "service:jmx:rmi:///jndi/rmi://:" + port + "/NullKeyFactory";
240            MBeanServer mbs = MBeanServerFactory.createMBeanServer();
241            JMXServiceURL url = null;
242            JMXConnectorServer jmxcs = null;
243            JMXConnector jmxc = null;
244
245            echo("\tJMXConnectorServerFactory.newJMXConnectorServer()");
246            try {
247                url = new JMXServiceURL(urlStr + "1");
248                jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
249                                                                        map3,
250                                                                        mbs);
251                errorCount++;
252                echo("\tTest FAILED!");
253            } catch (Exception e) {
254                echo("\tException Message: " + e.getMessage());
255                echo("\tTest PASSED!");
256            }
257
258            echo("\tJMXConnectorServerFactory.toJMXConnector()");
259            try {
260                url = new JMXServiceURL(urlStr + "2");
261                jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
262                                                                        null,
263                                                                        mbs);
264                jmxcs.start();
265                jmxcs.toJMXConnector(map3);
266                errorCount++;
267                echo("\tTest FAILED!");
268            } catch (Exception e) {
269                echo("\tException Message: " + e.getMessage());
270                echo("\tTest PASSED!");
271            } finally {
272                jmxcs.stop();
273            }
274
275            echo("\tJMXConnectorFactory.newJMXConnector()");
276            try {
277                url = new JMXServiceURL(urlStr + "3");
278                jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
279                                                                        null,
280                                                                        mbs);
281                jmxcs.start();
282                jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(),
283                                                           map3);
284                errorCount++;
285                echo("\tTest FAILED!");
286            } catch (Exception e) {
287                echo("\tException Message: " + e.getMessage());
288                echo("\tTest PASSED!");
289            } finally {
290                jmxcs.stop();
291            }
292
293            echo("\tJMXConnectorFactory.connect()");
294            try {
295                url = new JMXServiceURL(urlStr + "4");
296                jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
297                                                                        null,
298                                                                        mbs);
299                jmxcs.start();
300                jmxc = JMXConnectorFactory.connect(jmxcs.getAddress(), map3);
301                errorCount++;
302                echo("\tTest FAILED!");
303            } catch (Exception e) {
304                echo("\tException Message: " + e.getMessage());
305                echo("\tTest PASSED!");
306            } finally {
307                jmxcs.stop();
308            }
309
310            echo("\tJMXConnector.connect()");
311            try {
312                url = new JMXServiceURL(urlStr + "5");
313                jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
314                                                                        null,
315                                                                        mbs);
316                jmxcs.start();
317                jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(),
318                                                           null);
319                jmxc.connect(map3);
320                errorCount++;
321                echo("\tTest FAILED!");
322            } catch (Exception e) {
323                echo("\tException Message: " + e.getMessage());
324                echo("\tTest PASSED!");
325            } finally {
326                jmxcs.stop();
327            }
328
329        } catch (Exception e) {
330            echo("\tGot unexpected exception!");
331            e.printStackTrace(System.out);
332            errorCount = 1;
333        }
334
335        if (errorCount == 0) {
336            echo("");
337            echo(dashedMessage("Null Key Factory Tests PASSED!"));
338        } else {
339            echo("");
340            echo(dashedMessage("Null Key Factory Tests FAILED!"));
341        }
342        return errorCount;
343    }
344
345    private static String dashedMessage(String message) {
346        final int MAX_LINE = 80;
347        StringBuffer sb = new StringBuffer(message);
348        sb.append(" ");
349        for (int i = MAX_LINE; i > message.length() + 1; i--)
350            sb.append("-");
351        return sb.toString();
352    }
353
354    private static void echo(String message) {
355        System.out.println(message);
356    }
357
358    public static void main(String[] args) throws Exception {
359
360        int errorCount = 0;
361
362        MapNullValuesTest test = new MapNullValuesTest();
363
364        // Create an RMI registry
365        //
366        echo("");
367        echo(dashedMessage("Start RMI registry"));
368        Registry reg = null;
369        port = 7500;
370        while (port++ < 7550) {
371            try {
372                reg = LocateRegistry.createRegistry(port);
373                echo("\nRMI registry running on port " + port);
374                break;
375            } catch (RemoteException e) {
376                // Failed to create RMI registry...
377                //
378                echo("\nFailed to create RMI registry on port " + port);
379                e.printStackTrace(System.out);
380            }
381        }
382        if (reg == null) {
383            System.exit(1);
384        }
385
386        // Run tests
387        //
388        errorCount += test.mapToHashtableTests();
389        errorCount += test.jmxConnectorServerFactoryTests();
390        errorCount += test.jmxConnectorFactoryTests();
391        errorCount += test.nullKeyFactoryTests();
392
393        if (errorCount == 0) {
394            echo("\nNull values for key/value pairs in Map Tests PASSED!");
395        } else {
396            echo("\nNull values for key/value pairs in Map Tests FAILED!");
397            System.exit(1);
398        }
399    }
400}
401