1 21 package org.apache.derbyTesting.functionTests.tests.derbynet; 22 23 import java.sql.*; 24 import java.util.Vector ; 25 import java.util.Properties ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.BufferedOutputStream ; 30 import java.net.InetAddress ; 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 51 52 public class testProperties 53 { 54 55 private static Properties properties = new java.util.Properties (); 56 private static jvm jvm; 57 private static Vector vCmd; 58 private static BufferedOutputStream bos = null; 59 60 67 private static String timeoutMinutes = "2"; 68 69 private static String [] startServerCmd = 71 new String [] { "-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 private static String [] cmdWithoutArgs = 87 new String [] { "org.apache.derby.drda.NetworkServerControl"}; 88 89 private static String [] cmdUnknown = 91 new String [] { "org.apache.derby.drda.NetworkServerControl", 92 "unknowncmd"}; 93 94 private static String [] cmdWithWrongArgNum = 96 new String [] { "org.apache.derby.drda.NetworkServerControl", 97 "ping", 98 "arg1"}; 99 100 private static String [] cmdTraceOn = 102 new String [] { "org.apache.derby.drda.NetworkServerControl", 103 "trace", 104 "on", 105 "-p", 106 "1527"}; 107 108 private static String [] cmdTraceOff = 110 new String [] { "org.apache.derby.drda.NetworkServerControl", 111 "trace", 112 "off", 113 "-p", 114 "1527"}; 115 116 private static String [] cmdLogconnectionsOn = 118 new String [] { "org.apache.derby.drda.NetworkServerControl", 119 "logconnections", 120 "on", 121 "-p", 122 "1527"}; 123 131 132 private static void execCmdDumpResults (String [] args, boolean wait) throws Exception 133 { 134 ProcessStreamResult prout = null; 136 ProcessStreamResult prerr = null; 137 138 System.out.flush(); 139 bos.flush(); 140 141 BufferedOutputStream _bos = bos; 142 if (!wait) { 143 _bos = new BufferedOutputStream (new ByteArrayOutputStream ()); 147 } 148 Process pr = execCmd(args); 150 151 prout = new ProcessStreamResult(pr.getInputStream(), _bos, timeoutMinutes); 154 prerr = new ProcessStreamResult(pr.getErrorStream(), _bos, timeoutMinutes); 155 156 if (!wait) 157 return; 158 159 prout.Wait(); 161 prerr.Wait(); 162 _bos.flush(); 163 System.out.flush(); 164 165 } 166 167 168 private static Process execCmd (String [] args) throws Exception 169 { 170 StringBuffer sb = new StringBuffer (); 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 serverCmd[] = new String [totalSize]; 179 int i; 180 for (i = 0; i < vCmd.size(); i++) 181 { 182 serverCmd[i] = (String )vCmd.elementAt(i); 183 } 184 int j = 0; 185 for (; i < totalSize; i++) 186 { 187 serverCmd[i] = args[j++]; 188 } 189 190 Process pr = Runtime.getRuntime().exec(serverCmd); 192 return pr; 193 } 194 195 196 200 private static void derbyServerCmd(String cmd, String portString) throws Exception 201 { 202 String [] cmdArr = null; 203 boolean wait = (cmd.equals("start")) ? false : true; 206 207 if (portString == null) 208 cmdArr = new String [] {"org.apache.derby.drda.NetworkServerControl", cmd}; 209 else if (portString.startsWith("-D")) 210 cmdArr = new String [] 211 {portString,"org.apache.derby.drda.NetworkServerControl", cmd}; 212 else 213 cmdArr = new String [] {"org.apache.derby.drda.NetworkServerControl", cmd,"-p", portString}; 214 215 execCmdDumpResults(cmdArr, wait); 216 } 217 218 private static void waitForStart(String portString, int timeToWait) throws Exception 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 e) { 233 Thread 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 ie) { 244 } 245 } 246 } 247 } 248 } 249 250 private static void listProperties(String portString) throws Exception { 251 int port = Integer.parseInt(portString); 252 NetworkServerControl derbyServer = new NetworkServerControl( InetAddress.getByName("localhost"), 253 port); 254 Properties p = derbyServer.getCurrentProperties(); 255 p.list(System.out); 256 } 257 258 public static void main (String args[]) throws Exception 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"); vCmd = jvm.getCommandLine(); 265 try 266 { 267 bos = new BufferedOutputStream (System.out, 1024); 268 269 270 System.out.println("Start testProperties to test property priority"); 271 272 275 System.out.println("Testing derby.properties Port 1528 "); 277 Properties derbyProperties = new Properties (); 278 derbyProperties.put("derby.drda.portNumber","1528"); 279 FileOutputStream propFile = new FileOutputStream ("derby.properties"); 280 derbyProperties.store(propFile,"testing derby.properties"); 281 propFile.close(); 282 derbyServerCmd("start",null); 284 waitForStart("1528",60000); 285 System.out.println("Successfully Connected"); 286 derbyServerCmd("shutdown",null); 288 System.out.println("Testing System properties Port 1529 "); 289 derbyServerCmd("start","-Dderby.drda.portNumber=1529"); 291 292 waitForStart("1529",60000); 293 System.out.println("Successfully Connected"); 294 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 derbyServerCmd("shutdown","1530"); 302 303 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 derbyServerCmd("shutdown","1527"); 311 execCmdDumpResults(startServerCmd, false); 312 waitForStart("1527",60000); 313 listProperties("1527"); 315 316 execCmdDumpResults(cmdTraceOn, true); 318 execCmdDumpResults(cmdLogconnectionsOn, true); 319 listProperties("1527"); 320 execCmdDumpResults(cmdTraceOff, true); 321 listProperties("1527"); 322 derbyServerCmd("shutdown","1527"); 323 324 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 e) 333 { 334 e.printStackTrace(); 335 try {derbyServerCmd("shutdown","1527");} catch (Exception se) {} 338 try {derbyServerCmd("shutdown","1528");} catch (Exception se) {} 339 try {derbyServerCmd("shutdown","1529");} catch (Exception se) {} 340 try {derbyServerCmd("shutdown","1530");} catch (Exception se) {} 341 } 342 finally { 343 try { 344 File fileToDelete = new File ("derby.properties"); 345 fileToDelete.delete(); 346 } catch (Exception e) 347 { 348 e.printStackTrace(); 349 } 350 } 351 } 352 } 353 354 355 356 357 358 | Popular Tags |