| 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.internal.operators.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.Action; |
| 26 |
import io.reactivex.rxjava3.testsupport.*; |
| 27 |
|
| 28 |
public class MaybeDoOnTerminateTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void doOnTerminateSuccess() { |
| 32 |
final AtomicBoolean atomicBoolean = new AtomicBoolean(); |
| 33 |
Maybe.just(1).doOnTerminate(new Action() { |
| 34 |
@Override |
| 35 |
public void run() { |
| 36 |
atomicBoolean.set(true); |
| 37 |
} |
| 38 |
}) |
| 39 |
.test() |
| 40 |
.assertResult(1); |
| 41 |
|
| 42 |
assertTrue(atomicBoolean.get()); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void doOnTerminateError() { |
| 47 |
final AtomicBoolean atomicBoolean = new AtomicBoolean(); |
| 48 |
Maybe.error(new TestException()).doOnTerminate(new Action() { |
| 49 |
@Override |
| 50 |
public void run() { |
| 51 |
atomicBoolean.set(true); |
| 52 |
} |
| 53 |
}) |
| 54 |
.test() |
| 55 |
.assertFailure(TestException.class); |
| 56 |
|
| 57 |
assertTrue(atomicBoolean.get()); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void doOnTerminateComplete() { |
| 62 |
final AtomicBoolean atomicBoolean = new AtomicBoolean(); |
| 63 |
Maybe.empty().doOnTerminate(new Action() { |
| 64 |
@Override |
| 65 |
public void run() { |
| 66 |
atomicBoolean.set(true); |
| 67 |
} |
| 68 |
}) |
| 69 |
.test() |
| 70 |
.assertResult(); |
| 71 |
|
| 72 |
assertTrue(atomicBoolean.get()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void doOnTerminateSuccessCrash() { |
| 77 |
Maybe.just(1).doOnTerminate(new Action() { |
| 78 |
@Override |
| 79 |
public void run() { |
| 80 |
throw new TestException(); |
| 81 |
} |
| 82 |
}) |
| 83 |
.test() |
| 84 |
.assertFailure(TestException.class); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void doOnTerminateErrorCrash() { |
| 89 |
TestObserverEx<Object> to = Maybe.error(new TestException("Outer")) |
| 90 |
.doOnTerminate(new Action() { |
| 91 |
@Override |
| 92 |
public void run() { |
| 93 |
throw new TestException("Inner"); |
| 94 |
} |
| 95 |
}) |
| 96 |
.to(TestHelper.<Object>testConsumer()) |
| 97 |
.assertFailure(CompositeException.class); |
| 98 |
|
| 99 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 100 |
TestHelper.assertError(errors, 0, TestException.class, "Outer"); |
| 101 |
TestHelper.assertError(errors, 1, TestException.class, "Inner"); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void doOnTerminateCompleteCrash() { |
| 106 |
Maybe.empty() |
| 107 |
.doOnTerminate(new Action() { |
| 108 |
@Override |
| 109 |
public void run() { |
| 110 |
throw new TestException(); |
| 111 |
} |
| 112 |
}) |
| 113 |
.test() |
| 114 |
.assertFailure(TestException.class); |
| 115 |
} |
| 116 |
} |