001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.tools.derbyrunjartest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.tools;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.lang.Process;
027: import java.lang.Runtime;
028: import java.lang.SecurityException;
029: import java.net.URL;
030: import java.security.CodeSource;
031: import java.util.Vector;
032:
033: import org.apache.derbyTesting.functionTests.harness.BackgroundStreamSaver;
034: import org.apache.derbyTesting.functionTests.harness.jvm;
035:
036: public class derbyrunjartest {
037:
038: public static void main(String[] args) throws Exception {
039: // get location of run class.
040: CodeSource cs = null;
041: try {
042: cs = org.apache.derby.iapi.tools.run.class
043: .getProtectionDomain().getCodeSource();
044: } catch (SecurityException se) {
045: System.out
046: .println("Security exception: " + se.getMessage());
047: }
048:
049: URL result = cs.getLocation();
050: jvm jvm = null;
051: String derbyrunloc = null;
052:
053: if (result.toString().endsWith(".jar")) {
054: derbyrunloc = result.toString().substring(5);
055: if (System.getProperty("os.name").startsWith("Windows"))
056: derbyrunloc = derbyrunloc.substring(1);
057:
058: if ((System.getProperty("java.vm.name") != null)
059: && System.getProperty("java.vm.name").equals("J9")) {
060: jvm = jvm.getJvm("j9_13");
061: } else {
062: jvm = jvm.getJvm("currentjvm"); // ensure compatibility
063: }
064: }
065:
066: String[][] testCommands = new String[][] { { "ij", "--help" },
067: { "sysinfo", "-cp", "help" }, { "dblook" },
068: { "server" }, };
069:
070: for (int i = 0; i < testCommands.length; i++) {
071: runtool(jvm, derbyrunloc, testCommands[i]);
072: }
073: }
074:
075: private static void runtool(jvm jvm, String loc, String[] args)
076: throws IOException {
077: System.out.println(concatenate(args) + ':');
078:
079: if (jvm == null) {
080: org.apache.derby.iapi.tools.run.main(args);
081: return;
082: }
083:
084: Vector cmd = jvm.getCommandLine();
085: cmd.addElement("-jar");
086: cmd.addElement(loc);
087: for (int i = 0; i < args.length; i++) {
088: cmd.addElement(args[i]);
089: }
090: String command = concatenate((String[]) cmd
091: .toArray(new String[0]));
092:
093: Process pr = null;
094:
095: try {
096: pr = Runtime.getRuntime().exec(command);
097: BackgroundStreamSaver saver = new BackgroundStreamSaver(pr
098: .getInputStream(), System.out);
099: saver.finish();
100: pr.waitFor();
101: pr.destroy();
102: } catch (Throwable t) {
103: System.out.println("Process exception: " + t.getMessage());
104: if (pr != null) {
105: pr.destroy();
106: pr = null;
107: }
108: }
109: }
110:
111: private static String concatenate(String[] args) {
112: StringBuffer buf = new StringBuffer();
113: for (int i = 0; i < args.length; i++) {
114: buf.append(args[i]);
115: if (i + 1 < args.length)
116: buf.append(' ');
117: }
118: return buf.toString();
119: }
120: }
|