KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testProperties
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.ByteArrayOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.net.InetAddress JavaDoc;
31
32 import org.apache.derbyTesting.functionTests.harness.jvm;
33 import org.apache.derbyTesting.functionTests.harness.ProcessStreamResult;
34
35 import org.apache.derby.drda.NetworkServerControl;
36
37 /**
38     This test tests the derby.properties, system properties and
39     command line parameters to make sure the pick up settings in
40     the correct order. Search order is:
41        command line parameters
42        System properties
43        derby.properties
44        default
45
46        The command line should take precedence
47
48     It also tests start server by specifying system properties without values.
49     In this case the server will use default values.
50 */

51
52 public class testProperties
53 {
54
55     private static Properties JavaDoc properties = new java.util.Properties JavaDoc();
56     private static jvm jvm;
57     private static Vector JavaDoc vCmd;
58     private static BufferedOutputStream JavaDoc bos = null;
59     
60     /**
61      * For each new exec process done as part of this test, set
62      * timeout for ProcessStreamResult after which the thread that
63      * handles the streams for the process exits. Timeout is in minutes.
64      * Note: timeout handling will only come into effect when
65      * ProcessStreamResult#Wait() is called
66      */

67     private static String JavaDoc timeoutMinutes = "2";
68
69     //Command to start server specifying system properties without values
70
private static String JavaDoc[] startServerCmd =
71                     new String JavaDoc[] { "-Dderby.drda.logConnections",
72                                     "-Dderby.drda.traceAll",
73                                     "-Dderby.drda.traceDirectory",
74                                     "-Dderby.drda.keepAlive",
75                                     "-Dderby.drda.timeSlice",
76                                     "-Dderby.drda.host",
77                                     "-Dderby.drda.portNumber",
78                                     "-Dderby.drda.minThreads",
79                                     "-Dderby.drda.maxThreads",
80                                     "-Dderby.drda.startNetworkServer",
81                                     "-Dderby.drda.debug",
82                                     "org.apache.derby.drda.NetworkServerControl",
83                                     "start"};
84     
85     //No arguments
86
private static String JavaDoc[] cmdWithoutArgs =
87                     new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl"};
88     
89     //Unknown command
90
private static String JavaDoc[] cmdUnknown =
91                     new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl",
92                                     "unknowncmd"};
93     
94     //wrong no: of arguments
95
private static String JavaDoc[] cmdWithWrongArgNum =
96                     new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl",
97                                     "ping",
98                                     "arg1"};
99     
100     //trace on
101
private static String JavaDoc[] cmdTraceOn =
102                     new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl",
103                                     "trace",
104                                     "on",
105                                     "-p",
106                                     "1527"};
107     
108     //trace off
109
private static String JavaDoc[] cmdTraceOff =
110         new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl",
111                         "trace",
112                         "off",
113                         "-p",
114                         "1527"};
115     
116     //logconnections on
117
private static String JavaDoc[] cmdLogconnectionsOn =
118                     new String JavaDoc[] { "org.apache.derby.drda.NetworkServerControl",
119                                     "logconnections",
120                                     "on",
121                                     "-p",
122                                     "1527"};
123     /**
124      * Execute the given command and optionally wait and dump the results to standard out
125      *
126      * @param args command and arguments
127      * @param wait true =wait for either completion or timeout time and dump output,
128      * false don't wait and ignore the output.
129      * @exception Exception
130      */

