< prev index next >

test/java/rmi/testlibrary/JavaVM.java

Print this page




   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 java.io.File;
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.util.Arrays;
  28 import java.util.StringTokenizer;
  29 import java.util.concurrent.TimeoutException;
  30 
  31 /**
  32  * RMI regression test utility class that uses Runtime.exec to spawn a
  33  * java process that will run a named java class.
  34  */
  35 public class JavaVM {
  36 















  37     public static final long POLLTIME_MS = 100L;
  38 
  39     protected Process vm = null;
  40 
  41     private String classname = "";
  42     private String args = "";
  43     private String options = "";
  44     private OutputStream outputStream = System.out;
  45     private OutputStream errorStream = System.err;
  46     private String policyFileName = null;
  47     private StreamPipe outPipe;
  48     private StreamPipe errPipe;
  49 
  50     private static void mesg(Object mesg) {
  51         System.err.println("JAVAVM: " + mesg.toString());
  52     }
  53 
  54     /** string name of the program execd by JavaVM */
  55     private static String javaProgram = "java";
  56 
  57     static {
  58         try {
  59             javaProgram = TestLibrary.getProperty("java.home", "") +
  60                 File.separator + "bin" + File.separator + javaProgram;
  61         } catch (SecurityException se) {
  62         }
  63     }
  64 
  65     public JavaVM(String classname,
  66                   String options, String args) {
  67         this.classname = classname;
  68         this.options = options;
  69         this.args = args;
  70     }
  71 
  72     public JavaVM(String classname,
  73                   String options, String args,
  74                   OutputStream out, OutputStream err) {
  75         this(classname, options, args);
  76         this.outputStream = out;
  77         this.errorStream = err;
  78     }
  79 
  80     // Prepends passed opts array to current options
  81     public void addOptions(String... opts) {
  82         String newOpts = "";
  83         for (int i = 0 ; i < opts.length ; i ++) {
  84             newOpts += " " + opts[i];
  85         }
  86         newOpts += " ";
  87         options = newOpts + options;
  88     }
  89 
  90     // Prepends passed arguments array to current args
  91     public void addArguments(String... arguments) {
  92         String newArgs = "";
  93         for (int i = 0 ; i < arguments.length ; i ++) {
  94             newArgs += " " + arguments[i];
  95         }
  96         newArgs += " ";
  97         args = newArgs + args;
  98     }
  99 
 100     public void setPolicyFile(String policyFileName) {
 101         this.policyFileName = policyFileName;
 102     }
 103 
 104     /**
 105      * This method is used for setting VM options on spawned VMs.
 106      * It returns the extra command line options required
 107      * to turn on jcov code coverage analysis.
 108      */
 109     protected static String getCodeCoverageOptions() {
 110         return TestLibrary.getExtraProperty("jcov.options","");
 111     }
 112 













 113     /**
 114      * Exec the VM as specified in this object's constructor.
 115      */
 116     public void start() throws IOException {
 117 
 118         if (vm != null)
 119             throw new IllegalStateException("JavaVM already started");
 120 
 121         /*
 122          * If specified, add option for policy file
 123          */
 124         if (policyFileName != null) {
 125             String option = "-Djava.security.policy=" + policyFileName;
 126             addOptions(new String[] { option });
 127         }
 128 
 129         addOptions(new String[] {
 130             getCodeCoverageOptions(),
 131             TestParams.testJavaOpts,
 132             TestParams.testVmOpts




   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 java.io.ByteArrayOutputStream;
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.io.OutputStream;
  28 import java.util.Arrays;
  29 import java.util.StringTokenizer;
  30 import java.util.concurrent.TimeoutException;
  31 
  32 /**
  33  * RMI regression test utility class that uses Runtime.exec to spawn a
  34  * java process that will run a named java class.
  35  */
  36 public class JavaVM {
  37 
  38     static class CachedOutputStream extends OutputStream {
  39         ByteArrayOutputStream ba;
  40         OutputStream os;
  41 
  42         public CachedOutputStream(OutputStream os) {
  43             this.os = os;
  44             this.ba = new ByteArrayOutputStream();
  45         }
  46 
  47         public void write(int b) throws IOException {
  48             ba.write(b);
  49             os.write(b);
  50         }
  51     }
  52 
  53         public static final long POLLTIME_MS = 100L;
  54 
  55     protected Process vm = null;
  56 
  57     private String classname = "";
  58     private String args = "";
  59     private String options = "";
  60     private CachedOutputStream outputStream = new CachedOutputStream(System.out);
  61     private CachedOutputStream errorStream = new CachedOutputStream(System.err);
  62     private String policyFileName = null;
  63     private StreamPipe outPipe;
  64     private StreamPipe errPipe;
  65 
  66     private static void mesg(Object mesg) {
  67         System.err.println("JAVAVM: " + mesg.toString());
  68     }
  69 
  70     /** string name of the program execd by JavaVM */
  71     private static String javaProgram = "java";
  72 
  73     static {
  74         try {
  75             javaProgram = TestLibrary.getProperty("java.home", "") +
  76                 File.separator + "bin" + File.separator + javaProgram;
  77         } catch (SecurityException se) {
  78         }
  79     }
  80 
  81     public JavaVM(String classname,
  82                   String options, String args) {
  83         this.classname = classname;
  84         this.options = options;
  85         this.args = args;
  86     }
  87 
  88     public JavaVM(String classname,
  89                   String options, String args,
  90                   OutputStream out, OutputStream err) {
  91         this(classname, options, args);
  92         this.outputStream = new CachedOutputStream(out);
  93         this.errorStream = new CachedOutputStream(err);
  94     }
  95 
  96     // Prepends passed opts array to current options
  97     public void addOptions(String... opts) {
  98         String newOpts = "";
  99         for (int i = 0 ; i < opts.length ; i ++) {
 100             newOpts += " " + opts[i];
 101         }
 102         newOpts += " ";
 103         options = newOpts + options;
 104     }
 105 
 106     // Prepends passed arguments array to current args
 107     public void addArguments(String... arguments) {
 108         String newArgs = "";
 109         for (int i = 0 ; i < arguments.length ; i ++) {
 110             newArgs += " " + arguments[i];
 111         }
 112         newArgs += " ";
 113         args = newArgs + args;
 114     }
 115 
 116     public void setPolicyFile(String policyFileName) {
 117         this.policyFileName = policyFileName;
 118     }
 119 
 120     /**
 121      * This method is used for setting VM options on spawned VMs.
 122      * It returns the extra command line options required
 123      * to turn on jcov code coverage analysis.
 124      */
 125     protected static String getCodeCoverageOptions() {
 126         return TestLibrary.getExtraProperty("jcov.options","");
 127     }
 128 
 129     public boolean outputContains(String str) {
 130         return outputStream.ba.toString().indexOf(str) != -1
 131                 || errorStream.ba.toString().indexOf(str) != -1;
 132     }
 133 
 134     private String outputString() {
 135         return outputStream.ba.toString();
 136     }
 137 
 138     private String errorString() {
 139         return errorStream.ba.toString();
 140     }
 141 
 142     /**
 143      * Exec the VM as specified in this object's constructor.
 144      */
 145     public void start() throws IOException {
 146 
 147         if (vm != null)
 148             throw new IllegalStateException("JavaVM already started");
 149 
 150         /*
 151          * If specified, add option for policy file
 152          */
 153         if (policyFileName != null) {
 154             String option = "-Djava.security.policy=" + policyFileName;
 155             addOptions(new String[] { option });
 156         }
 157 
 158         addOptions(new String[] {
 159             getCodeCoverageOptions(),
 160             TestParams.testJavaOpts,
 161             TestParams.testVmOpts


< prev index next >