KickJava   Java API By Example, From Geeks To Geeks.

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


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

21
22 package org.apache.derbyTesting.functionTests.tests.derbynet;
23
24 import java.sql.*;
25 import org.apache.derby.drda.NetworkServerControl;
26 import org.apache.derbyTesting.functionTests.util.TestUtil;
27 import java.net.InetAddress JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 public class NSinSameJVM {
31
32     private static final int NETWORKSERVER_PORT = 20000;
33    private String JavaDoc databaseFileName = "NSinSameJVMTestDB;create=true";
34
35     public NSinSameJVM() {
36
37         // Load the Derby driver
38
try {
39             TestUtil.loadDriver();
40             dbg("Derby drivers loaded");
41         } catch (Exception JavaDoc e) {
42             e.printStackTrace();
43         }
44
45         
46
47         NetworkServerControl serverControl = null;
48         boolean started = false;
49
50
51         try {
52
53             serverControl = new
54                 NetworkServerControl(InetAddress.getByName("0.0.0.0"),
55                                      NETWORKSERVER_PORT);
56
57             serverControl.start(new PrintWriter JavaDoc(System.out,true));
58             
59             for (int i = 1; i < 50; i++)
60             {
61                 Thread.sleep(1000);
62                 if (isServerStarted(serverControl))
63                 {
64                     started = true;
65                     break;
66                 }
67             }
68                 
69         } catch (Exception JavaDoc e)
70         {
71             e.printStackTrace();
72         }
73         if(started)
74             dbg("NetworkServer started");
75         else
76         {
77             System.out.println("FAIL Network Server did not start");
78             return;
79         }
80         String JavaDoc hostName = TestUtil.getHostName();
81         String JavaDoc jdbcUrlPrefix = TestUtil.getJdbcUrlPrefix(hostName, NETWORKSERVER_PORT);
82
83         String JavaDoc url = jdbcUrlPrefix + databaseFileName;
84
85         Connection connection = null;
86            
87         try {
88             // Just connect, do something and close the connection
89
connection = DriverManager.getConnection(url, "user", "password");
90             Statement stmt = connection.createStatement();
91             ResultSet rs = stmt.executeQuery("Select tablename from sys.systables");
92             
93             while (rs.next())
94             {
95                 rs.getString(1);
96             }
97             rs.close();
98             dbg("Connected to database " + databaseFileName);
99             // Leave the connection open before shutdown to make
100
// sure the thread closes down.
101
// connection.close();
102

103             System.out.println("getting ready to shutdown");
104             serverControl.shutdown();
105             Thread.sleep(5000);
106
107         } catch (Exception JavaDoc e) {
108             System.out.print("FAIL: Unexpected exception" + e.getMessage());
109             e.printStackTrace();
110         }
111             
112         //
113

114     }
115
116     private void dbg(String JavaDoc s) {
117         System.out.println(Thread.currentThread().getName() + "-NSinSameJVM: " + s);
118     }
119
120     public static void main(String JavaDoc[] args) {
121         new NSinSameJVM();
122     }
123
124     private boolean isServerStarted(NetworkServerControl server)
125     {
126         try {
127             server.ping();
128         }
129         catch (Exception JavaDoc e) {
130             return false;
131         }
132         return true;
133     }
134 }
135
136
137
138
139
140
Popular Tags