131
132     private static void execCmdDumpResults (String JavaDoc[] args, boolean wait) throws Exception JavaDoc
133     {
134         // We need the process inputstream and errorstream
135
ProcessStreamResult prout = null;
136         ProcessStreamResult prerr = null;
137             
138         System.out.flush();
139         bos.flush();
140         
141         BufferedOutputStream JavaDoc _bos = bos;
142         if (!wait) {
143             // not interested in the output, don't expect a huge amount.
144
// information will just be written to the byte array in
145
// memory and never used.
146
_bos = new BufferedOutputStream JavaDoc(new ByteArrayOutputStream JavaDoc());
147         }
148         // Start a process to run the command
149
Process JavaDoc pr = execCmd(args);
150         
151         // Note, the timeout handling will only come into effect when we make
152
// the Wait() call on ProcessStreamResult.
153
prout = new ProcessStreamResult(pr.getInputStream(), _bos, timeoutMinutes);
154         prerr = new ProcessStreamResult(pr.getErrorStream(), _bos, timeoutMinutes);
155         
156         if (!wait)
157             return;
158
159         // wait until all the results have been processed or if we timed out
160
prout.Wait();
161         prerr.Wait();
162         _bos.flush();
163         System.out.flush();
164
165     }
166
167
168     private static Process JavaDoc execCmd (String JavaDoc[] args) throws Exception JavaDoc
169     {
170         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
171             
172         for (int i = 0; i < args.length; i++)
173         {
174             sb.append(args[i] + " ");
175         }
176         System.out.println(sb.toString());
177         int totalSize = vCmd.size() + args.length;
178         String JavaDoc serverCmd[] = new String JavaDoc[totalSize];
179         int i;
180         for (i = 0; i < vCmd.size(); i++)
181         {
182             serverCmd[i] = (String JavaDoc)vCmd.elementAt(i);
183         }
184         int j = 0;
185         for (; i < totalSize; i++)
186         {
187             serverCmd[i] = args[j++];
188         }
189  
190         // Start a process to run the command
191
Process JavaDoc pr = Runtime.getRuntime().exec(serverCmd);
192         return pr;
193     }
194
195
196     /**
197      * Issue derbyServer command if port is null, NetworkServerControl <cmd>
198      * else NetworkServerControl <cmd> -p <portstring>
199      */

