~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/testsuite/simple/EscapeProcessingTest.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2002-2004 MySQL AB
 
3
 
 
4
 This program is free software; you can redistribute it and/or modify
 
5
 it under the terms of version 2 of the GNU General Public License as 
 
6
 published by the Free Software Foundation.
 
7
 
 
8
 There are special exceptions to the terms and conditions of the GPL 
 
9
 as it is applied to this software. View the full text of the 
 
10
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 
11
 software distribution.
 
12
 
 
13
 This program is distributed in the hope that it will be useful,
 
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 GNU General Public License for more details.
 
17
 
 
18
 You should have received a copy of the GNU General Public License
 
19
 along with this program; if not, write to the Free Software
 
20
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 
 
22
 
 
23
 
 
24
 */
 
25
package testsuite.simple;
 
26
 
 
27
import java.sql.Connection;
 
28
import java.util.Properties;
 
29
import java.util.TimeZone;
 
30
 
 
31
import testsuite.BaseTestCase;
 
32
 
 
33
/**
 
34
 * Tests escape processing
 
35
 * 
 
36
 * @author Mark Matthews
 
37
 */
 
38
public class EscapeProcessingTest extends BaseTestCase {
 
39
        // ~ Constructors
 
40
        // -----------------------------------------------------------
 
41
 
 
42
        /**
 
43
         * Constructor for EscapeProcessingTest.
 
44
         * 
 
45
         * @param name
 
46
         *            the test to run
 
47
         */
 
48
        public EscapeProcessingTest(String name) {
 
49
                super(name);
 
50
        }
 
51
 
 
52
        // ~ Methods
 
53
        // ----------------------------------------------------------------
 
54
 
 
55
        /**
 
56
         * Tests the escape processing functionality
 
57
         * 
 
58
         * @throws Exception
 
59
         *             if an error occurs
 
60
         */
 
61
        public void testEscapeProcessing() throws Exception {
 
62
                String results = "select dayname (abs(now())),   -- Today    \n"
 
63
                                + "           '1997-05-24',  -- a date                    \n"
 
64
                                + "           '10:30:29',  -- a time                     \n"
 
65
                                + "           '1997-05-24 10:30:29', -- a timestamp  \n"
 
66
                                + "          '{string data with { or } will not be altered'   \n"
 
67
                                + "--  Also note that you can safely include { and } in comments";
 
68
 
 
69
                String exSql = "select {fn dayname ({fn abs({fn now()})})},   -- Today    \n"
 
70
                                + "           {d '1997-05-24'},  -- a date                    \n"
 
71
                                + "           {t '10:30:29' },  -- a time                     \n"
 
72
                                + "           {ts '1997-05-24 10:30:29.123'}, -- a timestamp  \n"
 
73
                                + "          '{string data with { or } will not be altered'   \n"
 
74
                                + "--  Also note that you can safely include { and } in comments";
 
75
 
 
76
                String escapedSql = this.conn.nativeSQL(exSql);
 
77
 
 
78
                assertTrue(results.equals(escapedSql));
 
79
 
 
80
        }
 
81
 
 
82
        /**
 
83
         * Runs all test cases in this test suite
 
84
         * 
 
85
         * @param args
 
86
         */
 
87
        public static void main(String[] args) {
 
88
                junit.textui.TestRunner.run(EscapeProcessingTest.class);
 
89
        }
 
90
 
 
91
        /**
 
92
         * JDBC-4.0 spec will allow either SQL_ or not for type in {fn convert ...}
 
93
         * 
 
94
         * @throws Exception
 
95
         *             if the test fails
 
96
         */
 
97
        public void testConvertEscape() throws Exception {
 
98
                assertEquals(conn.nativeSQL("{fn convert(abcd, SQL_INTEGER)}"), conn
 
99
                                .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.getAvailableIDs(currentTimezone.getRawOffset() + (3600 * 1000 * 2));
 
111
                String newTimezone = null;
 
112
                
 
113
                if (availableIds.length > 0) {
 
114
                        newTimezone = availableIds[0];
 
115
                } else {
 
116
                        newTimezone = "UTC"; // punt
 
117
                }
 
118
                
 
119
                Properties props = new Properties();
 
120
                
 
121
                props.setProperty("useTimezone", "true");
 
122
                props.setProperty("serverTimezone", newTimezone);
 
123
                Connection tzConn = null;
 
124
                
 
125
                try {
 
126
                        String escapeToken = "SELECT {ts '2002-11-12 10:00:00'} {t '05:11:02'}";
 
127
                        tzConn = getConnectionWithProps(props);
 
128
                        assertTrue(!tzConn.nativeSQL(escapeToken).equals(this.conn.nativeSQL(escapeToken)));
 
129
                } finally {
 
130
                        if (tzConn != null) {
 
131
                                tzConn.close();
 
132
                        }
 
133
                }
 
134
                
 
135
        }
 
136
}