001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.StreamUtil
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.util;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import org.apache.derby.iapi.error.StandardException;
026: import org.apache.derby.iapi.db.*;
027: import java.sql.*;
028: import java.io.ByteArrayInputStream;
029: import java.io.InputStream;
030: import java.math.BigDecimal;
031: import java.math.BigInteger;
032:
033: /**
034: * Methods for stream columns
035: */
036: public class StreamUtil {
037:
038: public static void insertAsciiColumn(Connection conn,
039: String stmtText, int colNumber, String value, int length)
040: throws Throwable {
041: PreparedStatement ps = conn.prepareStatement(stmtText);
042: setAsciiColumn(ps, colNumber, value.charAt(0), length);
043: ps.setInt(colNumber + 1, length);
044: ps.execute();
045: }
046:
047: public static void insertBinaryColumn(Connection conn,
048: String stmtText, int colNumber, String value, int length)
049: throws Throwable {
050: PreparedStatement ps = conn.prepareStatement(stmtText);
051: setBinaryColumn(ps, colNumber, value.charAt(0), length);
052: ps.setInt(colNumber + 1, length);
053: ps.execute();
054: }
055:
056: /**
057: * Set a particular column to whatever you
058: * wish.
059: */
060: private static void setAsciiColumn(PreparedStatement ps,
061: int colNumber, char value, int length) throws SQLException {
062: byte[] barray = new byte[length];
063: for (int i = 0; i < length; i++) {
064: barray[i] = (byte) value;
065: }
066: ps.setAsciiStream(colNumber, new ByteArrayInputStream(barray),
067: length);
068: }
069:
070: /**
071: * Set a particular column to whatever you
072: * wish.
073: */
074: private static void setBinaryColumn(PreparedStatement ps,
075: int colNumber, char value, int length) throws SQLException {
076: byte[] barray = new byte[length];
077: for (int i = 0; i < length; i++) {
078: barray[i] = (byte) value;
079: }
080: ps.setBinaryStream(colNumber, new ByteArrayInputStream(barray),
081: length);
082: }
083:
084: public static int getAsciiColumn(int whichRS, // 0 means old, 1 means new
085: int colNumber, String value) throws Throwable {
086: System.out.println("\ngetAsciiColumn() called");
087: ResultSet rs = getRowSet(whichRS);
088: if (rs == null)
089: return 0;
090: while (rs.next()) {
091: InputStream in = rs.getAsciiStream(colNumber);
092: int readlen = drainAndValidateStream(in, value.charAt(0));
093: if (readlen != rs.getInt(4))
094: throw new Exception("INCORRECT READ LENGTH " + readlen
095: + " <> " + rs.getInt(4));
096: }
097: return 1;
098: }
099:
100: private static ResultSet getRowSet(int whichRS) throws Throwable {
101: TriggerExecutionContext tec = org.apache.derby.iapi.db.Factory
102: .getTriggerExecutionContext();
103: if (tec == null) {
104: System.out.println("Not in a trigger.");
105: return null;
106: }
107:
108: return (whichRS == 0) ? tec.getOldRowSet() : tec.getNewRowSet();
109: }
110:
111: public static int getBinaryColumn(int whichRS, // 0 means old, 1 means new
112: int colNumber, String value) throws Throwable {
113: System.out.println("\ngetBinaryColumn() called");
114: ResultSet rs = getRowSet(whichRS);
115: if (rs == null)
116: return 0;
117: while (rs.next()) {
118: InputStream in = rs.getBinaryStream(colNumber);
119: int readlen = drainAndValidateStream(in, value.charAt(0));
120:
121: if (readlen != rs.getInt(4))
122: throw new Exception("INCORRECT READ LENGTH " + readlen
123: + " <> " + rs.getInt(4));
124: }
125: return 1;
126: }
127:
128: private static int drainAndValidateStream(InputStream in, char value)
129: throws Throwable {
130: byte[] buf = new byte[1024];
131: int inputLength = 0;
132: while (true) {
133: int size = 0;
134: try {
135: size = in.read(buf);
136: } catch (Throwable t) {
137: System.out.println("Got exception on byte "
138: + inputLength + ". Rethrowing...");
139: throw t;
140: }
141: if (size == -1)
142: break;
143:
144: for (int i = 0; i < size; i++) {
145: if (buf[i] != (byte) value) {
146: throw new Throwable("TEST ERROR: byte "
147: + (i + inputLength)
148: + " not what is expected. It is '"
149: + (char) buf[i] + "' rather than '" + value
150: + "'");
151: }
152: }
153: inputLength += size;
154: }
155: // System.out.println("...read "+inputLength+" bytes");
156: return inputLength;
157: }
158: }
|