KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Derby - Class ShutDownDBWhenNSShutsDownTest
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
21 package org.apache.derbyTesting.functionTests.tests.derbynet;
22
23 import java.util.Properties JavaDoc;
24 import org.apache.derbyTesting.functionTests.util.TestUtil;
25 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
26 import org.apache.derbyTesting.junit.BaseTestCase;
27 import org.apache.derby.drda.NetworkServerControl;
28
29 import junit.framework.*;
30 import java.sql.*;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.security.AccessController JavaDoc;
34
35 /**
36  * Derby-1274 - Network Server should shutdown the databases it has booted when
37  * started from the command line.
38  *
39  * Tests that the network server will shutdown the databases it has booted when
40  * started from the command line and that it will not shut down the databases
41  * when started from the API.
42  */

43 public class ShutDownDBWhenNSShutsDownTest extends BaseJDBCTestCase {
44
45
46     NetworkServerControl server = null;
47
48
49     /**
50      * Creates a new instance of ShutDownDBWhenNSShutsDownTest
51      */

52     public ShutDownDBWhenNSShutsDownTest(String JavaDoc name) {
53         super(name);
54     }
55
56     /**
57      * Test that the NetworkServer shuts down the databases it has booted when
58      * started from the command line, and that it does not shut down the
59      * databases it has booted when started from the API.
60      */

61     public void testDatabasesShutDownWhenNSShutdown()
62             throws Exception JavaDoc
63     {
64         server = new NetworkServerControl();
65         // The server was started from the command line when the test was
66
// started. Check that the database will be shut down when the server
67
// is shut down.
68
shutdownServerCheckDBShutDown(true);
69
70         // Start the server form the API and test that the databases will not
71
// be shutdown when the server is shutdown
72
server.start(null);
73
74         // wait until the server accepts connections
75
int i = 0;
76         while (!pingServer() && i < 10 ) {
77             Thread.sleep(1000);
78             i++;
79         }
80
81         // Check that the databases will not be shutdown when the server is
82
// shut down.
83
shutdownServerCheckDBShutDown(false);
84     }
85
86     /**
87      * Checks whether the server shuts down causes the databases it has booted
88      * to be shut down.
89      *
90      * Creates a database and shuts down the server. If the server was started
91      * from the command line the database should be shut down. If the server
92      * was started from the api the database should not be shut down.
93      *
94      * If the database has been shut down the db.lck file should not exist.
95      *
96      * @param dbShutDown Indicates whether the database should have been shut
97      * down.
98      */

99     private void shutdownServerCheckDBShutDown(boolean dbShutDown)
100             throws Exception JavaDoc
101     {
102         // connect to database
103
createDatabase();
104
105         // shut down the server
106
shutdownServer();
107
108         // check if db.lck exists
109
String JavaDoc fileName = getSystemProperty("derby.system.home") +
110                 java.io.File.separator + "wombat" +
111                 java.io.File.separator + "db.lck";
112
113         boolean fileNotFound = false;
114         int i = 0;
115         do {
116             Thread.sleep(500);
117             fileNotFound = !fileExists(fileName);
118             i ++;
119         } while (fileNotFound != dbShutDown && i < 120);
120
121         assertEquals("Database is shut down", dbShutDown, fileNotFound);
122     }
123
124     private boolean fileExists (final String JavaDoc fileName) throws Exception JavaDoc {
125         Boolean JavaDoc b = (Boolean JavaDoc) AccessController.doPrivileged
126             (new java.security.PrivilegedAction JavaDoc(){
127                 public Object JavaDoc run(){
128                     File JavaDoc file = new File JavaDoc(fileName);
129                     return new Boolean JavaDoc(file.exists());
130                 }
131         });
132
133         return b.booleanValue();
134     }
135
136     private boolean pingServer() {
137         try {
138             server.ping();
139         }
140         catch (Exception JavaDoc e) {
141             return false;
142         }
143         return true;
144     }
145
146     private void createDatabase() throws SQLException {
147         Connection conn = getConnection();
148         conn.setAutoCommit(false);
149         Statement st = conn.createStatement();
150         st.execute("CREATE TABLE T1 (a int)");
151         st.execute("INSERT INTO T1 VALUES (1), (2), (3), (4), (5)");
152         st.execute("DROP TABLE T1");
153         conn.commit();
154         conn.close();
155     }
156
157     private void shutdownServer() throws Exception JavaDoc {
158         server.shutdown();
159     }
160
161 }
162
Popular Tags