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; 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"; class Connector {
public static final String DB_USER = "";
public static final String DB_PASSWORD = ""; 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) { public void setName(String name) {
this.name = name; this.name = name;
} }
} }

View File

@ -6,9 +6,9 @@ import java.io.StringWriter;
import org.apache.commons.io.IOUtils; 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(); StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, "UTF-8"); IOUtils.copy(input, writer, "UTF-8");
return writer.toString(); return writer.toString();

View File

@ -9,28 +9,20 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class AutomapClassTest { public class AutomapClassTest {
private String DB_CONNECTION = Connector.DB_CONNECTION; ConnectionProvider connectionProvider = Connector.connectionProvider;
private String DB_USER = Connector.DB_USER; Database db = Database.from(connectionProvider);
private String DB_PASSWORD = Connector.DB_PASSWORD;
ConnectionProvider cp = null;
Database db = null;
Observable<Integer> create = null; Observable<Integer> create = null;
Observable<Integer> insert1, insert2 = null; Observable<Integer> insert1, insert2 = null;
@Before @Before
public void setup() { 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))") 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')") insert1 = db.update("INSERT INTO MANAGER(id, name) VALUES(1, 'Alan')")
@ -66,6 +58,6 @@ public class AutomapClassTest {
public void close() { public void close() {
db.update("DROP TABLE MANAGER") db.update("DROP TABLE MANAGER")
.dependsOn(create); .dependsOn(create);
cp.close(); connectionProvider.close();
} }
} }

View File

@ -9,28 +9,20 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class AutomapInterfaceTest { public class AutomapInterfaceTest {
private String DB_CONNECTION = Connector.DB_CONNECTION; ConnectionProvider connectionProvider = Connector.connectionProvider;
private String DB_USER = Connector.DB_USER; Database db = Database.from(connectionProvider);
private String DB_PASSWORD = Connector.DB_PASSWORD;
ConnectionProvider cp = null;
Database db = null;
Observable<Integer> create = null; Observable<Integer> create = null;
Observable<Integer> insert1, insert2 = null; Observable<Integer> insert1, insert2 = null;
@Before @Before
public void setup() { 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))") 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')") insert1 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'Alan')")
@ -66,7 +58,7 @@ public class AutomapInterfaceTest {
public void close() { public void close() {
db.update("DROP TABLE EMPLOYEE") db.update("DROP TABLE EMPLOYEE")
.dependsOn(create); .dependsOn(create);
cp.close(); connectionProvider.close();
} }
} }

View File

@ -6,31 +6,19 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class BasicQueryTypesTest { public class BasicQueryTypesTest {
private String DB_CONNECTION = Connector.DB_CONNECTION; ConnectionProvider connectionProvider = Connector.connectionProvider;
private String DB_USER = Connector.DB_USER; Database db = Database.from(connectionProvider);
private String DB_PASSWORD = Connector.DB_PASSWORD;
ConnectionProvider cp = null; Observable<Integer> create, insert1, insert2, insert3, update, delete = 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);
}
@Test @Test
public void whenCreateTableAndInsertRecords_thenCorrect() { public void whenCreateTableAndInsertRecords_thenCorrect() {
@ -48,36 +36,29 @@ public class BasicQueryTypesTest {
insert3 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(3, 'Mike')") insert3 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(3, 'Mike')")
.dependsOn(create) .dependsOn(create)
.count(); .count();
insert4 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(4, 'Jennifer')") delete = db.update("DELETE FROM EMPLOYEE WHERE id = 2")
.dependsOn(create) .dependsOn(create)
.count(); .count();
insert5 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(5, 'George')") List<String> names = db.select("select name from EMPLOYEE where id < ?")
.dependsOn(create) .parameter(3)
.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(create)
.dependsOn(insert1) .dependsOn(insert1)
.dependsOn(insert2) .dependsOn(insert2)
.dependsOn(insert3) .dependsOn(insert3)
.dependsOn(insert4)
.dependsOn(insert5)
.dependsOn(update) .dependsOn(update)
.dependsOn(delete) .dependsOn(delete)
.getAs(String.class) .getAs(String.class)
.toList() .toList()
.toBlocking() .toBlocking()
.single(); .single();
assertEquals(Arrays.asList("Mike", "Jennifer"), names);
assertEquals(Arrays.asList("Alan"), names);
} }
@After @After
public void close() { public void close() {
db.update("DROP TABLE EMPLOYEE") db.update("DROP TABLE EMPLOYEE")
.dependsOn(create); .dependsOn(create);
cp.close(); connectionProvider.close();
} }
} }

View File

