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.math.BigDecimal;
33  import java.util.Date;
34  import javax.sql.DataSource;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Test case for {@link SingleOutcome}.
42   *
43   * @since 0.1
44   */
45  final class SingleOutcomeTest {
46  
47      @Test
48      void retrievesByte() throws Exception {
49          MatcherAssert.assertThat(
50              new JdbcSession(this.datasource())
51                  .sql("CALL 65")
52                  .select(new SingleOutcome<>(Byte.class)),
53              Matchers.is((byte) 'A')
54          );
55      }
56  
57      @Test
58      void retrievesBigDecimal() throws Exception {
59          MatcherAssert.assertThat(
60              new JdbcSession(this.datasource())
61                  .sql("CALL POWER(10, 10)")
62                  .select(new SingleOutcome<>(BigDecimal.class)),
63              Matchers.is(new BigDecimal("1.0E+10"))
64          );
65      }
66  
67      @Test
68      void retrievesBytes() throws Exception {
69          final int size = 256;
70          MatcherAssert.assertThat(
71              new JdbcSession(this.datasource())
72                  .sql(String.format("CALL SECURE_RAND(%d)", size))
73                  .select(new SingleOutcome<>(byte[].class))
74                  .length,
75              Matchers.is(size)
76          );
77      }
78  
79      @Test
80      void retrievesUtc() throws Exception {
81          MatcherAssert.assertThat(
82              new JdbcSession(this.datasource())
83                  .sql("CALL CURRENT_TIMESTAMP()")
84                  .select(new SingleOutcome<>(Utc.class)),
85              Matchers.notNullValue()
86          );
87      }
88  
89      @Test
90      void retrievesDate() throws Exception {
91          MatcherAssert.assertThat(
92              new JdbcSession(this.datasource())
93                  .sql("CALL CURRENT_DATE()")
94                  .select(new SingleOutcome<>(Date.class)),
95              Matchers.notNullValue()
96          );
97      }
98  
99      @Test
100     void retrievesString() throws Exception {
101         final DataSource source = this.datasource();
102         new JdbcSession(source)
103             .autocommit(false)
104             .sql("CREATE TABLE foo (name VARCHAR(50))")
105             .execute()
106             .sql("INSERT INTO foo (name) VALUES (?)")
107             .set("Jeff Lebowski")
108             .execute()
109             .set("Walter Sobchak")
110             .execute()
111             .commit();
112         final String name = new JdbcSession(source)
113             .sql("SELECT name FROM foo")
114             .select(new SingleOutcome<String>(String.class));
115         MatcherAssert.assertThat(name, Matchers.startsWith("Jeff"));
116     }
117 
118     @Test
119     void failsFast() {
120         Assertions.assertThrows(
121             IllegalArgumentException.class,
122             () -> new SingleOutcome<>(Exception.class)
123         );
124     }
125 
126     /**
127      * Create datasource.
128      *
129      * @return Source.
130      */
131     private DataSource datasource() {
132         return new H2Source("ytt68");
133     }
134 }