blob: 6ccae911a8659758be056bbdaf82b3743ec75c84 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
alanb0d058232012-11-02 15:50:11 +00002 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
smarksf34bec82012-12-20 20:11:45 -080024import java.io.File;
25import java.io.IOException;
26import java.io.OutputStream;
duke6e45e102007-12-01 00:00:00 +000027import java.util.Arrays;
duke6e45e102007-12-01 00:00:00 +000028import java.util.StringTokenizer;
29
30/**
31 * RMI regression test utility class that uses Runtime.exec to spawn a
32 * java process that will run a named java class.
33 */
34public class JavaVM {
35
duke6e45e102007-12-01 00:00:00 +000036 protected Process vm = null;
37
38 private String classname = "";
39 private String args = "";
40 private String options = "";
41 private OutputStream outputStream = System.out;
42 private OutputStream errorStream = System.err;
43 private String policyFileName = null;
44
45 private static void mesg(Object mesg) {
46 System.err.println("JAVAVM: " + mesg.toString());
47 }
48
49 /** string name of the program execd by JavaVM */
50 private static String javaProgram = "java";
51
52 static {
53 try {
54 javaProgram = TestLibrary.getProperty("java.home", "") +
55 File.separator + "bin" + File.separator + javaProgram;
56 } catch (SecurityException se) {
57 }
58 }
59
60 public JavaVM(String classname) {
61 this.classname = classname;
62 }
63 public JavaVM(String classname,
64 String options, String args) {
65 this.classname = classname;
66 this.options = options;
67 this.args = args;
68 }
69
70 public JavaVM(String classname,
71 String options, String args,
72 OutputStream out, OutputStream err) {
73 this(classname, options, args);
74 this.outputStream = out;
75 this.errorStream = err;
76 }
77
olagneaub58eaf42012-05-11 14:13:29 -070078 // Prepends passed opts array to current options
duke6e45e102007-12-01 00:00:00 +000079 public void addOptions(String[] opts) {
80 String newOpts = "";
81 for (int i = 0 ; i < opts.length ; i ++) {
82 newOpts += " " + opts[i];
83 }
84 newOpts += " ";
85 options = newOpts + options;
86 }
olagneaub58eaf42012-05-11 14:13:29 -070087
88 // Prepends passed arguments array to current args
duke6e45e102007-12-01 00:00:00 +000089 public void addArguments(String[] arguments) {
90 String newArgs = "";
91 for (int i = 0 ; i < arguments.length ; i ++) {
92 newArgs += " " + arguments[i];
93 }
94 newArgs += " ";
95 args = newArgs + args;
96 }
97
98 public void setPolicyFile(String policyFileName) {
99 this.policyFileName = policyFileName;
100 }
101
102 /**
103 * This method is used for setting VM options on spawned VMs.
104 * It returns the extra command line options required
105 * to turn on jcov code coverage analysis.
106 */
107 protected static String getCodeCoverageOptions() {
108 return TestLibrary.getExtraProperty("jcov.options","");
109 }
110
111
112 /**
113 * Exec the VM as specified in this object's constructor.
114 */
115 public void start() throws IOException {
116
smarksf34bec82012-12-20 20:11:45 -0800117 if (vm != null)
118 throw new IllegalStateException("JavaVM already started");
duke6e45e102007-12-01 00:00:00 +0000119
120 /*
121 * If specified, add option for policy file
122 */
123 if (policyFileName != null) {
124 String option = "-Djava.security.policy=" + policyFileName;
125 addOptions(new String[] { option });
126 }
127
128 addOptions(new String[] { getCodeCoverageOptions() });
129
130 StringTokenizer optionsTokenizer = new StringTokenizer(options);
131 StringTokenizer argsTokenizer = new StringTokenizer(args);
132 int optionsCount = optionsTokenizer.countTokens();
133 int argsCount = argsTokenizer.countTokens();
134
135 String javaCommand[] = new String[optionsCount + argsCount + 2];
136 int count = 0;
137
138 javaCommand[count++] = JavaVM.javaProgram;
139 while (optionsTokenizer.hasMoreTokens()) {
140 javaCommand[count++] = optionsTokenizer.nextToken();
141 }
142 javaCommand[count++] = classname;
143 while (argsTokenizer.hasMoreTokens()) {
144 javaCommand[count++] = argsTokenizer.nextToken();
145 }
146
147 mesg("command = " + Arrays.asList(javaCommand).toString());
148 System.err.println("");
149
150 vm = Runtime.getRuntime().exec(javaCommand);
151
152 /* output from the execed process may optionally be captured. */
smarksf34bec82012-12-20 20:11:45 -0800153 StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
154 StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
duke6e45e102007-12-01 00:00:00 +0000155 }
156
157 public void destroy() {
158 if (vm != null) {
159 vm.destroy();
160 }
161 vm = null;
162 }
163
164 protected Process getVM() {
165 return vm;
166 }
167}