| 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.flowable; |
| 15 |
|
| 16 |
import static org.mockito.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.*; |
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 24 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
public class FlowableCastTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void cast() { |
| 31 |
Flowable<?> source = Flowable.just(1, 2); |
| 32 |
Flowable<Integer> flowable = source.cast(Integer.class); |
| 33 |
|
| 34 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 35 |
|
| 36 |
flowable.subscribe(subscriber); |
| 37 |
|
| 38 |
verify(subscriber, times(1)).onNext(1); |
| 39 |
verify(subscriber, times(1)).onNext(1); |
| 40 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 41 |
verify(subscriber, times(1)).onComplete(); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void castWithWrongType() { |
| 46 |
Flowable<?> source = Flowable.just(1, 2); |
| 47 |
Flowable<Boolean> flowable = source.cast(Boolean.class); |
| 48 |
|
| 49 |
Subscriber<Boolean> subscriber = TestHelper.mockSubscriber(); |
| 50 |
|
| 51 |
flowable.subscribe(subscriber); |
| 52 |
|
| 53 |
verify(subscriber, times(1)).onError(any(ClassCastException.class)); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void castCrashUnsubscribes() { |
| 58 |
|
| 59 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 60 |
|
| 61 |
TestSubscriber<String> ts = TestSubscriber.create(); |
| 62 |
|
| 63 |
pp.cast(String.class).subscribe(ts); |
| 64 |
|
| 65 |
Assert.assertTrue("Not subscribed?", pp.hasSubscribers()); |
| 66 |
|
| 67 |
pp.onNext(1); |
| 68 |
|
| 69 |
Assert.assertFalse("Subscribed?", pp.hasSubscribers()); |
| 70 |
|
| 71 |
ts.assertError(ClassCastException.class); |
| 72 |
} |
| 73 |
} |