KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > testconnection


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testconnection
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21 package org.apache.derbyTesting.functionTests.tests.derbynet;
22
23 import java.sql.*;
24 import java.util.Vector JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.BufferedOutputStream JavaDoc;
29
30 import org.apache.derbyTesting.functionTests.harness.jvm;
31 import org.apache.derbyTesting.functionTests.harness.ProcessStreamResult;
32 import org.apache.derbyTesting.functionTests.util.TestUtil;
33 import org.apache.derby.tools.ij;
34
35 /**
36     This tests the testconnection command
37 */

38
39 public class testconnection
40 {
41
42     private static jvm jvm;
43     private static Vector JavaDoc vCmd;
44     private static String JavaDoc hostName;
45     private static String JavaDoc[] TestConnectionCmd1 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
46         "ping"};
47     private static String JavaDoc[] TestConnectionCmd2 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
48         "ping", "-h", "localhost", "-p", "1527"};
49     private static String JavaDoc[] TestConnectionCmd3 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
50                     "ping", "-h", "notthere", "-p", "1527"};
51     private static String JavaDoc[] TestConnectionCmd3a = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl", "ping", "-h", "ihave-inmyname.com", "-p", "1527"};
52     private static String JavaDoc[] TestConnectionCmd4 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
53         "ping", "-h", "localhost"};
54     private static String JavaDoc[] TestConnectionCmd5 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
55         "ping", "-h", "localhost", "-p", "9393"};
56     private static String JavaDoc[] TestConnectionCmd6 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
57         "ping", "-h", "localhost", "-p", "1527"};
58     private static String JavaDoc[] TestConnectionCmd6b = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
59         "ping", "-h", "localhost", "-p", "1527"};
60     private static String JavaDoc[] TestConnectionCmd7 = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
61         "ping", "-h", "localhost", "-p", "9393"};
62     private static String JavaDoc[] TestConnectionCmd7b = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl",
63         "ping", "-h", "localhost", "-p", "9393"};
64
65     private static BufferedOutputStream JavaDoc bos = null;
66     
67     /**
68      * For each new exec process done as part of this test, set
69      * timeout for ProcessStreamResult after which the thread that
70      * handles the streams for the process exits. Timeout is in minutes.
71      * Note: timeout handling will only come into effect when
72      * ProcessStreamResult#Wait() is called
73      */

74     private static String JavaDoc timeoutMinutes = "2";
75     
76     /**
77      * Execute the given command and dump the results to standard out
78      *
79      * @param args command and arguments
80      * @param printcmd to printout the executing command or not
81      * @exception Exception
82      */

83     private static void execCmdDumpResults (String JavaDoc[] args, boolean printcmd)
84         throws Exception JavaDoc
85     {
86         execCmdDumpResults(args, 0, printcmd);
87     }
88
89     /**
90      * Execute the given command and dump the results to standard out
91      *
92      * @param args command and arguments
93      * @exception Exception
94      */

95     private static void execCmdDumpResults (String JavaDoc[] args)
96         throws Exception JavaDoc
97     {
98         execCmdDumpResults(args, 0, true);
99     }
100
101
102     /**
103      * Execute the given command and dump the results to standard out
104      *
105      * @param args command and arguments
106      * @param expectedExitCode the exit code that we expect from running this
107      */

108     private static void execCmdDumpResults (String JavaDoc[] args, int expectedExitCode)
109         throws Exception JavaDoc
110     {
111         execCmdDumpResults(args, expectedExitCode, true);
112     }
113
114     /**
115      * Execute the given command and dump the results to standard out
116      *
117      * @param args command and arguments
118      * @param expectedExitCode the exit code that we expect from running this
119      * @param printcmd to indicate if the executing command should get printed
120      * @exception Exception
121      */

