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 com.jcabi.aspects.Parallel;
33 import java.sql.PreparedStatement;
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import javax.sql.DataSource;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.jupiter.api.Test;
41
42
43
44
45
46 final class JdbcSessionTest {
47
48
49
50
51
52 @Test
53 void sendsSqlManipulationsToJdbcDriver() throws Exception {
54 final DataSource source = new H2Source("tiu78");
55 new JdbcSession(source)
56 .autocommit(false)
57 .sql("CREATE TABLE foo (name VARCHAR(50))")
58 .execute()
59 .sql("INSERT INTO foo (name) VALUES (?)")
60 .set("Jeff Lebowski")
61 .execute()
62 .commit();
63 final String name = new JdbcSession(source)
64 .sql("SELECT name FROM foo WHERE name = 'Jeff Lebowski'")
65 .select(
66 new Outcome<String>() {
67 @Override
68 public String handle(final ResultSet rset,
69 final Statement stmt)
70 throws SQLException {
71 rset.next();
72 return rset.getString(1);
73 }
74 }
75 );
76 MatcherAssert.assertThat(name, Matchers.startsWith("Jeff"));
77 }
78
79
80
81
82
83
84 @Test
85 void executesSql() throws Exception {
86 final DataSource source = new H2Source("tpl98");
87 new JdbcSession(source)
88 .autocommit(false)
89 .sql("CREATE TABLE foo5 (name VARCHAR(30))")
90 .execute()
91 .sql("DROP TABLE foo5")
92 .execute()
93 .commit();
94 }
95
96
97
98
99
100 @Test
101 void automaticallyCommitsByDefault() throws Exception {
102 final DataSource source = new H2Source("tt8u");
103 new JdbcSession(source)
104 .sql("CREATE TABLE foo16 (name VARCHAR(50))")
105 .execute()
106 .sql("INSERT INTO foo16 (name) VALUES (?)")
107 .prepare(
108 new Preparation() {
109 @Override
110 public void prepare(final PreparedStatement stmt)
111 throws SQLException {
112 stmt.setString(1, "Walter");
113 }
114 }
115 )
116 .execute();
117 final String name = new JdbcSession(source)
118 .sql("SELECT name FROM foo16 WHERE name = 'Walter'")
119 .select(
120 new Outcome<String>() {
121 @Override
122 public String handle(final ResultSet rset,
123 final Statement stmt)
124 throws SQLException {
125 rset.next();
126 return rset.getString(1);
127 }
128 }
129 );
130 MatcherAssert.assertThat(name, Matchers.startsWith("Wa"));
131 }
132
133
134
135
136
137
138 @Test
139 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
140 void releasesConnectionsFromThePool() throws Exception {
141 final DataSource source = new H2Source("t445p");
142 new JdbcSession(source)
143 .sql("CREATE TABLE foo776 (name VARCHAR(30))")
144 .execute();
145 for (int idx = 0; idx < 10; ++idx) {
146 new JdbcSession(source)
147 .sql("INSERT INTO foo776 VALUES ('hello, world!')")
148 .execute();
149 }
150 }
151
152
153
154
155
156
157 @Test
158 void executesSqlInParallelThreads() throws Exception {
159 final DataSource source = new H2Source("til87");
160 new JdbcSession(source)
161 .sql("CREATE TABLE foo99 (name VARCHAR(30))")
162 .execute();
163 this.insert(source, "foo99");
164 }
165
166
167
168
169
170 @Test
171 void rollbacksTransaction() throws Exception {
172 final DataSource source = new H2Source("t228x");
173 new JdbcSession(source)
174 .sql("CREATE TABLE t228x (name VARCHAR(30))")
175 .execute()
176 .sql("INSERT INTO t228x VALUES ('foo')")
177 .execute();
178 new JdbcSession(source).autocommit(false)
179 .sql("INSERT INTO t228x VALUES ('bar')")
180 .execute()
181 .rollback();
182 MatcherAssert.assertThat(
183 new JdbcSession(source).sql("SELECT * FROM t228x")
184 .select(new ListOutcome<>(rset -> rset.getString("name"))),
185 Matchers.contains("foo")
186 );
187 }
188
189
190
191
192
193
194
195
196 @Parallel(threads = 50)
197 private void insert(final DataSource src, final String table)
198 throws Exception {
199 new JdbcSession(src)
200 .sql(String.format("INSERT INTO %s VALUES ('hey')", table))
201 .execute();
202 }
203
204 }