Refactor rxjava-jdbc (#2663)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article

* BAEL-972 - Apache Commons Text - added another example

* BAEL-972 - Apache Commons Text - just indentation

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java - fix problems

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* fix formatting

* BAEL-1033 minor refactor

* BAEL-1035 Introduction to Eclipse Collections

* format

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* cleanup

* cleanup

* BAEL-1109 Introduction to JCache

* BAEL-1109 Introduction to JCache

* remove unneeded property in pom.xml

* fix formatting

* close cache instances properly

* remove latest commit

* BAEL-1057 Introduction to rxjava-jdbc

* refactor rxjava-jdbc

* Refactor rxjava-jdbc

* Refactoring rxjava-jdbc
This commit is contained in:
Ahmed Tawila 2017-09-23 21:37:30 +02:00 committed by Grzegorz Piwowarek
parent 68d807e0c4
commit dca9403876
10 changed files with 156 additions and 192 deletions

View File

@ -1,8 +1,13 @@
package com.baeldung.rxjava.jdbc;
public class Connector {
import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
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 = "";
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);
}

View File

@ -25,5 +25,4 @@ public class Manager {
public void setName(String name) {
this.name = name;
}
}

View File

@ -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();

View File

@ -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<Integer> create = null;
Observable<Integer> 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<Manager> 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();
}
}

View File

@ -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<Integer> create = null;
Observable<Integer> 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<Employee> 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();
}
}

View File

@ -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<Integer> 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<Integer> 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<String> 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<String> 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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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<Boolean> begin, commit = null;
Observable<Integer> 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();
}
}

View File

@ -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<Boolean> begin, commit = null;
Observable<Integer> 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<Boolean> begin = db.beginTransaction();
Observable<Integer> createStatement = db
.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))")
.dependsOn(begin)
.count();
Observable<Integer> insertStatement = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')")
.dependsOn(createStatement)
.count();
Observable<Integer> updateStatement = db.update("UPDATE EMPLOYEE SET name = 'Tom' WHERE id = 1")
.dependsOn(insertStatement)
.count();
Observable<Boolean> 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();
}
}