Mercurial > hg > monetdb-java
annotate tests/ConnectionTests.java @ 681:32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 03 Nov 2022 22:29:30 +0100 (2022-11-03) |
parents | b25d838b4bcf |
children | aeb268156580 |
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(); |
681
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
80 |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
81 tester.cleanup(); |
674
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
82 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
83 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
84 Connection connect() throws SQLException { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
85 TimeZone restore = null; |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
86 try { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
87 if (this.timeZone != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
88 restore = TimeZone.getDefault(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
89 TimeZone.setDefault(this.timeZone); |
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 if (connProps != null) { |
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, connProps); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
93 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
94 return DriverManager.getConnection(url); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
95 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
96 } finally { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
97 if (restore != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
98 TimeZone.setDefault(restore); |
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 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
102 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
103 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
|
104 // 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
|
105 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
|
106 // 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
|
107 boolean autocommitEnabled = conn.getAutoCommit(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
108 if (autocommitEnabled != expectAutocommit) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
109 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
|
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 // 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
|
113 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
|
114 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
|
115 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
|
116 if (!expectAutocommit) |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
117 conn.commit(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
118 // 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
|
119 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
|
120 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
121 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
122 // 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
|
123 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
|
124 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
|
125 rs.next(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
126 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
|
127 if (expectAutocommit) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
128 if (n != 2) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
129 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
|
130 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
131 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
132 if (n != 1) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
133 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
|
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 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
139 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
146 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
147 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
|
148 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
|
149 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
|
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 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
|
153 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
|
154 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
|
155 rs.next(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
156 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
|
157 if (!s.endsWith(suffix)) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
158 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
|
159 throw new Failure(msg); |
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 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
163 |
681
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
164 private void cleanup() throws SQLException { |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
165 try (Connection conn = connect(); Statement stmt = conn.createStatement()) { |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
166 stmt.execute("DROP TABLE IF EXISTS connectiontests"); |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
167 stmt.execute("DROP TABLE IF EXISTS connectiontests_ts"); |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
168 } |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
169 } |
32e7ac7b979e
Tests should always cleanup their created objects, so added cleanup() method.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
678
diff
changeset
|
170 |
674
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
171 public class Failure extends Exception { |
678
b25d838b4bcf
Resolve javac compilation warning:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
677
diff
changeset
|
172 private static final long serialVersionUID = 1L; |
b25d838b4bcf
Resolve javac compilation warning:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
677
diff
changeset
|
173 |
674
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
174 public Failure(String msg) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
175 super(msg); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
176 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
177 |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
178 @Override |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
179 public String getMessage() { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
180 StringBuilder msg = new StringBuilder(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
181 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
|
182 msg.append(url); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
183 if (timeZone != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
184 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
|
185 msg.append(timeZone.getID()); |
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(timeZone.getDisplayName()); |
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 } else { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
190 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
|
191 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
192 if (connProps != null) { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
193 msg.append(", with "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
194 msg.append(connProps.size()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
195 msg.append(" properties"); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
196 connProps.forEach((k, v) -> { |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
197 msg.append(", "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
198 msg.append(k); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
199 msg.append("="); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
200 msg.append(v); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
201 }); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
202 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
203 msg.append(": "); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
204 msg.append(super.getMessage()); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
205 return msg.toString(); |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
206 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
207 } |
b885de91095d
Add tests for autocommit and timezone handshake options
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
208 } |