001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple;
026:
027: import java.sql.Connection;
028: import java.util.Properties;
029: import java.util.TimeZone;
030:
031: import testsuite.BaseTestCase;
032:
033: /**
034: * Tests escape processing
035: *
036: * @author Mark Matthews
037: */
038: public class EscapeProcessingTest extends BaseTestCase {
039: // ~ Constructors
040: // -----------------------------------------------------------
041:
042: /**
043: * Constructor for EscapeProcessingTest.
044: *
045: * @param name
046: * the test to run
047: */
048: public EscapeProcessingTest(String name) {
049: super (name);
050: }
051:
052: // ~ Methods
053: // ----------------------------------------------------------------
054:
055: /**
056: * Tests the escape processing functionality
057: *
058: * @throws Exception
059: * if an error occurs
060: */
061: public void testEscapeProcessing() throws Exception {
062: String results = "select dayname (abs(now())), -- Today \n"
063: + " '1997-05-24', -- a date \n"
064: + " '10:30:29', -- a time \n"
065: + " '1997-05-24 10:30:29', -- a timestamp \n"
066: + " '{string data with { or } will not be altered' \n"
067: + "-- Also note that you can safely include { and } in comments";
068:
069: String exSql = "select {fn dayname ({fn abs({fn now()})})}, -- Today \n"
070: + " {d '1997-05-24'}, -- a date \n"
071: + " {t '10:30:29' }, -- a time \n"
072: + " {ts '1997-05-24 10:30:29.123'}, -- a timestamp \n"
073: + " '{string data with { or } will not be altered' \n"
074: + "-- Also note that you can safely include { and } in comments";
075:
076: String escapedSql = this .conn.nativeSQL(exSql);
077:
078: assertTrue(results.equals(escapedSql));
079:
080: }
081:
082: /**
083: * Runs all test cases in this test suite
084: *
085: * @param args
086: */
087: public static void main(String[] args) {
088: junit.textui.TestRunner.run(EscapeProcessingTest.class);
089: }
090:
091: /**
092: * JDBC-4.0 spec will allow either SQL_ or not for type in {fn convert ...}
093: *
094: * @throws Exception
095: * if the test fails
096: */
097: public void testConvertEscape() throws Exception {
098: assertEquals(conn.nativeSQL("{fn convert(abcd, SQL_INTEGER)}"),
099: conn.nativeSQL("{fn convert(abcd, INTEGER)}"));
100: }
101:
102: /**
103: * Tests that the escape tokenizer converts timestamp values
104: * wrt. timezones when useTimezone=true.
105: *
106: * @throws Exception if the test fails.
107: */
108: public void testTimestampConversion() throws Exception {
109: TimeZone currentTimezone = TimeZone.getDefault();
110: String[] availableIds = TimeZone
111: .getAvailableIDs(currentTimezone.getRawOffset()
112: + (3600 * 1000 * 2));
113: String newTimezone = null;
114:
115: if (availableIds.length > 0) {
116: newTimezone = availableIds[0];
117: } else {
118: newTimezone = "UTC"; // punt
119: }
120:
121: Properties props = new Properties();
122:
123: props.setProperty("useTimezone", "true");
124: props.setProperty("serverTimezone", newTimezone);
125: Connection tzConn = null;
126:
127: try {
128: String escapeToken = "SELECT {ts '2002-11-12 10:00:00'} {t '05:11:02'}";
129: tzConn = getConnectionWithProps(props);
130: assertTrue(!tzConn.nativeSQL(escapeToken).equals(
131: this.conn.nativeSQL(escapeToken)));
132: } finally {
133: if (tzConn != null) {
134: tzConn.close();
135: }
136: }
137:
138: }
139: }
|