001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testProperties
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.ByteArrayOutputStream;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.BufferedOutputStream;
030: import java.net.InetAddress;
031:
032: import org.apache.derbyTesting.functionTests.harness.jvm;
033: import org.apache.derbyTesting.functionTests.harness.ProcessStreamResult;
034:
035: import org.apache.derby.drda.NetworkServerControl;
036:
037: /**
038: This test tests the derby.properties, system properties and
039: command line parameters to make sure the pick up settings in
040: the correct order. Search order is:
041: command line parameters
042: System properties
043: derby.properties
044: default
045:
046: The command line should take precedence
047:
048: It also tests start server by specifying system properties without values.
049: In this case the server will use default values.
050: */
051:
052: public class testProperties {
053:
054: private static Properties properties = new java.util.Properties();
055: private static jvm jvm;
056: private static Vector vCmd;
057: private static BufferedOutputStream bos = null;
058:
059: /**
060: * For each new exec process done as part of this test, set
061: * timeout for ProcessStreamResult after which the thread that
062: * handles the streams for the process exits. Timeout is in minutes.
063: * Note: timeout handling will only come into effect when
064: * ProcessStreamResult#Wait() is called
065: */
066: private static String timeoutMinutes = "2";
067:
068: //Command to start server specifying system properties without values
069: private static String[] startServerCmd = new String[] {
070: "-Dderby.drda.logConnections", "-Dderby.drda.traceAll",
071: "-Dderby.drda.traceDirectory", "-Dderby.drda.keepAlive",
072: "-Dderby.drda.timeSlice", "-Dderby.drda.host",
073: "-Dderby.drda.portNumber", "-Dderby.drda.minThreads",
074: "-Dderby.drda.maxThreads",
075: "-Dderby.drda.startNetworkServer", "-Dderby.drda.debug",
076: "org.apache.derby.drda.NetworkServerControl", "start" };
077:
078: //No arguments
079: private static String[] cmdWithoutArgs = new String[] { "org.apache.derby.drda.NetworkServerControl" };
080:
081: //Unknown command
082: private static String[] cmdUnknown = new String[] {
083: "org.apache.derby.drda.NetworkServerControl", "unknowncmd" };
084:
085: //wrong no: of arguments
086: private static String[] cmdWithWrongArgNum = new String[] {
087: "org.apache.derby.drda.NetworkServerControl", "ping",
088: "arg1" };
089:
090: //trace on
091: private static String[] cmdTraceOn = new String[] {
092: "org.apache.derby.drda.NetworkServerControl", "trace",
093: "on", "-p", "1527" };
094:
095: //trace off
096: private static String[] cmdTraceOff = new String[] {
097: "org.apache.derby.drda.NetworkServerControl", "trace",
098: "off", "-p", "1527" };
099:
100: //logconnections on
101: private static String[] cmdLogconnectionsOn = new String[] {
102: "org.apache.derby.drda.NetworkServerControl",
103: "logconnections", "on", "-p", "1527" };
104:
105: /**
106: * Execute the given command and optionally wait and dump the results to standard out
107: *
108: * @param args command and arguments
109: * @param wait true =wait for either completion or timeout time and dump output,
110: * false don't wait and ignore the output.
111: * @exception Exception
112: */
113:
114: private static void execCmdDumpResults(String[] args, boolean wait)
115: throws Exception {
116: // We need the process inputstream and errorstream
117: ProcessStreamResult prout = null;
118: ProcessStreamResult prerr = null;
119:
120: System.out.flush();
121: bos.flush();
122:
123: BufferedOutputStream _bos = bos;
124: if (!wait) {
125: // not interested in the output, don't expect a huge amount.
126: // information will just be written to the byte array in
127: // memory and never used.
128: _bos = new BufferedOutputStream(new ByteArrayOutputStream());
129: }
130: // Start a process to run the command
131: Process pr = execCmd(args);
132:
133: // Note, the timeout handling will only come into effect when we make
134: // the Wait() call on ProcessStreamResult.
135: prout = new ProcessStreamResult(pr.getInputStream(), _bos,
136: timeoutMinutes);
137: prerr = new ProcessStreamResult(pr.getErrorStream(), _bos,
138: timeoutMinutes);
139:
140: if (!wait)
141: return;
142:
143: // wait until all the results have been processed or if we timed out
144: prout.Wait();
145: prerr.Wait();
146: _bos.flush();
147: System.out.flush();
148:
149: }
150:
151: private static Process execCmd(String[] args) throws Exception {
152: StringBuffer sb = new StringBuffer();
153:
154: for (int i = 0; i < args.length; i++) {
155: sb.append(args[i] + " ");
156: }
157: System.out.println(sb.toString());
158: int totalSize = vCmd.size() + args.length;
159: String serverCmd[] = new String[totalSize];
160: int i;
161: for (i = 0; i < vCmd.size(); i++) {
162: serverCmd[i] = (String) vCmd.elementAt(i);
163: }
164: int j = 0;
165: for (; i < totalSize; i++) {
166: serverCmd[i] = args[j++];
167: }
168:
169: // Start a process to run the command
170: Process pr = Runtime.getRuntime().exec(serverCmd);
171: return pr;
172: }
173:
174: /**
175: * Issue derbyServer command if port is null, NetworkServerControl <cmd>
176: * else NetworkServerControl <cmd> -p <portstring>
177: */
178: private static void derbyServerCmd(String cmd, String portString)
179: throws Exception {
180: String[] cmdArr = null;
181: // For start we don't wait or capture results, just
182: // rely on test Connection to verify the start.
183: boolean wait = (cmd.equals("start")) ? false : true;
184:
185: if (portString == null)
186: cmdArr = new String[] {
187: "org.apache.derby.drda.NetworkServerControl", cmd };
188: else if (portString.startsWith("-D"))
189: cmdArr = new String[] { portString,
190: "org.apache.derby.drda.NetworkServerControl", cmd };
191: else
192: cmdArr = new String[] {
193: "org.apache.derby.drda.NetworkServerControl", cmd,
194: "-p", portString };
195:
196: execCmdDumpResults(cmdArr, wait);
197: }
198:
199: private static void waitForStart(String portString, int timeToWait)
200: throws Exception {
201: int waitTime = 0;
202: int port = Integer.parseInt(portString);
203:
204: NetworkServerControl derbyServer = new NetworkServerControl(
205: InetAddress.getByName("localhost"), port);
206:
207: while (waitTime < timeToWait) {
208: try {
209: derbyServer.ping();
210: return;
211: } catch (Exception e) {
212: Thread currentThread = Thread.currentThread();
213: synchronized (currentThread) {
214: try {
215: currentThread.wait(1000);
216: waitTime += 1000;
217: if (waitTime >= timeToWait) {
218: System.out
219: .println("Giving up on wait, waited: "
220: + waitTime);
221: throw e;
222: }
223: } catch (InterruptedException ie) {
224: }
225: }
226: }
227: }
228: }
229:
230: private static void listProperties(String portString)
231: throws Exception {
232: int port = Integer.parseInt(portString);
233: NetworkServerControl derbyServer = new NetworkServerControl(
234: InetAddress.getByName("localhost"), port);
235: Properties p = derbyServer.getCurrentProperties();
236: p.list(System.out);
237: }
238:
239: public static void main(String args[]) throws Exception {
240: if ((System.getProperty("java.vm.name") != null)
241: && System.getProperty("java.vm.name").equals("J9"))
242: jvm = jvm.getJvm("j9_13");
243: else
244: jvm = jvm.getJvm("currentjvm"); // ensure compatibility
245: vCmd = jvm.getCommandLine();
246: try {
247: bos = new BufferedOutputStream(System.out, 1024);
248:
249: System.out
250: .println("Start testProperties to test property priority");
251:
252: /************************************************************
253: * Test port setting priorty
254: ************************************************************/
255: // derby.drda.portNumber set in derby.properties to 1528
256: System.out.println("Testing derby.properties Port 1528 ");
257: Properties derbyProperties = new Properties();
258: derbyProperties.put("derby.drda.portNumber", "1528");
259: FileOutputStream propFile = new FileOutputStream(
260: "derby.properties");
261: derbyProperties.store(propFile, "testing derby.properties");
262: propFile.close();
263: //test start no parameters - Pickup 1528 from derby.properties
264: derbyServerCmd("start", null);
265: waitForStart("1528", 60000);
266: System.out.println("Successfully Connected");
267: //shutdown - also picks up from derby.properties
268: derbyServerCmd("shutdown", null);
269: System.out.println("Testing System properties Port 1529 ");
270: //test start with system property. Overrides derby.properties
271: derbyServerCmd("start", "-Dderby.drda.portNumber=1529");
272:
273: waitForStart("1529", 60000);
274: System.out.println("Successfully Connected");
275: //shutdown - also picks up from System Properties
276: derbyServerCmd("shutdown", "1529");
277: System.out
278: .println("Testing command line option. Port 1530");
279: derbyServerCmd("start", "1530");
280: waitForStart("1530", 60000);
281: System.out.println("Successfully Connected");
282: //shutdown - with command line option
283: derbyServerCmd("shutdown", "1530");
284:
285: /**********************************************************************
286: * Test start server specifying system properties without values
287: *********************************************************************/
288: System.out
289: .println("Testing start server by specifying system properties without values");
290: System.out
291: .println("First shutdown server started on default port by the test harness");
292:
293: //Shutdown the server started by test
294: derbyServerCmd("shutdown", "1527");
295: execCmdDumpResults(startServerCmd, false);
296: waitForStart("1527", 60000);
297: //check that default properties are used
298: listProperties("1527");
299:
300: //Test trace and logconnections commands
301: execCmdDumpResults(cmdTraceOn, true);
302: execCmdDumpResults(cmdLogconnectionsOn, true);
303: listProperties("1527");
304: execCmdDumpResults(cmdTraceOff, true);
305: listProperties("1527");
306: derbyServerCmd("shutdown", "1527");
307:
308: //Test error conditions in command-line
309: execCmdDumpResults(cmdWithoutArgs, true);
310: execCmdDumpResults(cmdUnknown, true);
311: execCmdDumpResults(cmdWithWrongArgNum, true);
312:
313: System.out.println("End test");
314: bos.close();
315: } catch (Exception e) {
316: e.printStackTrace();
317: // If something went wrong,
318: // make sure all these servers are shutdown
319: try {
320: derbyServerCmd("shutdown", "1527");
321: } catch (Exception se) {
322: }
323: try {
324: derbyServerCmd("shutdown", "1528");
325: } catch (Exception se) {
326: }
327: try {
328: derbyServerCmd("shutdown", "1529");
329: } catch (Exception se) {
330: }
331: try {
332: derbyServerCmd("shutdown", "1530");
333: } catch (Exception se) {
334: }
335: } finally {
336: try {
337: File fileToDelete = new File("derby.properties");
338: fileToDelete.delete();
339: } catch (Exception e) {
340: e.printStackTrace();
341: }
342: }
343: }
344: }
|