KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > NetworkServerTestSetup


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.junit.NetworkServerTestSetup
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,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.junit;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import junit.extensions.TestSetup;
29 import junit.framework.Test;
30 import org.apache.derby.drda.NetworkServerControl;
31
32 /**
33  * Test decorator that starts the network server on startup
34  * and stops it on teardown.
35  *
36  * It does not start it if the test is configured to run in
37  * embedded mode.
38  *
39  * Currently it will start the network server in the same VM
40  * and it does not support starting it from a remote
41  * machine.
42  */

43 final public class NetworkServerTestSetup extends TestSetup {
44
45     private FileOutputStream JavaDoc serverOutput;
46     
47     /**
48      * Decorator this test with the NetworkServerTestSetup
49      */

50     public NetworkServerTestSetup(Test test) {
51         super(test);
52     }
53
54     /**
55      * Start the network server.
56      */

57     protected void setUp() throws Exception JavaDoc {
58         
59         TestConfiguration config = TestConfiguration.getCurrent();
60         
61         if (!config.getJDBCClient().isEmbedded()) {
62             BaseTestCase.println("Starting network server:");
63
64             
65             serverOutput = (FileOutputStream JavaDoc)
66             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
67                 public Object JavaDoc run() {
68                     String JavaDoc fileName = System.getProperty("derby.system.home") +
69                             "serverConsoleOutput.log";
70                     FileOutputStream JavaDoc fos = null;
71                     try {
72                         fos = (new FileOutputStream JavaDoc(fileName));
73                     } catch (FileNotFoundException JavaDoc 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 JavaDoc(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 JavaDoc 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     /**
102      * Stop the network server.
103      */

104     protected void tearDown() throws Exception JavaDoc {
105         if (networkServerController != null) {
106             networkServerController.shutdown();
107             serverOutput.close();
108         }
109     }
110     
111     /* Network Server Control */
112     private NetworkServerControl networkServerController;
113     
114     /** Wait maximum 1 minute for server to start */
115     private static final int WAIT_TIME = 60000;
116     
117     /** Sleep for 50 ms before pinging the network server (again) */
118     private static final int SLEEP_TIME = 50;
119 }
120
Popular Tags