annotate tests/ConnectionTests.java @ 677:e73c4a1f41c6

Add missing Copyright header info
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 03 Nov 2022 16:37:36 +0100 (2022-11-03)
parents b885de91095d
children b25d838b4bcf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
677
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
1 /*
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
2 * This Source Code Form is subject to the terms of the Mozilla Public
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
5 *
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
7 */
e73c4a1f41c6 Add missing Copyright header info
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 674
diff changeset
8
674
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
9 import java.sql.Connection;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
10 import java.sql.DriverManager;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
11 import java.sql.ResultSet;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
12 import java.sql.SQLException;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
13 import java.sql.Statement;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
14 import java.util.Properties;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
15 import java.util.SimpleTimeZone;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
16 import java.util.TimeZone;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
17
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
18 public class ConnectionTests {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
19
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
20 private final String url;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
21 private final Properties connProps;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
22 private final TimeZone timeZone;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
23
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
24 public ConnectionTests(String url, Properties props, TimeZone timeZone) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
25 this.url = url;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
26 Properties myProps = null;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
27 if (props != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
28 myProps = new Properties();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
29 myProps.putAll(props);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
30 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
31 this.connProps = myProps;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
32 this.timeZone = timeZone;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
33 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
34
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
35 public ConnectionTests(String url) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
36 this(url, null, null);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
37 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
38
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
39 public ConnectionTests withSuffix(String suffix) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
40 String newUrl = url;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
41
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
42 if (newUrl.contains("?")) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
43 newUrl += "&";
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
44 } else {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
45 newUrl += "?";
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
46 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
47 newUrl += suffix;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
48
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
49 return new ConnectionTests(newUrl, this.connProps, this.timeZone);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
50 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
51
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
52 public ConnectionTests withProp(String key, String value) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
53 ConnectionTests sub = new ConnectionTests(this.url, new Properties(), this.timeZone);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
54 if (this.connProps != null)
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
55 sub.connProps.putAll(this.connProps);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
56 sub.connProps.setProperty(key, value);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
57 return sub;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
58 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
59
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
60 public ConnectionTests withTimeZone(int offsetMinutes) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
61 TimeZone tz = new SimpleTimeZone(offsetMinutes * 60 * 1000, "Custom" + offsetMinutes);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
62 return new ConnectionTests(this.url, this.connProps, tz);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
63 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
64
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
65 public static void main(String[] args) throws SQLException, Failure {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
66 String url = args[0];
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
67 runTests(url);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
68 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
69
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
70 public static void runTests(String url) throws SQLException, Failure {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
71 ConnectionTests tester = new ConnectionTests(url);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
72
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
73 tester.checkAutoCommit(true);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
74 tester.withSuffix("autocommit=true").checkAutoCommit(true);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
75 tester.withSuffix("autocommit=false").checkAutoCommit(false);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
76 tester.withProp("autocommit", "true").checkAutoCommit(true);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
77 tester.withProp("autocommit", "false").checkAutoCommit(false);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
78
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
79 tester.testTimeZone();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
80 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
81
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
82 Connection connect() throws SQLException {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
83 TimeZone restore = null;
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
84 try {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
85 if (this.timeZone != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
86 restore = TimeZone.getDefault();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
87 TimeZone.setDefault(this.timeZone);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
88 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
89 if (connProps != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
90 return DriverManager.getConnection(url, connProps);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
91 } else {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
92 return DriverManager.getConnection(url);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
93 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
94 } finally {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
95 if (restore != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
96 TimeZone.setDefault(restore);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
97 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
98 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
99 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
100
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
101 private void checkAutoCommit(boolean expectAutocommit) throws SQLException, Failure {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
102 // Create and fill the table, leave one row uncommitted.
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
103 try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
104 // Does the connection itself believe to be in the correct mode?
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
105 boolean autocommitEnabled = conn.getAutoCommit();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
106 if (autocommitEnabled != expectAutocommit) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
107 throw new Failure("Expected autocommit to start as " + expectAutocommit + ", got " + autocommitEnabled);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
108 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
109
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
110 // Let's test how it works out in practice
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
111 stmt.execute("DROP TABLE IF EXISTS connectiontests");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
112 stmt.execute("CREATE TABLE connectiontests(i INT)");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
113 stmt.execute("INSERT INTO connectiontests VALUES (42)");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
114 if (!expectAutocommit)
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
115 conn.commit();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
116 // This will only be committed in autocommit mode
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
117 stmt.execute("INSERT INTO connectiontests VALUES (99)");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
118 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
119
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
120 // Check whether the uncommitted row is there or not
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
121 try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
122 try (ResultSet rs = stmt.executeQuery("SELECT COUNT(i) FROM connectiontests")) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
123 rs.next();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
124 int n = rs.getInt(1);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
125 if (expectAutocommit) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
126 if (n != 2) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
127 throw new Failure("Expected 2 rows because autocommit should be on, got " + n);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
128 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
129 } else {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
130 if (n != 1) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
131 throw new Failure("Expected 1 row because autocommit should be off, got " + n);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
132 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
133 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
134 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
135 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
136 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
137
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
138 private void testTimeZone() throws SQLException, Failure {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
139 try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
140 stmt.execute("DROP TABLE IF EXISTS connectiontests_ts");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
141 stmt.execute("CREATE TABLE connectiontests_ts(ts TIMESTAMP WITH TIME ZONE)");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
142 stmt.execute("INSERT INTO connectiontests_ts VALUES (str_to_timestamp(100, '%s'))");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
143 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
144
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
145 this.withTimeZone(0).verifyTimeZoneSuffix("+00:00");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
146 this.withTimeZone(240).verifyTimeZoneSuffix("+04:00");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
147 this.withTimeZone(270).verifyTimeZoneSuffix("+04:30");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
148 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
149
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
150 private void verifyTimeZoneSuffix(String suffix) throws SQLException, Failure {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
151 try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
152 ResultSet rs = stmt.executeQuery("SELECT * FROM connectiontests_ts");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
153 rs.next();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
154 String s = rs.getString(1);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
155 if (!s.endsWith(suffix)) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
156 String msg = String.format("Expected suffix '%s', got timestamp '%s'", suffix, s);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
157 throw new Failure(msg);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
158 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
159 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
160 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
161
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
162 public class Failure extends Exception {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
163 public Failure(String msg) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
164 super(msg);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
165 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
166
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
167 @Override
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
168 public String getMessage() {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
169 StringBuilder msg = new StringBuilder();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
170 msg.append("When connected to ");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
171 msg.append(url);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
172 if (timeZone != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
173 msg.append(", in time zone ");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
174 msg.append(timeZone.getID());
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
175 msg.append(" (");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
176 msg.append(timeZone.getDisplayName());
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
177 msg.append(")");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
178 } else {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
179 msg.append(", in the default time zone");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
180 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
181 if (connProps != null) {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
182 msg.append(", with ");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
183 msg.append(connProps.size());
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
184 msg.append(" properties");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
185 connProps.forEach((k, v) -> {
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
186 msg.append(", ");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
187 msg.append(k);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
188 msg.append("=");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
189 msg.append(v);
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
190 });
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
191 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
192 msg.append(": ");
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
193 msg.append(super.getMessage());
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
194 return msg.toString();
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
195 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
196 }
b885de91095d Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
197 }