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.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
42
43
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
128
129
130
131 private DataSource datasource() {
132 return new H2Source("ytt68");
133 }
134 }