1 21 22 package org.apache.derbyTesting.functionTests.tests.derbynet; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.InputStream ; 26 import java.net.InetAddress ; 27 import java.sql.DriverManager ; 28 import java.sql.Connection ; 29 import java.sql.Statement ; 30 import java.sql.PreparedStatement ; 31 import java.sql.CallableStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.Blob ; 34 35 import java.io.IOException ; 36 import java.sql.SQLException ; 37 38 import org.apache.derby.drda.NetworkServerControl; 39 import org.apache.derby.tools.ij; 40 import org.apache.derby.client.am.SqlCode; 41 import org.apache.derbyTesting.functionTests.util.TestUtil; 42 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 43 44 52 public class SuicideOfStreaming extends BaseJDBCTestCase { 53 54 private static NetworkServerControl networkServer = null; 55 56 public static void main(String [] args){ 57 58 try{ 59 60 setSystemProperty("derby.debug.suicideOfLayerBStreaming","true"); 61 62 startServer(); 63 64 createTestTable(); 65 testInterruptedReadOfLob(); 66 67 shutdownServer(); 68 69 fail("Streaming was not encountered exception. Suicide of streaming seems to be failed."); 70 71 }catch(Throwable t){ 72 examineThrowable(t); 73 74 } 75 } 76 77 78 private static void createTestTable() 79 throws SQLException , 80 IllegalAccessException , 81 ClassNotFoundException , 82 InstantiationException 83 { 84 85 Connection conn = connectServer(); 86 87 Statement createTableSt = conn.createStatement(); 88 createTableSt.execute("create table TEST_TABLE( TEST_COL blob( 65536 ))"); 89 createTableSt.close(); 90 91 conn.commit(); 92 conn.close(); 93 94 } 95 96 97 private static void testInterruptedReadOfLob() 98 throws SQLException , 99 IOException , 100 IllegalAccessException , 101 ClassNotFoundException , 102 InstantiationException 103 { 104 105 Connection conn = 106 connectServer(); 107 108 conn.setAutoCommit(false); 109 110 PreparedStatement insertLobSt = 111 conn.prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)"); 112 insertLobSt.setBinaryStream(1, 113 createOriginalDataInputStream( 65536 ), 114 65536 ); 115 insertLobSt.executeUpdate(); 116 insertLobSt.close(); 117 118 conn.commit(); 119 120 PreparedStatement st = conn.prepareStatement("select TEST_COL from TEST_TABLE"); 121 ResultSet rs = st.executeQuery(); 122 123 rs.next(); 124 125 InputStream is = rs.getBinaryStream(1); 126 127 int c; 128 while( ( c = is.read() ) > -1 ){ 129 130 System.out.print(c); 131 System.out.print(","); 132 133 if( ( (c + 1) % 256 ) == 0 ) 134 System.out.println(); 135 136 } 137 138 is.close(); 139 140 rs.close(); 141 st.close(); 142 143 conn.commit(); 144 conn.close(); 145 146 } 147 148 149 private static ByteArrayInputStream createOriginalDataInputStream(int length){ 150 151 byte[] originalValue = new byte[ length ]; 152 153 for(int i = 0; i < originalValue.length; i ++){ 154 originalValue[i] = (byte) (i % 256); 155 } 156 157 return new ByteArrayInputStream (originalValue); 158 159 } 160 161 162 protected static boolean isServerStarted(NetworkServerControl server, 163 int ntries){ 164 for (int i = 1; i <= ntries; i ++){ 165 try { 166 Thread.sleep(500); 167 server.ping(); 168 return true; 169 } catch (Exception e) { 170 if (i == ntries) 171 return false; 172 } 173 } 174 return false; 175 } 176 177 178 private static void startServer() 179 throws Exception { 180 181 try{ 182 TestUtil.loadDriver(); 183 184 }catch(Exception e){ 185 e.printStackTrace(); 186 } 187 188 189 networkServer = 190 new NetworkServerControl(InetAddress.getByName("localhost"), 191 1527); 192 networkServer.start( null ); 193 194 if(! isServerStarted( networkServer, 195 60 ) ) 196 System.exit(-1); 197 198 } 199 200 201 private static void shutdownServer() 202 throws Exception { 203 204 networkServer.shutdown(); 205 206 } 207 208 209 private static Connection connectServer() 210 throws SQLException { 211 212 return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix("localhost", 213 1527) + 214 "wombat;create=true", 215 "testuser", 216 "testpassword"); 217 218 } 219 220 221 222 private static void examineThrowable( Throwable t ) { 223 224 if(t instanceof SQLException ){ 225 examineSQLException( ( SQLException ) t ); 226 227 }else{ 228 t.printStackTrace(); 229 fail( t.getMessage() ); 230 231 } 232 233 } 234 235 236 private static void examineSQLException( SQLException sqlex ) { 237 238 if( ( usingDerbyNetClient() && 239 examineExpectedInDerbyNetClient(sqlex) ) || 240 ( usingDerbyNet() && 241 examineExpectedInDerbyNet(sqlex) ) ){ 242 243 return; 244 245 } 246 247 fail(sqlex.getMessage() + "," + 248 "SqlState: " + sqlex.getSQLState() + "," + 249 "SqlCode: " + sqlex.getErrorCode() ); 250 251 } 252 253 254 private static boolean examineExpectedInDerbyNetClient( SQLException sqlex ) { 255 return 256 sqlex.getSQLState().equals("58009") && 257 sqlex.getErrorCode() == SqlCode.disconnectError.getCode(); 258 } 259 260 261 private static boolean examineExpectedInDerbyNet( SQLException sqlex ) { 262 return sqlex.getErrorCode() == SqlCode.disconnectError.getCode(); 263 } 264 265 266 public SuicideOfStreaming(){ 269 super("testSuicideOfStreaming"); 270 } 271 272 273 public void testSuicideOfStreaming(){ 274 main(new String [0]); 275 } 276 277 } 278 279 | Popular Tags |