@ -5,28 +5,21 @@ import static org.junit.Assert.assertEquals;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class InsertBlobTest { public class InsertBlobTest {
private String DB_CONNECTION = Connector.DB_CONNECTION; ConnectionProvider connectionProvider = Connector.connectionProvider;
private String DB_USER = Connector.DB_USER; Database db = Database.from(connectionProvider);
private String DB_PASSWORD = Connector.DB_PASSWORD;
ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD);
Database db = Database.from(cp);
String expectedDocument = null; String expectedDocument = null;
String actualDocument = null; String actualDocument = null;
@ -67,6 +60,6 @@ public class InsertBlobTest {
public void close() { public void close() {
db.update("DROP TABLE SERVERLOG") db.update("DROP TABLE SERVERLOG")
.dependsOn(create); .dependsOn(create);
cp.close(); connectionProvider.close();
} }
} }

View File

@ -11,19 +11,14 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class InsertClobTest { public class InsertClobTest {
private String DB_CONNECTION = Connector.DB_CONNECTION; ConnectionProvider connectionProvider = Connector.connectionProvider;
private String DB_USER = Connector.DB_USER; Database db = Database.from(connectionProvider);
private String DB_PASSWORD = Connector.DB_PASSWORD;
ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD);
Database db = Database.from(cp);
String expectedDocument = null; String expectedDocument = null;
String actualDocument = null; String actualDocument = null;
@ -63,6 +58,6 @@ public class InsertClobTest {
public void close() { public void close() {
db.update("DROP TABLE SERVERLOG") db.update("DROP TABLE SERVERLOG")
.dependsOn(create); .dependsOn(create);
cp.close(); connectionProvider.close();
} }
} }

View File

@ -2,26 +2,22 @@ package com.baeldung.rxjava.jdbc;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class ReturnKeysTest { 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<Boolean> begin, commit = null;
Observable<Integer> createStatement, insertStatement, updateStatement = null; Observable<Integer> createStatement, insertStatement, updateStatement = null;
ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); ConnectionProvider connectionProvider = Connector.connectionProvider;
Database db = Database.from(cp); Database db = Database.from(connectionProvider);
@Before @Before
public void setup() { public void setup() {
@ -42,4 +38,11 @@ public class ReturnKeysTest {
.single(); .single();
assertThat(key).isEqualTo(1); assertThat(key).isEqualTo(1);
} }
@After
public void close() {
db.update("DROP TABLE EMPLOYEE")
.dependsOn(createStatement);
connectionProvider.close();
}
} }

View File

@ -2,39 +2,36 @@ package com.baeldung.rxjava.jdbc;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import com.github.davidmoten.rx.jdbc.ConnectionProvider; import com.github.davidmoten.rx.jdbc.ConnectionProvider;
import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database; import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable; import rx.Observable;
public class TransactionTest { 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<Boolean> begin, commit = null;
Observable<Integer> createStatement, insertStatement, updateStatement = null; Observable<Integer> createStatement, insertStatement, updateStatement = null;
ConnectionProvider cp = new ConnectionProviderFromUrl(DB_CONNECTION, DB_USER, DB_PASSWORD); ConnectionProvider connectionProvider = Connector.connectionProvider;
Database db = Database.from(cp); Database db = Database.from(connectionProvider);
@Test @Test
public void whenCommitTransaction_thenRecordUpdated() { public void whenCommitTransaction_thenRecordUpdated() {
begin = db.beginTransaction(); Observable<Boolean> begin = db.beginTransaction();
createStatement = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))") Observable<Integer> createStatement = db
.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))")
.dependsOn(begin) .dependsOn(begin)
.count(); .count();
insertStatement = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')") Observable<Integer> insertStatement = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')")
.dependsOn(createStatement) .dependsOn(createStatement)
.count(); .count();
updateStatement = db.update("UPDATE EMPLOYEE SET name = 'Tom' WHERE id = 1") Observable<Integer> updateStatement = db.update("UPDATE EMPLOYEE SET name = 'Tom' WHERE id = 1")
.dependsOn(insertStatement) .dependsOn(insertStatement)
.count(); .count();
commit = db.commit(updateStatement); Observable<Boolean> commit = db.commit(updateStatement);
String name = db.select("select name from EMPLOYEE WHERE id = 1") String name = db.select("select name from EMPLOYEE WHERE id = 1")
.dependsOn(commit) .dependsOn(commit)
.getAs(String.class) .getAs(String.class)
@ -43,4 +40,11 @@ public class TransactionTest {
assertEquals("Tom", name); assertEquals("Tom", name);
} }
@After
public void close() {
db.update("DROP TABLE EMPLOYEE")
.dependsOn(createStatement);
connectionProvider.close();
}
} }