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