| 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.assertEquals; |
| 17 |
|
| 18 |
import java.util.List; |
| 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 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class MaybeDoFinallyTest extends RxJavaTest implements Action { |
| 31 |
|
| 32 |
int calls; |
| 33 |
|
| 34 |
@Override |
| 35 |
public void run() throws Exception { |
| 36 |
calls++; |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void normalJust() { |
| 41 |
Maybe.just(1) |
| 42 |
.doFinally(this) |
| 43 |
.test() |
| 44 |
.assertResult(1); |
| 45 |
|
| 46 |
assertEquals(1, calls); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void normalEmpty() { |
| 51 |
Maybe.empty() |
| 52 |
.doFinally(this) |
| 53 |
.test() |
| 54 |
.assertResult(); |
| 55 |
|
| 56 |
assertEquals(1, calls); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void normalError() { |
| 61 |
Maybe.error(new TestException()) |
| 62 |
.doFinally(this) |
| 63 |
.test() |
| 64 |
.assertFailure(TestException.class); |
| 65 |
|
| 66 |
assertEquals(1, calls); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void doubleOnSubscribe() { |
| 71 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, Maybe<Object>>() { |
| 72 |
@Override |
| 73 |
public Maybe<Object> apply(Maybe<Object> f) throws Exception { |
| 74 |
return f.doFinally(MaybeDoFinallyTest.this); |
| 75 |
} |
| 76 |
}); |
| 77 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, Maybe<Object>>() { |
| 78 |
@Override |
| 79 |
public Maybe<Object> apply(Maybe<Object> f) throws Exception { |
| 80 |
return f.doFinally(MaybeDoFinallyTest.this).filter(Functions.alwaysTrue()); |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void normalJustConditional() { |
| 87 |
Maybe.just(1) |
| 88 |
.doFinally(this) |
| 89 |
.filter(Functions.alwaysTrue()) |
| 90 |
.test() |
| 91 |
.assertResult(1); |
| 92 |
|
| 93 |
assertEquals(1, calls); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void normalEmptyConditional() { |
| 98 |
Maybe.empty() |
| 99 |
.doFinally(this) |
| 100 |
.filter(Functions.alwaysTrue()) |
| 101 |
.test() |
| 102 |
.assertResult(); |
| 103 |
|
| 104 |
assertEquals(1, calls); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void normalErrorConditional() { |
| 109 |
Maybe.error(new TestException()) |
| 110 |
.doFinally(this) |
| 111 |
.filter(Functions.alwaysTrue()) |
| 112 |
.test() |
| 113 |
.assertFailure(TestException.class); |
| 114 |
|
| 115 |
assertEquals(1, calls); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void actionThrows() { |
| 120 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 121 |
try { |
| 122 |
Maybe.just(1) |
| 123 |
.doFinally(new Action() { |
| 124 |
@Override |
| 125 |
public void run() throws Exception { |
| 126 |
throw new TestException(); |
| 127 |
} |
| 128 |
}) |
| 129 |
.test() |
| 130 |
.assertResult(1) |
| 131 |
.dispose(); |
| 132 |
|
| 133 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 134 |
} finally { |
| 135 |
RxJavaPlugins.reset(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void actionThrowsConditional() { |
| 141 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 142 |
try { |
| 143 |
Maybe.just(1) |
| 144 |
.doFinally(new Action() { |
| 145 |
@Override |
| 146 |
public void run() throws Exception { |
| 147 |
throw new TestException(); |
| 148 |
} |
| 149 |
}) |
| 150 |
.filter(Functions.alwaysTrue()) |
| 151 |
.test() |
| 152 |
.assertResult(1) |
| 153 |
.dispose(); |
| 154 |
|
| 155 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 156 |
} finally { |
| 157 |
RxJavaPlugins.reset(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void disposed() { |
| 163 |
TestHelper.checkDisposed(PublishSubject.create().singleElement().doFinally(this)); |
| 164 |
} |
| 165 |
} |