1 20 package org.apache.derbyTesting.junit; 21 22 import java.io.FileNotFoundException ; 23 import java.net.InetAddress ; 24 import java.io.FileOutputStream ; 25 import java.io.PrintWriter ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import junit.extensions.TestSetup; 29 import junit.framework.Test; 30 import org.apache.derby.drda.NetworkServerControl; 31 32 43 final public class NetworkServerTestSetup extends TestSetup { 44 45 private FileOutputStream serverOutput; 46 47 50 public NetworkServerTestSetup(Test test) { 51 super(test); 52 } 53 54 57 protected void setUp() throws Exception { 58 59 TestConfiguration config = TestConfiguration.getCurrent(); 60 61 if (!config.getJDBCClient().isEmbedded()) { 62 BaseTestCase.println("Starting network server:"); 63 64 65 serverOutput = (FileOutputStream ) 66 AccessController.doPrivileged(new PrivilegedAction () { 67 public Object run() { 68 String fileName = System.getProperty("derby.system.home") + 69 "serverConsoleOutput.log"; 70 FileOutputStream fos = null; 71 try { 72 fos = (new FileOutputStream (fileName)); 73 } catch (FileNotFoundException ex) { 74 ex.printStackTrace(); 75 } 76 return fos; 77 } 78 }); 79 80 networkServerController = new NetworkServerControl 81 (InetAddress.getByName(config.getHostName()), config.getPort()); 82 83 networkServerController.start(new PrintWriter (serverOutput)); 84 85 final long startTime = System.currentTimeMillis(); 86 while (true) { 87 Thread.sleep(SLEEP_TIME); 88 try { 89 networkServerController.ping(); 90 break; 91 } catch (Exception e) { 92 if (System.currentTimeMillis() - startTime > WAIT_TIME) { 93 e.printStackTrace(); 94 fail("Timed out waiting for network server to start"); 95 } 96 } 97 } 98 } 99 } 100 101 104 protected void tearDown() throws Exception { 105 if (networkServerController != null) { 106 networkServerController.shutdown(); 107 serverOutput.close(); 108 } 109 } 110 111 112 private NetworkServerControl networkServerController; 113 114 115 private static final int WAIT_TIME = 60000; 116 117 118 private static final int SLEEP_TIME = 50; 119 } 120 | Popular Tags |