KickJava   Java API By Example, From Geeks To Geeks.

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


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.SQLException JavaDoc;
37
38 import junit.framework.TestCase;
39 import junit.framework.TestResult;
40
41 /**
42  * HSQLDB TestBug778213 Junit test case. <p>
43  *
44  * Test to ensure that DDL can be executed through the
45  * HSQLDB PreparedStatement interface implementation and
46  * that the behaviour of the prepared statement object is
47  * nominally correct under "prepared" DDL.
48  *
49  * @author boucherb@users
50  * @version 1.7.2
51  * @since 1.7.2
52  */

53 public class TestBug778213 extends TestBase {
54
55     public TestBug778213(String JavaDoc name) {
56         super(name);
57     }
58
59     /* Implements the TestBug778213_Part3 test */
60     public void test() throws Exception JavaDoc {
61
62         Connection JavaDoc conn = newConnection();
63         PreparedStatement JavaDoc pstmt;
64         int updateCount;
65
66         try {
67             pstmt = conn.prepareStatement("drop table test if exists");
68
69             pstmt.executeUpdate();
70
71             pstmt = conn.prepareStatement("create table test(id int)");
72             updateCount = pstmt.executeUpdate();
73
74             assertTrue("expected update count of zero", updateCount == 0);
75
76             pstmt = conn.prepareStatement("drop table test");
77             updateCount = pstmt.executeUpdate();
78
79             assertTrue("expected update count of zero", updateCount == 0);
80         } catch (Exception JavaDoc e) {
81             assertTrue("unable to prepare or execute DDL", false);
82         } finally {
83             conn.close();
84         }
85
86         conn = newConnection();
87
88         try {
89             pstmt = conn.prepareStatement("create table test(id int)");
90
91             assertTrue("got data expecting update count", !pstmt.execute());
92         } catch (Exception JavaDoc e) {
93             assertTrue("unable to prepare or execute DDL", false);
94         } finally {
95             conn.close();
96         }
97
98         conn = newConnection();
99
100         boolean exception = true;
101
102         try {
103             pstmt = conn.prepareStatement("drop table test");
104
105             pstmt.executeQuery();
106         } catch (SQLException JavaDoc e) {
107             exception = false;
108         } finally {
109             conn.close();
110         }
111
112         if (exception) {
113             assertTrue("no exception thrown for executeQuery(DDL)", false);
114         }
115
116         conn = newConnection();
117
118         try {
119             pstmt = conn.prepareStatement("call identity()");
120
121             pstmt.execute();
122         } catch (Exception JavaDoc e) {
123             assertTrue("unable to prepare or execute call", false);
124         } finally {
125             conn.close();
126         }
127
128         exception = false;
129         conn = newConnection();
130
131         try {
132             pstmt = conn.prepareStatement("create table test(id int)");
133
134             pstmt.addBatch();
135         } catch (SQLException JavaDoc e) {
136             exception = true;
137         } finally {
138             conn.close();
139         }
140
141         if (exception) {
142             assertTrue("not expected exception batching prepared DDL", false);
143         }
144
145         conn = newConnection();
146
147         try {
148             pstmt = conn.prepareStatement("create table test(id int)");
149
150             assertTrue("expected null ResultSetMetadata for prepared DDL",
151                        null == pstmt.getMetaData());
152         } finally {
153             conn.close();
154         }
155
156         conn = newConnection();
157
158 //#ifdef JDBC3
159
/*
160
161         try {
162             pstmt = conn.prepareStatement("create table test(id int)");
163
164             assertTrue("expected zero parameter for prepared DDL",
165                        0 == pstmt.getParameterMetaData().getParameterCount());
166
167         } finally {
168             conn.close();
169         }
170
171 */

172
173 //#endif JDBC3
174
}
175
176     /* Runs TestBug778213_Part3 test from the command line*/
177     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
178
179         TestResult result;
180         TestCase test;
181         java.util.Enumeration JavaDoc failures;
182         int count;
183
184         result = new TestResult();
185         test = new TestBug778213("test");
186
187         test.run(result);
188
189         count = result.failureCount();
190
191         System.out.println("TestBug778213 failure count: " + count);
192
193         failures = result.failures();
194
195         while (failures.hasMoreElements()) {
196             System.out.println(failures.nextElement());
197         }
198     }
199 }
200
Popular Tags