View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test case of {@link Utc}.
51   * @since 0.1
52   */
53  final class UtcTest {
54  
55      /**
56       * Randomizer.
57       */
58      private static final Random RND = new SecureRandom();
59  
60      /**
61       * Format to use in tests.
62       */
63      private transient DateFormat fmt;
64  
65      /**
66       * Data source.
67       */
68      private transient DataSource source;
69  
70      /**
71       * Prepare this test case.
72       * @throws Exception If there is some problem inside
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       * Utc can save date to prepared statement.
89       * @throws Exception If there is some problem inside
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      * Utc can load date from result set.
122      * @throws Exception If there is some problem inside
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      * Utc can set and read message date, with different timezone.
157      * @throws Exception If there is some problem inside
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 }