001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testconnection
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.FileOutputStream;
028: import java.io.BufferedOutputStream;
029:
030: import org.apache.derbyTesting.functionTests.harness.jvm;
031: import org.apache.derbyTesting.functionTests.harness.ProcessStreamResult;
032: import org.apache.derbyTesting.functionTests.util.TestUtil;
033: import org.apache.derby.tools.ij;
034:
035: /**
036: This tests the testconnection command
037: */
038:
039: public class testconnection {
040:
041: private static jvm jvm;
042: private static Vector vCmd;
043: private static String hostName;
044: private static String[] TestConnectionCmd1 = new String[] {
045: "org.apache.derby.drda.NetworkServerControl", "ping" };
046: private static String[] TestConnectionCmd2 = new String[] {
047: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
048: "localhost", "-p", "1527" };
049: private static String[] TestConnectionCmd3 = new String[] {
050: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
051: "notthere", "-p", "1527" };
052: private static String[] TestConnectionCmd3a = new String[] {
053: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
054: "ihave-inmyname.com", "-p", "1527" };
055: private static String[] TestConnectionCmd4 = new String[] {
056: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
057: "localhost" };
058: private static String[] TestConnectionCmd5 = new String[] {
059: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
060: "localhost", "-p", "9393" };
061: private static String[] TestConnectionCmd6 = new String[] {
062: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
063: "localhost", "-p", "1527" };
064: private static String[] TestConnectionCmd6b = new String[] {
065: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
066: "localhost", "-p", "1527" };
067: private static String[] TestConnectionCmd7 = new String[] {
068: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
069: "localhost", "-p", "9393" };
070: private static String[] TestConnectionCmd7b = new String[] {
071: "org.apache.derby.drda.NetworkServerControl", "ping", "-h",
072: "localhost", "-p", "9393" };
073:
074: private static BufferedOutputStream bos = null;
075:
076: /**
077: * For each new exec process done as part of this test, set
078: * timeout for ProcessStreamResult after which the thread that
079: * handles the streams for the process exits. Timeout is in minutes.
080: * Note: timeout handling will only come into effect when
081: * ProcessStreamResult#Wait() is called
082: */
083: private static String timeoutMinutes = "2";
084:
085: /**
086: * Execute the given command and dump the results to standard out
087: *
088: * @param args command and arguments
089: * @param printcmd to printout the executing command or not
090: * @exception Exception
091: */
092: private static void execCmdDumpResults(String[] args,
093: boolean printcmd) throws Exception {
094: execCmdDumpResults(args, 0, printcmd);
095: }
096:
097: /**
098: * Execute the given command and dump the results to standard out
099: *
100: * @param args command and arguments
101: * @exception Exception
102: */
103: private static void execCmdDumpResults(String[] args)
104: throws Exception {
105: execCmdDumpResults(args, 0, true);
106: }
107:
108: /**
109: * Execute the given command and dump the results to standard out
110: *
111: * @param args command and arguments
112: * @param expectedExitCode the exit code that we expect from running this
113: */
114: private static void execCmdDumpResults(String[] args,
115: int expectedExitCode) throws Exception {
116: execCmdDumpResults(args, expectedExitCode, true);
117: }
118:
119: /**
120: * Execute the given command and dump the results to standard out
121: *
122: * @param args command and arguments
123: * @param expectedExitCode the exit code that we expect from running this
124: * @param printcmd to indicate if the executing command should get printed
125: * @exception Exception
126: */
127: private static void execCmdDumpResults(String[] args,
128: int expectedExitCode, boolean printcmd) throws Exception {
129: // We need the process inputstream and errorstream
130: ProcessStreamResult prout = null;
131: ProcessStreamResult prerr = null;
132:
133: StringBuffer sb = new StringBuffer();
134:
135: for (int i = 0; i < args.length; i++) {
136: sb.append(args[i] + " ");
137: }
138: if (printcmd)
139: System.out.println(sb.toString());
140: else
141: System.out
142: .println("org.apache.derby.drda.NetworkServerControl ping...");
143: int totalSize = vCmd.size() + args.length;
144: String serverCmd[] = new String[totalSize];
145: int i;
146: for (i = 0; i < vCmd.size(); i++) {
147: serverCmd[i] = (String) vCmd.elementAt(i);
148: // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
149: }
150: int j = 0;
151: for (; i < totalSize; i++) {
152: serverCmd[i] = args[j++];
153: // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
154: }
155:
156: // Start a process to run the command
157: Process pr = Runtime.getRuntime().exec(serverCmd);
158: prout = new ProcessStreamResult(pr.getInputStream(), bos,
159: timeoutMinutes);
160: prerr = new ProcessStreamResult(pr.getErrorStream(), bos,
161: timeoutMinutes);
162:
163: // wait until all the results have been processed
164: prout.Wait();
165: prerr.Wait();
166:
167: // wait until the process exits
168: pr.waitFor();
169:
170: // DERBY-214
171: if (pr.exitValue() != expectedExitCode) {
172: System.out.println("FAIL: expected exit code of "
173: + expectedExitCode + ", got exit code of "
174: + pr.exitValue());
175: }
176:
177: }
178:
179: public static void main(String args[]) throws Exception {
180: hostName = TestUtil.getHostName();
181: TestConnectionCmd2[3] = hostName;
182: TestConnectionCmd4[3] = hostName;
183: TestConnectionCmd5[3] = hostName;
184: TestConnectionCmd6b[3] = hostName;
185: TestConnectionCmd7b[3] = hostName;
186:
187: if ((System.getProperty("java.vm.name") != null)
188: && System.getProperty("java.vm.name").equals("J9"))
189: jvm = jvm.getJvm("j9_13");
190: else
191: jvm = jvm.getJvm("currentjvm"); // ensure compatibility
192: vCmd = jvm.getCommandLine();
193: try {
194: Connection conn1 = ij.startJBMS();
195: bos = new BufferedOutputStream(System.out, 1024);
196:
197: /************************************************************
198: * Test testconnection
199: ************************************************************/
200: System.out.println("Testing testconnection");
201: //test connection - no parameters
202: if (!hostName.equals("localhost")) // except with remote server, add the hostName
203: {
204: execCmdDumpResults(TestConnectionCmd4, 0, false);
205: } else
206: execCmdDumpResults(TestConnectionCmd1, false);
207: //test connection - specifying host and port
208: execCmdDumpResults(TestConnectionCmd2);
209: //test connection - specifying non-existant host and port
210: execCmdDumpResults(TestConnectionCmd3, 1);
211: //test connection - specifying non-existant host with '-' in the name
212: execCmdDumpResults(TestConnectionCmd3a, 1);
213: //test connection - specifying host but no port
214: execCmdDumpResults(TestConnectionCmd4);
215: //test connection - specifying host and invalid port
216: execCmdDumpResults(TestConnectionCmd5, 1);
217: //test connection - specifying no host and valid port
218: if (!hostName.equals("localhost")) // except with remote server, add the hostName
219: execCmdDumpResults(TestConnectionCmd6b, false);
220: else
221: execCmdDumpResults(TestConnectionCmd6, false);
222: //test connection - specifying no host and invalid port
223: if (!hostName.equals("localhost")) // except with remote server, add the hostName
224: execCmdDumpResults(TestConnectionCmd7b, 1, false);
225: else
226: execCmdDumpResults(TestConnectionCmd7, 1, false);
227:
228: System.out.println("End test");
229: bos.close();
230: } catch (Exception e) {
231: e.printStackTrace();
232: }
233: }
234: }
|