KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.DerbyNetNewServer
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 org.apache.derby.drda.NetworkServerControl;
25
26 import org.apache.derbyTesting.functionTests.harness.jvm;
27 import org.apache.derby.tools.ij;
28
29 import java.util.Properties JavaDoc;
30
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34
35 import java.sql.DriverManager JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.DatabaseMetaData JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Statement JavaDoc;
41 import org.apache.derbyTesting.functionTests.util.TestUtil;
42
43 /**
44  * Test NetworkServerControl.start(PrintWriter) writes to the print Writer
45  *
46  * test:
47  *<ul>
48  *<li> start( printWriter)
49  *<li> start( (PrintWriter) null)
50  *</ul>
51  */

52
53 public class DerbyNetNewServer extends Thread JavaDoc
54 {
55
56     private static final String JavaDoc DATABASE_NAME = "wombat";
57     private static boolean passed = true;
58     private static final Properties JavaDoc authenticationProperties;
59     static
60     {
61         authenticationProperties = new Properties JavaDoc();
62         authenticationProperties.put ("user", "admin");
63         authenticationProperties.put ("password", "admin");
64     }
65
66     private NetworkServerControl server;
67     
68     public static void main( String JavaDoc[] args)
69     {
70         try
71         {
72             TestUtil.loadDriver();
73             Class.forName( "org.apache.derby.jdbc.EmbeddedDriver").newInstance();
74             
75             ij.getPropertyArg(args);
76
77             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
78
79             testServer(new NetworkServerControl(), bos, "non-null PrintWriter");
80             testServer(new NetworkServerControl(), null, "null PrintWriter");
81         }
82         catch( Exception JavaDoc e)
83         {
84             e.printStackTrace();
85             passed = false;
86         }
87         if( passed)
88             System.out.println( "PASSED.");
89         else
90             System.out.println( "FAILED.");
91     }
92
93     private static void testServer( NetworkServerControl server,
94                                     ByteArrayOutputStream JavaDoc bos, String JavaDoc label)
95         throws Exception JavaDoc
96     {
97         PrintWriter JavaDoc writer = null;
98
99         System.out.println( "Testing " + label);
100         if( bos != null)
101         {
102             bos.reset();
103             // DERBY-1466, Test that messages are flushed to the
104
// writer irrespective of whether the user's writer is
105
// set to autoflush true.
106
writer = new PrintWriter JavaDoc(bos);
107         }
108         server.start(writer);
109         Connection JavaDoc conn = null;
110         
111         // Wait for it to start
112
for( int ntries = 1;; ntries++)
113         {
114             try
115             {
116                 Thread.sleep(500);
117             }
118             catch( InterruptedException JavaDoc ie){};
119             
120             try
121             {
122                 conn = DriverManager.getConnection(TestUtil.getJdbcUrlPrefix()
123                                                    + DATABASE_NAME +
124                                                    ";create=true",
125                                                     authenticationProperties);
126
127                 break;
128             }
129             catch( SQLException JavaDoc sqle)
130             {
131                 if( ntries > 10)
132                 {
133                     System.out.println( "Server start failed: " + sqle.getMessage());
134                     if( bos != null)
135                     {
136                         System.out.println( "Server log:");
137                         System.out.println( bos.toString());
138                     }
139                     passed = false;
140                     break;
141                 }
142             }
143         }
144         if( conn != null)
145         {
146             try
147             {
148                 conn.close();
149             }
150             catch( SQLException JavaDoc sqle)
151             {
152                 passed = false;
153                 System.out.println( "SQLException thrown in close: " + sqle.getMessage());
154             }
155         }
156         try
157         {
158             server.shutdown();
159         }
160         catch( Exception JavaDoc e)
161         {
162             passed = false;
163             System.out.println( "Server shutdown failed: " + e.getMessage());
164         }
165
166         if( bos != null)
167         {
168             if( bos.size() == 0)
169             {
170                 passed = false;
171                 System.out.println( "Nothing written to the server log.");
172             }
173         }
174     } // end of testServer
175

176     private DerbyNetNewServer( NetworkServerControl server)
177     {
178         this.server = server;
179     }
180
181 }
182
Popular Tags