| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.disposables.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.Cancellable; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class MaybeCreateTest extends RxJavaTest { |
| 30 |
@Test |
| 31 |
public void basic() { |
| 32 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 33 |
try { |
| 34 |
final Disposable d = Disposable.empty(); |
| 35 |
|
| 36 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 37 |
@Override |
| 38 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 39 |
e.setDisposable(d); |
| 40 |
|
| 41 |
e.onSuccess(1); |
| 42 |
e.onError(new TestException()); |
| 43 |
e.onSuccess(2); |
| 44 |
e.onError(new TestException()); |
| 45 |
} |
| 46 |
}).test().assertResult(1); |
| 47 |
|
| 48 |
assertTrue(d.isDisposed()); |
| 49 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 50 |
} finally { |
| 51 |
RxJavaPlugins.reset(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void basicWithCancellable() { |
| 57 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 58 |
try { |
| 59 |
final Disposable d1 = Disposable.empty(); |
| 60 |
final Disposable d2 = Disposable.empty(); |
| 61 |
|
| 62 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 63 |
@Override |
| 64 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 65 |
e.setDisposable(d1); |
| 66 |
e.setCancellable(new Cancellable() { |
| 67 |
@Override |
| 68 |
public void cancel() throws Exception { |
| 69 |
d2.dispose(); |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
e.onSuccess(1); |
| 74 |
e.onError(new TestException()); |
| 75 |
e.onSuccess(2); |
| 76 |
e.onError(new TestException()); |
| 77 |
} |
| 78 |
}).test().assertResult(1); |
| 79 |
|
| 80 |
assertTrue(d1.isDisposed()); |
| 81 |
assertTrue(d2.isDisposed()); |
| 82 |
|
| 83 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 84 |
} finally { |
| 85 |
RxJavaPlugins.reset(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void basicWithError() { |
| 91 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 92 |
try { |
| 93 |
final Disposable d = Disposable.empty(); |
| 94 |
|
| 95 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 96 |
@Override |
| 97 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 98 |
e.setDisposable(d); |
| 99 |
|
| 100 |
e.onError(new TestException()); |
| 101 |
e.onSuccess(2); |
| 102 |
e.onError(new TestException()); |
| 103 |
} |
| 104 |
}).test().assertFailure(TestException.class); |
| 105 |
|
| 106 |
assertTrue(d.isDisposed()); |
| 107 |
|
| 108 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 109 |
} finally { |
| 110 |
RxJavaPlugins.reset(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void basicWithCompletion() { |
| 116 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 117 |
try { |
| 118 |
final Disposable d = Disposable.empty(); |
| 119 |
|
| 120 |
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() { |
| 121 |
@Override |
| 122 |
public void subscribe(MaybeEmitter<Integer> e) throws Exception { |
| 123 |
e.setDisposable(d); |
| 124 |
|
| 125 |
e.onComplete(); |
| 126 |
e.onSuccess(2); |
| 127 |
e.onError(new TestException()); |
| 128 |
} |
| 129 |
}).test().assertComplete(); |
| 130 |
|
| 131 |
assertTrue(d.isDisposed()); |
| 132 |
|
| 133 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 134 |
} finally { |
| 135 |
RxJavaPlugins.reset(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Test(expected = IllegalArgumentException.class) |
| 140 |
public void unsafeCreate() { |
| 141 |
Maybe.unsafeCreate(Maybe.just(1)); |
| 142 |
} |
| 143 |
} |