KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestBug785429


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.Statement JavaDoc;
38
39 import junit.framework.TestCase;
40 import junit.framework.TestResult;
41
42 /**
43  * Tests Bug 785429 concerning BINARY values as PreparedStatement parameters
44  *
45  * @author boucherb@users
46  */

47 public class TestBug785429 extends TestBase {
48
49     Statement JavaDoc stmt;
50     Connection JavaDoc conn;
51
52     public TestBug785429(String JavaDoc name) {
53         super(name);
54     }
55
56     public void test() throws Exception JavaDoc {
57
58         Connection JavaDoc conn = newConnection();
59
60         conn.setAutoCommit(false);
61
62         Statement JavaDoc stmt = conn.createStatement();
63         String JavaDoc sql;
64         String JavaDoc msg;
65         PreparedStatement JavaDoc ps;
66         ResultSet JavaDoc rs;
67         int rowcount = 0;
68
69         stmt.executeUpdate("drop table testA if exists;");
70         stmt.executeUpdate("drop table testB if exists;");
71         stmt.executeUpdate(
72             "create table testA(oid binary(2), data integer);");
73         stmt.executeUpdate(
74             "create table testB(oid binary(2), data integer);");
75         stmt.executeUpdate("insert into testA values('0001',1);");
76         stmt.executeUpdate("insert into testB values('0001',1);");
77
78         sql = "select * from testA as ttt,(select oid,data from testB) as tst "
79               + "where (tst.oid=ttt.oid) and (tst.oid='0001');";
80         rs = stmt.executeQuery(sql);
81         rowcount = 0;
82
83         while (rs.next()) {
84             rowcount++;
85         }
86
87         msg = sql + ": row count:";
88
89         assertEquals(msg, 1, rowcount);
90         stmt.execute("drop table testA if exists");
91         stmt.execute("drop table testB if exists");
92         stmt.execute("create table testA(oid binary(2), data integer)");
93         stmt.execute("create table testB(oid binary(2), data integer)");
94
95         byte[] oid = new byte[] {
96             0, 1
97         };
98
99         ps = conn.prepareStatement("insert into testA values(?,1)");
100
101         ps.setBytes(1, oid);
102         ps.execute();
103
104         ps = conn.prepareStatement("insert into testB values (?,1)");
105
106         ps.setBytes(1, oid);
107         ps.execute();
108
109         sql = "select * from testA as ttt,(select oid,data from testB) as tst "
110               + "where (tst.oid=ttt.oid) and (tst.oid=?);";
111
112         try {
113             ps = conn.prepareStatement(sql);
114         } catch (Exception JavaDoc e) {
115             e.printStackTrace();
116         }
117
118         ps.setBytes(1, oid);
119
120         rs = ps.executeQuery();
121         rowcount = 0;
122
123         int colCount = rs.getMetaData().getColumnCount();
124
125         while (rs.next()) {
126
127 // for (int i= 1; i <= colCount; i++) {
128
// System.out.print(rs.getString(i) + ", ");
129
// }
130
//
131
// System.out.println();
132
rowcount++;
133         }
134
135         msg = sql + ": row count:";
136
137         assertEquals(msg, 1, rowcount);
138     }
139
140     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
141
142         TestResult result;
143         TestCase test;
144         java.util.Enumeration JavaDoc exceptions;
145         java.util.Enumeration JavaDoc failures;
146         int count;
147
148         result = new TestResult();
149         test = new TestBug785429("test");
150
151         test.run(result);
152
153         count = result.failureCount();
154
155         System.out.println("TestBug785429 failure count: " + count);
156
157         failures = result.failures();
158
159         while (failures.hasMoreElements()) {
160             System.out.println(failures.nextElement());
161         }
162     }
163 }
164
Popular Tags