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