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