| 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.mockito.Mockito.*; |
| 17 |
import org.junit.Test; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 21 |
import io.reactivex.rxjava3.functions.Function; |
| 22 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 23 |
import io.reactivex.rxjava3.subjects.MaybeSubject; |
| 24 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 25 |
|
| 26 |
public class MaybeDematerializeTest extends RxJavaTest { |
| 27 |
|
| 28 |
@Test |
| 29 |
public void success() { |
| 30 |
Maybe.just(Notification.createOnNext(1)) |
| 31 |
.dematerialize(Functions.<Notification<Integer>>identity()) |
| 32 |
.test() |
| 33 |
.assertResult(1); |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
public void empty() { |
| 38 |
Maybe.just(Notification.<Integer>createOnComplete()) |
| 39 |
.dematerialize(Functions.<Notification<Integer>>identity()) |
| 40 |
.test() |
| 41 |
.assertResult(); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void emptySource() throws Throwable { |
| 46 |
@SuppressWarnings("unchecked") |
| 47 |
Function<Notification<Integer>, Notification<Integer>> function = mock(Function.class); |
| 48 |
|
| 49 |
Maybe.<Notification<Integer>>empty() |
| 50 |
.dematerialize(function) |
| 51 |
.test() |
| 52 |
.assertResult(); |
| 53 |
|
| 54 |
verify(function, never()).apply(any()); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void error() { |
| 59 |
Maybe.<Notification<Integer>>error(new TestException()) |
| 60 |
.dematerialize(Functions.<Notification<Integer>>identity()) |
| 61 |
.test() |
| 62 |
.assertFailure(TestException.class); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void errorNotification() { |
| 67 |
Maybe.just(Notification.<Integer>createOnError(new TestException())) |
| 68 |
.dematerialize(Functions.<Notification<Integer>>identity()) |
| 69 |
.test() |
| 70 |
.assertFailure(TestException.class); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void doubleOnSubscribe() { |
| 75 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 76 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 77 |
@Override |
| 78 |
public MaybeSource<Object> apply(Maybe<Object> v) throws Exception { |
| 79 |
return v.dematerialize((Function)Functions.identity()); |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void dispose() { |
| 86 |
TestHelper.checkDisposed(MaybeSubject.<Notification<Integer>>create().dematerialize(Functions.<Notification<Integer>>identity())); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void selectorCrash() { |
| 91 |
Maybe.just(Notification.createOnNext(1)) |
| 92 |
.dematerialize(new Function<Notification<Integer>, Notification<Integer>>() { |
| 93 |
@Override |
| 94 |
public Notification<Integer> apply(Notification<Integer> v) throws Exception { |
| 95 |
throw new TestException(); |
| 96 |
} |
| 97 |
}) |
| 98 |
.test() |
| 99 |
.assertFailure(TestException.class); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void selectorNull() { |
| 104 |
Maybe.just(Notification.createOnNext(1)) |
| 105 |
.dematerialize(Functions.justFunction((Notification<Integer>)null)) |
| 106 |
.test() |
| 107 |
.assertFailure(NullPointerException.class); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void selectorDifferentType() { |
| 112 |
Maybe.just(Notification.createOnNext(1)) |
| 113 |
.dematerialize(new Function<Notification<Integer>, Notification<String>>() { |
| 114 |
@Override |
| 115 |
public Notification<String> apply(Notification<Integer> v) throws Exception { |
| 116 |
return Notification.createOnNext("Value-" + 1); |
| 117 |
} |
| 118 |
}) |
| 119 |
.test() |
| 120 |
.assertResult("Value-1"); |
| 121 |
} |
| 122 |
} |