| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
|
| 26 |
public class FlowableDoOnTest extends RxJavaTest { |
| 27 |
|
| 28 |
@Test |
| 29 |
public void doOnEach() { |
| 30 |
final AtomicReference<String> r = new AtomicReference<>(); |
| 31 |
String output = Flowable.just("one").doOnNext(new Consumer<String>() { |
| 32 |
@Override |
| 33 |
public void accept(String v) { |
| 34 |
r.set(v); |
| 35 |
} |
| 36 |
}).blockingSingle(); |
| 37 |
|
| 38 |
assertEquals("one", output); |
| 39 |
assertEquals("one", r.get()); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void doOnError() { |
| 44 |
final AtomicReference<Throwable> r = new AtomicReference<>(); |
| 45 |
Throwable t = null; |
| 46 |
try { |
| 47 |
Flowable.<String> error(new RuntimeException("an error")) |
| 48 |
.doOnError(new Consumer<Throwable>() { |
| 49 |
@Override |
| 50 |
public void accept(Throwable v) { |
| 51 |
r.set(v); |
| 52 |
} |
| 53 |
}).blockingSingle(); |
| 54 |
fail("expected exception, not a return value"); |
| 55 |
} catch (Throwable e) { |
| 56 |
t = e; |
| 57 |
} |
| 58 |
|
| 59 |
assertNotNull(t); |
| 60 |
assertEquals(t, r.get()); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void doOnCompleted() { |
| 65 |
final AtomicBoolean r = new AtomicBoolean(); |
| 66 |
String output = Flowable.just("one").doOnComplete(new Action() { |
| 67 |
@Override |
| 68 |
public void run() { |
| 69 |
r.set(true); |
| 70 |
} |
| 71 |
}).blockingSingle(); |
| 72 |
|
| 73 |
assertEquals("one", output); |
| 74 |
assertTrue(r.get()); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void doOnTerminateError() { |
| 79 |
final AtomicBoolean r = new AtomicBoolean(); |
| 80 |
Flowable.<String>error(new TestException()).doOnTerminate(new Action() { |
| 81 |
@Override |
| 82 |
public void run() { |
| 83 |
r.set(true); |
| 84 |
} |
| 85 |
}) |
| 86 |
.test() |
| 87 |
.assertFailure(TestException.class); |
| 88 |
assertTrue(r.get()); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void doOnTerminateComplete() { |
| 93 |
final AtomicBoolean r = new AtomicBoolean(); |
| 94 |
String output = Flowable.just("one").doOnTerminate(new Action() { |
| 95 |
@Override |
| 96 |
public void run() { |
| 97 |
r.set(true); |
| 98 |
} |
| 99 |
}).blockingSingle(); |
| 100 |
|
| 101 |
assertEquals("one", output); |
| 102 |
assertTrue(r.get()); |
| 103 |
|
| 104 |
} |
| 105 |
} |