| 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.io.IOException; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class MaybeCreateTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void callbackThrows() { |
| 33 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 34 |
@Override |
| 35 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 36 |
throw new TestException(); |
| 37 |
} |
| 38 |
}) |
| 39 |
.test() |
| 40 |
.assertFailure(TestException.class); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void onSuccessNull() { |
| 45 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 46 |
@Override |
| 47 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 48 |
e.onSuccess(null); |
| 49 |
} |
| 50 |
}) |
| 51 |
.test() |
| 52 |
.assertFailure(NullPointerException.class); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void onErrorNull() { |
| 57 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 58 |
@Override |
| 59 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 60 |
e.onError(null); |
| 61 |
} |
| 62 |
}) |
| 63 |
.test() |
| 64 |
.assertFailure(NullPointerException.class); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void dispose() { |
| 69 |
TestHelper.checkDisposed(Maybe.create(new MaybeOnSubscribe<Object>() { |
| 70 |
@Override |
| 71 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 72 |
e.onSuccess(1); |
| 73 |
} |
| 74 |
})); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void onSuccessThrows() { |
| 79 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 80 |
@Override |
| 81 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 82 |
Disposable d = Disposable.empty(); |
| 83 |
e.setDisposable(d); |
| 84 |
|
| 85 |
try { |
| 86 |
e.onSuccess(1); |
| 87 |
fail("Should have thrown"); |
| 88 |
} catch (TestException ex) { |
| 89 |
// expected |
| 90 |
} |
| 91 |
|
| 92 |
assertTrue(d.isDisposed()); |
| 93 |
assertTrue(e.isDisposed()); |
| 94 |
} |
| 95 |
}).subscribe(new MaybeObserver<Object>() { |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSubscribe(Disposable d) { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onSuccess(Object value) { |
| 104 |
throw new TestException(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onError(Throwable e) { |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onComplete() { |
| 114 |
|
| 115 |
} |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void onErrorThrows() { |
| 121 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 122 |
@Override |
| 123 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 124 |
Disposable d = Disposable.empty(); |
| 125 |
e.setDisposable(d); |
| 126 |
|
| 127 |
try { |
| 128 |
e.onError(new IOException()); |
| 129 |
fail("Should have thrown"); |
| 130 |
} catch (TestException ex) { |
| 131 |
// expected |
| 132 |
} |
| 133 |
|
| 134 |
assertTrue(d.isDisposed()); |
| 135 |
assertTrue(e.isDisposed()); |
| 136 |
} |
| 137 |
}).subscribe(new MaybeObserver<Object>() { |
| 138 |
|
| 139 |
@Override |
| 140 |
public void onSubscribe(Disposable d) { |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onSuccess(Object value) { |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onError(Throwable e) { |
| 151 |
throw new TestException(); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onComplete() { |
| 156 |
|
| 157 |
} |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void onCompleteThrows() { |
| 163 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 164 |
@Override |
| 165 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 166 |
Disposable d = Disposable.empty(); |
| 167 |
e.setDisposable(d); |
| 168 |
|
| 169 |
try { |
| 170 |
e.onComplete(); |
| 171 |
fail("Should have thrown"); |
| 172 |
} catch (TestException ex) { |
| 173 |
// expected |
| 174 |
} |
| 175 |
|
| 176 |
assertTrue(d.isDisposed()); |
| 177 |
assertTrue(e.isDisposed()); |
| 178 |
} |
| 179 |
}).subscribe(new MaybeObserver<Object>() { |
| 180 |
|
| 181 |
@Override |
| 182 |
public void onSubscribe(Disposable d) { |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public void onSuccess(Object value) { |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void onError(Throwable e) { |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public void onComplete() { |
| 198 |
throw new TestException(); |
| 199 |
} |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
@Test |
| 204 |
public void onSuccessThrows2() { |
| 205 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 206 |
@Override |
| 207 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 208 |
try { |
| 209 |
e.onSuccess(1); |
| 210 |
fail("Should have thrown"); |
| 211 |
} catch (TestException ex) { |
| 212 |
// expected |
| 213 |
} |
| 214 |
|
| 215 |
assertTrue(e.isDisposed()); |
| 216 |
} |
| 217 |
}).subscribe(new MaybeObserver<Object>() { |
| 218 |
|
| 219 |
@Override |
| 220 |
public void onSubscribe(Disposable d) { |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void onSuccess(Object value) { |
| 226 |
throw new TestException(); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void onError(Throwable e) { |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public void onComplete() { |
| 236 |
|
| 237 |
} |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void onErrorThrows2() { |
| 243 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 244 |
@Override |
| 245 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 246 |
try { |
| 247 |
e.onError(new IOException()); |
| 248 |
fail("Should have thrown"); |
| 249 |
} catch (TestException ex) { |
| 250 |
// expected |
| 251 |
} |
| 252 |
|
| 253 |
assertTrue(e.isDisposed()); |
| 254 |
} |
| 255 |
}).subscribe(new MaybeObserver<Object>() { |
| 256 |
|
| 257 |
@Override |
| 258 |
public void onSubscribe(Disposable d) { |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public void onSuccess(Object value) { |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public void onError(Throwable e) { |
| 269 |
throw new TestException(); |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public void onComplete() { |
| 274 |
|
| 275 |
} |
| 276 |
}); |
| 277 |
} |
| 278 |
|
| 279 |
@Test |
| 280 |
public void onCompleteThrows2() { |
| 281 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 282 |
@Override |
| 283 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 284 |
try { |
| 285 |
e.onComplete(); |
| 286 |
fail("Should have thrown"); |
| 287 |
} catch (TestException ex) { |
| 288 |
// expected |
| 289 |
} |
| 290 |
|
| 291 |
assertTrue(e.isDisposed()); |
| 292 |
} |
| 293 |
}).subscribe(new MaybeObserver<Object>() { |
| 294 |
|
| 295 |
@Override |
| 296 |
public void onSubscribe(Disposable d) { |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public void onSuccess(Object value) { |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public void onError(Throwable e) { |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
public void onComplete() { |
| 312 |
throw new TestException(); |
| 313 |
} |
| 314 |
}); |
| 315 |
} |
| 316 |
|
| 317 |
@Test |
| 318 |
public void tryOnError() { |
| 319 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 320 |
try { |
| 321 |
final Boolean[] response = { null }; |
| 322 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 323 |
@Override |
| 324 |
public void subscribe(MaybeEmitter<Object> e) throws Exception { |
| 325 |
e.onSuccess(1); |
| 326 |
response[0] = e.tryOnError(new TestException()); |
| 327 |
} |
| 328 |
}) |
| 329 |
.test() |
| 330 |
.assertResult(1); |
| 331 |
|
| 332 |
assertFalse(response[0]); |
| 333 |
|
| 334 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 335 |
} finally { |
| 336 |
RxJavaPlugins.reset(); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
@Test |
| 341 |
public void emitterHasToString() { |
| 342 |
Maybe.create(new MaybeOnSubscribe<Object>() { |
| 343 |
@Override |
| 344 |
public void subscribe(MaybeEmitter<Object> emitter) throws Exception { |
| 345 |
assertTrue(emitter.toString().contains(MaybeCreate.Emitter.class.getSimpleName())); |
| 346 |
} |
| 347 |
}).test().assertEmpty(); |
| 348 |
} |
| 349 |
} |