001: /*
002: *
003: * Derby - Class ShutDownDBWhenNSShutsDownTest
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,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020:
021: package org.apache.derbyTesting.functionTests.tests.derbynet;
022:
023: import java.util.Properties;
024: import org.apache.derbyTesting.functionTests.util.TestUtil;
025: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
026: import org.apache.derbyTesting.junit.BaseTestCase;
027: import org.apache.derby.drda.NetworkServerControl;
028:
029: import junit.framework.*;
030: import java.sql.*;
031: import java.io.PrintWriter;
032: import java.io.File;
033: import java.security.AccessController;
034:
035: /**
036: * Derby-1274 - Network Server should shutdown the databases it has booted when
037: * started from the command line.
038: *
039: * Tests that the network server will shutdown the databases it has booted when
040: * started from the command line and that it will not shut down the databases
041: * when started from the API.
042: */
043: public class ShutDownDBWhenNSShutsDownTest extends BaseJDBCTestCase {
044:
045: NetworkServerControl server = null;
046:
047: /**
048: * Creates a new instance of ShutDownDBWhenNSShutsDownTest
049: */
050: public ShutDownDBWhenNSShutsDownTest(String name) {
051: super (name);
052: }
053:
054: /**
055: * Test that the NetworkServer shuts down the databases it has booted when
056: * started from the command line, and that it does not shut down the
057: * databases it has booted when started from the API.
058: */
059: public void testDatabasesShutDownWhenNSShutdown() throws Exception {
060: server = new NetworkServerControl();
061: // The server was started from the command line when the test was
062: // started. Check that the database will be shut down when the server
063: // is shut down.
064: shutdownServerCheckDBShutDown(true);
065:
066: // Start the server form the API and test that the databases will not
067: // be shutdown when the server is shutdown
068: server.start(null);
069:
070: // wait until the server accepts connections
071: int i = 0;
072: while (!pingServer() && i < 10) {
073: Thread.sleep(1000);
074: i++;
075: }
076:
077: // Check that the databases will not be shutdown when the server is
078: // shut down.
079: shutdownServerCheckDBShutDown(false);
080: }
081:
082: /**
083: * Checks whether the server shuts down causes the databases it has booted
084: * to be shut down.
085: *
086: * Creates a database and shuts down the server. If the server was started
087: * from the command line the database should be shut down. If the server
088: * was started from the api the database should not be shut down.
089: *
090: * If the database has been shut down the db.lck file should not exist.
091: *
092: * @param dbShutDown Indicates whether the database should have been shut
093: * down.
094: */
095: private void shutdownServerCheckDBShutDown(boolean dbShutDown)
096: throws Exception {
097: // connect to database
098: createDatabase();
099:
100: // shut down the server
101: shutdownServer();
102:
103: // check if db.lck exists
104: String fileName = getSystemProperty("derby.system.home")
105: + java.io.File.separator + "wombat"
106: + java.io.File.separator + "db.lck";
107:
108: boolean fileNotFound = false;
109: int i = 0;
110: do {
111: Thread.sleep(500);
112: fileNotFound = !fileExists(fileName);
113: i++;
114: } while (fileNotFound != dbShutDown && i < 120);
115:
116: assertEquals("Database is shut down", dbShutDown, fileNotFound);
117: }
118:
119: private boolean fileExists(final String fileName) throws Exception {
120: Boolean b = (Boolean) AccessController
121: .doPrivileged(new java.security.PrivilegedAction() {
122: public Object run() {
123: File file = new File(fileName);
124: return new Boolean(file.exists());
125: }
126: });
127:
128: return b.booleanValue();
129: }
130:
131: private boolean pingServer() {
132: try {
133: server.ping();
134: } catch (Exception e) {
135: return false;
136: }
137: return true;
138: }
139:
140: private void createDatabase() throws SQLException {
141: Connection conn = getConnection();
142: conn.setAutoCommit(false);
143: Statement st = conn.createStatement();
144: st.execute("CREATE TABLE T1 (a int)");
145: st.execute("INSERT INTO T1 VALUES (1), (2), (3), (4), (5)");
146: st.execute("DROP TABLE T1");
147: conn.commit();
148: conn.close();
149: }
150:
151: private void shutdownServer() throws Exception {
152: server.shutdown();
153: }
154:
155: }
|