diff --git a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Connector.java b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Connector.java index ca241d74c4..b7416e471a 100644 --- a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Connector.java +++ b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Connector.java @@ -1,8 +1,13 @@ package com.baeldung.rxjava.jdbc; -public class Connector { - - public static final String DB_CONNECTION = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"; - public static final String DB_USER = ""; - public static final String DB_PASSWORD = ""; +import com.github.davidmoten.rx.jdbc.ConnectionProvider; +import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; + +class Connector { + + static final String DB_CONNECTION = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"; + static final String DB_USER = ""; + static final String DB_PASSWORD = ""; + + static final ConnectionProvider connectionProvider = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); } diff --git a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Manager.java b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Manager.java index d2b87fc897..56faa4cae7 100644 --- a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Manager.java +++ b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Manager.java @@ -25,5 +25,4 @@ public class Manager { public void setName(String name) { this.name = name; } - } diff --git a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Utils.java b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Utils.java index f1dbd2676b..401962d1a9 100644 --- a/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Utils.java +++ b/rxjava/src/main/java/com/baeldung/rxjava/jdbc/Utils.java @@ -6,9 +6,9 @@ import java.io.StringWriter; import org.apache.commons.io.IOUtils; -public class Utils { +class Utils { - public static String getStringFromInputStream(InputStream input) throws IOException { + static String getStringFromInputStream(InputStream input) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(input, writer, "UTF-8"); return writer.toString(); diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapClassTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapClassTest.java index e8bca98701..f44d4ac6b8 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapClassTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapClassTest.java @@ -9,63 +9,55 @@ import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class AutomapClassTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - - ConnectionProvider cp = null; - Database db = null; + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); Observable create = null; Observable insert1, insert2 = null; @Before public void setup() { - cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - db = Database.from(cp); - create = db.update("CREATE TABLE IF NOT EXISTS MANAGER(id int primary key, name varchar(255))") - .count(); + .count(); insert1 = db.update("INSERT INTO MANAGER(id, name) VALUES(1, 'Alan')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); insert2 = db.update("INSERT INTO MANAGER(id, name) VALUES(2, 'Sarah')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); } @Test public void whenSelectManagersAndAutomap_thenCorrect() { List managers = db.select("select id, name from MANAGER") - .dependsOn(create) - .dependsOn(insert1) - .dependsOn(insert2) - .autoMap(Manager.class) - .toList() - .toBlocking() - .single(); + .dependsOn(create) + .dependsOn(insert1) + .dependsOn(insert2) + .autoMap(Manager.class) + .toList() + .toBlocking() + .single(); assertThat(managers.get(0) - .getId()).isEqualTo(1); + .getId()).isEqualTo(1); assertThat(managers.get(0) - .getName()).isEqualTo("Alan"); + .getName()).isEqualTo("Alan"); assertThat(managers.get(1) - .getId()).isEqualTo(2); + .getId()).isEqualTo(2); assertThat(managers.get(1) - .getName()).isEqualTo("Sarah"); + .getName()).isEqualTo("Sarah"); } @After public void close() { db.update("DROP TABLE MANAGER") - .dependsOn(create); - cp.close(); + .dependsOn(create); + connectionProvider.close(); } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapInterfaceTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapInterfaceTest.java index f636dda155..79bae281eb 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapInterfaceTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/AutomapInterfaceTest.java @@ -9,64 +9,56 @@ import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class AutomapInterfaceTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - - ConnectionProvider cp = null; - Database db = null; + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); Observable create = null; Observable insert1, insert2 = null; @Before public void setup() { - cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - db = Database.from(cp); - create = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))") - .count(); + .count(); insert1 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'Alan')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); insert2 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(2, 'Sarah')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); } @Test public void whenSelectFromTableAndAutomap_thenCorrect() { List employees = db.select("select id, name from EMPLOYEE") - .dependsOn(create) - .dependsOn(insert1) - .dependsOn(insert2) - .autoMap(Employee.class) - .toList() - .toBlocking() - .single(); + .dependsOn(create) + .dependsOn(insert1) + .dependsOn(insert2) + .autoMap(Employee.class) + .toList() + .toBlocking() + .single(); assertThat(employees.get(0) - .id()).isEqualTo(1); + .id()).isEqualTo(1); assertThat(employees.get(0) - .name()).isEqualTo("Alan"); + .name()).isEqualTo("Alan"); assertThat(employees.get(1) - .id()).isEqualTo(2); + .id()).isEqualTo(2); assertThat(employees.get(1) - .name()).isEqualTo("Sarah"); + .name()).isEqualTo("Sarah"); } @After public void close() { db.update("DROP TABLE EMPLOYEE") .dependsOn(create); - cp.close(); + connectionProvider.close(); } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/BasicQueryTypesTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/BasicQueryTypesTest.java index 6118e6f127..7677b2375d 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/BasicQueryTypesTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/BasicQueryTypesTest.java @@ -6,78 +6,59 @@ import java.util.Arrays; import java.util.List; import org.junit.After; -import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class BasicQueryTypesTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); - ConnectionProvider cp = null; - Database db = null; - - Observable create, insert1, insert2, insert3, insert4, insert5, update, delete = null; - - @Before - public void setup() { - cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - db = Database.from(cp); - } + Observable create, insert1, insert2, insert3, update, delete = null; @Test public void whenCreateTableAndInsertRecords_thenCorrect() { create = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))") - .count(); + .count(); insert1 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); update = db.update("UPDATE EMPLOYEE SET name = 'Alan' WHERE id = 1") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); insert2 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(2, 'Sarah')") - .dependsOn(create) - .count(); + .dependsOn(create) + .count(); insert3 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(3, 'Mike')") - .dependsOn(create) - .count(); - insert4 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(4, 'Jennifer')") - .dependsOn(create) - .count(); - insert5 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(5, 'George')") - .dependsOn(create) - .count(); - delete = db.update("DELETE FROM EMPLOYEE WHERE id = 5") - .dependsOn(create) - .count(); - List names = db.select("select name from EMPLOYEE where id > ?") - .parameter(2) - .dependsOn(create) - .dependsOn(insert1) - .dependsOn(insert2) - .dependsOn(insert3) - .dependsOn(insert4) - .dependsOn(insert5) - .dependsOn(update) - .dependsOn(delete) - .getAs(String.class) - .toList() - .toBlocking() - .single(); - assertEquals(Arrays.asList("Mike", "Jennifer"), names); + .dependsOn(create) + .count(); + delete = db.update("DELETE FROM EMPLOYEE WHERE id = 2") + .dependsOn(create) + .count(); + List names = db.select("select name from EMPLOYEE where id < ?") + .parameter(3) + .dependsOn(create) + .dependsOn(insert1) + .dependsOn(insert2) + .dependsOn(insert3) + .dependsOn(update) + .dependsOn(delete) + .getAs(String.class) + .toList() + .toBlocking() + .single(); + + assertEquals(Arrays.asList("Alan"), names); } @After public void close() { db.update("DROP TABLE EMPLOYEE") - .dependsOn(create); - cp.close(); + .dependsOn(create); + connectionProvider.close(); } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertBlobTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertBlobTest.java index ced394eccb..fb3018ede4 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertBlobTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertBlobTest.java @@ -5,28 +5,21 @@ import static org.junit.Assert.assertEquals; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.StringWriter; import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class InsertBlobTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - - ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - Database db = Database.from(cp); + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); String expectedDocument = null; String actualDocument = null; @@ -36,7 +29,7 @@ public class InsertBlobTest { @Before public void setup() throws IOException { create = db.update("CREATE TABLE IF NOT EXISTS SERVERLOG (id int primary key, document BLOB)") - .count(); + .count(); InputStream actualInputStream = new FileInputStream("src/test/resources/actual_clob"); this.actualDocument = Utils.getStringFromInputStream(actualInputStream); @@ -45,28 +38,28 @@ public class InsertBlobTest { InputStream expectedInputStream = new FileInputStream("src/test/resources/expected_clob"); this.expectedDocument = Utils.getStringFromInputStream(expectedInputStream); this.insert = db.update("insert into SERVERLOG(id,document) values(?,?)") - .parameter(1) - .parameter(Database.toSentinelIfNull(bytes)) - .dependsOn(create) - .count(); + .parameter(1) + .parameter(Database.toSentinelIfNull(bytes)) + .dependsOn(create) + .count(); } @Test public void whenInsertBLOB_thenCorrect() throws IOException { db.select("select document from SERVERLOG where id = 1") - .dependsOn(create) - .dependsOn(insert) - .getAs(String.class) - .toList() - .toBlocking() - .single(); + .dependsOn(create) + .dependsOn(insert) + .getAs(String.class) + .toList() + .toBlocking() + .single(); assertEquals(expectedDocument, actualDocument); } @After public void close() { db.update("DROP TABLE SERVERLOG") - .dependsOn(create); - cp.close(); + .dependsOn(create); + connectionProvider.close(); } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertClobTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertClobTest.java index 90f798aa43..d29c2e3de2 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertClobTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/InsertClobTest.java @@ -11,19 +11,14 @@ import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class InsertClobTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - - ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - Database db = Database.from(cp); + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); String expectedDocument = null; String actualDocument = null; @@ -33,7 +28,7 @@ public class InsertClobTest { @Before public void setup() throws IOException { create = db.update("CREATE TABLE IF NOT EXISTS SERVERLOG (id int primary key, document CLOB)") - .count(); + .count(); InputStream actualInputStream = new FileInputStream("src/test/resources/actual_clob"); this.actualDocument = Utils.getStringFromInputStream(actualInputStream); @@ -41,28 +36,28 @@ public class InsertClobTest { InputStream expectedInputStream = new FileInputStream("src/test/resources/expected_clob"); this.expectedDocument = Utils.getStringFromInputStream(expectedInputStream); this.insert = db.update("insert into SERVERLOG(id,document) values(?,?)") - .parameter(1) - .parameter(Database.toSentinelIfNull(actualDocument)) - .dependsOn(create) - .count(); + .parameter(1) + .parameter(Database.toSentinelIfNull(actualDocument)) + .dependsOn(create) + .count(); } @Test public void whenSelectCLOB_thenCorrect() throws IOException { db.select("select document from SERVERLOG where id = 1") - .dependsOn(create) - .dependsOn(insert) - .getAs(String.class) - .toList() - .toBlocking() - .single(); + .dependsOn(create) + .dependsOn(insert) + .getAs(String.class) + .toList() + .toBlocking() + .single(); assertEquals(expectedDocument, actualDocument); } @After public void close() { db.update("DROP TABLE SERVERLOG") - .dependsOn(create); - cp.close(); + .dependsOn(create); + connectionProvider.close(); } } \ No newline at end of file diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/ReturnKeysTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/ReturnKeysTest.java index d67fe1d4c1..87604b6c5f 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/ReturnKeysTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/ReturnKeysTest.java @@ -2,44 +2,47 @@ package com.baeldung.rxjava.jdbc; import static org.assertj.core.api.Assertions.assertThat; +import org.junit.After; import org.junit.Before; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class ReturnKeysTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - Observable begin, commit = null; Observable createStatement, insertStatement, updateStatement = null; - ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - Database db = Database.from(cp); + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); @Before public void setup() { begin = db.beginTransaction(); createStatement = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int auto_increment primary key, name varchar(255))") - .dependsOn(begin) - .count(); + .dependsOn(begin) + .count(); } @Test public void whenInsertAndReturnGeneratedKey_thenCorrect() { Integer key = db.update("INSERT INTO EMPLOYEE(name) VALUES('John')") - .dependsOn(createStatement) - .returnGeneratedKeys() - .getAs(Integer.class) - .count() - .toBlocking() - .single(); + .dependsOn(createStatement) + .returnGeneratedKeys() + .getAs(Integer.class) + .count() + .toBlocking() + .single(); assertThat(key).isEqualTo(1); } + + @After + public void close() { + db.update("DROP TABLE EMPLOYEE") + .dependsOn(createStatement); + connectionProvider.close(); + } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/TransactionTest.java b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/TransactionTest.java index a0076861da..9603a11c46 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/jdbc/TransactionTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/jdbc/TransactionTest.java @@ -2,45 +2,49 @@ package com.baeldung.rxjava.jdbc; import static org.junit.Assert.assertEquals; +import org.junit.After; import org.junit.Test; import com.github.davidmoten.rx.jdbc.ConnectionProvider; -import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl; import com.github.davidmoten.rx.jdbc.Database; import rx.Observable; public class TransactionTest { - private String DB_CONNECTION = Connector.DB_CONNECTION; - private String DB_USER = Connector.DB_USER; - private String DB_PASSWORD = Connector.DB_PASSWORD; - Observable begin, commit = null; Observable createStatement, insertStatement, updateStatement = null; - ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); - Database db = Database.from(cp); + ConnectionProvider connectionProvider = Connector.connectionProvider; + Database db = Database.from(connectionProvider); @Test public void whenCommitTransaction_thenRecordUpdated() { - begin = db.beginTransaction(); - createStatement = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))") - .dependsOn(begin) - .count(); - insertStatement = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')") - .dependsOn(createStatement) - .count(); - updateStatement = db.update("UPDATE EMPLOYEE SET name = 'Tom' WHERE id = 1") - .dependsOn(insertStatement) - .count(); - commit = db.commit(updateStatement); + Observable begin = db.beginTransaction(); + Observable createStatement = db + .update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))") + .dependsOn(begin) + .count(); + Observable insertStatement = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')") + .dependsOn(createStatement) + .count(); + Observable updateStatement = db.update("UPDATE EMPLOYEE SET name = 'Tom' WHERE id = 1") + .dependsOn(insertStatement) + .count(); + Observable commit = db.commit(updateStatement); String name = db.select("select name from EMPLOYEE WHERE id = 1") - .dependsOn(commit) - .getAs(String.class) - .toBlocking() - .single(); + .dependsOn(commit) + .getAs(String.class) + .toBlocking() + .single(); assertEquals("Tom", name); } + + @After + public void close() { + db.update("DROP TABLE EMPLOYEE") + .dependsOn(createStatement); + connectionProvider.close(); + } }