| 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 java.io.IOException; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 22 |
import io.reactivex.rxjava3.functions.*; |
| 23 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 24 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
public class MaybeOnErrorXTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void onErrorReturnConst() { |
| 31 |
Maybe.error(new TestException()) |
| 32 |
.onErrorReturnItem(1) |
| 33 |
.test() |
| 34 |
.assertResult(1); |
| 35 |
} |
| 36 |
|
| 37 |
@Test |
| 38 |
public void onErrorReturn() { |
| 39 |
Maybe.error(new TestException()) |
| 40 |
.onErrorReturn(Functions.justFunction(1)) |
| 41 |
.test() |
| 42 |
.assertResult(1); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void onErrorComplete() { |
| 47 |
Maybe.error(new TestException()) |
| 48 |
.onErrorComplete() |
| 49 |
.test() |
| 50 |
.assertResult(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void onErrorCompleteTrue() { |
| 55 |
Maybe.error(new TestException()) |
| 56 |
.onErrorComplete(Functions.alwaysTrue()) |
| 57 |
.test() |
| 58 |
.assertResult(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void onErrorCompleteFalse() { |
| 63 |
Maybe.error(new TestException()) |
| 64 |
.onErrorComplete(Functions.alwaysFalse()) |
| 65 |
.test() |
| 66 |
.assertFailure(TestException.class); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void onErrorReturnFunctionThrows() { |
| 71 |
TestHelper.assertCompositeExceptions(Maybe.error(new TestException()) |
| 72 |
.onErrorReturn(new Function<Throwable, Object>() { |
| 73 |
@Override |
| 74 |
public Object apply(Throwable v) throws Exception { |
| 75 |
throw new IOException(); |
| 76 |
} |
| 77 |
}) |
| 78 |
.to(TestHelper.testConsumer()), TestException.class, IOException.class); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void onErrorCompletePredicateThrows() { |
| 83 |
TestHelper.assertCompositeExceptions(Maybe.error(new TestException()) |
| 84 |
.onErrorComplete(new Predicate<Throwable>() { |
| 85 |
@Override |
| 86 |
public boolean test(Throwable v) throws Exception { |
| 87 |
throw new IOException(); |
| 88 |
} |
| 89 |
}) |
| 90 |
.to(TestHelper.testConsumer()), TestException.class, IOException.class); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void onErrorResumeNext() { |
| 95 |
Maybe.error(new TestException()) |
| 96 |
.onErrorResumeNext(Functions.justFunction(Maybe.just(1))) |
| 97 |
.test() |
| 98 |
.assertResult(1); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void onErrorResumeNextFunctionThrows() { |
| 103 |
TestHelper.assertCompositeExceptions(Maybe.error(new TestException()) |
| 104 |
.onErrorResumeNext(new Function<Throwable, Maybe<Object>>() { |
| 105 |
@Override |
| 106 |
public Maybe<Object> apply(Throwable v) throws Exception { |
| 107 |
throw new IOException(); |
| 108 |
} |
| 109 |
}) |
| 110 |
.to(TestHelper.testConsumer()), TestException.class, IOException.class); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void onErrorReturnSuccess() { |
| 115 |
Maybe.just(1) |
| 116 |
.onErrorReturnItem(2) |
| 117 |
.test() |
| 118 |
.assertResult(1); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void onErrorReturnEmpty() { |
| 123 |
Maybe.<Integer>empty() |
| 124 |
.onErrorReturnItem(2) |
| 125 |
.test() |
| 126 |
.assertResult(); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void onErrorReturnDispose() { |
| 131 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().onErrorReturnItem(1)); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void onErrorReturnDoubleOnSubscribe() { |
| 136 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 137 |
@Override |
| 138 |
public MaybeSource<Object> apply(Maybe<Object> v) throws Exception { |
| 139 |
return v.onErrorReturnItem(1); |
| 140 |
} |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void onErrorCompleteSuccess() { |
| 146 |
Maybe.just(1) |
| 147 |
.onErrorComplete() |
| 148 |
.test() |
| 149 |
.assertResult(1); |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void onErrorCompleteEmpty() { |
| 154 |
Maybe.<Integer>empty() |
| 155 |
.onErrorComplete() |
| 156 |
.test() |
| 157 |
.assertResult(); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void onErrorCompleteDispose() { |
| 162 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().onErrorComplete()); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void onErrorCompleteDoubleOnSubscribe() { |
| 167 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 168 |
@Override |
| 169 |
public MaybeSource<Object> apply(Maybe<Object> v) throws Exception { |
| 170 |
return v.onErrorComplete(); |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void onErrorNextDispose() { |
| 177 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().onErrorResumeWith(Maybe.just(1))); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void onErrorNextDoubleOnSubscribe() { |
| 182 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 183 |
@Override |
| 184 |
public MaybeSource<Object> apply(Maybe<Object> v) throws Exception { |
| 185 |
return v.onErrorResumeWith(Maybe.just(1)); |
| 186 |
} |
| 187 |
}); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void onErrorNextIsAlsoError() { |
| 192 |
Maybe.error(new TestException("Main")) |
| 193 |
.onErrorResumeWith(Maybe.error(new TestException("Secondary"))) |
| 194 |
.to(TestHelper.testConsumer()) |
| 195 |
.assertFailureAndMessage(TestException.class, "Secondary"); |
| 196 |
} |
| 197 |
} |