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