1 21 22 package org.apache.derbyTesting.functionTests.util; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.error.StandardException; 26 import org.apache.derby.iapi.db.*; 27 import java.sql.*; 28 import java.io.ByteArrayInputStream ; 29 import java.io.InputStream ; 30 import java.math.BigDecimal ; 31 import java.math.BigInteger ; 32 33 36 public class StreamUtil 37 { 38 39 public static void insertAsciiColumn 40 ( 41 Connection conn, 42 String stmtText, 43 int colNumber, 44 String value, 45 int length 46 ) 47 throws Throwable 48 { 49 PreparedStatement ps = conn.prepareStatement(stmtText); 50 setAsciiColumn(ps, colNumber, value.charAt(0), length); 51 ps.setInt(colNumber + 1, length); 52 ps.execute(); 53 } 54 55 public static void insertBinaryColumn 56 ( 57 Connection conn, 58 String stmtText, 59 int colNumber, 60 String value, 61 int length 62 ) 63 throws Throwable 64 { 65 PreparedStatement ps = conn.prepareStatement(stmtText); 66 setBinaryColumn(ps, colNumber, value.charAt(0), length); 67 ps.setInt(colNumber + 1, length); 68 ps.execute(); 69 } 70 71 75 private static void setAsciiColumn 76 ( 77 PreparedStatement ps, 78 int colNumber, 79 char value, 80 int length 81 ) throws SQLException 82 { 83 byte[] barray = new byte[length]; 84 for (int i = 0; i < length; i++) 85 { 86 barray[i] = (byte)value; 87 } 88 ps.setAsciiStream(colNumber, new ByteArrayInputStream (barray), length); 89 } 90 91 95 private static void setBinaryColumn 96 ( 97 PreparedStatement ps, 98 int colNumber, 99 char value, 100 int length 101 ) throws SQLException 102 { 103 byte[] barray = new byte[length]; 104 for (int i = 0; i < length; i++) 105 { 106 barray[i] = (byte)value; 107 } 108 ps.setBinaryStream(colNumber, new ByteArrayInputStream (barray), length); 109 } 110 111 public static int getAsciiColumn 112 ( 113 int whichRS, int colNumber, 115 String value 116 ) throws Throwable 117 { 118 System.out.println("\ngetAsciiColumn() called"); 119 ResultSet rs = getRowSet(whichRS); 120 if( rs == null) 121 return 0; 122 while (rs.next()) 123 { 124 InputStream in = rs.getAsciiStream(colNumber); 125 int readlen = drainAndValidateStream(in, value.charAt(0)); 126 if (readlen != rs.getInt(4)) 127 throw new Exception ("INCORRECT READ LENGTH " + readlen + " <> " + rs.getInt(4)); 128 } 129 return 1; 130 } 131 132 private static ResultSet getRowSet( int whichRS) 133 throws Throwable 134 { 135 TriggerExecutionContext tec = org.apache.derby.iapi.db.Factory.getTriggerExecutionContext(); 136 if( tec == null) 137 { 138 System.out.println( "Not in a trigger."); 139 return null; 140 } 141 142 return (whichRS == 0) ? tec.getOldRowSet() : tec.getNewRowSet(); 143 } 144 145 public static int getBinaryColumn 146 ( 147 int whichRS, int colNumber, 149 String value 150 ) throws Throwable 151 { 152 System.out.println("\ngetBinaryColumn() called"); 153 ResultSet rs = getRowSet(whichRS); 154 if( rs == null) 155 return 0; 156 while (rs.next()) 157 { 158 InputStream in = rs.getBinaryStream(colNumber); 159 int readlen = drainAndValidateStream(in, value.charAt(0)); 160 161 if (readlen != rs.getInt(4)) 162 throw new Exception ("INCORRECT READ LENGTH " + readlen + " <> " + rs.getInt(4)); 163 } 164 return 1; 165 } 166 167 private static int drainAndValidateStream(InputStream in, char value) 168 throws Throwable 169 { 170 byte[] buf = new byte[1024]; 171 int inputLength = 0; 172 while(true) 173 { 174 int size = 0; 175 try 176 { 177 size = in.read(buf); 178 } catch(Throwable t) 179 { 180 System.out.println("Got exception on byte "+inputLength+". Rethrowing..."); 181 throw t; 182 } 183 if (size == -1) 184 break; 185 186 for (int i = 0; i < size; i++) 187 { 188 if (buf[i] != (byte)value) 189 { 190 throw new Throwable ("TEST ERROR: byte "+(i+inputLength)+" not what is expected. It is '"+(char)buf[i]+"' rather than '"+value+"'"); 191 } 192 } 193 inputLength += size; 194 } 195 return inputLength; 197 } 198 } 199 | Popular Tags |