001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.NSinSameJVM
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, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.derbynet;
023:
024: import java.sql.*;
025: import org.apache.derby.drda.NetworkServerControl;
026: import org.apache.derbyTesting.functionTests.util.TestUtil;
027: import java.net.InetAddress;
028: import java.io.PrintWriter;
029:
030: public class NSinSameJVM {
031:
032: private static final int NETWORKSERVER_PORT = 20000;
033: private String databaseFileName = "NSinSameJVMTestDB;create=true";
034:
035: public NSinSameJVM() {
036:
037: // Load the Derby driver
038: try {
039: TestUtil.loadDriver();
040: dbg("Derby drivers loaded");
041: } catch (Exception e) {
042: e.printStackTrace();
043: }
044:
045: NetworkServerControl serverControl = null;
046: boolean started = false;
047:
048: try {
049:
050: serverControl = new NetworkServerControl(InetAddress
051: .getByName("0.0.0.0"), NETWORKSERVER_PORT);
052:
053: serverControl.start(new PrintWriter(System.out, true));
054:
055: for (int i = 1; i < 50; i++) {
056: Thread.sleep(1000);
057: if (isServerStarted(serverControl)) {
058: started = true;
059: break;
060: }
061: }
062:
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: if (started)
067: dbg("NetworkServer started");
068: else {
069: System.out.println("FAIL Network Server did not start");
070: return;
071: }
072: String hostName = TestUtil.getHostName();
073: String jdbcUrlPrefix = TestUtil.getJdbcUrlPrefix(hostName,
074: NETWORKSERVER_PORT);
075:
076: String url = jdbcUrlPrefix + databaseFileName;
077:
078: Connection connection = null;
079:
080: try {
081: // Just connect, do something and close the connection
082: connection = DriverManager.getConnection(url, "user",
083: "password");
084: Statement stmt = connection.createStatement();
085: ResultSet rs = stmt
086: .executeQuery("Select tablename from sys.systables");
087:
088: while (rs.next()) {
089: rs.getString(1);
090: }
091: rs.close();
092: dbg("Connected to database " + databaseFileName);
093: // Leave the connection open before shutdown to make
094: // sure the thread closes down.
095: // connection.close();
096:
097: System.out.println("getting ready to shutdown");
098: serverControl.shutdown();
099: Thread.sleep(5000);
100:
101: } catch (Exception e) {
102: System.out.print("FAIL: Unexpected exception"
103: + e.getMessage());
104: e.printStackTrace();
105: }
106:
107: //
108:
109: }
110:
111: private void dbg(String s) {
112: System.out.println(Thread.currentThread().getName()
113: + "-NSinSameJVM: " + s);
114: }
115:
116: public static void main(String[] args) {
117: new NSinSameJVM();
118: }
119:
120: private boolean isServerStarted(NetworkServerControl server) {
121: try {
122: server.ping();
123: } catch (Exception e) {
124: return false;
125: }
126: return true;
127: }
128: }
|