KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HSQLDB TestINPredicate Junit test case. <p>
44  *
45  * @author boucherb@users
46  * @version 1.7.2
47  * @since 1.7.2
48  */

49 public class TestINPredicateParameterizationAndCorrelation extends TestBase {
50
51     public TestINPredicateParameterizationAndCorrelation(String JavaDoc name) {
52         super(name);
53     }
54
55     /* Implements the TestINPredicate test */
56     public void test() throws Exception JavaDoc {
57
58         Connection JavaDoc conn = newConnection();
59         Statement JavaDoc stmt = conn.createStatement();
60         PreparedStatement JavaDoc pstmt;
61         ResultSet JavaDoc rs;
62         int actualCount;
63         int expectedCount;
64         String JavaDoc sql;
65
66         stmt.execute("drop table test if exists");
67
68         sql = "create table test(id int)";
69
70         stmt.execute(sql);
71
72         sql = "insert into test values(?)";
73         pstmt = conn.prepareStatement(sql);
74
75         for (int i = 0; i < 10; i++) {
76             pstmt.setInt(1, i);
77             pstmt.addBatch();
78         }
79
80         pstmt.executeBatch();
81
82         sql = "select count(*) from test where id in(?,?)";
83         pstmt = conn.prepareStatement(sql);
84
85         pstmt.setInt(1, 0);
86         pstmt.setInt(2, 9);
87
88         rs = pstmt.executeQuery();
89
90         rs.next();
91
92         expectedCount = 2;
93         actualCount = rs.getInt(1);
94         sql = "\"select count(*) from test where id in(0,9)\"";
95
96         assertEquals(sql, expectedCount, actualCount);
97
98         sql = "select count(*) from test a, test b where 0 in(a.id, b.id)";
99         rs = stmt.executeQuery(sql);
100
101         rs.next();
102
103         expectedCount = rs.getInt(1);
104         sql = "select count(*) from test a, test b where ? in (a.id, b.id)";
105         pstmt = conn.prepareStatement(sql);
106
107         pstmt.setInt(1, 0);
108
109         rs = pstmt.executeQuery();
110
111         rs.next();
112
113         actualCount = rs.getInt(1);
114         sql = "\"select count(*) from test a, test b where 0 in (a.id, b.id)\"";
115
116         assertEquals(sql, expectedCount, actualCount);
117
118         try {
119             sql = "select count(*) from test a, test b where ? in(?, b.id)";
120             pstmt = conn.prepareStatement(sql);
121
122             assertTrue("expected exception preparing \"" + sql + "\"", false);
123         } catch (Exception JavaDoc e) {
124
125             // this is the expected result
126
assertTrue(e.toString(), true);
127         }
128
129         try {
130             sql = "select count(*) from test a, test b where a.id in(?, ?)";
131             pstmt = conn.prepareStatement(sql);
132         } catch (Exception JavaDoc e) {
133             assertTrue("unexpected exception preparing \"" + sql + "\":" + e,
134                        false);
135         }
136
137         sql = "select count(*) from "
138               + "(select * from test where id in (1,2)) a,"
139               + "(select * from test where id in (3,4)) b "
140               + "where a.id < 2 and b.id < 4";
141         rs = stmt.executeQuery(sql);
142
143         rs.next();
144
145         expectedCount = rs.getInt(1);
146         sql = "select count(*) from "
147               + "(select * from test where id in (?,?)) a,"
148               + "(select * from test where id in (?,?)) b "
149               + "where a.id < ? and b.id < ?";
150         pstmt = conn.prepareStatement(sql);
151
152         pstmt.setInt(1, 1);
153         pstmt.setInt(2, 2);
154         pstmt.setInt(3, 3);
155         pstmt.setInt(4, 4);
156         pstmt.setInt(5, 2);
157         pstmt.setInt(6, 4);
158
159         rs = pstmt.executeQuery();
160
161         rs.next();
162
163         actualCount = rs.getInt(1);
164
165         assertEquals("row count: ", expectedCount, actualCount);
166     }
167
168     /* Runs TestINPredicate test from the command line*/
169     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
170
171         TestResult result;
172         TestCase test;
173         java.util.Enumeration JavaDoc failures;
174         int count;
175
176         result = new TestResult();
177         test = new TestINPredicateParameterizationAndCorrelation("test");
178
179         test.run(result);
180
181         count = result.failureCount();
182
183         System.out.println(
184             "TestINPredicateParameterizationAndCorrelation failure count: "
185             + count);
186
187         failures = result.failures();
188
189         while (failures.hasMoreElements()) {
190             System.out.println(failures.nextElement());
191         }
192     }
193 }
194
Popular Tags