Path: blob/master/test/jdk/com/sun/jdi/CompatibleConnectors.java
41149 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 428759625* @summary Unit test for "Pluggable Connectors and Transports" feature.26*27* This test checks that VirtualMachineManager creates Connectors that28* are "compatible" those created by 1.4 or earilier releases.29*/3031import com.sun.jdi.*;32import com.sun.jdi.connect.*;33import java.util.*;3435public class CompatibleConnectors {3637// number of tests that fail38static int failures;3940static void fail(String msg) {41System.out.println(msg + " - test failed.");42failures++;43}4445// the AttachingConnectors that we expect46static Object[][] attachingConnectors() {47return new Object[][] {48{ "com.sun.jdi.SocketAttach",49"dt_socket",50new String[] { "hostname", "Connector.StringArgument", "false" },51new String[] { "port", "Connector.IntegerArgument", "true" }52},5354{ "com.sun.jdi.SharedMemoryAttach",55"dt_shmem",56new String[] { "name", "Connector.StringArgument", "true" }57}58};59}6061// the ListeningConnectors that we expect62static Object[][] listeningConnectors() {63return new Object[][] {64{ "com.sun.jdi.SocketListen",65"dt_socket",66new String[] { "port", "Connector.IntegerArgument", "true" }67},6869{ "com.sun.jdi.SharedMemoryListen",70"dt_shmem",71new String[] { "name", "Connector.StringArgument", "false" }72}73};7475}7677// the LaunchingConnectors that we expect78// - note that we don't indicate the transport name (as it varies79// for these connectors)80static Object[][] launchingConnectors() {81return new Object[][] {82{ "com.sun.jdi.CommandLineLaunch",83null,84new String[] { "home", "Connector.StringArgument", "false" },85new String[] { "options", "Connector.StringArgument", "false" },86new String[] { "main", "Connector.StringArgument", "true" },87new String[] { "suspend", "Connector.BooleanArgument", "false" },88new String[] { "quote", "Connector.StringArgument", "true" },89new String[] { "vmexec", "Connector.StringArgument", "true" }90},9192{ "com.sun.jdi.RawCommandLineLaunch",93null,94new String[] { "command", "Connector.StringArgument", "true" },95new String[] { "address", "Connector.StringArgument", "true" },96new String[] { "quote", "Connector.StringArgument", "true" }97}98};99100}101102// find Connector by name, return null if not found103static Connector find(String name, List l) {104Iterator i = l.iterator();105while (i.hasNext()) {106Connector c = (Connector)i.next();107if (c.name().equals(name)) {108return c;109}110}111return null;112}113114// check that a connector is of the expected type115static void type_match(String arg_name, String arg_type, Connector.Argument arg) {116boolean fail = false;117if (arg_type.equals("Connector.StringArgument")) {118if (!(arg instanceof Connector.StringArgument)) {119fail = true;120}121}122if (arg_type.equals("Connector.IntegerArgument")) {123if (!(arg instanceof Connector.IntegerArgument)) {124fail = true;125}126}127if (arg_type.equals("Connector.BooleanArgument")) {128if (!(arg instanceof Connector.BooleanArgument)) {129fail = true;130}131}132if (arg_type.equals("Connector.SelectedArgument")) {133if (!(arg instanceof Connector.IntegerArgument)) {134fail = true;135}136}137if (fail) {138fail(arg_name + " is of type: " + arg.getClass() + ", expected: "139+ arg_type);140}141}142143144// check that a Connector is compatible145static void check(Object[] desc, Connector connector) {146String name = (String)desc[0];147String transport_name = (String)desc[1];148149// if the transport name is "null" it means its transport may150// vary (eg: SunCommandLineLauncher will choose shared memory151// on Windows and dt_socket on Solaris). In that case we can't152// check the transport name.153//154if (transport_name != null) {155System.out.println("Checking transpot name");156if (!(transport_name.equals(connector.transport().name()))) {157fail("transport().name() returns: " +158connector.transport().name() + ", expected: " + transport_name);159}160}161162// check that all the old arguments still exist163for (int i=2; i<desc.length; i++) {164String[] args = (String[])desc[i];165String arg_name = args[0];166String arg_type = args[1];167String arg_mandatory = args[2];168169System.out.println("Checking argument: " + arg_name);170171// check that the arg still exists172Map defaultArgs = connector.defaultArguments();173Object value = defaultArgs.get(arg_name);174if (value == null) {175fail(name + " is missing Connector.Argument: " + arg_name);176continue;177}178179// next check that the type matches180Connector.Argument connector_arg = (Connector.Argument)value;181182// check that the argument type hasn't changed183type_match(arg_name, arg_type, connector_arg);184185// check that an optional argument has been made mandatory186if (arg_mandatory.equals("false")) {187if (connector_arg.mustSpecify()) {188fail(arg_name + " is now mandatory");189}190}191}192193// next we check for new arguments that are mandatory but194// have no default value195196System.out.println("Checking for new arguments");197Map dfltArgs = connector.defaultArguments();198Iterator iter = dfltArgs.keySet().iterator();199while (iter.hasNext()) {200String arg_name = (String)iter.next();201202// see if the argument is new203boolean found = false;204for (int j=2; j<desc.length; j++) {205String[] args = (String[])desc[j];206if (args[0].equals(arg_name)) {207found = true;208break;209}210}211212if (!found) {213Connector.Argument connector_arg =214(Connector.Argument)dfltArgs.get(arg_name);215216if (connector_arg.mustSpecify()) {217String value = connector_arg.value();218if (value.equals("")) {219value = null;220}221if (value == null) {222fail("New Connector.Argument \"" + connector_arg.name() +223"\" added - argument is mandatory");224}225}226}227}228}229230231// compare the actual list of Connectors against the232// expected list of Connectors.233static void compare(Object[][] prev, List list) {234String os = System.getProperty("os.name");235for (int i=0; i<prev.length; i++) {236Object[] desc = prev[i];237String name = (String)desc[0];238239// ignore Windows specific Connectors are non-Windows machines240if (!(os.startsWith("Windows"))) {241if (name.equals("com.sun.jdi.SharedMemoryAttach") ||242name.equals("com.sun.jdi.SharedMemoryListen")) {243continue;244}245}246247System.out.println("");248System.out.println("Checking Connector " + name);249250// check that the Connector exists251Connector c = find(name, list);252if (c == null) {253fail("Connector is missing");254continue;255}256257check(desc, c);258}259}260261public static void main(String args[]) throws Exception {262VirtualMachineManager vmm = Bootstrap.virtualMachineManager();263264// in 1.2/1.3/1.4 the defualtConnector was265// com.sun.jdi.CommandLineLaunch. Many debuggers probably266// depend on this so check that it's always the default.267//268String expected = "com.sun.jdi.CommandLineLaunch";269System.out.println("Checking that defaultConnector is: " + expected);270String dflt = vmm.defaultConnector().name();271if (!(dflt.equals(expected))) {272System.err.println("defaultConnector() is: " + dflt +273", expected:" + expected);274failures++;275} else {276System.out.println("Okay");277}278279compare(attachingConnectors(), vmm.attachingConnectors());280compare(listeningConnectors(), vmm.listeningConnectors());281compare(launchingConnectors(), vmm.launchingConnectors());282283// test results284if (failures > 0) {285System.out.println("");286throw new RuntimeException(failures + " test(s) failed");287}288}289}290291292