|
| 1 | +package com.baeldung.persistence.jdbi; |
| 2 | + |
| 3 | +import org.jdbi.v3.core.Handle; |
| 4 | +import org.jdbi.v3.core.Jdbi; |
| 5 | +import org.jdbi.v3.core.result.ResultBearing; |
| 6 | +import org.jdbi.v3.core.result.ResultProducer; |
| 7 | +import org.jdbi.v3.core.statement.Query; |
| 8 | +import org.jdbi.v3.core.statement.StatementContext; |
| 9 | +import org.jdbi.v3.core.statement.Update; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.sql.PreparedStatement; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.util.*; |
| 15 | +import java.util.function.Supplier; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +public class JdbiTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenJdbiCreated_thenSuccess() { |
| 24 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 25 | + |
| 26 | + Jdbi.create("WRONG"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void whenJdbiWithProperties_thenSuccess() { |
| 31 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 32 | + jdbi.open().close(); |
| 33 | + Properties properties = new Properties(); |
| 34 | + properties.setProperty("username", "sa"); |
| 35 | + properties.setProperty("password", ""); |
| 36 | + jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", properties); |
| 37 | + jdbi.open().close(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenHandle_thenBoh() { |
| 42 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 43 | + final Handle[] handleRef = new Handle[1]; |
| 44 | + boolean closed = jdbi.withHandle(handle -> { |
| 45 | + handleRef[0] = handle; |
| 46 | + return handle.isClosed(); |
| 47 | + }); |
| 48 | + |
| 49 | + assertFalse(closed); |
| 50 | + assertTrue(handleRef[0].isClosed()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenTableCreated_thenInsertIsPossible() { |
| 55 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 56 | + jdbi.useHandle(handle -> { |
| 57 | + int updateCount = handle.execute("create table PROJECT_1 (id integer identity, name varchar(50), url varchar(100))"); |
| 58 | + |
| 59 | + assertEquals(0, updateCount); |
| 60 | + |
| 61 | + updateCount = handle.execute("INSERT INTO PROJECT_1 VALUES (1, 'tutorials', 'github.com/eugenp/tutorials')"); |
| 62 | + |
| 63 | + assertEquals(1, updateCount); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void whenIdentityColumn_thenInsertReturnsNewId() { |
| 69 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 70 | + jdbi.useHandle(handle -> { |
| 71 | + handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 72 | + Update update = handle.createUpdate( |
| 73 | + "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 74 | + ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys(); |
| 75 | + |
| 76 | + assertEquals(0, generatedKeys.mapToMap().findOnly().get("id")); |
| 77 | + |
| 78 | + update = handle.createUpdate( |
| 79 | + "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 80 | + |
| 81 | + assertEquals(1, generatedKeys.mapToMap().findOnly().get("id")); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void whenSelectMapToMap_thenResultsAreMapEntries() { |
| 87 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 88 | + jdbi.useHandle(handle -> { |
| 89 | + handle.execute("create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 90 | + handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 91 | + handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 92 | + Query query = handle.createQuery("select * from PROJECT_3 order by id"); |
| 93 | + List<Map<String, Object>> list = query.mapToMap().list(); |
| 94 | + |
| 95 | + assertEquals("tutorials", list.get(0).get("name")); |
| 96 | + assertEquals("REST with Spring", list.get(1).get("name")); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void whenSelectMapToString_thenResultIsString() { |
| 102 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 103 | + jdbi.useHandle(handle -> { |
| 104 | + handle.execute("create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 105 | + handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 106 | + handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 107 | + Query query = handle.createQuery("select name, url from PROJECT_4 order by id"); |
| 108 | + List<String> list = query.mapTo(String.class).list(); |
| 109 | + |
| 110 | + assertEquals("tutorials", list.get(0)); |
| 111 | + assertEquals("REST with Spring", list.get(1)); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void whenSelectMapToString_thenStream() { |
| 117 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 118 | + jdbi.useHandle(handle -> { |
| 119 | + handle.execute("create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 120 | + handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 121 | + handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 122 | + Query query = handle.createQuery("select name, url from PROJECT_5 order by id"); |
| 123 | + query.mapTo(String.class).useStream((Stream<String> stream) -> assertEquals("tutorials", stream.findFirst().get())); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void whenNoResults_thenFindFirstReturnsNone() { |
| 129 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 130 | + jdbi.useHandle(handle -> { |
| 131 | + handle.execute("create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 132 | + |
| 133 | + assertFalse(handle.createQuery("select * from project_6").mapToMap().findFirst().isPresent()); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void whenMultipleResults_thenFindFirstReturnsFirst() { |
| 139 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 140 | + jdbi.useHandle(handle -> { |
| 141 | + handle.execute("create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 142 | + handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 143 | + handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 144 | + |
| 145 | + Query query = handle.createQuery("select * from project_7 order by id"); |
| 146 | + Optional<Map<String, Object>> first = query.mapToMap().findFirst(); |
| 147 | + |
| 148 | + assertTrue(first.isPresent()); |
| 149 | + assertEquals("tutorials", first.get().get("name")); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void whenNoResults_thenFindOnlyThrows() { |
| 155 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 156 | + jdbi.useHandle(handle -> { |
| 157 | + handle.execute("create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 158 | + |
| 159 | + try { |
| 160 | + handle.createQuery("select * from project_8").mapToMap().findOnly(); |
| 161 | + |
| 162 | + fail("Exception expected"); |
| 163 | + } catch (Exception e) { |
| 164 | + e.printStackTrace(); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void whenMultipleResults_thenFindOnlyThrows() { |
| 171 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 172 | + jdbi.useHandle(handle -> { |
| 173 | + handle.execute("create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 174 | + handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')"); |
| 175 | + handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')"); |
| 176 | + |
| 177 | + try { |
| 178 | + Query query = handle.createQuery("select * from project_9"); |
| 179 | + Map<String, Object> onlyResult = query.mapToMap().findOnly(); |
| 180 | + |
| 181 | + fail("Exception expected"); |
| 182 | + } catch (Exception e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void whenParameters_thenReplacement() { |
| 190 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 191 | + jdbi.useHandle(handle -> { |
| 192 | + handle.execute("create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 193 | + Update update1 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (?, ?)"); |
| 194 | + update1.bind(0, "tutorials"); |
| 195 | + update1.bind(1, "github.com/eugenp/tutorials"); |
| 196 | + int rows = update1.execute(); |
| 197 | + |
| 198 | + assertEquals(1, rows); |
| 199 | + |
| 200 | + Update update2 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (:name, :url)"); |
| 201 | + update2.bind("name", "REST with Spring"); |
| 202 | + update2.bind("url", "github.com/eugenp/REST-With-Spring"); |
| 203 | + rows = update2.execute(); |
| 204 | + |
| 205 | + assertEquals(1, rows); |
| 206 | + |
| 207 | + List<Map<String, Object>> list = |
| 208 | + handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'tutorials'").mapToMap().list(); |
| 209 | + |
| 210 | + assertEquals(1, list.size()); |
| 211 | + |
| 212 | + list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'REST with Spring'").mapToMap().list(); |
| 213 | + |
| 214 | + assertEquals(1, list.size()); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void whenMultipleParameters_thenReplacement() { |
| 220 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 221 | + jdbi.useHandle(handle -> { |
| 222 | + handle.execute("create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 223 | + Update update = handle.createUpdate("INSERT INTO PROJECT_11 (NAME, URL) VALUES (:name, :url)"); |
| 224 | + Map<String, String> params = new HashMap<>(); |
| 225 | + params.put("name", "REST with Spring"); |
| 226 | + params.put("url", "github.com/eugenp/REST-With-Spring"); |
| 227 | + update.bindMap(params); |
| 228 | + int rows = update.execute(); |
| 229 | + |
| 230 | + assertEquals(1, rows); |
| 231 | + |
| 232 | + List<Map<String, Object>> list = |
| 233 | + handle.select("SELECT * FROM PROJECT_11").mapToMap().list(); |
| 234 | + |
| 235 | + assertEquals(1, list.size()); |
| 236 | + |
| 237 | + class Params { |
| 238 | + private String name; |
| 239 | + private String url; |
| 240 | + |
| 241 | + public Params(String name, String url) { |
| 242 | + this.name = name; |
| 243 | + this.url = url; |
| 244 | + } |
| 245 | + |
| 246 | + public String getName() { |
| 247 | + return name; |
| 248 | + } |
| 249 | + |
| 250 | + public void setName(String name) { |
| 251 | + this.name = name; |
| 252 | + } |
| 253 | + |
| 254 | + public String getUrl() { |
| 255 | + return url; |
| 256 | + } |
| 257 | + |
| 258 | + public void setUrl(String url) { |
| 259 | + this.url = url; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + update.bindBean(new Params("tutorials", "github.com/eugenp/tutorials")); |
| 264 | + rows = update.execute(); |
| 265 | + |
| 266 | + assertEquals(1, rows); |
| 267 | + |
| 268 | + list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list(); |
| 269 | + |
| 270 | + assertEquals(2, list.size()); |
| 271 | + }); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void whenTransactionRollback_thenNoDataInserted() { |
| 276 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 277 | + jdbi.useHandle(handle -> { |
| 278 | + handle.useTransaction(h -> { |
| 279 | + handle.execute("create table PROJECT_12 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 280 | + handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); |
| 281 | + handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); |
| 282 | + handle.rollback(); |
| 283 | + List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_12").mapToMap().list(); |
| 284 | + |
| 285 | + assertEquals(0, list.size()); |
| 286 | + }); |
| 287 | + }); |
| 288 | + } |
| 289 | + |
| 290 | + @Test |
| 291 | + public void whenTransactionRollbackThenCommit_thenOnlyLastInserted() { |
| 292 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 293 | + jdbi.useHandle(handle -> { |
| 294 | + handle.useTransaction((Handle h) -> { |
| 295 | + assertEquals(handle, h); |
| 296 | + |
| 297 | + handle.execute("create table PROJECT_13 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 298 | + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); |
| 299 | + handle.rollback(); |
| 300 | + handle.begin(); |
| 301 | + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); |
| 302 | + handle.rollback(); |
| 303 | + handle.begin(); |
| 304 | + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); |
| 305 | + handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')"); |
| 306 | + handle.commit(); |
| 307 | + }); |
| 308 | + List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_13").mapToMap().list(); |
| 309 | + |
| 310 | + assertEquals(2, list.size()); |
| 311 | + }); |
| 312 | + } |
| 313 | + |
| 314 | + |
| 315 | + @Test |
| 316 | + public void whenException_thenTransactionIsRolledBack() { |
| 317 | + Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", ""); |
| 318 | + jdbi.useHandle(handle -> { |
| 319 | + try { |
| 320 | + handle.useTransaction(h -> { |
| 321 | + h.execute("create table PROJECT_14 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))"); |
| 322 | + h.execute("INSERT INTO PROJECT_14 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')"); |
| 323 | + List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list(); |
| 324 | + |
| 325 | + assertTrue(h.isInTransaction()); |
| 326 | + assertEquals(1, list.size()); |
| 327 | + |
| 328 | + throw new Exception("rollback"); |
| 329 | + }); |
| 330 | + } catch (Exception ignored) {} |
| 331 | + List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list(); |
| 332 | + |
| 333 | + assertFalse(handle.isInTransaction()); |
| 334 | + assertEquals(0, list.size()); |
| 335 | + }); |
| 336 | + } |
| 337 | + |
| 338 | +} |
0 commit comments