Home | History | Annotate | Download | only in framework
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  */
     18 
     19 /**
     20  * @author Vitaly A. Provodin
     21  */
     22 
     23 /**
     24  * Created on 26.01.2005
     25  */
     26 package org.apache.harmony.jpda.tests.framework;
     27 
     28 import java.util.HashMap;
     29 
     30 /**
     31  * This class provides access to options for running JPDA tests.
     32  * <p>
     33  * The settings are presented as a set of getters and setters for test options,
     34  * which can be implemented in different ways. In this implementation test
     35  * options are implemented via VM system properties, which can be specified
     36  * using option '-D' in VM command line.
     37  * <p>
     38  * The following options are currently recognized:
     39  * <ul>
     40  * <li><code>jpda.settings.debuggeeJavaHome</code>
     41  *   - path to Java bundle to run debuggee on
     42  * <li><code>jpda.settings.debuggeeJavaExec</code>
     43  *   - name of Java executable to run debuggee on
     44  * <li><code>jpda.settings.debuggeeJavaPath</code>
     45  *   - full path to Java executable to run debuggee on
     46  * <li><code>jpda.settings.debuggeeAgentName</code>
     47  *   - name of agent native library
     48  * <li><code>jpda.settings.debuggeeAgentExtraOptions</code>
     49  *   - extra options for agent
     50  * <li><code>jpda.settings.debuggeeClasspath</code>
     51  *   - classpath to run debuggee with
     52  * <li><code>jpda.settings.debuggeeClassName</code>
     53  *   - full name of class to run debuggee with
     54  * <li><code>jpda.settings.debuggeeVMExtraOptions</code>
     55  *   - extra options to run debuggee with
     56  * <li><code>jpda.settings.debuggeeSuspend</code>
     57  *   - debuggee suspend mode ("y"|"n")
     58  * <li><code>jpda.settings.transportWrapperClass</code>
     59  *   - class name of TransportWrapper implementation
     60  * <li><code>jpda.settings.transportAddress</code>
     61  *   - address for JDWP connection
     62  * <li><code>jpda.settings.connectorKind</code>
     63  *   - type of JDWP connection (attach or listen)
     64  * <li><code>jpda.settings.syncPort</code>
     65  *   - port number for sync connection (chosen by the OS if not set or set to 0)
     66  * <li><code>jpda.settings.timeout</code>
     67  *   - timeout used in JPDA tests
     68  * <li><code>jpda.settings.waitingTime</code>
     69  *   - timeout for waiting events
     70  * </ul>
     71  * <li><code>jpda.settings.verbose</code>
     72  *   - flag that disables (default) or enables writing messages to the log
     73  * </ul>
     74  * All options have default values, if they are not specified.
     75  *
     76  */
     77 public class TestOptions {
     78 
     79     /** Default timeout value for various operations. */
     80     public static final int DEFAULT_TIMEOUT = 1 * 60 * 1000; // 1 minute
     81 
     82     /** Default time interval for waiting for various events. */
     83     public static final int DEFAULT_WAITING_TIME = DEFAULT_TIMEOUT;
     84 
     85     /** Default static address for transport connection. */
     86     public static final String DEFAULT_ATTACHING_ADDRESS = "127.0.0.1:9898";
     87 
     88     /** Default port number for sync connection. */
     89     public static final String DEFAULT_STATIC_SYNC_PORT = "9797";
     90 
     91     /** Default port number for sync connection. */
     92     public static final int DEFAULT_SYNC_PORT = 0;
     93 
     94     /** Default class name for transport wrapper. */
     95     public static final String DEFAULT_TRANSPORT_WRAPPER
     96         = "org.apache.harmony.jpda.tests.framework.jdwp.SocketTransportWrapper";
     97 
     98     /** Default aclass name for debuggee application. */
     99     public static final String DEFAULT_DEBUGGEE_CLASS_NAME
    100         = "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
    101 
    102     // current waiting time value (negative means using default value)
    103     private long waitingTime = -1;
    104 
    105     // current timeout value (negative means using default value)
    106     private long timeout = -1;
    107 
    108     // internally set property values
    109     private HashMap<String, String> internalProperties = new HashMap<>();
    110 
    111     /**
    112      * Constructs an instance of this class.
    113      */
    114     public TestOptions() {
    115         super();
    116     }
    117 
    118     /**
    119      * Returns path to Java bundle to run debuggee on.
    120      *
    121      * @return option "jpda.settings.debuggeeJavaHome" or system property
    122      *         "java.home" by default.
    123      */
    124     public String getDebuggeeJavaHome() {
    125         return getProperty("jpda.settings.debuggeeJavaHome", getProperty("java.home", null));
    126     }
    127 
    128     /**
    129      * Returns name of Java executable to run debuggee on.
    130      *
    131      * @return option "jpda.settings.debuggeeJavaExec" or "java" by default.
    132      */
    133     public String getDebuggeeJavaExec() {
    134         return getProperty("jpda.settings.debuggeeJavaExec", "java");
    135     }
    136 
    137     /**
    138      * Returns full path to Java executable to run debuggee on.
    139      *
    140      * @return option "jpda.settings.debuggeeJavaPath" or construct path from
    141      *         getDebuggeeJavaHome() and getDebuggeeJavaExec() by default.
    142      */
    143     public String getDebuggeeJavaPath() {
    144         return getProperty("jpda.settings.debuggeeJavaPath",
    145                 getDebuggeeJavaHome() + "/bin/" + getDebuggeeJavaExec());
    146     }
    147 
    148     /**
    149      * Returns class name of TransportWrapper implementation.
    150      *
    151      * @return option "jpda.settings.transportWrapperClass" or
    152      *         DEFAULT_TRANSPORT_WRAPPER by default.
    153      */
    154     public String getTransportWrapperClassName() {
    155         return getProperty("jpda.settings.transportWrapperClass",
    156                 DEFAULT_TRANSPORT_WRAPPER);
    157     }
    158 
    159     /**
    160      * Returns address for JDWP connection or null for dynamic address.
    161      *
    162      * @return option "jpda.settings.transportAddress" or null by default.
    163      */
    164     public String getTransportAddress() {
    165         return getProperty("jpda.settings.transportAddress", null);
    166     }
    167 
    168     /**
    169      * Sets address to attach to debuggee.
    170      *
    171      * @param address to attach
    172      */
    173     public void setTransportAddress(String address) {
    174         setProperty("jpda.settings.transportAddress", address);
    175     }
    176 
    177     /**
    178      * Returns name of JDWP agent library.
    179      *
    180      * @return option "jpda.settings.debuggeeAgentName" or "jdwp" by default
    181      */
    182     public String getDebuggeeAgentName() {
    183         return getProperty("jpda.settings.debuggeeAgentName", "jdwp");
    184     }
    185 
    186     /**
    187      * Returns string with extra options for agent.
    188      *
    189      * @return option "jpda.settings.debuggeeAgentExtraOptions" or "" by default
    190      */
    191     public String getDebuggeeAgentExtraOptions() {
    192         return getProperty("jpda.settings.debuggeeAgentExtraOptions", "");
    193     }
    194 
    195     /**
    196      * Returns string with all options for agent including specified connection
    197      * address.
    198      *
    199      * @param address - address to attach
    200      * @param isDebuggerListen - true if debugger is listening for connection
    201      *
    202      * @return string with all agent options
    203      */
    204     public String getDebuggeeAgentOptions(String address, boolean isDebuggerListen) {
    205         String serv;
    206         if (isDebuggerListen) {
    207             serv = "n";
    208         } else {
    209             serv = "y";
    210         }
    211 
    212         // add ',' to agent extra options if required
    213         String agentExtraOptions = getDebuggeeAgentExtraOptions();
    214         if (agentExtraOptions.length() > 0 && agentExtraOptions.charAt(0) != ',') {
    215             agentExtraOptions = "," + agentExtraOptions;
    216         }
    217 
    218         return getProperty("jpda.settings.debuggeeAgentOptions",
    219                 "transport=dt_socket,address=" + address + ",server=" + serv
    220                 + ",suspend=" + getDebuggeeSuspend() + agentExtraOptions);
    221     }
    222 
    223     /**
    224      * Returns VM classpath value to run debuggee with.
    225      *
    226      * @return option "jpda.settings.debuggeeClasspath" or system property "java.class.path" by
    227      * default.
    228      */
    229     public String getDebuggeeClassPath() {
    230         return getProperty("jpda.settings.debuggeeClasspath", getProperty("java.class.path", null));
    231     }
    232 
    233     /**
    234      * Returns full name of the class to start debuggee with.
    235      *
    236      * @return option "jpda.settings.debuggeeClassName" or
    237      *         "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld" by default
    238      */
    239     public String getDebuggeeClassName() {
    240         return getProperty("jpda.settings.debuggeeClassName", DEFAULT_DEBUGGEE_CLASS_NAME);
    241     }
    242 
    243     /**
    244      * Sets full name of the class to start debuggee with.
    245      *
    246      * @param className
    247      *            full class name
    248      */
    249     public void setDebuggeeClassName(String className) {
    250         setProperty("jpda.settings.debuggeeClassName", className);
    251     }
    252 
    253     /**
    254      * Returns string with extra options to start debuggee with.
    255      *
    256      * @return option "jpda.settings.debuggeeVMExtraOptions" or "" by default
    257      */
    258     public String getDebuggeeVMExtraOptions() {
    259         String extOpts = getProperty("jpda.settings.debuggeeVMExtraOptions", "");
    260         extOpts = extOpts + " -Djpda.settings.verbose=" + isVerbose();
    261         return extOpts;
    262     }
    263 
    264     /**
    265      * Returns debuggee suspend mode ("y"|"n").
    266      *
    267      * @return option "jpda.settings.debuggeeSuspend" or "y" by default
    268      */
    269     public String getDebuggeeSuspend() {
    270         return getProperty("jpda.settings.debuggeeSuspend", "y");
    271     }
    272 
    273     /**
    274      * Returns debuggee suspend mode ("y"|"n").
    275      *
    276      * @param mode
    277      *            suspend mode
    278      */
    279     public void setDebuggeeSuspend(String mode) {
    280         setProperty("jpda.settings.debuggeeSuspend", mode);
    281     }
    282 
    283     /**
    284      * Checks if debuggee is launched in suspend mode.
    285      *
    286      * @return true if debuggee is launched in suspend mode
    287      */
    288     public boolean isDebuggeeSuspend() {
    289         return getDebuggeeSuspend().equals("y");
    290     }
    291 
    292     /**
    293      * Returns string representation of TCP/IP port for synchronization channel.
    294      *
    295      * @return string with port number or null
    296      */
    297     public String getSyncPortString() {
    298         return getProperty("jpda.settings.syncPort", null);
    299     }
    300 
    301     /**
    302      * Returns type of connection with debuggee.
    303      *
    304      * @return system property "jpda.settings.connectorKind" or "listen" by default.
    305      */
    306     public String getConnectorKind() {
    307         return getProperty("jpda.settings.connectorKind", "listen");
    308     }
    309 
    310     /**
    311      * Checks if attach connection with debuggee.
    312      *
    313      * @return true, if attach connection, false otherwise.
    314      */
    315     public boolean isAttachConnectorKind() {
    316         return ((getConnectorKind()).equals("attach"));
    317 
    318     }
    319 
    320     /**
    321      * Checks if listen connection with debuggee.
    322      *
    323      * @return true, if listen connection, false otherwise.
    324      */
    325     public boolean isListenConnectorKind() {
    326         return (getConnectorKind().equals("listen"));
    327     }
    328 
    329     /**
    330      * Sets connectorKind to attach to debuggee.
    331      */
    332     public void setAttachConnectorKind() {
    333         setConnectorKind("attach");
    334     }
    335 
    336     /**
    337      * Sets connectorKind to listen connection from debuggee.
    338      */
    339     public void setListenConnectorKind() {
    340         setConnectorKind("listen");
    341     }
    342 
    343     /**
    344      * Sets kind of connector (attach or listen).
    345      */
    346     public void setConnectorKind(String kind) {
    347         setProperty("jpda.settings.connectorKind", kind);
    348     }
    349 
    350     /**
    351      * Returns kind of launching debuggee VM, which can be "auto" or "manual".
    352      *
    353      * @return option "jpda.settings.debuggeeLaunchKind" or "auto" by default.
    354      */
    355     public String getDebuggeeLaunchKind() {
    356         return getProperty("jpda.settings.debuggeeLaunchKind", "auto");
    357     }
    358 
    359     /**
    360      * Returns TCP/IP port for synchronization channel.
    361      *
    362      * @return port number if it is set, or DEFAULT_SYNC_PORT otherwise.
    363      */
    364     public int getSyncPortNumber() {
    365         String buf = getSyncPortString();
    366         if (buf == null) {
    367             return DEFAULT_SYNC_PORT;
    368         }
    369 
    370         try {
    371             return Integer.parseInt(buf);
    372         } catch (NumberFormatException e) {
    373             throw new TestErrorException(e);
    374         }
    375     }
    376 
    377     /**
    378      * Returns timeout for JPDA tests in milliseconds.
    379      *
    380      * @return option "jpda.settings.timeout" or DEFAULT_TIMEOUT by default.
    381      */
    382     public long getTimeout() {
    383         if (timeout < 0) {
    384             timeout = DEFAULT_TIMEOUT;
    385             String buf = getProperty("jpda.settings.timeout", null);
    386             if (buf != null) {
    387                 try {
    388                     timeout = Long.parseLong(buf);
    389                 } catch (NumberFormatException e) {
    390                     throw new TestErrorException(e);
    391                 }
    392             }
    393         }
    394         return timeout;
    395     }
    396 
    397     /**
    398      * Sets timeout for JPDA tests in milliseconds.
    399      *
    400      * @param timeout
    401      *            timeout to be set
    402      */
    403     public void setTimeout(long timeout) {
    404         if (timeout < 0) {
    405             throw new TestErrorException("Cannot set negative timeout value: "
    406                     + timeout);
    407         }
    408         this.timeout = timeout;
    409     }
    410 
    411     /**
    412      * Returns waiting time for events in milliseconds.
    413      *
    414      * @return waiting time
    415      */
    416     public long getWaitingTime() {
    417         if (waitingTime < 0) {
    418             waitingTime = DEFAULT_WAITING_TIME;
    419             String buf = getProperty("jpda.settings.waitingTime", null);
    420             if (buf != null) {
    421                 try {
    422                     waitingTime = Long.parseLong(buf);
    423                 } catch (NumberFormatException e) {
    424                     throw new TestErrorException(e);
    425                 }
    426             }
    427         }
    428         return waitingTime;
    429     }
    430 
    431     /**
    432      * Sets waiting time for events in milliseconds.
    433      *
    434      * @param waitingTime
    435      *            waiting time to be set
    436      */
    437     public void setWaitingTime(long waitingTime) {
    438         this.waitingTime = waitingTime;
    439     }
    440 
    441     /**
    442      * Returns whether print to log is enabled.
    443      *
    444      * @return false (default) if log is disabled or true otherwise.
    445      */
    446     public boolean isVerbose() {
    447         return isTrue(getProperty("jpda.settings.verbose", "true"));
    448     }
    449 
    450     /**
    451      * Converts text to boolean.
    452      *
    453      * @param str string representing boolean value
    454      * @return boolean
    455      */
    456     static public boolean isTrue(String str) {
    457         return str != null && (
    458             str.equalsIgnoreCase("true") ||
    459             str.equalsIgnoreCase("yes") ||
    460             str.equalsIgnoreCase("on") ||
    461             str.equals("1"));
    462     }
    463 
    464     /**
    465      * Returns value of given property if it was set internally or specified in system properties.
    466      *
    467      * @param name
    468      *           property name
    469      * @param defaultValue
    470      *           default value for given property
    471      * @return string value of given property or default value if no such property found
    472      */
    473     protected String getProperty(String name, String defaultValue) {
    474         String value = internalProperties.get(name);
    475         if (value != null) {
    476             return value;
    477         }
    478         return System.getProperty(name, defaultValue);
    479     }
    480 
    481     /**
    482      * Sets internal value of given property to override corresponding system property.
    483      *
    484      * @param name
    485      *           proparty name
    486      * @param value
    487      *           value for given property
    488      */
    489     protected void setProperty(String name, String value) {
    490         internalProperties.put(name, value);
    491     }
    492 
    493 }
    494