001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.SuicideOfStreaming
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.net.InetAddress;
027: import java.sql.DriverManager;
028: import java.sql.Connection;
029: import java.sql.Statement;
030: import java.sql.PreparedStatement;
031: import java.sql.CallableStatement;
032: import java.sql.ResultSet;
033: import java.sql.Blob;
034:
035: import java.io.IOException;
036: import java.sql.SQLException;
037:
038: import org.apache.derby.drda.NetworkServerControl;
039: import org.apache.derby.tools.ij;
040: import org.apache.derby.client.am.SqlCode;
041: import org.apache.derbyTesting.functionTests.util.TestUtil;
042: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
043:
044: /**
045: *
046: * This test needs to be build with sanity=true and executed with system property of derby.debug.suicideOfLayerBStreaming=true.
047: * In thosee situation, exception will be happen when streaming from server to client as negative test.
048: *
049: * See SuicideOfStreaming_app.properties also.
050: *
051: */
052: public class SuicideOfStreaming extends BaseJDBCTestCase {
053:
054: private static NetworkServerControl networkServer = null;
055:
056: public static void main(String[] args) {
057:
058: try {
059:
060: setSystemProperty("derby.debug.suicideOfLayerBStreaming",
061: "true");
062:
063: startServer();
064:
065: createTestTable();
066: testInterruptedReadOfLob();
067:
068: shutdownServer();
069:
070: fail("Streaming was not encountered exception. Suicide of streaming seems to be failed.");
071:
072: } catch (Throwable t) {
073: examineThrowable(t);
074:
075: }
076: }
077:
078: private static void createTestTable() throws SQLException,
079: IllegalAccessException, ClassNotFoundException,
080: InstantiationException {
081:
082: Connection conn = connectServer();
083:
084: Statement createTableSt = conn.createStatement();
085: createTableSt
086: .execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
087: createTableSt.close();
088:
089: conn.commit();
090: conn.close();
091:
092: }
093:
094: private static void testInterruptedReadOfLob() throws SQLException,
095: IOException, IllegalAccessException,
096: ClassNotFoundException, InstantiationException {
097:
098: Connection conn = connectServer();
099:
100: conn.setAutoCommit(false);
101:
102: PreparedStatement insertLobSt = conn
103: .prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)");
104: insertLobSt.setBinaryStream(1,
105: createOriginalDataInputStream(65536), 65536);
106: insertLobSt.executeUpdate();
107: insertLobSt.close();
108:
109: conn.commit();
110:
111: PreparedStatement st = conn
112: .prepareStatement("select TEST_COL from TEST_TABLE");
113: ResultSet rs = st.executeQuery();
114:
115: rs.next();
116:
117: InputStream is = rs.getBinaryStream(1);
118:
119: int c;
120: while ((c = is.read()) > -1) {
121:
122: System.out.print(c);
123: System.out.print(",");
124:
125: if (((c + 1) % 256) == 0)
126: System.out.println();
127:
128: }
129:
130: is.close();
131:
132: rs.close();
133: st.close();
134:
135: conn.commit();
136: conn.close();
137:
138: }
139:
140: private static ByteArrayInputStream createOriginalDataInputStream(
141: int length) {
142:
143: byte[] originalValue = new byte[length];
144:
145: for (int i = 0; i < originalValue.length; i++) {
146: originalValue[i] = (byte) (i % 256);
147: }
148:
149: return new ByteArrayInputStream(originalValue);
150:
151: }
152:
153: protected static boolean isServerStarted(
154: NetworkServerControl server, int ntries) {
155: for (int i = 1; i <= ntries; i++) {
156: try {
157: Thread.sleep(500);
158: server.ping();
159: return true;
160: } catch (Exception e) {
161: if (i == ntries)
162: return false;
163: }
164: }
165: return false;
166: }
167:
168: private static void startServer() throws Exception {
169:
170: try {
171: TestUtil.loadDriver();
172:
173: } catch (Exception e) {
174: e.printStackTrace();
175: }
176:
177: networkServer = new NetworkServerControl(InetAddress
178: .getByName("localhost"), 1527);
179: networkServer.start(null);
180:
181: if (!isServerStarted(networkServer, 60))
182: System.exit(-1);
183:
184: }
185:
186: private static void shutdownServer() throws Exception {
187:
188: networkServer.shutdown();
189:
190: }
191:
192: private static Connection connectServer() throws SQLException {
193:
194: return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix(
195: "localhost", 1527)
196: + "wombat;create=true", "testuser", "testpassword");
197:
198: }
199:
200: private static void examineThrowable(Throwable t) {
201:
202: if (t instanceof SQLException) {
203: examineSQLException((SQLException) t);
204:
205: } else {
206: t.printStackTrace();
207: fail(t.getMessage());
208:
209: }
210:
211: }
212:
213: private static void examineSQLException(SQLException sqlex) {
214:
215: if ((usingDerbyNetClient() && examineExpectedInDerbyNetClient(sqlex))
216: || (usingDerbyNet() && examineExpectedInDerbyNet(sqlex))) {
217:
218: return;
219:
220: }
221:
222: fail(sqlex.getMessage() + "," + "SqlState: "
223: + sqlex.getSQLState() + "," + "SqlCode: "
224: + sqlex.getErrorCode());
225:
226: }
227:
228: private static boolean examineExpectedInDerbyNetClient(
229: SQLException sqlex) {
230: return sqlex.getSQLState().equals("58009")
231: && sqlex.getErrorCode() == SqlCode.disconnectError
232: .getCode();
233: }
234:
235: private static boolean examineExpectedInDerbyNet(SQLException sqlex) {
236: return sqlex.getErrorCode() == SqlCode.disconnectError
237: .getCode();
238: }
239:
240: //JUnit test support.
241: //
242: public SuicideOfStreaming() {
243: super ("testSuicideOfStreaming");
244: }
245:
246: public void testSuicideOfStreaming() {
247: main(new String[0]);
248: }
249:
250: }
|