KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > AutoloadBooting


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.AutoloadBooting
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  * <p>
23  * This JUnit test verifies driver autoloading does not boot the engine.
24  * This test is only run embedded because we manually bring the server up and down.
25  * </p>
26  *
27  * @author Rick
28  */

29
30 package org.apache.derbyTesting.functionTests.tests.jdbc4;
31
32 import java.sql.*;
33 import java.util.*;
34 import junit.framework.*;
35
36 import org.apache.derby.iapi.services.monitor.Monitor;
37 import org.apache.derby.drda.NetworkServerControl;
38 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
39 import org.apache.derbyTesting.junit.TestConfiguration;
40
41 public class AutoloadBooting extends BaseJDBCTestCase
42 {
43     /////////////////////////////////////////////////////////////
44
//
45
// CONSTANTS
46
//
47
/////////////////////////////////////////////////////////////
48

49     private static final String JavaDoc HEADER_LINE = "-------------------------------------";
50     private static final String JavaDoc SPACER_LINE = "-- ";
51     private static final String JavaDoc DRIVER_FACTORY = "org.apache.derby.jdbc.InternalDriver";
52     private static final String JavaDoc DRIVER_SERVICE = "jdbc";
53     private static final String JavaDoc NONEXISTENT_DATABASE = "nonexistentDatabase";
54     private static final String JavaDoc CLIENT_DRIVER_NAME = "org.apache.derby.jdbc.ClientDriver";
55     private static final int SERVER_PORT = 1527;
56     private static final long SLEEP_TIME_MILLIS = 5000L;
57     private static final int PING_COUNT = 6;
58     
59
60     /////////////////////////////////////////////////////////////
61
//
62
// STATE
63
//
64
/////////////////////////////////////////////////////////////
65

66
67     /////////////////////////////////////////////////////////////
68
//
69
// CONSTRUCTOR
70
//
71
/////////////////////////////////////////////////////////////
72

73     public AutoloadBooting( String JavaDoc name ) { super( name ); }
74
75     /////////////////////////////////////////////////////////////
76
//
77
// ENTRY POINT
78
//
79
/////////////////////////////////////////////////////////////
80

81     /////////////////////////////////////////////////////////////
82
//
83
// JUnit BEHAVIOR
84
//
85
/////////////////////////////////////////////////////////////
86

87     /**
88      * Only run embedded.
89      */

90     public static Test suite() {
91         TestSuite suite = new TestSuite();
92         
93         if (usingEmbedded())
94             suite.addTestSuite(AutoloadBooting.class);
95         
96         return suite;
97     }
98     
99     /////////////////////////////////////////////////////////////
100
//
101
// TEST ENTRY POINTS
102
//
103
/////////////////////////////////////////////////////////////
104

105     /**
106      * <p>
107      * Tests that the JDBC driver module is not booted by the autoloading
108      * of drivers.
109      * </p>
110      */

111     public void testBooting()
112         throws Exception JavaDoc
113     {
114         //
115
// Uncomment this line if you want to the test to describe
116
// its progress.
117
//
118
//CONFIG.setVerbosity( true );
119

120         vetInitialization();
121         scenario1_3();
122         scenario2();
123     }
124
125     /**
126      * <p>
127      * Make sure that things look right at initialization.
128      * </p>
129      */

130     private void vetInitialization()
131         throws Exception JavaDoc
132     {
133         printBanner( "Initialization" );
134
135         //
136
// The engine should not be up when we start.
137
//
138
embeddedEngineIsUp( "In the beginning...", false );
139     }
140     
141     /**
142      * <p>
143      * Scenarios (1) and (3) from the problem cases attached to DERBY-1459:
144      * http://issues.apache.org/jira/secure/attachment/12336017/autoloading_scenarios.html.
145      * </p>
146      *
147      * <p>
148      * In scenario (1), we verify that the embedded engine does not boot when
149      * you request a connection from some driver other than the embedded driver.
150      * In scenario (3), we verify that the network server also does not
151      * accidentally come up if you request a connection from some driver
152      * other than the embedded driver.
153      * </p>
154      */

155     private void scenario1_3()
156         throws Exception JavaDoc
157     {
158         printBanner( "Scenarios 1 and 3" );
159         
160         embeddedEngineIsUp( "Before loading client driver...", false );
161
162         //
163
// Request the network server to come up if the engine boots.
164
//
165
requestNetworkServerBoot();
166         
167         //
168
// The engine should not come up when we load the network client driver.
169
//
170
loadNetworkClientDriver();
171         embeddedEngineIsUp( "After loading network client...", false );
172
173         //
174
// The network server should not be up.
175
//
176
ping( false );
177     }
178
179     /**
180      * <p>
181      * Scenario (2) from the problem cases attached to DERBY-1459:
182      * http://issues.apache.org/jira/secure/attachment/12336017/autoloading_scenarios.html.
183      * </p>
184      *
185      * <p>
186      * In this scenario, we verify that the engine boots when we instantiate the
187      * embedded driver. We also test that the network server comes up if
188      * we set the appropriate system property.
189      * </p>
190      */

191     private void scenario2()
192         throws Exception JavaDoc
193     {
194         printBanner( "Scenario 2" );
195
196         embeddedEngineIsUp( "Before instantiating embedded driver...", false );
197
198         //
199
// Request the network server to come up.
200
//
201
requestNetworkServerBoot();
202         
203         //
204
// The engine should come up when we manually instantiate the EmbeddedDriver.
205
//
206
instantiateEmbeddedDriver();
207         embeddedEngineIsUp( "After instantiating EmbeddedDriver...", true );
208
209         //
210
// The network server should also be booted because we set the
211
// requesting system property.
212
//
213
ping( true );
214     
215         //
216
// Now bring down the server and the engine.
217
//
218
bringDownServer();
219         shutdownDerby();
220         embeddedEngineIsUp( "After bringing down server...", false );
221     }
222         
223     /////////////////////////////////////////////////////////////
224
//
225
// MINIONS
226
//
227
/////////////////////////////////////////////////////////////
228

229     /**
230      * <p>
231      * Verify whether the network server came up.
232      * </p>
233      */

234     private void ping( boolean shouldBeUp )
235         throws Exception JavaDoc
236     {
237         NetworkServerControl controller = new NetworkServerControl();
238         Exception JavaDoc finalException = null;
239         boolean isUp = false;
240         
241         for ( int i = 0; i < PING_COUNT; i++ )
242         {
243             try {
244                 controller.ping();
245                 isUp = true;
246                 println( "Network server came up!" );
247                 
248                 break;
249             }
250             catch (Exception JavaDoc e)
251             {
252                 finalException = e;
253                 println( "Network server still down!" );
254             }
255             
256             Thread.sleep( SLEEP_TIME_MILLIS );
257         }
258
259         assertEquals( "Network Server status", shouldBeUp, isUp );
260     }
261
262     /**
263      * <p>
264      * Tear down the network server.
265      * </p>
266      */

267     private void bringDownServer()
268         throws Exception JavaDoc
269     {
270         NetworkServerControl controller = new NetworkServerControl();
271
272         controller.shutdown();
273     }
274     
275     /**
276      * <p>
277      * Set the system property which requests the network server to boot.
278      * </p>
279      */

280     private void requestNetworkServerBoot()
281         throws Exception JavaDoc
282     {
283         setSystemProperty( "derby.drda.startNetworkServer", "true" );
284     }
285
286     /**
287      * <p>
288      * Bring down the engine.
289      * </p>
290      */

291     private void shutdownDerby()
292         throws Exception JavaDoc
293     {
294         // swallow the shutdown exception
295
try{
296             DriverManager.getConnection("jdbc:derby:;shutdown=true");
297         } catch (SQLException e) {}
298     }
299     
300     /**
301      * <p>
302      * Print out the banner for a test scenario.
303      * </p>
304      */

305     private void printBanner( String JavaDoc bannerText )
306     {
307         println( HEADER_LINE );
308         println( SPACER_LINE );
309         println( SPACER_LINE + bannerText );
310         println( SPACER_LINE );
311         println( HEADER_LINE );
312     }
313
314     /**
315      * <p>
316      * Verify whether the embedded JDBC driver (and engine) has booted.
317      * </p>
318      */

319     private void embeddedEngineIsUp( String JavaDoc banner, boolean isUp )
320     {
321         Object JavaDoc service = null;
322
323         // We get an NPE if the service doesn't exist
324
try {
325             service = Monitor.findService( DRIVER_FACTORY, DRIVER_SERVICE );
326         }
327         catch (NullPointerException JavaDoc npe) {}
328
329         boolean actualState = (service != null);
330
331         println( banner + " Engine's booted status should be " + isUp + ", and is " + actualState );
332         
333         assertEquals( "JDBC driver status", isUp, actualState );
334     }
335
336     /**
337      * <p>
338      * Load the embedded driver.
339      * </p>
340      */

341     private void instantiateEmbeddedDriver()
342         throws Exception JavaDoc
343     {
344         Class.forName( "org.apache.derby.jdbc.EmbeddedDriver" ).newInstance();
345     }
346
347
348     /**
349      * <p>
350      * Load the network client.
351      * </p>
352      */

353     private void loadNetworkClientDriver()
354         throws Exception JavaDoc
355     {
356         // This test is only run in JDBC 4 or higher which means the
357
// drivers will always be auto-loading when the classes are
358
// being loaded from the jars.
359
boolean isAutoloading = TestConfiguration.loadingFromJars();
360         
361         //
362
// Forcibly load the network client if we are not autoloading it.
363
//
364
if ( !isAutoloading )
365         {
366             println( "Not autoloading, so forcibly faulting in the client driver." );
367
368             Class.forName( CLIENT_DRIVER_NAME ).newInstance();
369         }
370
371         //
372
// We should fail to get a connection to the nonexistent database.
373
// However, this call should force the client driver to register itself.
374
//
375
String JavaDoc clientURL = "jdbc:derby://localhost:" + SERVER_PORT + "/" + NONEXISTENT_DATABASE;
376
377         try {
378             DriverManager.getConnection( clientURL );
379
380             fail( "Should not have connected to " + clientURL );
381         }
382         catch (SQLException se)
383         {
384             println( "As expected, failed to connect to " + clientURL );
385         }
386
387         //
388
// Verify that the client driver registered itself.
389
//
390
Driver clientDriver = DriverManager.getDriver( clientURL );
391
392         assertNotNull( "Client driver should be registered.", clientDriver );
393         assertEquals
394             ( "Client driver has correct name.", CLIENT_DRIVER_NAME, clientDriver.getClass().getName() );
395     }
396
397 }
398
399
Popular Tags