1 /*
   2  * Copyright (c) 2002, 2015, 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 /**
  25  *  @test
  26  *  @bug 4446294
  27  *  @summary JDI spec/impl: default "home" for CommandLineLaunch isn't java.home
  28  *
  29  *  @author Tim Bell (based on "HomeTest.java" by Eugene I. Latkin)
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g HomeTest.java
  34  *  @run driver HomeTest
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.connect.*;
  38 import com.sun.jdi.event.*;
  39 import com.sun.jdi.request.*;
  40 
  41 import java.util.*;
  42 
  43     /********** target program **********/
  44 
  45 class HomeTarg {
  46     public static void main(String[] args){
  47         System.out.println("Howdy!");
  48         System.out.println("Goodbye from HomeTarg!");
  49     }
  50 }
  51 
  52     /********** test program **********/
  53 
  54 public class HomeTest extends TestScaffold {
  55     ReferenceType targetClass;
  56     ThreadReference mainThread;
  57 
  58     HomeTest (String args[]) {
  59         super(args);
  60     }
  61 
  62     public static void main(String[] args)      throws Exception {
  63         new HomeTest(args).startTests();
  64     }
  65 
  66     /********** test core **********/
  67     private static String getValue(Map arguments, String key) {
  68         Connector.Argument x = (Connector.StringArgument) arguments.get(key);
  69         return x.value();
  70     }
  71     protected void runTests() throws Exception {
  72         /*
  73          * Get to the top of main()
  74          * to determine targetClass and mainThread
  75          */
  76         BreakpointEvent bpe = startToMain("HomeTarg");
  77         targetClass = bpe.location().declaringType();
  78         mainThread = bpe.thread();
  79         EventRequestManager erm = vm().eventRequestManager();
  80 
  81         VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
  82         Connector defaultConnector = vmm.defaultConnector();
  83         Map arguments = defaultConnector.defaultArguments();
  84         String argsHome = getValue(arguments,"home");
  85         String javaHome = System.getProperty("java.home");
  86         if (!argsHome.equals(javaHome)) {
  87             failure("FAILURE: Value for \"home\" does not match value for \"java.home\"");
  88             failure("     home is: " + argsHome);
  89         }
  90         println("java.home is: " + javaHome);
  91         /*
  92          * resume the target listening for events
  93          */
  94         listenUntilVMDisconnect();
  95 
  96         /*
  97          * deal with results of test
  98          * if anything has called failure("foo") testFailed will be true
  99          */
 100         if (!testFailed) {
 101             println("HomeTest: passed");
 102         } else {
 103             throw new Exception("HomeTest: failed");
 104         }
 105     }
 106 }