001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testij
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: package org.apache.derbyTesting.functionTests.tests.derbynet;
022:
023: import java.sql.*;
024: import java.util.Vector;
025: import java.util.Properties;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import java.io.BufferedOutputStream;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032: import java.io.ByteArrayInputStream;
033: import java.io.FileNotFoundException;
034:
035: import org.apache.derbyTesting.functionTests.harness.jvm;
036: import org.apache.derbyTesting.functionTests.harness.Sed;
037: import org.apache.derbyTesting.functionTests.util.TestUtil;
038: import org.apache.derbyTesting.functionTests.util.ExecProcUtil;
039:
040: import org.apache.derby.drda.NetworkServerControl;
041:
042: public class testij {
043:
044: private static Properties properties = new java.util.Properties();
045: private static jvm jvm;
046: private static Vector vCmd;
047:
048: private static String IjCmd = "org.apache.derby.tools.ij";
049: private static String SqlDir = "extin";
050: private static String jccSqlFile = "testij.sql";
051: private static String sep;
052: private static String clientSqlFile = "testclientij.sql";
053: private static String altExtinDir;
054: private static boolean useAltExtinDir = false;
055:
056: private static void execCmd(String[] args) throws Exception {
057: int totalSize = vCmd.size() + args.length;
058: String serverCmd[] = new String[totalSize];
059: int i;
060: for (i = 0; i < vCmd.size(); i++) {
061: serverCmd[i] = (String) vCmd.elementAt(i);
062: // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
063: }
064: int j = 0;
065: for (; i < totalSize; i++) {
066: serverCmd[i] = args[j++];
067: // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
068: }
069:
070: // Start a process to run the command
071: Process pr = Runtime.getRuntime().exec(serverCmd);
072: pr.waitFor(); // make sure this is executed first
073: }
074:
075: public static void massageSqlFile(String hostName, String fileName)
076: throws Exception {
077: // only called if hostName is *not* localhost.
078: // Need to replace each occurrence of the string 'localhost' with
079: // whatever is the hostName
080: File tmpFile = new File("extin", "tmpFile.sql");
081: File orgFile = new File("extin", fileName);
082: // wrap this in a try to possibly try using user.dir to find the file
083: InputStream original;
084: OutputStream copy;
085: try {
086: fileName = SqlDir + sep + fileName;
087: original = new FileInputStream(fileName);
088: copy = new FileOutputStream(tmpFile);
089: } catch (FileNotFoundException fnfe) {
090: // we must be running from within a suite...
091: useAltExtinDir = true;
092: String userdir = System.getProperty("user.dir");
093: altExtinDir = userdir + sep + "..";
094: tmpFile = new File(altExtinDir, "tmpFile.sql");
095: orgFile = new File(altExtinDir, fileName);
096: fileName = altExtinDir + sep + fileName;
097: original = new FileInputStream(fileName);
098: copy = new FileOutputStream(tmpFile);
099: }
100: int content;
101: while ((content = original.read()) > 0) {
102: copy.write(content);
103: }
104: copy.close();
105: original.close();
106: Sed hostSed = new Sed();
107: InputStream sedIs = new ByteArrayInputStream(
108: ("substitute=localhost;" + hostName).getBytes("UTF-8"));
109: hostSed.exec(tmpFile, orgFile, sedIs, false, false, false);
110: }
111:
112: public static void main(String args[]) throws Exception {
113: if ((System.getProperty("java.vm.name") != null)
114: && System.getProperty("java.vm.name").equals("J9"))
115: jvm = jvm.getJvm("j9_13");
116: else
117: jvm = jvm.getJvm("currentjvm"); // ensure compatibility
118: vCmd = jvm.getCommandLine();
119: sep = System.getProperty("file.separator");
120: try {
121: BufferedOutputStream bos = new BufferedOutputStream(
122: System.out, 1024);
123: /************************************************************
124: * Test comments in front of select's doesn't cause problems
125: ************************************************************/
126: //create wombat database
127: NetworkServerControl server = new NetworkServerControl();
128: System.out
129: .println("Testing various ij connections and comments in front of selects");
130:
131: // first, we have to massage the .sql file to replace localhost, if
132: // there is a system property set.
133:
134: String hostName = TestUtil.getHostName();
135: if (TestUtil.isJCCFramework()) {
136: // use jccSqlfile
137: if (!hostName.equals("localhost"))
138: massageSqlFile(hostName, jccSqlFile);
139: if (useAltExtinDir)
140: ExecProcUtil
141: .execCmdDumpResults(
142: new String[] {
143: IjCmd,
144: (altExtinDir + sep + SqlDir
145: + sep + jccSqlFile) },
146: vCmd, bos);
147: ExecProcUtil.execCmdDumpResults(new String[] { IjCmd,
148: (SqlDir + sep + jccSqlFile) }, vCmd, bos);
149: } else { // Derby Client
150: // use clientSqlFile
151: if (!hostName.equals("localhost")) {
152: massageSqlFile(hostName, clientSqlFile);
153: if (useAltExtinDir)
154: ExecProcUtil
155: .execCmdDumpResults(
156: new String[] {
157: IjCmd,
158: (altExtinDir + sep
159: + SqlDir + sep + clientSqlFile) },
160: vCmd, bos);
161: }
162: ExecProcUtil.execCmdDumpResults(new String[] { IjCmd,
163: (SqlDir + sep + clientSqlFile) }, vCmd, bos);
164: }
165: System.out.println("End test");
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169: }
170:
171: }
|