Build optimization 26.09.2017 (#2676)
* Optimize spring-rest * Refactor RxJava * Refactor RxJava
This commit is contained in:
parent
c9a754f0e4
commit
d10d758c84
@ -3,7 +3,6 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
@ -8,7 +8,7 @@ import static junit.framework.Assert.assertTrue;
|
||||
|
||||
public class ObservableTest {
|
||||
|
||||
String result = "";
|
||||
private String result = "";
|
||||
|
||||
@Test
|
||||
public void givenString_whenJustAndSubscribe_thenEmitsSingleItem() {
|
||||
@ -85,7 +85,7 @@ public class ObservableTest {
|
||||
.groupBy(i -> 0 == (i % 2) ? "EVEN" : "ODD")
|
||||
.subscribe(group ->
|
||||
group.subscribe((number) -> {
|
||||
if (group.getKey().toString().equals("EVEN")) {
|
||||
if (group.getKey().equals("EVEN")) {
|
||||
EVEN[0] += number;
|
||||
} else {
|
||||
ODD[0] += number;
|
||||
@ -141,5 +141,4 @@ public class ObservableTest {
|
||||
|
||||
assertTrue(sum[0] == 10);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,16 +12,12 @@ public class ResourceManagementTest {
|
||||
|
||||
String[] result = {""};
|
||||
Observable<Character> values = Observable.using(
|
||||
() -> {
|
||||
return "MyResource";
|
||||
},
|
||||
r -> {
|
||||
return Observable.create(o -> {
|
||||
for (Character c : r.toCharArray())
|
||||
o.onNext(c);
|
||||
o.onCompleted();
|
||||
});
|
||||
},
|
||||
() -> "MyResource",
|
||||
r -> Observable.create(o -> {
|
||||
for (Character c : r.toCharArray())
|
||||
o.onNext(c);
|
||||
o.onCompleted();
|
||||
}),
|
||||
r -> System.out.println("Disposed: " + r)
|
||||
);
|
||||
|
||||
|
@ -27,7 +27,6 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent();
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -60,7 +59,6 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -77,7 +75,6 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -88,15 +85,14 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
|
||||
// when
|
||||
source.sample(100, TimeUnit.MILLISECONDS)
|
||||
// .throttleFirst(100, TimeUnit.MILLISECONDS)
|
||||
.observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
// .throttleFirst(100, TimeUnit.MILLISECONDS)
|
||||
.observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
IntStream.range(0, 1_000).forEach(source::onNext);
|
||||
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -111,7 +107,6 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -120,11 +115,11 @@ public class RxJavaBackpressureLongRunningUnitTest {
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
|
||||
// when
|
||||
Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,15 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
import rx.Observable.Operator;
|
||||
import rx.Observable.Transformer;
|
||||
import rx.Subscriber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.baelding.rxjava.operator.ToCleanString.toCleanString;
|
||||
import static com.baelding.rxjava.operator.ToLength.toLength;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
@ -7,20 +17,6 @@ import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import rx.Observable;
|
||||
import rx.Observable.Operator;
|
||||
import rx.Observable.Transformer;
|
||||
import rx.Subscriber;
|
||||
|
||||
import com.baelding.rxjava.operator.ToCleanString;
|
||||
import com.baelding.rxjava.operator.ToLength;
|
||||
|
||||
public class RxJavaCustomOperatorUnitTest {
|
||||
|
||||
@Test
|
||||
@ -29,7 +25,7 @@ public class RxJavaCustomOperatorUnitTest {
|
||||
final List<String> results = new ArrayList<>();
|
||||
|
||||
final Observable<String> observable = Observable.from(list)
|
||||
.lift(toCleanString());
|
||||
.lift(toCleanString());
|
||||
|
||||
// when
|
||||
observable.subscribe(results::add);
|
||||
@ -46,7 +42,7 @@ public class RxJavaCustomOperatorUnitTest {
|
||||
final List<Integer> results = new ArrayList<>();
|
||||
|
||||
final Observable<Integer> observable = Observable.from(list)
|
||||
.compose(toLength());
|
||||
.compose(toLength());
|
||||
|
||||
// when
|
||||
observable.subscribe(results::add);
|
||||
@ -85,8 +81,8 @@ public class RxJavaCustomOperatorUnitTest {
|
||||
|
||||
final List<String> results = new ArrayList<>();
|
||||
Observable.from(Arrays.asList("ap_p-l@e", "or-an?ge"))
|
||||
.lift(cleanStringFn)
|
||||
.subscribe(results::add);
|
||||
.lift(cleanStringFn)
|
||||
.subscribe(results::add);
|
||||
|
||||
assertThat(results, notNullValue());
|
||||
assertThat(results, hasSize(2));
|
||||
@ -99,8 +95,8 @@ public class RxJavaCustomOperatorUnitTest {
|
||||
|
||||
final List<Integer> results = new ArrayList<>();
|
||||
Observable.from(Arrays.asList("apple", "orange"))
|
||||
.compose(toLengthFn)
|
||||
.subscribe(results::add);
|
||||
.compose(toLengthFn)
|
||||
.subscribe(results::add);
|
||||
|
||||
assertThat(results, notNullValue());
|
||||
assertThat(results, hasSize(2));
|
||||
|
@ -10,7 +10,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class RxJavaUnitTest {
|
||||
@ -19,7 +21,8 @@ public class RxJavaUnitTest {
|
||||
// given
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
List<String> results = new ArrayList<>();
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), (string, index) -> index + "-" + string);
|
||||
Observable<String> observable = Observable.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE), (string, index) -> index + "-" + string);
|
||||
|
||||
// when
|
||||
observable.subscribe(results::add);
|
||||
@ -36,7 +39,8 @@ public class RxJavaUnitTest {
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
TestSubscriber<String> subscriber = new TestSubscriber<>();
|
||||
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string));
|
||||
Observable<String> observable = Observable.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string));
|
||||
|
||||
// when
|
||||
observable.subscribe(subscriber);
|
||||
@ -54,7 +58,9 @@ public class RxJavaUnitTest {
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
TestSubscriber<String> subscriber = new TestSubscriber<>();
|
||||
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)).concatWith(Observable.error(new RuntimeException("error in Observable")));
|
||||
Observable<String> observable = Observable.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string))
|
||||
.concatWith(Observable.error(new RuntimeException("error in Observable")));
|
||||
|
||||
// when
|
||||
observable.subscribe(subscriber);
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@ -23,9 +21,9 @@ import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class SchedulersTest {
|
||||
String result = "";
|
||||
String result1 = "";
|
||||
String result2 = "";
|
||||
private String result = "";
|
||||
private String result1 = "";
|
||||
private String result2 = "";
|
||||
|
||||
@Test
|
||||
public void givenScheduledWorker_whenScheduleAnAction_thenResultAction() throws InterruptedException {
|
||||
@ -108,7 +106,6 @@ public class SchedulersTest {
|
||||
Assert.assertTrue(result.equals("main"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenTrampolineScheduled_thenExecuteOnMainThread() throws InterruptedException {
|
||||
System.out.println("trampoline_1");
|
||||
@ -161,7 +158,8 @@ public class SchedulersTest {
|
||||
subscriber.onNext("Alfa");
|
||||
subscriber.onNext("Beta");
|
||||
subscriber.onCompleted();
|
||||
});;
|
||||
});
|
||||
;
|
||||
|
||||
observable
|
||||
.subscribeOn(schedulerA)
|
||||
@ -203,10 +201,10 @@ public class SchedulersTest {
|
||||
|
||||
Observable<Long> tick = Observable.interval(1, TimeUnit.SECONDS, scheduler);
|
||||
|
||||
Observable.from(letters)
|
||||
.zipWith(tick, (string, index) -> index + "-" + string)
|
||||
.subscribeOn(scheduler)
|
||||
.subscribe(subscriber);
|
||||
Observable.from(letters)
|
||||
.zipWith(tick, (string, index) -> index + "-" + string)
|
||||
.subscribeOn(scheduler)
|
||||
.subscribe(subscriber);
|
||||
|
||||
subscriber.assertNoValues();
|
||||
subscriber.assertNotCompleted();
|
||||
@ -229,10 +227,9 @@ public class SchedulersTest {
|
||||
Scheduler schedulerA = Schedulers.from(poolA);
|
||||
Observable.just('A', 'B')
|
||||
.delay(1, TimeUnit.SECONDS, schedulerA)
|
||||
.subscribe(i -> result+= Thread.currentThread().getName() + i + " ");
|
||||
.subscribe(i -> result += Thread.currentThread().getName() + i + " ");
|
||||
|
||||
Thread.sleep(2000);
|
||||
Assert.assertTrue(result.equals("Sched1-A Sched1-B "));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,5 +20,4 @@ public class SingleTest {
|
||||
single.subscribe();
|
||||
assertTrue(result[0].equals("Hello"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import static junit.framework.Assert.assertTrue;
|
||||
public class SubjectTest {
|
||||
|
||||
@Test
|
||||
public void givenSubjectAndTwoSubscribers_whenSubscribeOnSubject_thenSubscriberBeginsToAdd(){
|
||||
public void givenSubjectAndTwoSubscribers_whenSubscribeOnSubject_thenSubscriberBeginsToAdd() {
|
||||
PublishSubject<Integer> subject = PublishSubject.create();
|
||||
|
||||
subject.subscribe(SubjectImpl.getFirstObserver());
|
||||
|
@ -7,6 +7,7 @@ import rx.Observable;
|
||||
import rx.Observer;
|
||||
import rx.exceptions.OnErrorNotImplementedException;
|
||||
import rx.schedulers.Schedulers;
|
||||
import rx.schedulers.Timestamped;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -14,9 +15,9 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class UtilityOperatorsTest {
|
||||
|
||||
int emittedTotal = 0;
|
||||
int receivedTotal = 0;
|
||||
String result = "";
|
||||
private int emittedTotal = 0;
|
||||
private int receivedTotal = 0;
|
||||
private String result = "";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
@ -44,7 +45,6 @@ public class UtilityOperatorsTest {
|
||||
assertTrue(receivedTotal == 15000);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenObserveOnBeforeOnNext_thenEmitsEventsOnComputeScheduler() throws InterruptedException {
|
||||
|
||||
@ -68,7 +68,6 @@ public class UtilityOperatorsTest {
|
||||
assertTrue(receivedTotal == 15000);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenSubscribeOn_thenEmitsEventsOnComputeScheduler() throws InterruptedException {
|
||||
|
||||
@ -92,7 +91,6 @@ public class UtilityOperatorsTest {
|
||||
assertTrue(receivedTotal == 15000);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservableWithOneEvent_whenSingle_thenEmitEvent() {
|
||||
|
||||
@ -197,15 +195,13 @@ public class UtilityOperatorsTest {
|
||||
@Test
|
||||
public void givenObservables_whenDelay_thenEventsStartAppearAfterATime() throws InterruptedException {
|
||||
|
||||
Observable source
|
||||
= Observable.interval(1, TimeUnit.SECONDS)
|
||||
Observable<Timestamped<Long>> source = Observable.interval(1, TimeUnit.SECONDS)
|
||||
.take(5)
|
||||
.timestamp();
|
||||
|
||||
Observable delay
|
||||
= source.delaySubscription(2, TimeUnit.SECONDS);
|
||||
Observable<Timestamped<Long>> delay = source.delaySubscription(2, TimeUnit.SECONDS);
|
||||
|
||||
source.subscribe(
|
||||
source.<Long>subscribe(
|
||||
value -> System.out.println("source :" + value),
|
||||
t -> System.out.println("source error"),
|
||||
() -> System.out.println("source completed"));
|
||||
@ -231,14 +227,12 @@ public class UtilityOperatorsTest {
|
||||
|
||||
Observable<Character> values = Observable.using(
|
||||
() -> "resource",
|
||||
r -> {
|
||||
return Observable.create(o -> {
|
||||
for (Character c : r.toCharArray()) {
|
||||
o.onNext(c);
|
||||
}
|
||||
o.onCompleted();
|
||||
});
|
||||
},
|
||||
r -> Observable.create(o -> {
|
||||
for (Character c : r.toCharArray()) {
|
||||
o.onNext(c);
|
||||
}
|
||||
o.onCompleted();
|
||||
}),
|
||||
r -> System.out.println("Disposed: " + r)
|
||||
);
|
||||
values.subscribe(
|
||||
@ -248,7 +242,6 @@ public class UtilityOperatorsTest {
|
||||
assertTrue(result.equals("resource"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservableCached_whenSubscribesWith2Actions_thenEmitsCachedValues() {
|
||||
|
||||
@ -269,5 +262,4 @@ public class UtilityOperatorsTest {
|
||||
});
|
||||
assertTrue(receivedTotal == 8);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,18 +8,16 @@ 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.Database;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
public class AutomapClassTest {
|
||||
public class AutomapClassIntegrationTest {
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
Observable<Integer> create = null;
|
||||
Observable<Integer> insert1, insert2 = null;
|
||||
private Observable<Integer> create = null;
|
||||
private Observable<Integer> insert1, insert2 = null;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@ -58,6 +56,6 @@ public class AutomapClassTest {
|
||||
public void close() {
|
||||
db.update("DROP TABLE MANAGER")
|
||||
.dependsOn(create);
|
||||
connectionProvider.close();
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -8,18 +8,16 @@ 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.Database;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
public class AutomapInterfaceTest {
|
||||
public class AutomapInterfaceIntegrationTest {
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
Observable<Integer> create = null;
|
||||
Observable<Integer> insert1, insert2 = null;
|
||||
private Observable<Integer> create = null;
|
||||
private Observable<Integer> insert1, insert2 = null;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@ -58,7 +56,7 @@ public class AutomapInterfaceTest {
|
||||
public void close() {
|
||||
db.update("DROP TABLE EMPLOYEE")
|
||||
.dependsOn(create);
|
||||
connectionProvider.close();
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
|
||||
}
|
@ -1,42 +1,38 @@
|
||||
package com.baeldung.rxjava.jdbc;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.ConnectionProvider;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
public class BasicQueryTypesIntegrationTest {
|
||||
|
||||
import rx.Observable;
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
public class BasicQueryTypesTest {
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
|
||||
Observable<Integer> create, insert1, insert2, insert3, update, delete = null;
|
||||
private Observable<Integer> create;
|
||||
|
||||
@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')")
|
||||
Observable<Integer> 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")
|
||||
Observable<Integer> 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')")
|
||||
Observable<Integer> 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')")
|
||||
Observable<Integer> insert3 = db.update("INSERT INTO EMPLOYEE(id, name) VALUES(3, 'Mike')")
|
||||
.dependsOn(create)
|
||||
.count();
|
||||
delete = db.update("DELETE FROM EMPLOYEE WHERE id = 2")
|
||||
Observable<Integer> delete = db.update("DELETE FROM EMPLOYEE WHERE id = 2")
|
||||
.dependsOn(create)
|
||||
.count();
|
||||
List<String> names = db.select("select name from EMPLOYEE where id < ?")
|
||||
@ -59,6 +55,6 @@ public class BasicQueryTypesTest {
|
||||
public void close() {
|
||||
db.update("DROP TABLE EMPLOYEE")
|
||||
.dependsOn(create);
|
||||
connectionProvider.close();
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -1,30 +1,26 @@
|
||||
package com.baeldung.rxjava.jdbc;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.ConnectionProvider;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
public class InsertBlobIntegrationTest {
|
||||
|
||||
import rx.Observable;
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
public class InsertBlobTest {
|
||||
private String expectedDocument = null;
|
||||
private String actualDocument = null;
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
|
||||
String expectedDocument = null;
|
||||
String actualDocument = null;
|
||||
|
||||
Observable<Integer> create, insert = null;
|
||||
private Observable<Integer> create, insert = null;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
@ -60,6 +56,6 @@ public class InsertBlobTest {
|
||||
public void close() {
|
||||
db.update("DROP TABLE SERVERLOG")
|
||||
.dependsOn(create);
|
||||
connectionProvider.close();
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -1,29 +1,25 @@
|
||||
package com.baeldung.rxjava.jdbc;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.ConnectionProvider;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
public class InsertClobIntegrationTest {
|
||||
|
||||
import rx.Observable;
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
public class InsertClobTest {
|
||||
private String expectedDocument = null;
|
||||
private String actualDocument = null;
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
|
||||
String expectedDocument = null;
|
||||
String actualDocument = null;
|
||||
|
||||
Observable<Integer> create, insert = null;
|
||||
private Observable<Integer> create, insert = null;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
@ -58,6 +54,6 @@ public class InsertClobTest {
|
||||
public void close() {
|
||||
db.update("DROP TABLE SERVERLOG")
|
||||
.dependsOn(create);
|
||||
connectionProvider.close();
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -1,28 +1,24 @@
|
||||
package com.baeldung.rxjava.jdbc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
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.Database;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
public class ReturnKeysTest {
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
Observable<Boolean> begin, commit = null;
|
||||
Observable<Integer> createStatement, insertStatement, updateStatement = null;
|
||||
public class ReturnKeysIntegrationTest {
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
private Observable<Integer> createStatement;
|
||||
|
||||
private Database db = Database.from(Connector.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))")
|
||||
Observable<Boolean> begin = db.beginTransaction();
|
||||
createStatement = db
|
||||
.update("CREATE TABLE IF NOT EXISTS EMPLOYEE(id int auto_increment primary key, name varchar(255))")
|
||||
.dependsOn(begin)
|
||||
.count();
|
||||
}
|
||||
@ -41,8 +37,7 @@ public class ReturnKeysTest {
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
db.update("DROP TABLE EMPLOYEE")
|
||||
.dependsOn(createStatement);
|
||||
connectionProvider.close();
|
||||
db.update("DROP TABLE EMPLOYEE");
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -1,22 +1,15 @@
|
||||
package com.baeldung.rxjava.jdbc;
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
public class TransactionIntegrationTest {
|
||||
|
||||
import com.github.davidmoten.rx.jdbc.ConnectionProvider;
|
||||
import com.github.davidmoten.rx.jdbc.Database;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
public class TransactionTest {
|
||||
|
||||
Observable<Boolean> begin, commit = null;
|
||||
Observable<Integer> createStatement, insertStatement, updateStatement = null;
|
||||
|
||||
ConnectionProvider connectionProvider = Connector.connectionProvider;
|
||||
Database db = Database.from(connectionProvider);
|
||||
private Database db = Database.from(Connector.connectionProvider);
|
||||
|
||||
@Test
|
||||
public void whenCommitTransaction_thenRecordUpdated() {
|
||||
@ -43,8 +36,7 @@ public class TransactionTest {
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
db.update("DROP TABLE EMPLOYEE")
|
||||
.dependsOn(createStatement);
|
||||
connectionProvider.close();
|
||||
db.update("DROP TABLE EMPLOYEE");
|
||||
Connector.connectionProvider.close();
|
||||
}
|
||||
}
|
@ -9,9 +9,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class ExceptionHandlingTest {
|
||||
|
||||
private Error UNKNOWN_ERROR = new Error("unknown error");
|
||||
@ -19,10 +16,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenHandleOnErrorReturn_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.onErrorReturn(Throwable::getMessage)
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -34,10 +31,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenHandleOnErrorResume_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.onErrorResumeNext(Observable.just("one", "two"))
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -49,10 +46,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenHandleOnErrorResumeItem_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.onErrorReturnItem("singleValue")
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -64,10 +61,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenHandleOnErrorResumeFunc_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.onErrorResumeNext(throwable -> {
|
||||
return Observable.just(throwable.getMessage(), "nextValue");
|
||||
})
|
||||
@ -81,11 +78,11 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenChangeStateOnError_thenErrorThrown() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
final AtomicBoolean state = new AtomicBoolean(false);
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.doOnError(throwable -> state.set(true))
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -97,10 +94,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenExceptionOccurOnError_thenCompositeExceptionThrown() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.doOnError(throwable -> {
|
||||
throw new RuntimeException("unexcepted");
|
||||
})
|
||||
@ -113,10 +110,10 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndException_whenHandleOnException_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_EXCEPTION)
|
||||
.<String>error(UNKNOWN_EXCEPTION)
|
||||
.onExceptionResumeNext(Observable.just("exceptionResumed"))
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -128,14 +125,14 @@ public class ExceptionHandlingTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenHandleOnException_thenNotResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.onExceptionResumeNext(Observable.just("exceptionResumed"))
|
||||
.subscribe(testObserver);
|
||||
|
||||
testObserver.assertError(UNKNOWN_ERROR);
|
||||
testObserver.assertNotComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,20 +9,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class OnErrorRetryTest {
|
||||
|
||||
private Error UNKNOWN_ERROR = new Error("unknown error");
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryOnError_thenRetryConfirmed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
Observable
|
||||
.error(() -> {
|
||||
.<String>error(() -> {
|
||||
atomicCounter.incrementAndGet();
|
||||
return UNKNOWN_ERROR;
|
||||
})
|
||||
@ -37,12 +34,12 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryConditionallyOnError_thenRetryConfirmed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
|
||||
AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
Observable
|
||||
.error(() -> {
|
||||
.<String>error(() -> {
|
||||
atomicCounter.incrementAndGet();
|
||||
return UNKNOWN_ERROR;
|
||||
})
|
||||
@ -57,11 +54,11 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryUntilOnError_thenRetryConfirmed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.retryUntil(() -> atomicCounter.incrementAndGet() > 3)
|
||||
.subscribe(testObserver);
|
||||
|
||||
@ -73,12 +70,12 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryWhenOnError_thenRetryConfirmed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
Exception noretryException = new Exception("don't retry");
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.retryWhen(throwableObservable -> Observable.error(noretryException))
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.retryWhen(throwableObservable -> Observable.<String>error(noretryException))
|
||||
.subscribe(testObserver);
|
||||
|
||||
testObserver.assertError(noretryException);
|
||||
@ -88,11 +85,11 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryWhenOnError_thenCompleted() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
Observable
|
||||
.error(() -> {
|
||||
.<String>error(() -> {
|
||||
atomicCounter.incrementAndGet();
|
||||
return UNKNOWN_ERROR;
|
||||
})
|
||||
@ -107,11 +104,11 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryWhenOnError_thenResubscribed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
AtomicInteger atomicCounter = new AtomicInteger(0);
|
||||
|
||||
Observable
|
||||
.error(() -> {
|
||||
.<String>error(() -> {
|
||||
atomicCounter.incrementAndGet();
|
||||
return UNKNOWN_ERROR;
|
||||
})
|
||||
@ -126,11 +123,11 @@ public class OnErrorRetryTest {
|
||||
|
||||
@Test
|
||||
public void givenSubscriberAndError_whenRetryWhenForMultipleTimesOnError_thenResumed() {
|
||||
TestObserver testObserver = new TestObserver();
|
||||
TestObserver<String> testObserver = new TestObserver<>();
|
||||
long before = System.currentTimeMillis();
|
||||
|
||||
Observable
|
||||
.error(UNKNOWN_ERROR)
|
||||
.<String>error(UNKNOWN_ERROR)
|
||||
.retryWhen(throwableObservable -> throwableObservable
|
||||
.zipWith(Observable.range(1, 3), (throwable, integer) -> integer)
|
||||
.flatMap(integer -> {
|
||||
|
@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerRequestUnitTest {
|
||||
public class ExamplePostControllerRequestIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
@ -23,7 +23,7 @@ import org.baeldung.config.MainApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerResponseUnitTest {
|
||||
public class ExamplePostControllerResponseIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
@ -1,33 +0,0 @@
|
||||
package com.baeldung.web.log.test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import com.baeldung.web.log.data.TaxiRide;
|
||||
|
||||
public class TestTaxiFareController {
|
||||
|
||||
private static final String URL = "http://localhost:" + 8082 + "/spring-rest/taxifare/";
|
||||
|
||||
@Test
|
||||
public void givenRequest_whenFetchTaxiFareRateCard_thanOK() {
|
||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||
ResponseEntity<String> response = testRestTemplate.getForEntity(URL + "get/", String.class);
|
||||
|
||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTaxiRide_whenCalculatedFare_thanStatus200() {
|
||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||
TaxiRide taxiRide = new TaxiRide(true, 10l);
|
||||
String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide, String.class);
|
||||
|
||||
assertThat(fare, equalTo("200"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user