close cache instances properly (#2630)

* 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
This commit is contained in:
Ahmed Tawila 2017-09-23 11:42:36 +02:00 committed by Grzegorz Piwowarek
parent f5c539f8ba
commit 68d807e0c4
19 changed files with 3653 additions and 4 deletions

View File

@ -550,6 +550,21 @@
<artifactId>functionaljava</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>org.functionaljava</groupId>
<artifactId>functionaljava-java8</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>org.functionaljava</groupId>
<artifactId>functionaljava-quickcheck</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>org.functionaljava</groupId>
<artifactId>functionaljava-java-core</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>

View File

@ -41,4 +41,4 @@ public class CacheLoaderTest {
assertEquals("fromCache" + i, value);
}
}
}
}

View File

@ -38,4 +38,4 @@ public class EntryProcessorTest {
this.cache.invoke("key", new SimpleEntryProcessor());
assertEquals("value - modified", cache.get("key"));
}
}
}

View File

@ -53,4 +53,4 @@ public class EventListenerTest {
assertEquals(true, this.listener.getUpdated());
}
}
}

View File

@ -24,4 +24,4 @@ public class JCacheTest {
assertEquals("value2", cache.get("key2"));
cacheManager.close();
}
}
}

View File

@ -31,10 +31,29 @@
<artifactId>awaitility</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>rxjava-jdbc</artifactId>
<version>${rx.java.jdbc.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
</dependencies>
<properties>
<assertj.version>3.8.0</assertj.version>
<rx.java.version>1.2.5</rx.java.version>
<rx.java.jdbc.version>0.7.11</rx.java.jdbc.version>
<h2.version>1.4.196</h2.version>
</properties>

View File

@ -0,0 +1,8 @@
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 = "";
}

View File

@ -0,0 +1,13 @@
package com.baeldung.rxjava.jdbc;
import com.github.davidmoten.rx.jdbc.annotations.Column;
public interface Employee {
@Column("id")
int id();
@Column("name")
String name();
}

View File

@ -0,0 +1,29 @@
package com.baeldung.rxjava.jdbc;
public class Manager {
private int id;
private String name;
public Manager(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.rxjava.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
public class Utils {
public static String getStringFromInputStream(InputStream input) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, "UTF-8");
return writer.toString();
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.rxjava.jdbc;
import static org.assertj.core.api.Assertions.assertThat;
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 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;
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();
insert1 = db.update("INSERT INTO MANAGER(id, name) VALUES(1, 'Alan')")
.dependsOn(create)
.count();
insert2 = db.update("INSERT INTO MANAGER(id, name) VALUES(2, 'Sarah')")
.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();
assertThat(managers.get(0)
.getId()).isEqualTo(1);
assertThat(managers.get(0)
.getName()).isEqualTo("Alan");
assertThat(managers.get(1)
.getId()).isEqualTo(2);
assertThat(managers.get(1)
.getName()).isEqualTo("Sarah");
}
@After
public void close() {
db.update("DROP TABLE MANAGER")
.dependsOn(create);
cp.close();
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.rxjava.jdbc;
import static org.assertj.core.api.Assertions.assertThat;
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 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;
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();
insert1 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'Alan')")
.dependsOn(create)
.count();
insert2 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(2, 'Sarah')")
.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();
assertThat(employees.get(0)
.id()).isEqualTo(1);
assertThat(employees.get(0)
.name()).isEqualTo("Alan");
assertThat(employees.get(1)
.id()).isEqualTo(2);
assertThat(employees.get(1)
.name()).isEqualTo("Sarah");
}
@After
public void close() {
db.update("DROP TABLE EMPLOYEE")
.dependsOn(create);
cp.close();
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.rxjava.jdbc;
import static org.junit.Assert.assertEquals;
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 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);
}
@Test
public void whenCreateTableAndInsertRecords_thenCorrect() {
create = db.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int primary key, name varchar(255))")
.count();
insert1 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(1, 'John')")
.dependsOn(create)
.count();
update = db.update("UPDATE EMPLOYEE SET name = 'Alan' WHERE id = 1")
.dependsOn(create)
.count();
insert2 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(2, 'Sarah')")
.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);
}
@After
public void close() {
db.update("DROP TABLE EMPLOYEE")
.dependsOn(create);
cp.close();
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.rxjava.jdbc;
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);
String expectedDocument = null;
String actualDocument = null;
Observable<Integer> create, insert = null;
@Before
public void setup() throws IOException {
create = db.update("CREATE TABLE IF NOT EXISTS SERVERLOG (id int primary key, document BLOB)")
.count();
InputStream actualInputStream = new FileInputStream("src/test/resources/actual_clob");
this.actualDocument = Utils.getStringFromInputStream(actualInputStream);
byte[] bytes = this.actualDocument.getBytes(StandardCharsets.UTF_8);
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();
}
@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();
assertEquals(expectedDocument, actualDocument);
}
@After
public void close() {
db.update("DROP TABLE SERVERLOG")
.dependsOn(create);
cp.close();
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.rxjava.jdbc;
import static org.junit.Assert.assertEquals;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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 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);
String expectedDocument = null;
String actualDocument = null;
Observable<Integer> create, insert = null;
@Before
public void setup() throws IOException {
create = db.update("CREATE TABLE IF NOT EXISTS SERVERLOG (id int primary key, document CLOB)")
.count();
InputStream actualInputStream = new FileInputStream("src/test/resources/actual_clob");
this.actualDocument = Utils.getStringFromInputStream(actualInputStream);
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();
}
@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();
assertEquals(expectedDocument, actualDocument);
}
@After
public void close() {
db.update("DROP TABLE SERVERLOG")
.dependsOn(create);
cp.close();
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.rxjava.jdbc;
import static org.assertj.core.api.Assertions.assertThat;
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);
@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();
}
@Test
public void whenInsertAndReturnGeneratedKey_thenCorrect() {
Integer key = db.update("INSERT INTO EMPLOYEE(name) VALUES('John')")
.dependsOn(createStatement)
.returnGeneratedKeys()
.getAs(Integer.class)
.count()
.toBlocking()
.single();
assertThat(key).isEqualTo(1);
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.rxjava.jdbc;
import static org.junit.Assert.assertEquals;
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);
@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);
String name = db.select("select name from EMPLOYEE WHERE id = 1")
.dependsOn(commit)
.getAs(String.class)
.toBlocking()
.single();
assertEquals("Tom", name);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff