001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.AutoloadBooting
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: * <p>
023: * This JUnit test verifies driver autoloading does not boot the engine.
024: * This test is only run embedded because we manually bring the server up and down.
025: * </p>
026: *
027: * @author Rick
028: */package org.apache.derbyTesting.functionTests.tests.jdbc4;
029:
030: import java.sql.*;
031: import java.util.*;
032: import junit.framework.*;
033:
034: import org.apache.derby.iapi.services.monitor.Monitor;
035: import org.apache.derby.drda.NetworkServerControl;
036: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
037: import org.apache.derbyTesting.junit.TestConfiguration;
038:
039: public class AutoloadBooting extends BaseJDBCTestCase {
040: /////////////////////////////////////////////////////////////
041: //
042: // CONSTANTS
043: //
044: /////////////////////////////////////////////////////////////
045:
046: private static final String HEADER_LINE = "-------------------------------------";
047: private static final String SPACER_LINE = "-- ";
048: private static final String DRIVER_FACTORY = "org.apache.derby.jdbc.InternalDriver";
049: private static final String DRIVER_SERVICE = "jdbc";
050: private static final String NONEXISTENT_DATABASE = "nonexistentDatabase";
051: private static final String CLIENT_DRIVER_NAME = "org.apache.derby.jdbc.ClientDriver";
052: private static final int SERVER_PORT = 1527;
053: private static final long SLEEP_TIME_MILLIS = 5000L;
054: private static final int PING_COUNT = 6;
055:
056: /////////////////////////////////////////////////////////////
057: //
058: // STATE
059: //
060: /////////////////////////////////////////////////////////////
061:
062: /////////////////////////////////////////////////////////////
063: //
064: // CONSTRUCTOR
065: //
066: /////////////////////////////////////////////////////////////
067:
068: public AutoloadBooting(String name) {
069: super (name);
070: }
071:
072: /////////////////////////////////////////////////////////////
073: //
074: // ENTRY POINT
075: //
076: /////////////////////////////////////////////////////////////
077:
078: /////////////////////////////////////////////////////////////
079: //
080: // JUnit BEHAVIOR
081: //
082: /////////////////////////////////////////////////////////////
083:
084: /**
085: * Only run embedded.
086: */
087: public static Test suite() {
088: TestSuite suite = new TestSuite();
089:
090: if (usingEmbedded())
091: suite.addTestSuite(AutoloadBooting.class);
092:
093: return suite;
094: }
095:
096: /////////////////////////////////////////////////////////////
097: //
098: // TEST ENTRY POINTS
099: //
100: /////////////////////////////////////////////////////////////
101:
102: /**
103: * <p>
104: * Tests that the JDBC driver module is not booted by the autoloading
105: * of drivers.
106: * </p>
107: */
108: public void testBooting() throws Exception {
109: //
110: // Uncomment this line if you want to the test to describe
111: // its progress.
112: //
113: //CONFIG.setVerbosity( true );
114:
115: vetInitialization();
116: scenario1_3();
117: scenario2();
118: }
119:
120: /**
121: * <p>
122: * Make sure that things look right at initialization.
123: * </p>
124: */
125: private void vetInitialization() throws Exception {
126: printBanner("Initialization");
127:
128: //
129: // The engine should not be up when we start.
130: //
131: embeddedEngineIsUp("In the beginning...", false);
132: }
133:
134: /**
135: * <p>
136: * Scenarios (1) and (3) from the problem cases attached to DERBY-1459:
137: * http://issues.apache.org/jira/secure/attachment/12336017/autoloading_scenarios.html.
138: * </p>
139: *
140: * <p>
141: * In scenario (1), we verify that the embedded engine does not boot when
142: * you request a connection from some driver other than the embedded driver.
143: * In scenario (3), we verify that the network server also does not
144: * accidentally come up if you request a connection from some driver
145: * other than the embedded driver.
146: * </p>
147: */
148: private void scenario1_3() throws Exception {
149: printBanner("Scenarios 1 and 3");
150:
151: embeddedEngineIsUp("Before loading client driver...", false);
152:
153: //
154: // Request the network server to come up if the engine boots.
155: //
156: requestNetworkServerBoot();
157:
158: //
159: // The engine should not come up when we load the network client driver.
160: //
161: loadNetworkClientDriver();
162: embeddedEngineIsUp("After loading network client...", false);
163:
164: //
165: // The network server should not be up.
166: //
167: ping(false);
168: }
169:
170: /**
171: * <p>
172: * Scenario (2) from the problem cases attached to DERBY-1459:
173: * http://issues.apache.org/jira/secure/attachment/12336017/autoloading_scenarios.html.
174: * </p>
175: *
176: * <p>
177: * In this scenario, we verify that the engine boots when we instantiate the
178: * embedded driver. We also test that the network server comes up if
179: * we set the appropriate system property.
180: * </p>
181: */
182: private void scenario2() throws Exception {
183: printBanner("Scenario 2");
184:
185: embeddedEngineIsUp("Before instantiating embedded driver...",
186: false);
187:
188: //
189: // Request the network server to come up.
190: //
191: requestNetworkServerBoot();
192:
193: //
194: // The engine should come up when we manually instantiate the EmbeddedDriver.
195: //
196: instantiateEmbeddedDriver();
197: embeddedEngineIsUp("After instantiating EmbeddedDriver...",
198: true);
199:
200: //
201: // The network server should also be booted because we set the
202: // requesting system property.
203: //
204: ping(true);
205:
206: //
207: // Now bring down the server and the engine.
208: //
209: bringDownServer();
210: shutdownDerby();
211: embeddedEngineIsUp("After bringing down server...", false);
212: }
213:
214: /////////////////////////////////////////////////////////////
215: //
216: // MINIONS
217: //
218: /////////////////////////////////////////////////////////////
219:
220: /**
221: * <p>
222: * Verify whether the network server came up.
223: * </p>
224: */
225: private void ping(boolean shouldBeUp) throws Exception {
226: NetworkServerControl controller = new NetworkServerControl();
227: Exception finalException = null;
228: boolean isUp = false;
229:
230: for (int i = 0; i < PING_COUNT; i++) {
231: try {
232: controller.ping();
233: isUp = true;
234: println("Network server came up!");
235:
236: break;
237: } catch (Exception e) {
238: finalException = e;
239: println("Network server still down!");
240: }
241:
242: Thread.sleep(SLEEP_TIME_MILLIS);
243: }
244:
245: assertEquals("Network Server status", shouldBeUp, isUp);
246: }
247:
248: /**
249: * <p>
250: * Tear down the network server.
251: * </p>
252: */
253: private void bringDownServer() throws Exception {
254: NetworkServerControl controller = new NetworkServerControl();
255:
256: controller.shutdown();
257: }
258:
259: /**
260: * <p>
261: * Set the system property which requests the network server to boot.
262: * </p>
263: */
264: private void requestNetworkServerBoot() throws Exception {
265: setSystemProperty("derby.drda.startNetworkServer", "true");
266: }
267:
268: /**
269: * <p>
270: * Bring down the engine.
271: * </p>
272: */
273: private void shutdownDerby() throws Exception {
274: // swallow the shutdown exception
275: try {
276: DriverManager.getConnection("jdbc:derby:;shutdown=true");
277: } catch (SQLException e) {
278: }
279: }
280:
281: /**
282: * <p>
283: * Print out the banner for a test scenario.
284: * </p>
285: */
286: private void printBanner(String bannerText) {
287: println(HEADER_LINE);
288: println(SPACER_LINE);
289: println(SPACER_LINE + bannerText);
290: println(SPACER_LINE);
291: println(HEADER_LINE);
292: }
293:
294: /**
295: * <p>
296: * Verify whether the embedded JDBC driver (and engine) has booted.
297: * </p>
298: */
299: private void embeddedEngineIsUp(String banner, boolean isUp) {
300: Object service = null;
301:
302: // We get an NPE if the service doesn't exist
303: try {
304: service = Monitor.findService(DRIVER_FACTORY,
305: DRIVER_SERVICE);
306: } catch (NullPointerException npe) {
307: }
308:
309: boolean actualState = (service != null);
310:
311: println(banner + " Engine's booted status should be " + isUp
312: + ", and is " + actualState);
313:
314: assertEquals("JDBC driver status", isUp, actualState);
315: }
316:
317: /**
318: * <p>
319: * Load the embedded driver.
320: * </p>
321: */
322: private void instantiateEmbeddedDriver() throws Exception {
323: Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
324: .newInstance();
325: }
326:
327: /**
328: * <p>
329: * Load the network client.
330: * </p>
331: */
332: private void loadNetworkClientDriver() throws Exception {
333: // This test is only run in JDBC 4 or higher which means the
334: // drivers will always be auto-loading when the classes are
335: // being loaded from the jars.
336: boolean isAutoloading = TestConfiguration.loadingFromJars();
337:
338: //
339: // Forcibly load the network client if we are not autoloading it.
340: //
341: if (!isAutoloading) {
342: println("Not autoloading, so forcibly faulting in the client driver.");
343:
344: Class.forName(CLIENT_DRIVER_NAME).newInstance();
345: }
346:
347: //
348: // We should fail to get a connection to the nonexistent database.
349: // However, this call should force the client driver to register itself.
350: //
351: String clientURL = "jdbc:derby://localhost:" + SERVER_PORT
352: + "/" + NONEXISTENT_DATABASE;
353:
354: try {
355: DriverManager.getConnection(clientURL);
356:
357: fail("Should not have connected to " + clientURL);
358: } catch (SQLException se) {
359: println("As expected, failed to connect to " + clientURL);
360: }
361:
362: //
363: // Verify that the client driver registered itself.
364: //
365: Driver clientDriver = DriverManager.getDriver(clientURL);
366:
367: assertNotNull("Client driver should be registered.",
368: clientDriver);
369: assertEquals("Client driver has correct name.",
370: CLIENT_DRIVER_NAME, clientDriver.getClass().getName());
371: }
372:
373: }
|