001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.TestConnectionMethods
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: package org.apache.derbyTesting.functionTests.tests.jdbc4;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.io.PrintWriter;
032: import java.net.InetAddress;
033: import java.sql.Blob;
034: import java.sql.Clob;
035: import java.sql.Connection;
036: import java.sql.DriverManager;
037: import java.sql.PreparedStatement;
038: import java.sql.SQLException;
039: import java.sql.Statement;
040: import org.apache.derby.iapi.error.StandardException;
041: import org.apache.derby.drda.NetworkServerControl;
042: import org.apache.derby.tools.ij;
043: import org.apache.derby.shared.common.reference.SQLState;
044: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
045: import org.apache.derbyTesting.functionTests.util.TestUtil;
046:
047: /**
048: * This class is used to test the implementations of the JDBC 4.0 methods
049: * in the Connection interface
050: */
051:
052: public class TestConnectionMethods {
053: Connection conn = null;
054:
055: private FileOutputStream serverOutput;
056:
057: /**
058: * Constructor for an object that is used for running test of the
059: * new connection methods defined by JDBC 4.
060: */
061: public TestConnectionMethods(Connection connIn) {
062: conn = connIn;
063: }
064:
065: /**
066: * Test the createClob method implementation in the Connection interface
067: * in the Network Client
068: */
069: void t_createClob_Client() {
070: int c;
071: Clob clob;
072: try {
073: Statement s = conn.createStatement();
074: s.execute("create table clobtable2(n int,clobcol CLOB)");
075: PreparedStatement ps = conn
076: .prepareStatement("insert into clobtable2"
077: + " values(?,?)");
078: ps.setInt(1, 1000);
079: clob = conn.createClob();
080: File file = new File("extin/short.txt");
081: FileInputStream is = new FileInputStream(file);
082: OutputStream os = clob.setAsciiStream(1);
083: c = is.read();
084: while (c > 0) {
085: os.write(c);
086: c = is.read();
087: }
088: ps.setClob(2, clob);
089: ps.executeUpdate();
090: } catch (SQLException e) {
091: e.printStackTrace();
092: } catch (FileNotFoundException fnfe) {
093: fnfe.printStackTrace();
094: } catch (IOException ioe) {
095: ioe.printStackTrace();
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099: }
100:
101: /**
102: * Test the createBlob method implementation in the Connection interface for
103: * in the Network Client
104: */
105: void t_createBlob_Client() {
106: int c;
107: Blob blob;
108: try {
109: Statement s = conn.createStatement();
110: s.execute("create table blobtable2(n int,blobcol BLOB)");
111: PreparedStatement ps = conn
112: .prepareStatement("insert into blobtable2"
113: + " values(?,?)");
114: ps.setInt(1, 1000);
115: blob = conn.createBlob();
116: File file = new File("extin/short.txt");
117: FileInputStream is = new FileInputStream(file);
118: OutputStream os = blob.setBinaryStream(1);
119: c = is.read();
120: while (c > 0) {
121: os.write(c);
122: c = is.read();
123: }
124: ps.setBlob(2, blob);
125: ps.executeUpdate();
126: } catch (SQLException e) {
127: e.printStackTrace();
128: } catch (FileNotFoundException fnfe) {
129: fnfe.printStackTrace();
130: } catch (IOException ioe) {
131: ioe.printStackTrace();
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136:
137: /**
138: * Test the Connection.isValid method
139: */
140: void t_isValid() {
141: /*
142: * Test illegal parameter values
143: */
144: try {
145: conn.isValid(-1); // Negative timeout
146: System.out.println("FAIL: isValid(-1): "
147: + "Invalid argument execption not thrown");
148: } catch (SQLException e) {
149: if (!StandardException.getSQLStateFromIdentifier(
150: SQLState.INVALID_API_PARAMETER).equals(
151: e.getSQLState())) {
152: System.out
153: .println("FAIL: isValid(-1): Unexpected SQLException"
154: + e);
155: }
156: }
157:
158: /*
159: * Test with no timeout
160: */
161: try {
162: if (!conn.isValid(0)) {
163: System.out.println("FAIL: isValid(0): returned false");
164: }
165: } catch (Exception e) {
166: System.out
167: .println("FAIL: isValid(0): Unexpected exception: "
168: + e);
169: }
170:
171: /*
172: * Test with a valid timeout
173: */
174: try {
175: if (!conn.isValid(1)) {
176: System.out.println("FAIL: isValid(1): returned false");
177: }
178: } catch (Exception e) {
179: System.out
180: .println("FAIL: isValid(1): Unexpected exception: "
181: + e);
182: }
183:
184: /*
185: * Test on a closed connection
186: */
187: try {
188: conn.close();
189: } catch (SQLException e) {
190: System.out
191: .println("FAIL: close failed: Unexpected exception: "
192: + e);
193: }
194:
195: try {
196: if (conn.isValid(0)) {
197: System.out
198: .println("FAIL: isValid(0) on closed connection: "
199: + "returned true");
200: }
201: } catch (Exception e) {
202: System.out
203: .println("FAIL: isValid(0) on closed connection: "
204: + "Unexpected exception: " + e);
205: }
206:
207: /* Open a new connection and test it */
208: try {
209: conn = ij.startJBMS();
210: } catch (Exception e) {
211: System.out.println("FAIL: failed to open new connection: "
212: + "Unexpected exception: " + e);
213: }
214:
215: try {
216: if (!conn.isValid(0)) {
217: System.out
218: .println("FAIL: isValid(0) on open connection: "
219: + "returned false");
220: }
221: } catch (Exception e) {
222: System.out.println("FAIL: isValid(0) on open connection: "
223: + "Unexpected exception: " + e);
224: }
225:
226: /*
227: * Test on stopped database
228: */
229: shutdownDatabase();
230:
231: /* Test if that connection is not valid */
232: try {
233: if (conn.isValid(0)) {
234: System.out
235: .println("FAIL: isValid(0) on stopped database: "
236: + "returned true");
237: }
238: } catch (Exception e) {
239: System.out
240: .println("FAIL: isValid(0) on a stopped database: "
241: + "Unexpected exception: " + e);
242: }
243:
244: /* Start the database by getting a new connection to it */
245: try {
246: conn = ij.startJBMS();
247: } catch (Exception e) {
248: System.out.println("FAIL: failed to re-start database: "
249: + "Unexpected exception: " + e);
250: }
251:
252: /* Check that a new connection to the newly started database is valid */
253: try {
254: if (!conn.isValid(0)) {
255: System.out
256: .println("FAIL: isValid(0) on new connection: "
257: + "returned false");
258: }
259: } catch (Exception e) {
260: System.out.println("FAIL: isValid(0) on new connection: "
261: + "Unexpected exception: " + e);
262: }
263:
264: /*
265: * Test on stopped Network Server client
266: */
267: if (!usingEmbeddedClient()) {
268: stopNetworkServer();
269:
270: /* Test that the connection is not valid */
271: try {
272: if (conn.isValid(0)) {
273: System.out
274: .println("FAIL: isValid(0) on stopped database: "
275: + "returned true");
276: }
277: } catch (Exception e) {
278: System.out
279: .println("FAIL: isValid(0) on a stopped database: "
280: + "Unexpected exception: " + e);
281: }
282:
283: /*
284: * Start the network server and get a new connection and check that
285: * the new connection is valid.
286: */
287: startNetworkServer();
288:
289: try {
290: // Get a new connection to the database
291: conn = ij.startJBMS();
292: } catch (Exception e) {
293: System.out
294: .println("FAIL: failed to re-start database: "
295: + "Unexpected exception: " + e);
296: e.printStackTrace();
297: }
298:
299: /* Check that a new connection to the newly started Derby is valid */
300: try {
301: if (!conn.isValid(0)) {
302: System.out
303: .println("FAIL: isValid(0) on new connection: "
304: + "returned false");
305: }
306: } catch (Exception e) {
307: System.out
308: .println("FAIL: isValid(0) on new connection: "
309: + "Unexpected exception: " + e);
310: }
311: }
312: }
313:
314: public void startTestConnectionMethods_Client() {
315: t_createClob_Client();
316: t_createBlob_Client();
317: t_isValid();
318: }
319:
320: public void startTestConnectionMethods_Embedded() {
321: t_isValid();
322: }
323:
324: /**
325: * Shut down the test database
326: */
327: private void shutdownDatabase() {
328: try {
329: // Get the name for the database from the test's property file
330: String databaseName = System
331: .getProperty("ij.dataSource.databaseName");
332: if (databaseName != null) {
333: TestUtil.getConnection(databaseName, "shutdown=true");
334: } else {
335: System.out
336: .println("FAIL: shutdownDatabase: "
337: + "property ij.dataSource.databaseName not defined");
338: }
339: } catch (Exception e) {
340: // Ignore any exeptions from shutdown
341: }
342: }
343:
344: /**
345: * Stop the network server
346: */
347: private void stopNetworkServer() {
348: try {
349: NetworkServerControl networkServer = new NetworkServerControl();
350: networkServer.shutdown();
351: if (serverOutput != null) {
352: serverOutput.close();
353: }
354: } catch (Exception e) {
355: System.out
356: .println("INFO: Network server shutdown returned: "
357: + e);
358: }
359: }
360:
361: /**
362: * Start the network server
363: */
364: private void startNetworkServer() {
365: String hostName = null;
366: int serverPort;
367:
368: // Determines which host and port to run the network server on
369: // This is based how it is done in the test testSecMec.java
370: String serverName = TestUtil.getHostName();
371: if (serverName.equals("localhost")) {
372: serverPort = 1527;
373: } else {
374: serverPort = 20000;
375: }
376:
377: try {
378: String fileName = System.getProperty("derby.system.home",
379: "")
380: + "serverConsoleOutput.log";
381: serverOutput = new FileOutputStream(fileName);
382:
383: NetworkServerControl networkServer = new NetworkServerControl(
384: InetAddress.getByName(serverName), serverPort);
385: networkServer.start(new PrintWriter(serverOutput));
386:
387: // Wait for the network server to start
388: boolean started = false;
389: int retries = 10; // Max retries = max seconds to wait
390: while (!started && retries > 0) {
391: try {
392: // Sleep 1 second and then ping the network server
393: Thread.sleep(1000);
394: networkServer.ping();
395:
396: // If ping does not throw an exception the server has started
397: started = true;
398: } catch (Exception e) {
399: System.out.println("INFO: ping returned: " + e);
400: retries--;
401: }
402: }
403:
404: // Check if we got a reply on ping
405: if (!started) {
406: System.out
407: .println("FAIL: Failed to start network server");
408: }
409: } catch (Exception e) {
410: System.out
411: .println("FAIL: startNetworkServer got exception: "
412: + e);
413: }
414: }
415:
416: /**
417: * <p>
418: * Return true if we're running under the embedded client.
419: * </p>
420: */
421: private static boolean usingEmbeddedClient() {
422: return "embedded".equals(System.getProperty("framework"));
423: }
424:
425: public static void main(String args[]) {
426: try {
427: // use the ij utility to read the property file and
428: // make the initial connection.
429: ij.getPropertyArg(args);
430:
431: Connection conn_main = ij.startJBMS();
432:
433: if (usingEmbeddedClient()) {
434: TestConnectionMethods tcm = new TestConnectionMethods(
435: conn_main);
436: tcm.startTestConnectionMethods_Embedded();
437: } else // DerbyNetClient
438: {
439: TestConnectionMethods tcm1 = new TestConnectionMethods(
440: conn_main);
441: tcm1.startTestConnectionMethods_Client();
442: }
443:
444: conn_main.close();
445:
446: } catch (Exception e) {
447: System.out.println("" + e);
448: e.printStackTrace();
449: }
450: }
451: }
|