1 21 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 49 private static final String HEADER_LINE = "-------------------------------------"; 50 private static final String SPACER_LINE = "-- "; 51 private static final String DRIVER_FACTORY = "org.apache.derby.jdbc.InternalDriver"; 52 private static final String DRIVER_SERVICE = "jdbc"; 53 private static final String NONEXISTENT_DATABASE = "nonexistentDatabase"; 54 private static final String 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 66 67 73 public AutoloadBooting( String name ) { super( name ); } 74 75 81 87 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 105 111 public void testBooting() 112 throws Exception 113 { 114 120 vetInitialization(); 121 scenario1_3(); 122 scenario2(); 123 } 124 125 130 private void vetInitialization() 131 throws Exception 132 { 133 printBanner( "Initialization" ); 134 135 embeddedEngineIsUp( "In the beginning...", false ); 139 } 140 141 155 private void scenario1_3() 156 throws Exception 157 { 158 printBanner( "Scenarios 1 and 3" ); 159 160 embeddedEngineIsUp( "Before loading client driver...", false ); 161 162 requestNetworkServerBoot(); 166 167 loadNetworkClientDriver(); 171 embeddedEngineIsUp( "After loading network client...", false ); 172 173 ping( false ); 177 } 178 179 191 private void scenario2() 192 throws Exception 193 { 194 printBanner( "Scenario 2" ); 195 196 embeddedEngineIsUp( "Before instantiating embedded driver...", false ); 197 198 requestNetworkServerBoot(); 202 203 instantiateEmbeddedDriver(); 207 embeddedEngineIsUp( "After instantiating EmbeddedDriver...", true ); 208 209 ping( true ); 214 215 bringDownServer(); 219 shutdownDerby(); 220 embeddedEngineIsUp( "After bringing down server...", false ); 221 } 222 223 229 234 private void ping( boolean shouldBeUp ) 235 throws Exception 236 { 237 NetworkServerControl controller = new NetworkServerControl(); 238 Exception 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 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 267 private void bringDownServer() 268 throws Exception 269 { 270 NetworkServerControl controller = new NetworkServerControl(); 271 272 controller.shutdown(); 273 } 274 275 280 private void requestNetworkServerBoot() 281 throws Exception 282 { 283 setSystemProperty( "derby.drda.startNetworkServer", "true" ); 284 } 285 286 291 private void shutdownDerby() 292 throws Exception 293 { 294 try{ 296 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 297 } catch (SQLException e) {} 298 } 299 300 305 private void printBanner( String 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 319 private void embeddedEngineIsUp( String banner, boolean isUp ) 320 { 321 Object service = null; 322 323 try { 325 service = Monitor.findService( DRIVER_FACTORY, DRIVER_SERVICE ); 326 } 327 catch (NullPointerException 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 341 private void instantiateEmbeddedDriver() 342 throws Exception 343 { 344 Class.forName( "org.apache.derby.jdbc.EmbeddedDriver" ).newInstance(); 345 } 346 347 348 353 private void loadNetworkClientDriver() 354 throws Exception 355 { 356 boolean isAutoloading = TestConfiguration.loadingFromJars(); 360 361 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 String 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 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 |