KickJava   Java API By Example, From Geeks To Geeks.

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


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.DriverManager JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
44
45 /**
46  * Test handling of quote characters in strings
47  *
48  * @author <a HREF="mailto:david@walend.net">David Walend</a>
49  * @author <a HREF="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
50  */

51 public class TestQuotes extends TestCase {
52
53     private static final String JavaDoc CREATETABLE =
54         "create table quotetest (test varchar)";
55     private static final String JavaDoc DELETE = "delete from quotetest";
56     private static final String JavaDoc TESTSTRING =
57         "insert into quotetest (test) values (?)";
58     private static final String JavaDoc NOQUOTES = "the house of the dog of kevin";
59     private static final String JavaDoc QUOTES = "kevin's dog's house";
60     private static final String JavaDoc RESULT = "select * from quotetest";
61
62     public TestQuotes(String JavaDoc testName) {
63         super(testName);
64     }
65
66     /**
67      * Run all related test methods
68      */

69     public static Test suite() {
70         return new TestSuite(org.hsqldb.test.TestQuotes.class);
71     }
72
73     public void testSetString() {
74
75         Connection JavaDoc connection = null;
76         Statement JavaDoc statement = null;
77         PreparedStatement JavaDoc pStatement = null;
78         ResultSet JavaDoc rs1 = null;
79         ResultSet JavaDoc rs2 = null;
80
81         try {
82             DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
83
84             connection = DriverManager.getConnection("jdbc:hsqldb:.", "sa",
85                     "");
86             statement = connection.createStatement();
87
88             statement.executeUpdate(CREATETABLE);
89
90             pStatement = connection.prepareStatement(TESTSTRING);
91
92             pStatement.setString(1, NOQUOTES);
93             pStatement.executeUpdate();
94
95             rs1 = statement.executeQuery(RESULT);
96
97             rs1.next();
98
99             String JavaDoc result1 = rs1.getString(1);
100
101             assertTrue("result1 is -" + result1 + "- not -" + NOQUOTES + "-",
102                        NOQUOTES.equals(result1));
103             statement.executeUpdate(DELETE);
104             pStatement.setString(1, QUOTES);
105             pStatement.executeUpdate();
106
107             rs2 = statement.executeQuery(RESULT);
108
109             rs2.next();
110
111             String JavaDoc result2 = rs2.getString(1);
112
113             assertTrue("result2 is " + result2, QUOTES.equals(result2));
114         } catch (SQLException JavaDoc sqle) {
115             fail(sqle.getMessage());
116         } finally {
117             if (rs2 != null) {
118                 try {
119                     rs2.close();
120                 } catch (SQLException JavaDoc sqle) {
121                     sqle.printStackTrace();
122                 }
123             }
124
125             if (rs1 != null) {
126                 try {
127                     rs1.close();
128                 } catch (SQLException JavaDoc sqle) {
129                     sqle.printStackTrace();
130                 }
131             }
132
133             if (statement != null) {
134                 try {
135                     statement.close();
136                 } catch (SQLException JavaDoc sqle) {
137                     sqle.printStackTrace();
138                 }
139             }
140
141             if (pStatement != null) {
142                 try {
143                     pStatement.close();
144                 } catch (SQLException JavaDoc sqle) {
145                     sqle.printStackTrace();
146                 }
147             }
148
149             if (connection != null) {
150                 try {
151                     connection.close();
152                 } catch (SQLException JavaDoc sqle) {
153                     sqle.printStackTrace();
154                 }
155             }
156         }
157     }
158 }
159
Popular Tags