| 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.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 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.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class MaybeDoAfterSuccessTest extends RxJavaTest { |
| 32 |
|
| 33 |
final List<Integer> values = new ArrayList<>(); |
| 34 |
|
| 35 |
final Consumer<Integer> afterSuccess = new Consumer<Integer>() { |
| 36 |
@Override |
| 37 |
public void accept(Integer e) throws Exception { |
| 38 |
values.add(-e); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
final TestObserver<Integer> to = new TestObserver<Integer>() { |
| 43 |
@Override |
| 44 |
public void onNext(Integer t) { |
| 45 |
super.onNext(t); |
| 46 |
MaybeDoAfterSuccessTest.this.values.add(t); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
@Test |
| 51 |
public void just() { |
| 52 |
Maybe.just(1) |
| 53 |
.doAfterSuccess(afterSuccess) |
| 54 |
.subscribeWith(to) |
| 55 |
.assertResult(1); |
| 56 |
|
| 57 |
assertEquals(Arrays.asList(1, -1), values); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void error() { |
| 62 |
Maybe.<Integer>error(new TestException()) |
| 63 |
.doAfterSuccess(afterSuccess) |
| 64 |
.subscribeWith(to) |
| 65 |
.assertFailure(TestException.class); |
| 66 |
|
| 67 |
assertTrue(values.isEmpty()); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void empty() { |
| 72 |
Maybe.<Integer>empty() |
| 73 |
.doAfterSuccess(afterSuccess) |
| 74 |
.subscribeWith(to) |
| 75 |
.assertResult(); |
| 76 |
|
| 77 |
assertTrue(values.isEmpty()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void justConditional() { |
| 82 |
Maybe.just(1) |
| 83 |
.doAfterSuccess(afterSuccess) |
| 84 |
.filter(Functions.alwaysTrue()) |
| 85 |
.subscribeWith(to) |
| 86 |
.assertResult(1); |
| 87 |
|
| 88 |
assertEquals(Arrays.asList(1, -1), values); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void errorConditional() { |
| 93 |
Maybe.<Integer>error(new TestException()) |
| 94 |
.doAfterSuccess(afterSuccess) |
| 95 |
.filter(Functions.alwaysTrue()) |
| 96 |
.subscribeWith(to) |
| 97 |
.assertFailure(TestException.class); |
| 98 |
|
| 99 |
assertTrue(values.isEmpty()); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void emptyConditional() { |
| 104 |
Maybe.<Integer>empty() |
| 105 |
.doAfterSuccess(afterSuccess) |
| 106 |
.filter(Functions.alwaysTrue()) |
| 107 |
.subscribeWith(to) |
| 108 |
.assertResult(); |
| 109 |
|
| 110 |
assertTrue(values.isEmpty()); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void consumerThrows() { |
| 115 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 116 |
try { |
| 117 |
Maybe.just(1) |
| 118 |
.doAfterSuccess(new Consumer<Integer>() { |
| 119 |
@Override |
| 120 |
public void accept(Integer e) throws Exception { |
| 121 |
throw new TestException(); |
| 122 |
} |
| 123 |
}) |
| 124 |
.test() |
| 125 |
.assertResult(1); |
| 126 |
|
| 127 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 128 |
} finally { |
| 129 |
RxJavaPlugins.reset(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void dispose() { |
| 135 |
TestHelper.checkDisposed(PublishSubject.<Integer>create().singleElement().doAfterSuccess(afterSuccess)); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void doubleOnSubscribe() { |
| 140 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, MaybeSource<Integer>>() { |
| 141 |
@Override |
| 142 |
public MaybeSource<Integer> apply(Maybe<Integer> m) throws Exception { |
| 143 |
return m.doAfterSuccess(afterSuccess); |
| 144 |
} |
| 145 |
}); |
| 146 |
} |
| 147 |
} |