Mercurial > hg > monetdb-java
annotate tests/ConnectionTests.java @ 674:b885de91095d
Add tests for autocommit and timezone handshake options
The other options are hard to test
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Mon, 31 Oct 2022 16:39:11 +0100 (2022-10-31) |
parents | |
children | e73c4a1f41c6 |
rev | line source |
---|---|
674
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
1 import java.sql.Connection; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
2 import java.sql.DriverManager; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
3 import java.sql.ResultSet; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
4 import java.sql.SQLException; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
5 import java.sql.Statement; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
6 import java.util.Properties; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
7 import java.util.SimpleTimeZone; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
8 import java.util.TimeZone; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
9 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
10 public class ConnectionTests { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
11 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
12 private final String url; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
13 private final Properties connProps; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
14 private final TimeZone timeZone; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
15 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
16 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
|
17 this.url = url; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
18 Properties myProps = null; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
19 if (props != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
20 myProps = new Properties(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
21 myProps.putAll(props); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
22 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
23 this.connProps = myProps; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
24 this.timeZone = timeZone; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
25 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
26 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
27 public ConnectionTests(String url) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
28 this(url, null, null); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
29 } |
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 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
|
32 String newUrl = url; |
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 if (newUrl.contains("?")) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
35 newUrl += "&"; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
36 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
37 newUrl += "?"; |
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 newUrl += suffix; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
40 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
41 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
|
42 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
43 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
44 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
|
45 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
|
46 if (this.connProps != null) |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
47 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
|
48 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
|
49 return sub; |
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 withTimeZone(int offsetMinutes) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
53 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
|
54 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
|
55 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
56 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
57 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
|
58 String url = args[0]; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
59 runTests(url); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
60 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
61 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
62 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
|
63 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
|
64 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
65 tester.checkAutoCommit(true); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 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
|
70 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
71 tester.testTimeZone(); |
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 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
74 Connection connect() throws SQLException { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
75 TimeZone restore = null; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
76 try { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
77 if (this.timeZone != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
78 restore = TimeZone.getDefault(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
79 TimeZone.setDefault(this.timeZone); |
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 if (connProps != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
82 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
|
83 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
84 return DriverManager.getConnection(url); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
85 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
86 } finally { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
87 if (restore != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
88 TimeZone.setDefault(restore); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
89 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
90 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
91 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
92 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
93 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
|
94 // 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
|
95 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
|
96 // 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
|
97 boolean autocommitEnabled = conn.getAutoCommit(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
98 if (autocommitEnabled != expectAutocommit) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
99 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
|
100 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
101 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
102 // 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
|
103 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
|
104 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
|
105 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
|
106 if (!expectAutocommit) |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
107 conn.commit(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
108 // 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
|
109 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
|
110 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
111 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
112 // 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
|
113 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
|
114 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
|
115 rs.next(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
116 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
|
117 if (expectAutocommit) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
118 if (n != 2) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
119 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
|
120 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
121 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
122 if (n != 1) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
123 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
|
124 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
125 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
126 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
127 } |
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 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
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 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
|
138 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
|
139 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
|
140 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
141 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 rs.next(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
146 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
|
147 if (!s.endsWith(suffix)) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
148 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
|
149 throw new Failure(msg); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
150 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
151 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
152 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
153 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
154 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
|
155 public Failure(String msg) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
156 super(msg); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
157 } |
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 @Override |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
160 public String getMessage() { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
161 StringBuilder msg = new StringBuilder(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
162 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
|
163 msg.append(url); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
164 if (timeZone != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
165 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
|
166 msg.append(timeZone.getID()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
167 msg.append(" ("); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
168 msg.append(timeZone.getDisplayName()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
169 msg.append(")"); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
170 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
171 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
|
172 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
173 if (connProps != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
174 msg.append(", with "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
175 msg.append(connProps.size()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
176 msg.append(" properties"); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
177 connProps.forEach((k, v) -> { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
178 msg.append(", "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
179 msg.append(k); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
180 msg.append("="); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
181 msg.append(v); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
182 }); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
183 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
184 msg.append(": "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
185 msg.append(super.getMessage()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
186 return msg.toString(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
187 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
188 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
189 } |