1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.jdbc;
31
32 import java.security.SecureRandom;
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.ResultSet;
36 import java.text.DateFormat;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.Locale;
41 import java.util.Random;
42 import java.util.TimeZone;
43 import javax.sql.DataSource;
44 import org.hamcrest.MatcherAssert;
45 import org.hamcrest.Matchers;
46 import org.junit.jupiter.api.BeforeEach;
47 import org.junit.jupiter.api.Test;
48
49
50
51
52
53 final class UtcTest {
54
55
56
57
58 private static final Random RND = new SecureRandom();
59
60
61
62
63 private transient DateFormat fmt;
64
65
66
67
68 private transient DataSource source;
69
70
71
72
73
74 @BeforeEach
75 void prepare() throws Exception {
76 this.source = new H2Source(
77 String.format("xpo%d", UtcTest.RND.nextInt())
78 );
79 new JdbcSession(this.source)
80 .sql("CREATE TABLE foo (date DATETIME)")
81 .execute();
82 this.fmt = new SimpleDateFormat(
83 "yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH
84 );
85 }
86
87
88
89
90
91 @Test
92 void savesDateWithUtcTimezone() throws Exception {
93 this.fmt.setCalendar(
94 new GregorianCalendar(TimeZone.getTimeZone("GMT-5"))
95 );
96 final Date date = this.fmt.parse("2008-05-24 05:06:07.000");
97 final String saved;
98 try (Connection conn = this.source.getConnection()) {
99 final PreparedStatement ustmt = conn.prepareStatement(
100 "INSERT INTO foo (date) VALUES (?)"
101 );
102 new Utc(date).setTimestamp(ustmt, 1);
103 ustmt.executeUpdate();
104 final PreparedStatement rstmt = conn.prepareStatement(
105 "SELECT date FROM foo"
106 );
107 try (ResultSet rset = rstmt.executeQuery()) {
108 if (!rset.next()) {
109 throw new IllegalArgumentException();
110 }
111 saved = rset.getString(1);
112 }
113 }
114 MatcherAssert.assertThat(
115 saved,
116 Matchers.startsWith("2008-05-24 10:06:07")
117 );
118 }
119
120
121
122
123
124 @Test
125 void loadsDateWithUtcTimezone() throws Exception {
126 final Connection conn = this.source.getConnection();
127 final Date loaded;
128 try {
129 final PreparedStatement ustmt = conn.prepareStatement(
130 "INSERT INTO foo (date) VALUES (?) "
131 );
132 ustmt.setString(1, "2005-02-02 10:07:08.000");
133 ustmt.executeUpdate();
134 final PreparedStatement rstmt = conn.prepareStatement(
135 "SELECT date FROM foo "
136 );
137 try (ResultSet rset = rstmt.executeQuery()) {
138 if (!rset.next()) {
139 throw new IllegalArgumentException();
140 }
141 loaded = Utc.getTimestamp(rset, 1);
142 }
143 } finally {
144 conn.close();
145 }
146 this.fmt.setCalendar(
147 new GregorianCalendar(TimeZone.getTimeZone("GMT-3"))
148 );
149 MatcherAssert.assertThat(
150 this.fmt.format(loaded),
151 Matchers.startsWith("2005-02-02 07:07:08")
152 );
153 }
154
155
156
157
158
159 @Test
160 void setsAndReadsDateWithDifferentTimezone() throws Exception {
161 final Date date = new Date();
162 new JdbcSession(this.source)
163 .sql("INSERT INTO foo VALUES (?) ")
164 .set(new Utc(date))
165 .insert(Outcome.VOID);
166 final Connection conn = this.source.getConnection();
167 final String saved;
168 try {
169 final PreparedStatement stmt = conn.prepareStatement(
170 "SELECT date FROM foo "
171 );
172 try (ResultSet rset = stmt.executeQuery()) {
173 if (!rset.next()) {
174 throw new IllegalStateException();
175 }
176 saved = rset.getString(1);
177 }
178 } finally {
179 conn.close();
180 }
181 this.fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
182 final Date absolute = this.fmt.parse(saved);
183 MatcherAssert.assertThat(
184 absolute.toString(),
185 Matchers.equalTo(date.toString())
186 );
187 }
188
189 }