001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.test;
032:
033: import java.sql.Connection;
034: import java.sql.PreparedStatement;
035: import java.sql.ResultSet;
036: import java.sql.Statement;
037:
038: import junit.framework.TestCase;
039: import junit.framework.TestResult;
040:
041: /**
042: * Tests Bug 785429 concerning BINARY values as PreparedStatement parameters
043: *
044: * @author boucherb@users
045: */
046: public class TestBug785429 extends TestBase {
047:
048: Statement stmt;
049: Connection conn;
050:
051: public TestBug785429(String name) {
052: super (name);
053: }
054:
055: public void test() throws Exception {
056:
057: Connection conn = newConnection();
058:
059: conn.setAutoCommit(false);
060:
061: Statement stmt = conn.createStatement();
062: String sql;
063: String msg;
064: PreparedStatement ps;
065: ResultSet rs;
066: int rowcount = 0;
067:
068: stmt.executeUpdate("drop table testA if exists;");
069: stmt.executeUpdate("drop table testB if exists;");
070: stmt
071: .executeUpdate("create table testA(oid binary(2), data integer);");
072: stmt
073: .executeUpdate("create table testB(oid binary(2), data integer);");
074: stmt.executeUpdate("insert into testA values('0001',1);");
075: stmt.executeUpdate("insert into testB values('0001',1);");
076:
077: sql = "select * from testA as ttt,(select oid,data from testB) as tst "
078: + "where (tst.oid=ttt.oid) and (tst.oid='0001');";
079: rs = stmt.executeQuery(sql);
080: rowcount = 0;
081:
082: while (rs.next()) {
083: rowcount++;
084: }
085:
086: msg = sql + ": row count:";
087:
088: assertEquals(msg, 1, rowcount);
089: stmt.execute("drop table testA if exists");
090: stmt.execute("drop table testB if exists");
091: stmt.execute("create table testA(oid binary(2), data integer)");
092: stmt.execute("create table testB(oid binary(2), data integer)");
093:
094: byte[] oid = new byte[] { 0, 1 };
095:
096: ps = conn.prepareStatement("insert into testA values(?,1)");
097:
098: ps.setBytes(1, oid);
099: ps.execute();
100:
101: ps = conn.prepareStatement("insert into testB values (?,1)");
102:
103: ps.setBytes(1, oid);
104: ps.execute();
105:
106: sql = "select * from testA as ttt,(select oid,data from testB) as tst "
107: + "where (tst.oid=ttt.oid) and (tst.oid=?);";
108:
109: try {
110: ps = conn.prepareStatement(sql);
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114:
115: ps.setBytes(1, oid);
116:
117: rs = ps.executeQuery();
118: rowcount = 0;
119:
120: int colCount = rs.getMetaData().getColumnCount();
121:
122: while (rs.next()) {
123:
124: // for (int i= 1; i <= colCount; i++) {
125: // System.out.print(rs.getString(i) + ", ");
126: // }
127: //
128: // System.out.println();
129: rowcount++;
130: }
131:
132: msg = sql + ": row count:";
133:
134: assertEquals(msg, 1, rowcount);
135: }
136:
137: public static void main(String[] args) throws Exception {
138:
139: TestResult result;
140: TestCase test;
141: java.util.Enumeration exceptions;
142: java.util.Enumeration failures;
143: int count;
144:
145: result = new TestResult();
146: test = new TestBug785429("test");
147:
148: test.run(result);
149:
150: count = result.failureCount();
151:
152: System.out.println("TestBug785429 failure count: " + count);
153:
154: failures = result.failures();
155:
156: while (failures.hasMoreElements()) {
157: System.out.println(failures.nextElement());
158: }
159: }
160: }
|