122     private static void execCmdDumpResults (String JavaDoc[] args, int expectedExitCode, boolean printcmd)
123         throws Exception JavaDoc
124     {
125         // We need the process inputstream and errorstream
126
ProcessStreamResult prout = null;
127         ProcessStreamResult prerr = null;
128             
129         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
130             
131         for (int i = 0; i < args.length; i++)
132         {
133             sb.append(args[i] + " ");
134         }
135         if (printcmd)
136             System.out.println(sb.toString());
137         else
138             System.out.println("org.apache.derby.drda.NetworkServerControl ping...");
139         int totalSize = vCmd.size() + args.length;
140         String JavaDoc serverCmd[] = new String JavaDoc[totalSize];
141         int i;
142         for (i = 0; i < vCmd.size(); i++)
143         {
144             serverCmd[i] = (String JavaDoc)vCmd.elementAt(i);
145         // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
146
}
147         int j = 0;
148         for (; i < totalSize; i++)
149         {
150             serverCmd[i] = args[j++];
151         // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
152
}
153  
154         // Start a process to run the command
155
Process JavaDoc pr = Runtime.getRuntime().exec(serverCmd);
156         prout = new ProcessStreamResult(pr.getInputStream(), bos, timeoutMinutes);
157         prerr = new ProcessStreamResult(pr.getErrorStream(), bos, timeoutMinutes);
158
159         // wait until all the results have been processed
160
prout.Wait();
161         prerr.Wait();
162         
163         // wait until the process exits
164
pr.waitFor();
165         
166         // DERBY-214
167
if ( pr.exitValue() != expectedExitCode )
168         {
169             System.out.println("FAIL: expected exit code of " +
170                 expectedExitCode + ", got exit code of " + pr.exitValue());
171         }
172
173     }
174
175
176     public static void main (String JavaDoc args[]) throws Exception JavaDoc
177     {
178         hostName = TestUtil.getHostName();
179         TestConnectionCmd2[3] = hostName;
180         TestConnectionCmd4[3] = hostName;
181         TestConnectionCmd5[3] = hostName;
182         TestConnectionCmd6b[3] = hostName;
183         TestConnectionCmd7b[3] = hostName;
184         
185  
186         
187         if ((System.getProperty("java.vm.name") != null) && System.getProperty("java.vm.name").equals("J9"))
188             jvm = jvm.getJvm("j9_13");
189         else
190             jvm = jvm.getJvm("currentjvm"); // ensure compatibility
191
vCmd = jvm.getCommandLine();
192         try
193         {
194             Connection conn1 = ij.startJBMS();
195             bos = new BufferedOutputStream JavaDoc(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             }
206             else
207                 execCmdDumpResults(TestConnectionCmd1, false);
208             //test connection - specifying host and port
209
execCmdDumpResults(TestConnectionCmd2);
210             //test connection - specifying non-existant host and port
211
execCmdDumpResults(TestConnectionCmd3, 1);
212             //test connection - specifying non-existant host with '-' in the name
213
execCmdDumpResults(TestConnectionCmd3a, 1);
214             //test connection - specifying host but no port
215
execCmdDumpResults(TestConnectionCmd4);
216             //test connection - specifying host and invalid port
217
execCmdDumpResults(TestConnectionCmd5, 1);
218             //test connection - specifying no host and valid port
219
if (!hostName.equals("localhost")) // except with remote server, add the hostName
220
execCmdDumpResults(TestConnectionCmd6b, false);
221             else
222                 execCmdDumpResults(TestConnectionCmd6, false);
223             //test connection - specifying no host and invalid port
224
if (!hostName.equals("localhost")) // except with remote server, add the hostName
225
execCmdDumpResults(TestConnectionCmd7b, 1, false);
226             else
227                 execCmdDumpResults(TestConnectionCmd7, 1, false);
228
229             System.out.println("End test");
230             bos.close();
231         }
232         catch (Exception JavaDoc e)
233         {
234             e.printStackTrace();
235         }
236     }
237 }
238
Popular Tags