| 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.testsupport; |
| 15 |
|
| 16 |
import org.junit.rules.TestRule; |
| 17 |
import org.junit.runner.Description; |
| 18 |
import org.junit.runners.model.Statement; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.exceptions.UndeliverableException; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* A rule for suppressing UndeliverableException handling. |
| 25 |
* |
| 26 |
* <p>Test classes that use this rule can suppress UndeliverableException |
| 27 |
* handling by annotating the test method with SuppressUndeliverable. |
| 28 |
*/ |
| 29 |
public class SuppressUndeliverableRule implements TestRule { |
| 30 |
|
| 31 |
static final class SuppressUndeliverableRuleStatement extends Statement { |
| 32 |
private Statement base; |
| 33 |
|
| 34 |
SuppressUndeliverableRuleStatement(Statement base) { |
| 35 |
this.base = base; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void evaluate() throws Throwable { |
| 40 |
try { |
| 41 |
RxJavaPlugins.setErrorHandler(throwable -> { |
| 42 |
if (!(throwable instanceof UndeliverableException)) { |
| 43 |
throwable.printStackTrace(); |
| 44 |
Thread currentThread = Thread.currentThread(); |
| 45 |
currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, throwable); |
| 46 |
} |
| 47 |
}); |
| 48 |
base.evaluate(); |
| 49 |
} finally { |
| 50 |
RxJavaPlugins.setErrorHandler(null); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Statement apply(Statement base, Description description) { |
| 57 |
if (description != null && description.getAnnotation(SuppressUndeliverable.class) != null) { |
| 58 |
return new SuppressUndeliverableRuleStatement(base); |
| 59 |
} else { |
| 60 |
return base; |
| 61 |
} |
| 62 |
} |
| 63 |
} |