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.DriverManager;
035: import java.sql.PreparedStatement;
036: import java.sql.ResultSet;
037: import java.sql.SQLException;
038: import java.sql.Statement;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: /**
045: * Test handling of quote characters in strings
046: *
047: * @author <a href="mailto:david@walend.net">David Walend</a>
048: * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
049: */
050: public class TestQuotes extends TestCase {
051:
052: private static final String CREATETABLE = "create table quotetest (test varchar)";
053: private static final String DELETE = "delete from quotetest";
054: private static final String TESTSTRING = "insert into quotetest (test) values (?)";
055: private static final String NOQUOTES = "the house of the dog of kevin";
056: private static final String QUOTES = "kevin's dog's house";
057: private static final String RESULT = "select * from quotetest";
058:
059: public TestQuotes(String testName) {
060: super (testName);
061: }
062:
063: /**
064: * Run all related test methods
065: */
066: public static Test suite() {
067: return new TestSuite(org.hsqldb.test.TestQuotes.class);
068: }
069:
070: public void testSetString() {
071:
072: Connection connection = null;
073: Statement statement = null;
074: PreparedStatement pStatement = null;
075: ResultSet rs1 = null;
076: ResultSet rs2 = null;
077:
078: try {
079: DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
080:
081: connection = DriverManager.getConnection("jdbc:hsqldb:.",
082: "sa", "");
083: statement = connection.createStatement();
084:
085: statement.executeUpdate(CREATETABLE);
086:
087: pStatement = connection.prepareStatement(TESTSTRING);
088:
089: pStatement.setString(1, NOQUOTES);
090: pStatement.executeUpdate();
091:
092: rs1 = statement.executeQuery(RESULT);
093:
094: rs1.next();
095:
096: String result1 = rs1.getString(1);
097:
098: assertTrue("result1 is -" + result1 + "- not -" + NOQUOTES
099: + "-", NOQUOTES.equals(result1));
100: statement.executeUpdate(DELETE);
101: pStatement.setString(1, QUOTES);
102: pStatement.executeUpdate();
103:
104: rs2 = statement.executeQuery(RESULT);
105:
106: rs2.next();
107:
108: String result2 = rs2.getString(1);
109:
110: assertTrue("result2 is " + result2, QUOTES.equals(result2));
111: } catch (SQLException sqle) {
112: fail(sqle.getMessage());
113: } finally {
114: if (rs2 != null) {
115: try {
116: rs2.close();
117: } catch (SQLException sqle) {
118: sqle.printStackTrace();
119: }
120: }
121:
122: if (rs1 != null) {
123: try {
124: rs1.close();
125: } catch (SQLException sqle) {
126: sqle.printStackTrace();
127: }
128: }
129:
130: if (statement != null) {
131: try {
132: statement.close();
133: } catch (SQLException sqle) {
134: sqle.printStackTrace();
135: }
136: }
137:
138: if (pStatement != null) {
139: try {
140: pStatement.close();
141: } catch (SQLException sqle) {
142: sqle.printStackTrace();
143: }
144: }
145:
146: if (connection != null) {
147: try {
148: connection.close();
149: } catch (SQLException sqle) {
150: sqle.printStackTrace();
151: }
152: }
153: }
154: }
155: }
|