0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4287596
0N/A * @summary Unit test for "Pluggable Connectors and Transports" feature.
0N/A *
0N/A * This test checks that VirtualMachineManager creates Connectors that
0N/A * are "compatible" those created by 1.4 or earilier releases.
0N/A */
0N/A
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.connect.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class CompatibleConnectors {
0N/A
0N/A // number of tests that fail
0N/A static int failures;
0N/A
0N/A static void fail(String msg) {
0N/A System.out.println(msg + " - test failed.");
0N/A failures++;
0N/A }
0N/A
0N/A // the AttachingConnectors that we expect
0N/A static Object[][] attachingConnectors() {
0N/A return new Object[][] {
0N/A { "com.sun.jdi.SocketAttach",
0N/A "dt_socket",
0N/A new String[] { "hostname", "Connector.StringArgument", "false" },
0N/A new String[] { "port", "Connector.IntegerArgument", "true" }
0N/A },
0N/A
0N/A { "com.sun.jdi.SharedMemoryAttach",
0N/A "dt_shmem",
0N/A new String[] { "name", "Connector.StringArgument", "true" }
0N/A }
0N/A };
0N/A }
0N/A
0N/A // the ListeningConnectors that we expect
0N/A static Object[][] listeningConnectors() {
0N/A return new Object[][] {
0N/A { "com.sun.jdi.SocketListen",
0N/A "dt_socket",
0N/A new String[] { "port", "Connector.IntegerArgument", "true" }
0N/A },
0N/A
0N/A { "com.sun.jdi.SharedMemoryListen",
0N/A "dt_shmem",
0N/A new String[] { "name", "Connector.StringArgument", "false" }
0N/A }
0N/A };
0N/A
0N/A }
0N/A
0N/A // the LaunchingConnectors that we expect
0N/A // - note that we don't indicate the transport name (as it varies
0N/A // for these connectors)
0N/A static Object[][] launchingConnectors() {
0N/A return new Object[][] {
0N/A { "com.sun.jdi.CommandLineLaunch",
0N/A null,
0N/A new String[] { "home", "Connector.StringArgument", "false" },
0N/A new String[] { "options", "Connector.StringArgument", "false" },
0N/A new String[] { "main", "Connector.StringArgument", "true" },
0N/A new String[] { "suspend", "Connector.BooleanArgument", "false" },
0N/A new String[] { "quote", "Connector.StringArgument", "true" },
0N/A new String[] { "vmexec", "Connector.StringArgument", "true" }
0N/A },
0N/A
0N/A { "com.sun.jdi.RawCommandLineLaunch",
0N/A null,
0N/A new String[] { "command", "Connector.StringArgument", "true" },
0N/A new String[] { "address", "Connector.StringArgument", "true" },
0N/A new String[] { "quote", "Connector.StringArgument", "true" }
0N/A }
0N/A };
0N/A
0N/A }
0N/A
0N/A // find Connector by name, return null if not found
0N/A static Connector find(String name, List l) {
0N/A Iterator i = l.iterator();
0N/A while (i.hasNext()) {
0N/A Connector c = (Connector)i.next();
0N/A if (c.name().equals(name)) {
0N/A return c;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A // check that a connector is of the expected type
0N/A static void type_match(String arg_name, String arg_type, Connector.Argument arg) {
0N/A boolean fail = false;
0N/A if (arg_type.equals("Connector.StringArgument")) {
0N/A if (!(arg instanceof Connector.StringArgument)) {
0N/A fail = true;
0N/A }
0N/A }
0N/A if (arg_type.equals("Connector.IntegerArgument")) {
0N/A if (!(arg instanceof Connector.IntegerArgument)) {
0N/A fail = true;
0N/A }
0N/A }
0N/A if (arg_type.equals("Connector.BooleanArgument")) {
0N/A if (!(arg instanceof Connector.BooleanArgument)) {
0N/A fail = true;
0N/A }
0N/A }
0N/A if (arg_type.equals("Connector.SelectedArgument")) {
0N/A if (!(arg instanceof Connector.IntegerArgument)) {
0N/A fail = true;
0N/A }
0N/A }
0N/A if (fail) {
0N/A fail(arg_name + " is of type: " + arg.getClass() + ", expected: "
0N/A + arg_type);
0N/A }
0N/A }
0N/A
0N/A
0N/A // check that a Connector is compatible
0N/A static void check(Object[] desc, Connector connector) {
0N/A String name = (String)desc[0];
0N/A String transport_name = (String)desc[1];
0N/A
0N/A // if the transport name is "null" it means its transport may
0N/A // vary (eg: SunCommandLineLauncher will choose shared memory
0N/A // on Windows and dt_socket on Solaris). In that case we can't
0N/A // check the transport name.
0N/A //
0N/A if (transport_name != null) {
0N/A System.out.println("Checking transpot name");
0N/A if (!(transport_name.equals(connector.transport().name()))) {
0N/A fail("transport().name() returns: " +
0N/A connector.transport().name() + ", expected: " + transport_name);
0N/A }
0N/A }
0N/A
0N/A // check that all the old arguments still exist
0N/A for (int i=2; i<desc.length; i++) {
0N/A String[] args = (String[])desc[i];
0N/A String arg_name = args[0];
0N/A String arg_type = args[1];
0N/A String arg_mandatory = args[2];
0N/A
0N/A System.out.println("Checking argument: " + arg_name);
0N/A
0N/A // check that the arg still exists
0N/A Map defaultArgs = connector.defaultArguments();
0N/A Object value = defaultArgs.get(arg_name);
0N/A if (value == null) {
0N/A fail(name + " is missing Connector.Argument: " + arg_name);
0N/A continue;
0N/A }
0N/A
0N/A // next check that the type matches
0N/A Connector.Argument connector_arg = (Connector.Argument)value;
0N/A
0N/A // check that the argument type hasn't changed
0N/A type_match(arg_name, arg_type, connector_arg);
0N/A
0N/A // check that an optional argument has been made mandatory
0N/A if (arg_mandatory.equals("false")) {
0N/A if (connector_arg.mustSpecify()) {
0N/A fail(arg_name + " is now mandatory");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // next we check for new arguments that are mandatory but
0N/A // have no default value
0N/A
0N/A System.out.println("Checking for new arguments");
0N/A Map dfltArgs = connector.defaultArguments();
0N/A Iterator iter = dfltArgs.keySet().iterator();
0N/A while (iter.hasNext()) {
0N/A String arg_name = (String)iter.next();
0N/A
0N/A // see if the argument is new
0N/A boolean found = false;
0N/A for (int j=2; j<desc.length; j++) {
0N/A String[] args = (String[])desc[j];
0N/A if (args[0].equals(arg_name)) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (!found) {
0N/A Connector.Argument connector_arg =
0N/A (Connector.Argument)dfltArgs.get(arg_name);
0N/A
0N/A if (connector_arg.mustSpecify()) {
0N/A String value = connector_arg.value();
0N/A if (value.equals("")) {
0N/A value = null;
0N/A }
0N/A if (value == null) {
0N/A fail("New Connector.Argument \"" + connector_arg.name() +
0N/A "\" added - argument is mandatory");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A // compare the actual list of Connectors against the
0N/A // expected list of Connectors.
0N/A static void compare(Object[][] prev, List list) {
0N/A String os = System.getProperty("os.name");
0N/A for (int i=0; i<prev.length; i++) {
0N/A Object[] desc = prev[i];
0N/A String name = (String)desc[0];
0N/A
0N/A // ignore Windows specific Connectors are non-Windows machines
0N/A if (!(os.startsWith("Windows"))) {
0N/A if (name.equals("com.sun.jdi.SharedMemoryAttach") ||
0N/A name.equals("com.sun.jdi.SharedMemoryListen")) {
0N/A continue;
0N/A }
0N/A }
0N/A
0N/A System.out.println("");
0N/A System.out.println("Checking Connector " + name);
0N/A
0N/A // check that the Connector exists
0N/A Connector c = find(name, list);
0N/A if (c == null) {
0N/A fail("Connector is missing");
0N/A continue;
0N/A }
0N/A
0N/A check(desc, c);
0N/A }
0N/A }
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
0N/A
0N/A // in 1.2/1.3/1.4 the defualtConnector was
0N/A // com.sun.jdi.CommandLineLaunch. Many debuggers probably
0N/A // depend on this so check that it's always the default.
0N/A //
0N/A String expected = "com.sun.jdi.CommandLineLaunch";
0N/A System.out.println("Checking that defaultConnector is: " + expected);
0N/A String dflt = vmm.defaultConnector().name();
0N/A if (!(dflt.equals(expected))) {
0N/A System.err.println("defaultConnector() is: " + dflt +
0N/A ", expected:" + expected);
0N/A failures++;
0N/A } else {
0N/A System.out.println("Okay");
0N/A }
0N/A
0N/A compare(attachingConnectors(), vmm.attachingConnectors());
0N/A compare(listeningConnectors(), vmm.listeningConnectors());
0N/A compare(launchingConnectors(), vmm.launchingConnectors());
0N/A
0N/A // test results
0N/A if (failures > 0) {
0N/A System.out.println("");
0N/A throw new RuntimeException(failures + " test(s) failed");
0N/A }
0N/A }
0N/A}