200     private static void derbyServerCmd(String JavaDoc cmd, String JavaDoc portString) throws Exception JavaDoc
201     {
202         String JavaDoc [] cmdArr = null;
203         // For start we don't wait or capture results, just
204
// rely on test Connection to verify the start.
205
boolean wait = (cmd.equals("start")) ? false : true;
206         
207         if (portString == null)
208             cmdArr = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl", cmd};
209         else if (portString.startsWith("-D"))
210             cmdArr = new String JavaDoc[]
211              {portString,"org.apache.derby.drda.NetworkServerControl", cmd};
212         else
213             cmdArr = new String JavaDoc[] {"org.apache.derby.drda.NetworkServerControl", cmd,"-p", portString};
214         
215         execCmdDumpResults(cmdArr, wait);
216     }
217     
218     private static void waitForStart(String JavaDoc portString, int timeToWait) throws Exception JavaDoc
219     {
220         int waitTime = 0;
221         int port = Integer.parseInt(portString);
222         
223         NetworkServerControl derbyServer = new NetworkServerControl( InetAddress.getByName("localhost"),
224                                                   port);
225         
226         
227
228         while (waitTime < timeToWait) {
229             try {
230                 derbyServer.ping();
231                 return;
232             } catch (Exception JavaDoc e) {
233                 Thread JavaDoc currentThread = Thread.currentThread();
234                 synchronized (currentThread) {
235                     try {
236                         currentThread.wait(1000);
237                         waitTime += 1000;
238                         if (waitTime >= timeToWait) {
239                             System.out.println(
240                                 "Giving up on wait, waited: " + waitTime);
241                             throw e;
242                         }
243                     } catch (InterruptedException JavaDoc ie) {
244                     }
245                 }
246             }
247         }
248     }
249
250     private static void listProperties(String JavaDoc portString) throws Exception JavaDoc{
251         int port = Integer.parseInt(portString);
252         NetworkServerControl derbyServer = new NetworkServerControl( InetAddress.getByName("localhost"),
253                                                     port);
254         Properties JavaDoc p = derbyServer.getCurrentProperties();
255         p.list(System.out);
256     }
257
258     public static void main (String JavaDoc args[]) throws Exception JavaDoc
259     {
260         if ((System.getProperty("java.vm.name") != null) && System.getProperty("java.vm.name").equals("J9"))
261             jvm = jvm.getJvm("j9_13");
262         else
263             jvm = jvm.getJvm("currentjvm"); // ensure compatibility
264
vCmd = jvm.getCommandLine();
265         try
266         {
267             bos = new BufferedOutputStream JavaDoc(System.out, 1024);
268             
269
270             System.out.println("Start testProperties to test property priority");
271
272             /************************************************************
273              * Test port setting priorty
274              ************************************************************/

275             // derby.drda.portNumber set in derby.properties to 1528
276
System.out.println("Testing derby.properties Port 1528 ");
277             Properties JavaDoc derbyProperties = new Properties JavaDoc();
278             derbyProperties.put("derby.drda.portNumber","1528");
279             FileOutputStream JavaDoc propFile = new FileOutputStream JavaDoc("derby.properties");
280             derbyProperties.store(propFile,"testing derby.properties");
281             propFile.close();
282             //test start no parameters - Pickup 1528 from derby.properties
283
derbyServerCmd("start",null);
284             waitForStart("1528",60000);
285             System.out.println("Successfully Connected");
286             //shutdown - also picks up from derby.properties
287
derbyServerCmd("shutdown",null);
288             System.out.println("Testing System properties Port 1529 ");
289             //test start with system property. Overrides derby.properties
290
derbyServerCmd("start","-Dderby.drda.portNumber=1529");
291
292             waitForStart("1529",60000);
293             System.out.println("Successfully Connected");
294             //shutdown - also picks up from System Properties
295
derbyServerCmd("shutdown","1529");
296             System.out.println("Testing command line option. Port 1530");
297             derbyServerCmd("start","1530");
298             waitForStart("1530",60000);
299             System.out.println("Successfully Connected");
300             //shutdown - with command line option
301
derbyServerCmd("shutdown","1530");
302
303             /**********************************************************************
304              * Test start server specifying system properties without values
305              *********************************************************************/

306             System.out.println("Testing start server by specifying system properties without values");
307             System.out.println("First shutdown server started on default port by the test harness");
308
309             //Shutdown the server started by test
310
derbyServerCmd("shutdown","1527");
311             execCmdDumpResults(startServerCmd, false);
312             waitForStart("1527",60000);
313             //check that default properties are used
314
listProperties("1527");
315             
316             //Test trace and logconnections commands
317
execCmdDumpResults(cmdTraceOn, true);
318             execCmdDumpResults(cmdLogconnectionsOn, true);
319             listProperties("1527");
320             execCmdDumpResults(cmdTraceOff, true);
321             listProperties("1527");
322             derbyServerCmd("shutdown","1527");
323             
324             //Test error conditions in command-line
325
execCmdDumpResults(cmdWithoutArgs, true);
326             execCmdDumpResults(cmdUnknown, true);
327             execCmdDumpResults(cmdWithWrongArgNum, true);
328             
329             System.out.println("End test");
330             bos.close();
331         }
332         catch (Exception JavaDoc e)
333         {
334             e.printStackTrace();
335             // If something went wrong,
336
// make sure all these servers are shutdown
337
try {derbyServerCmd("shutdown","1527");} catch (Exception JavaDoc se) {}
338             try {derbyServerCmd("shutdown","1528");} catch (Exception JavaDoc se) {}
339             try {derbyServerCmd("shutdown","1529");} catch (Exception JavaDoc se) {}
340             try {derbyServerCmd("shutdown","1530");} catch (Exception JavaDoc se) {}
341         }
342         finally {
343             try {
344                 File JavaDoc fileToDelete = new File JavaDoc("derby.properties");
345                 fileToDelete.delete();
346             } catch (Exception JavaDoc e)
347             {
348                 e.printStackTrace();
349             }
350         }
351     }
352 }
353
354
355
356
357
358
Popular Tags