1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import com.sun.jdi.connect.*;
  25 import com.sun.jdi.*;
  26 import java.util.Map;
  27 import java.util.List;
  28 import jdk.test.lib.Asserts;
  29 
  30 /*
  31  * @test
  32  * @summary Verifies that PathSearchingVirtualMachine.bootClassPath()
  33  *          returns an empty list in case no bootclass path specified
  34  *          regardless of sun.boot.class.path option, which is now obsolete
  35  *
  36  * @library /test/lib
  37  *
  38  * @compile TestClass.java
  39  * @compile SunBootClassPathEmptyTest.java
  40  * @run main/othervm SunBootClassPathEmptyTest
  41  */
  42 public class SunBootClassPathEmptyTest {
  43 
  44     /**
  45      * Helper class to facilitate the debuggee VM launching
  46      */
  47     private static class VmConnector {
  48 
  49         LaunchingConnector lc;
  50         VirtualMachine vm;
  51 
  52         VmConnector() {
  53             for (LaunchingConnector c : Bootstrap.virtualMachineManager().launchingConnectors()) {
  54                 System.out.println("name: " + c.name());
  55                 if (c.name().equals("com.sun.jdi.CommandLineLaunch")) {
  56                     lc = c;
  57                     break;
  58                 }
  59             }
  60             if (lc == null) {
  61                 throw new RuntimeException("Connector not found");
  62             }
  63         }
  64 
  65         PathSearchingVirtualMachine launchVm(String cmdLine, String options) throws Exception {
  66             Map<String, Connector.Argument> vmArgs = lc.defaultArguments();
  67             vmArgs.get("main").setValue(cmdLine);
  68             if (options != null) {
  69                 vmArgs.get("options").setValue(options);
  70             }
  71             System.out.println("Debugger is launching vm ...");
  72             vm = lc.launch(vmArgs);
  73             if (!(vm instanceof PathSearchingVirtualMachine)) {
  74                 throw new RuntimeException("VM is not a PathSearchingVirtualMachine");
  75             }
  76             return (PathSearchingVirtualMachine) vm;
  77         }
  78 
  79     }
  80 
  81     private static VmConnector connector = new VmConnector();
  82 
  83     public static void main(String[] args) throws Exception {
  84         testWithObsoleteClassPathOption(null);
  85         testWithObsoleteClassPathOption("someclasspath");
  86     }
  87 
  88     private static void testWithObsoleteClassPathOption(String obsoleteClassPath) throws Exception {
  89         PathSearchingVirtualMachine vm = connector.launchVm("TestClass", makeClassPathOptions(obsoleteClassPath));
  90         List<String> bootClassPath = vm.bootClassPath();
  91         Asserts.assertNotNull(bootClassPath, "Expected bootClassPath to be empty but was null");
  92         Asserts.assertEquals(0, bootClassPath.size(), "Expected bootClassPath.size() 0 but was: " + bootClassPath.size());
  93     }
  94 
  95     private static String makeClassPathOptions(String obsoleteClassPath) {
  96         return obsoleteClassPath == null ? null : "-Dsun.boot.class.path=" + obsoleteClassPath;
  97     }
  98 
  99 }