001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.resultsetStream
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.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.ResultSetMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.sql.SQLException;
030: import java.sql.Types;
031: import java.sql.PreparedStatement;
032:
033: import org.apache.derby.tools.ij;
034: import org.apache.derby.tools.JDBCDisplayUtil;
035: import org.apache.derbyTesting.functionTests.util.TestUtil;
036:
037: import java.io.InputStream;
038: import java.io.IOException;
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.BufferedInputStream;
042: import java.util.zip.CRC32;
043: import java.io.Reader;
044: import java.io.StringReader;
045:
046: /**
047: * Test of JDBC result set Stream calls.
048: *
049: * @author djd
050: */
051:
052: public class resultsetStream {
053:
054: public static void main(String[] args) {
055: Connection con;
056: ResultSetMetaData met;
057: ResultSet rs;
058: Statement stmt;
059:
060: System.out.println("Test resultsetStream starting");
061:
062: try {
063: // use the ij utility to read the property file and
064: // make the initial connection.
065: ij.getPropertyArg(args);
066: con = ij.startJBMS();
067:
068: stmt = con.createStatement();
069:
070: stmt
071: .execute("create table t2 (len int, data LONG VARCHAR FOR BIT DATA)");
072: PreparedStatement ppw = con
073: .prepareStatement("insert into t2 (len, data) values (?, ?)");
074: String filePath = "extin";
075: String sep = System.getProperty("file.separator");
076: boolean exists = (new File("extin", "littleclob.utf"))
077: .exists();
078: if (!exists) {
079: String userDir = System.getProperty("user.dir");
080: filePath = userDir + sep + ".." + sep + filePath;
081: }
082: File file = new File(filePath + sep + "littleclob.utf");
083: int fileSize = (int) file.length();
084: BufferedInputStream fileData = new BufferedInputStream(
085: new FileInputStream(file));
086: ppw.setInt(1, fileSize);
087: ppw.setBinaryStream(2, fileData, fileSize);
088: ppw.executeUpdate();
089:
090: file = new File(filePath + sep + "short.utf");
091: fileSize = (int) file.length();
092: fileData = new BufferedInputStream(
093: new FileInputStream(file));
094: ppw.setInt(1, fileSize);
095: ppw.setBinaryStream(2, fileData, fileSize);
096: ppw.executeUpdate();
097: // null binary value
098: ppw.setInt(1, -1);
099: ppw.setBinaryStream(2, (java.io.InputStream) null, 0);
100: ppw.executeUpdate();
101:
102: // value copied over from original Java object test.
103: File rssg = new java.io.File(filePath + sep
104: + "resultsetStream.gif");
105: int rssgLength = (int) rssg.length();
106: ppw.setInt(1, (int) rssgLength);
107: ppw.setBinaryStream(2, new FileInputStream(rssg),
108: rssgLength);
109: ppw.executeUpdate();
110:
111: // try binary stream processing on a known file.
112: rs = stmt.executeQuery("select data from t2 where len = "
113: + rssgLength);
114: met = rs.getMetaData();
115: System.out.println("getColumnCount(): "
116: + met.getColumnCount());
117: while (rs.next()) {
118: // JDBC columns use 1-based counting
119:
120: // get the first column as a stream
121: try {
122:
123: InputStream is = rs.getBinaryStream(1);
124: if (is == null) {
125: System.out
126: .println("FAIL - getBinaryStream() return null");
127: break;
128: }
129:
130: // read the first 200 bytes from the stream and checksum them
131: byte[] b200 = new byte[200];
132:
133: // no guaratees to read all 200 bytes in one read call.
134: int count = 0;
135:
136: while (count < 200) {
137: int r = is.read(b200, count, 200 - count);
138: if (r == -1)
139: break;
140: count += r;
141: }
142:
143: if (count != 200) {
144: System.out
145: .println("FAIL - failed to read 200 bytes from known file");
146: break;
147: }
148:
149: CRC32 cs = new CRC32();
150:
151: cs.reset();
152: cs.update(b200);
153:
154: System.out.println("Checksum of first 200 bytes "
155: + cs.getValue());
156:
157: count = 200;
158: for (; is.read() != -1; count++) {
159: }
160: System.out.println("Size of file = " + count);
161: is.close();
162: } catch (Throwable e) {
163: System.out
164: .println("FAIL - exection while processing valid file");
165: if (e instanceof SQLException)
166: JDBCDisplayUtil.ShowSQLException(System.out,
167: (SQLException) e);
168: }
169: }
170: rs.close();
171:
172: // check the stream is closed once another get call is made.
173: rs = stmt
174: .executeQuery("select data, len from t2 where len = "
175: + rssgLength);
176: met = rs.getMetaData();
177: System.out.println("getColumnCount(): "
178: + met.getColumnCount());
179: while (rs.next()) {
180: // JDBC columns use 1-based counting
181:
182: // get the first column as a stream
183: try {
184:
185: InputStream is = rs.getBinaryStream(1);
186: if (is == null) {
187: System.out
188: .println("FAIL - getBinaryStream() return null");
189: break;
190: }
191:
192: // read the first 200 bytes from the stream and checksum them
193: byte[] b200 = new byte[200];
194:
195: // no guaratees to read all 200 bytes in one read call.
196: int count = 0;
197:
198: while (count < 200) {
199: int r = is.read(b200, count, 200 - count);
200: if (r == -1)
201: break;
202: count += r;
203: }
204:
205: if (count != 200) {
206: System.out
207: .println("FAIL - failed to read 200 bytes from known file");
208: break;
209: }
210:
211: CRC32 cs = new CRC32();
212:
213: cs.reset();
214: cs.update(b200);
215:
216: System.out.println("Checksum of first 200 bytes "
217: + cs.getValue());
218:
219: System.out.println("second columns is "
220: + rs.getInt(2));
221:
222: System.out.println("FAILS DUE TO BUG 5710");
223: try {
224: is.read();
225: System.out
226: .println("FAIL - stream was not closed after a get*() call. "
227: + is.getClass());
228: break;
229: } catch (IOException ioe) {
230: // yes, stream should be closed
231: }
232: } catch (Throwable e) {
233: System.out
234: .println("FAIL - exection while processing valid file");
235: if (e instanceof SQLException)
236: JDBCDisplayUtil.ShowSQLException(System.out,
237: (SQLException) e);
238: }
239: }
240: rs.close();
241:
242: // check a SQL null object gets a null stream
243: rs = stmt
244: .executeQuery("select data from t2 where len = -1");
245: met = rs.getMetaData();
246: System.out.println("getColumnCount(): "
247: + met.getColumnCount());
248: while (rs.next()) {
249: // JDBC columns use 1-based counting
250:
251: // get the first column as a stream
252:
253: InputStream is = rs.getBinaryStream(1);
254: if (is != null) {
255: System.out
256: .println("FAIL - getBinaryStream() did not return null for SQL null");
257: break;
258: }
259:
260: }
261: rs.close();
262:
263: rs = stmt
264: .executeQuery("select len, data from t2 where len = "
265: + fileSize);
266: rs.next();
267: fileSize = rs.getInt(1);
268: fileData = new BufferedInputStream(rs.getBinaryStream(2));
269: int readCount = 0;
270: while (true) {
271: int data = fileData.read();
272: if (data == -1)
273: break;
274: readCount++;
275: }
276: fileData.close();
277: System.out.println("len=" + fileSize);
278: System.out.println("number of reads=" + readCount);
279:
280: // check binary input streams of invalid length.
281: // JDBC 3.0 tutorial says stream contents must match length.
282:
283: byte[] tooFew = new byte[234];
284:
285: ppw.setInt(1, 234);
286: ppw.setBinaryStream(2, new java.io.ByteArrayInputStream(
287: tooFew), 234); // matching length
288: ppw.executeUpdate();
289:
290: ppw.setInt(1, 235);
291: ppw.setBinaryStream(2, new java.io.ByteArrayInputStream(
292: tooFew), 235); // too few bytes in stream
293: try {
294: ppw.executeUpdate();
295: System.out
296: .println("FAIL - execute with setBinaryStream() with too few bytes succeeded");
297: } catch (SQLException sqle) {
298: org.apache.derbyTesting.functionTests.util.TestUtil
299: .dumpSQLExceptions(sqle, true);
300: }
301:
302: ppw.setInt(1, 233);
303: ppw.setBinaryStream(2, new java.io.ByteArrayInputStream(
304: tooFew), 233); // too many bytes
305: try {
306: ppw.executeUpdate();
307: System.out
308: .println("FAIL - execute with setBinaryStream() with too many bytes succeeded");
309: } catch (SQLException sqle) {
310: org.apache.derbyTesting.functionTests.util.TestUtil
311: .dumpSQLExceptions(sqle, true);
312: }
313:
314: ppw.close();
315: rs.close();
316:
317: TestOfGetAsciiStream.executeTestOfGetAsciiStream(con);
318:
319: cleanUp(stmt);
320: stmt.close();
321: con.close();
322:
323: } catch (SQLException e) {
324: dumpSQLExceptions(e);
325: } catch (Throwable e) {
326: System.out.println("FAIL -- unexpected exception:"
327: + e.toString());
328: e.printStackTrace();
329: }
330:
331: System.out.println("Test resultsetStream finished");
332: }
333:
334: static private void dumpSQLExceptions(SQLException se) {
335: System.out.println("FAIL -- unexpected exception: "
336: + se.toString());
337: while (se != null) {
338: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
339: se = se.getNextException();
340: }
341: }
342:
343: private static void cleanUp(Statement stmt) throws SQLException {
344: String[] testObjects = { " table t3", "table t2" };
345: TestUtil.cleanUpTest(stmt, testObjects);
346: }
347:
348: static class TestOfGetAsciiStream {
349:
350: final static String TEST_STRING_DATA = "ABCDEFG"
351: + "\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5"
352: + "\u00ff\u0100" + "\u3042\u3044\u3046\u3048\u304a";
353:
354: static private void executeTestOfGetAsciiStream(Connection conn)
355: throws SQLException {
356:
357: System.out.println("Test of getAsciiStream");
358: createTestTable(conn);
359: executeTestRun(conn);
360:
361: }
362:
363: static private void createTestTable(Connection conn)
364: throws SQLException {
365:
366: PreparedStatement st = null;
367:
368: try {
369: st = conn
370: .prepareStatement("create table t3(text_data clob)");
371: st.executeUpdate();
372:
373: } finally {
374: if (st != null)
375: st.close();
376:
377: }
378:
379: }
380:
381: static private void executeTestRun(Connection conn)
382: throws SQLException {
383:
384: insertTestData(conn);
385: printTestData(conn);
386:
387: }
388:
389: static private void insertTestData(Connection conn)
390: throws SQLException {
391:
392: PreparedStatement st = null;
393:
394: try {
395:
396: st = conn
397: .prepareStatement("insert into t3(text_data) values(?)");
398: st.setCharacterStream(1, new StringReader(
399: TEST_STRING_DATA), TEST_STRING_DATA.length());
400: st.executeUpdate();
401:
402: } finally {
403: if (st != null)
404: st.close();
405:
406: }
407:
408: }
409:
410: static private void printTestData(Connection conn)
411: throws SQLException {
412:
413: PreparedStatement st = null;
414: ResultSet rs = null;
415:
416: try {
417: st = conn.prepareStatement("select "
418: + "text_data as text_data_col1,"
419: + "text_data as text_data_col2 " + "from "
420: + "t3");
421: rs = st.executeQuery();
422:
423: while (rs.next()) {
424: printTestDataInARowViaStream(rs);
425: printTestDataInARowViaReader(rs);
426: }
427:
428: } catch (IOException e) {
429: System.out.println("FAIL -- unexpected IOException: "
430: + e.toString());
431: e.printStackTrace();
432:
433: } finally {
434: if (rs != null) {
435: rs.close();
436: }
437:
438: if (st != null) {
439: st.close();
440: }
441:
442: }
443: }
444:
445: static private void printTestDataInARowViaStream(ResultSet rs)
446: throws SQLException, IOException {
447:
448: InputStream is = null;
449:
450: try {
451: is = rs.getAsciiStream(1);
452:
453: for (int c = is.read(); c > -1; c = is.read()) {
454:
455: System.out.print(getCharacterCodeString((char) c));
456: }
457:
458: System.out.println();
459:
460: } finally {
461: if (is != null)
462: is.close();
463: }
464:
465: }
466:
467: static private void printTestDataInARowViaReader(ResultSet rs)
468: throws SQLException, IOException {
469:
470: Reader reader = null;
471:
472: try {
473: reader = rs.getCharacterStream(2);
474:
475: for (int c = reader.read(); c > -1; c = reader.read()) {
476:
477: System.out.print(getCharacterCodeString((char) c));
478:
479: }
480:
481: System.out.println();
482:
483: } finally {
484: if (reader != null) {
485: reader.close();
486: }
487: }
488:
489: }
490:
491: private static String getCharacterCodeString(char c) {
492:
493: String hexString = Integer.toHexString((int) c);
494:
495: while (hexString.length() < 4) {
496: hexString = "0" + hexString;
497: }
498:
499: return "U+" + hexString;
500: }
501:
502: }
503:
504: }
|