001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.OutBufferedStream
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.derbynet;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.io.FileOutputStream;
028: import java.net.InetAddress;
029: import java.sql.DriverManager;
030: import java.sql.Connection;
031: import java.sql.Statement;
032: import java.sql.PreparedStatement;
033: import java.sql.CallableStatement;
034: import java.sql.ResultSet;
035: import java.sql.Blob;
036:
037: import java.io.IOException;
038: import java.io.FileNotFoundException;
039: import java.sql.SQLException;
040:
041: import org.apache.derby.drda.NetworkServerControl;
042: import org.apache.derby.tools.ij;
043: import org.apache.derbyTesting.functionTests.util.TestUtil;
044:
045: /**
046: *
047: * This program tests streaming lob with derby.drda.streamOutBufferSize configuration.
048: *
049: * When derby.drda.streamOutBufferSize was configured,
050: * a buffer of configured size was placed at network server just before sending stream to client.
051: *
052: * The configuration was written in OutBufferedStream_app.properties.
053: *
054: */
055: public class OutBufferedStream {
056: private static NetworkServerControl networkServer = null;
057:
058: // Need this to keep track of database has been created or not
059: // to avoid case of DERBY-300
060: private static boolean dbNotCreated = true;
061:
062: public static void main(String[] args) {
063:
064: try {
065:
066: ij.getPropertyArg(args);
067:
068: SwitchablePrintStream testExecutionOutput = new SwitchablePrintStream(
069: System.out);
070: SwitchablePrintStream testExecutionErrOutput = new SwitchablePrintStream(
071: System.out);
072:
073: OutputStream originalOut = System.out;
074: OutputStream originalErr = System.err;
075:
076: System.setOut(testExecutionOutput);
077: System.setErr(testExecutionErrOutput);
078:
079: startServer();
080:
081: System.out.println("start test.");
082:
083: createTestTable();
084: testReadOfLob();
085:
086: System.out.println("test done.");
087:
088: OutputStream shutdownLogFileStream = null;
089: OutputStream shutdownErrLogFileStream = null;
090:
091: try {
092: shutdownLogFileStream = getShutdownLogFileStream();
093: shutdownErrLogFileStream = getShutdownErrLogFileStream();
094:
095: testExecutionOutput.switchOutput(shutdownLogFileStream);
096: testExecutionErrOutput
097: .switchOutput(shutdownErrLogFileStream);
098:
099: shutdownServer();
100:
101: testExecutionOutput.switchOutput(originalOut);
102: testExecutionErrOutput.switchOutput(originalErr);
103:
104: } finally {
105:
106: if (shutdownLogFileStream != null) {
107: shutdownLogFileStream.flush();
108: shutdownLogFileStream.close();
109: }
110:
111: if (shutdownErrLogFileStream != null) {
112: shutdownErrLogFileStream.flush();
113: shutdownErrLogFileStream.close();
114: }
115: }
116:
117: } catch (Throwable t) {
118: t.printStackTrace();
119:
120: }
121: }
122:
123: private static void createTestTable() throws SQLException,
124: IllegalAccessException, ClassNotFoundException,
125: InstantiationException {
126:
127: Connection conn = getConnection();
128:
129: Statement createTableSt = conn.createStatement();
130: createTableSt
131: .execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
132: createTableSt.close();
133:
134: conn.commit();
135: conn.close();
136:
137: }
138:
139: private static void testReadOfLob() throws SQLException,
140: IOException, IllegalAccessException,
141: ClassNotFoundException, InstantiationException {
142:
143: Connection conn = getConnection();
144:
145: conn.setAutoCommit(false);
146:
147: PreparedStatement insertLobSt = conn
148: .prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)");
149: insertLobSt.setBinaryStream(1,
150: createOriginalDataInputStream(65536), 65536);
151: insertLobSt.executeUpdate();
152: insertLobSt.close();
153:
154: conn.commit();
155:
156: PreparedStatement st = conn
157: .prepareStatement("select TEST_COL from TEST_TABLE");
158: ResultSet rs = st.executeQuery();
159:
160: rs.next();
161:
162: InputStream is = rs.getBinaryStream(1);
163:
164: int c;
165: while ((c = is.read()) > -1) {
166:
167: System.out.print(c);
168: System.out.print(",");
169:
170: if (((c + 1) % 256) == 0)
171: System.out.println();
172:
173: }
174:
175: is.close();
176:
177: rs.close();
178: st.close();
179:
180: conn.commit();
181: conn.close();
182:
183: System.out.println();
184:
185: }
186:
187: private static ByteArrayInputStream createOriginalDataInputStream(
188: int length) {
189:
190: byte[] originalValue = new byte[length];
191:
192: for (int i = 0; i < originalValue.length; i++) {
193: originalValue[i] = (byte) (i % 256);
194: }
195:
196: return new ByteArrayInputStream(originalValue);
197:
198: }
199:
200: protected static boolean isServerStarted(
201: NetworkServerControl server, int ntries) {
202: for (int i = 1; i <= ntries; i++) {
203: try {
204: Thread.sleep(500);
205: server.ping();
206: return true;
207: } catch (Exception e) {
208: if (i == ntries)
209: return false;
210: }
211: }
212: return false;
213: }
214:
215: private static void startServer() throws Exception {
216:
217: try {
218: TestUtil.loadDriver();
219:
220: } catch (Exception e) {
221: e.printStackTrace();
222: }
223:
224: networkServer = new NetworkServerControl(InetAddress
225: .getByName("localhost"), 1527);
226: networkServer.start(null);
227:
228: if (!isServerStarted(networkServer, 60))
229: System.exit(-1);
230:
231: }
232:
233: private static void shutdownServer() throws Exception {
234:
235: networkServer.shutdown();
236:
237: }
238:
239: private static Connection getConnection() throws SQLException {
240:
241: String dbName = "wombat";
242: if (dbNotCreated) {
243: dbName = dbName + ";create=true";
244: dbNotCreated = false;
245: }
246: return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix(
247: "localhost", 1527)
248: + dbName, "testuser", "testpassword");
249:
250: }
251:
252: private static OutputStream getShutdownLogFileStream()
253: throws FileNotFoundException {
254:
255: return new FileOutputStream("outBufferedStream."
256: + System.getProperty("framework", "") + "."
257: + "shutdown.std.log");
258: }
259:
260: private static OutputStream getShutdownErrLogFileStream()
261: throws FileNotFoundException {
262:
263: return new FileOutputStream("outBufferedStream."
264: + System.getProperty("framework", "") + "."
265: + "shutdown.err.log");
266: }
267:
268: }
|