parent
c8ee65b1d2
commit
9f2f10dfa2
|
@ -1,5 +1,8 @@
|
|||
package com.baeldung.rxjava;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.reactivex.Completable;
|
||||
|
@ -14,203 +17,190 @@ import io.reactivex.schedulers.Schedulers;
|
|||
|
||||
public class RxJavaHooksUnitTest {
|
||||
|
||||
private boolean initHookCalled = false;
|
||||
private boolean hookCalled = false;
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenError_shouldExecuteTheHook() {
|
||||
RxJavaPlugins.setErrorHandler(throwable -> {
|
||||
hookCalled = true;
|
||||
});
|
||||
|
||||
Observable.error(new IllegalStateException())
|
||||
.subscribe();
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompletable_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnCompletableAssembly(completable -> {
|
||||
System.out.println("Assembling Completable");
|
||||
hookCalled = true;
|
||||
return completable;
|
||||
});
|
||||
Completable.fromSingle(Single.just(1));
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompletable_whenSubscribed_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnCompletableSubscribe((completable, observer) -> {
|
||||
System.out.println("Subscribing to Completable");
|
||||
hookCalled = true;
|
||||
return observer;
|
||||
});
|
||||
|
||||
Completable.fromSingle(Single.just(1))
|
||||
.test(true);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
.test();
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnObservableAssembly(observable -> {
|
||||
System.out.println("Assembling Observable");
|
||||
hookCalled = true;
|
||||
return observable;
|
||||
});
|
||||
|
||||
Observable.range(1, 10);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenSubscribed_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnObservableSubscribe((observable, observer) -> {
|
||||
System.out.println("Suscribing to Observable");
|
||||
hookCalled = true;
|
||||
return observer;
|
||||
});
|
||||
|
||||
Observable.range(1, 10)
|
||||
.test(true);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
.test();
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConnectableObservable_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnConnectableObservableAssembly(connectableObservable -> {
|
||||
System.out.println("Assembling ConnectableObservable");
|
||||
hookCalled = true;
|
||||
return connectableObservable;
|
||||
});
|
||||
|
||||
ConnectableObservable.range(1, 10)
|
||||
.publish()
|
||||
.connect();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFlowable_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnFlowableAssembly(flowable -> {
|
||||
System.out.println("Assembling Flowable");
|
||||
hookCalled = true;
|
||||
return flowable;
|
||||
});
|
||||
|
||||
Flowable.range(1, 10);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFlowable_whenSubscribed_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnFlowableSubscribe((flowable, observer) -> {
|
||||
System.out.println("Suscribing to Flowable");
|
||||
hookCalled = true;
|
||||
return observer;
|
||||
});
|
||||
|
||||
Flowable.range(1, 10)
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConnectableFlowable_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnConnectableFlowableAssembly(connectableFlowable -> {
|
||||
System.out.println("Assembling ConnectableFlowable");
|
||||
hookCalled = true;
|
||||
return connectableFlowable;
|
||||
});
|
||||
|
||||
ConnectableFlowable.range(1, 10)
|
||||
.publish()
|
||||
.connect();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParallel_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnParallelAssembly(parallelFlowable -> {
|
||||
System.out.println("Assembling ParallelFlowable");
|
||||
hookCalled = true;
|
||||
return parallelFlowable;
|
||||
});
|
||||
|
||||
Flowable.range(1, 10)
|
||||
.parallel();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMaybe_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnMaybeAssembly(maybe -> {
|
||||
System.out.println("Assembling Maybe");
|
||||
hookCalled = true;
|
||||
return maybe;
|
||||
});
|
||||
|
||||
Maybe.just(1);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMaybe_whenSubscribed_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnMaybeSubscribe((maybe, observer) -> {
|
||||
System.out.println("Suscribing to Maybe");
|
||||
hookCalled = true;
|
||||
return observer;
|
||||
});
|
||||
|
||||
Maybe.just(1)
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingle_whenAssembled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setOnSingleAssembly(single -> {
|
||||
System.out.println("Assembling Single");
|
||||
hookCalled = true;
|
||||
return single;
|
||||
});
|
||||
|
||||
Single.just(1);
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingle_whenSubscribed_shouldExecuteTheHook() {
|
||||
|
||||
try {
|
||||
RxJavaPlugins.setOnSingleSubscribe((single, observer) -> {
|
||||
System.out.println("Suscribing to Single");
|
||||
hookCalled = true;
|
||||
return observer;
|
||||
});
|
||||
|
||||
Single.just(1)
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnyScheduler_whenCalled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setScheduleHandler((runnable) -> {
|
||||
System.out.println("Executing Scheduler");
|
||||
hookCalled = true;
|
||||
return runnable;
|
||||
});
|
||||
|
||||
|
@ -218,25 +208,23 @@ public class RxJavaHooksUnitTest {
|
|||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.single())
|
||||
.test();
|
||||
|
||||
hookCalled = false;
|
||||
Observable.range(1, 10)
|
||||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComputationScheduler_whenCalled_shouldExecuteTheHooks() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setInitComputationSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Initializing Computation Scheduler");
|
||||
initHookCalled = true;
|
||||
return scheduler.call();
|
||||
});
|
||||
RxJavaPlugins.setComputationSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Executing Computation Scheduler");
|
||||
hookCalled = true;
|
||||
return scheduler;
|
||||
});
|
||||
|
||||
|
@ -244,21 +232,19 @@ public class RxJavaHooksUnitTest {
|
|||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled && initHookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIOScheduler_whenCalled_shouldExecuteTheHooks() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setInitIoSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Initializing IO Scheduler");
|
||||
initHookCalled = true;
|
||||
return scheduler.call();
|
||||
});
|
||||
|
||||
RxJavaPlugins.setIoSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Executing IO Scheduler");
|
||||
hookCalled = true;
|
||||
return scheduler;
|
||||
});
|
||||
|
||||
|
@ -266,21 +252,19 @@ public class RxJavaHooksUnitTest {
|
|||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.test();
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled && initHookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewThreadScheduler_whenCalled_shouldExecuteTheHook() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setInitNewThreadSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Initializing newThread Scheduler");
|
||||
initHookCalled = true;
|
||||
return scheduler.call();
|
||||
});
|
||||
|
||||
RxJavaPlugins.setNewThreadSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Executing newThread Scheduler");
|
||||
hookCalled = true;
|
||||
return scheduler;
|
||||
});
|
||||
|
||||
|
@ -288,22 +272,19 @@ public class RxJavaHooksUnitTest {
|
|||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.newThread())
|
||||
.test();
|
||||
|
||||
} finally {
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
assertTrue(hookCalled && initHookCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleScheduler_whenCalled_shouldExecuteTheHooks() {
|
||||
try {
|
||||
|
||||
RxJavaPlugins.setInitSingleSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Initializing Single Scheduler");
|
||||
initHookCalled = true;
|
||||
return scheduler.call();
|
||||
});
|
||||
|
||||
RxJavaPlugins.setSingleSchedulerHandler((scheduler) -> {
|
||||
System.out.println("Executing Single Scheduler");
|
||||
hookCalled = true;
|
||||
return scheduler;
|
||||
});
|
||||
|
||||
|
@ -311,19 +292,14 @@ public class RxJavaHooksUnitTest {
|
|||
.map(v -> v * 2)
|
||||
.subscribeOn(Schedulers.single())
|
||||
.test();
|
||||
assertTrue(hookCalled && initHookCalled);
|
||||
|
||||
} finally {
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() {
|
||||
initHookCalled = false;
|
||||
hookCalled = false;
|
||||
RxJavaPlugins.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenError_shouldExecuteTheHook() {
|
||||
RxJavaPlugins.setErrorHandler(throwable -> {
|
||||
System.out.println("Handling error" + throwable.getCause());
|
||||
});
|
||||
|
||||
Observable.error(new IllegalStateException())
|
||||
.subscribe();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue