3131import rx .subjects .PublishSubject ;
3232import rx .subscriptions .*;
3333
34+ import static org .mockito .Mockito .*;
35+ import static org .junit .Assert .*;
36+
3437/**
3538 * Test Completable methods and operators.
3639 */
@@ -3410,4 +3413,135 @@ public void endWithFlowableError() {
34103413 ts .assertError (TestException .class );
34113414 ts .assertNotCompleted ();
34123415 }
3416+
3417+ @ Test
3418+ public void usingFactoryThrows () {
3419+ @ SuppressWarnings ("unchecked" )
3420+ Action1 <Integer > onDispose = mock (Action1 .class );
3421+
3422+ TestSubscriber <Integer > ts = TestSubscriber .create ();
3423+
3424+ Completable .using (new Func0 <Integer >() {
3425+ @ Override
3426+ public Integer call () {
3427+ return 1 ;
3428+ }
3429+ },
3430+ new Func1 <Integer , Completable >() {
3431+ @ Override
3432+ public Completable call (Integer t ) {
3433+ throw new TestException ();
3434+ }
3435+ }, onDispose ).subscribe (ts );
3436+
3437+ verify (onDispose ).call (1 );
3438+
3439+ ts .assertNoValues ();
3440+ ts .assertNotCompleted ();
3441+ ts .assertError (TestException .class );
3442+ }
3443+
3444+ @ Test
3445+ public void usingFactoryAndDisposerThrow () {
3446+ Action1 <Integer > onDispose = new Action1 <Integer >() {
3447+ @ Override
3448+ public void call (Integer t ) {
3449+ throw new TestException ();
3450+ }
3451+ };
3452+
3453+ TestSubscriber <Integer > ts = TestSubscriber .create ();
3454+
3455+ Completable .using (new Func0 <Integer >() {
3456+ @ Override
3457+ public Integer call () {
3458+ return 1 ;
3459+ }
3460+ },
3461+ new Func1 <Integer , Completable >() {
3462+ @ Override
3463+ public Completable call (Integer t ) {
3464+ throw new TestException ();
3465+ }
3466+ }, onDispose ).subscribe (ts );
3467+
3468+ ts .assertNoValues ();
3469+ ts .assertNotCompleted ();
3470+ ts .assertError (CompositeException .class );
3471+
3472+ CompositeException ex = (CompositeException )ts .getOnErrorEvents ().get (0 );
3473+
3474+ List <Throwable > listEx = ex .getExceptions ();
3475+
3476+ assertEquals (2 , listEx .size ());
3477+
3478+ assertTrue (listEx .get (0 ).toString (), listEx .get (0 ) instanceof TestException );
3479+ assertTrue (listEx .get (1 ).toString (), listEx .get (1 ) instanceof TestException );
3480+ }
3481+
3482+ @ Test
3483+ public void usingFactoryReturnsNull () {
3484+ @ SuppressWarnings ("unchecked" )
3485+ Action1 <Integer > onDispose = mock (Action1 .class );
3486+
3487+ TestSubscriber <Integer > ts = TestSubscriber .create ();
3488+
3489+ Completable .using (new Func0 <Integer >() {
3490+ @ Override
3491+ public Integer call () {
3492+ return 1 ;
3493+ }
3494+ },
3495+ new Func1 <Integer , Completable >() {
3496+ @ Override
3497+ public Completable call (Integer t ) {
3498+ return null ;
3499+ }
3500+ }, onDispose ).subscribe (ts );
3501+
3502+ verify (onDispose ).call (1 );
3503+
3504+ ts .assertNoValues ();
3505+ ts .assertNotCompleted ();
3506+ ts .assertError (NullPointerException .class );
3507+ }
3508+
3509+ @ Test
3510+ public void usingFactoryReturnsNullAndDisposerThrows () {
3511+ Action1 <Integer > onDispose = new Action1 <Integer >() {
3512+ @ Override
3513+ public void call (Integer t ) {
3514+ throw new TestException ();
3515+ }
3516+ };
3517+
3518+ TestSubscriber <Integer > ts = TestSubscriber .create ();
3519+
3520+ Completable .using (new Func0 <Integer >() {
3521+ @ Override
3522+ public Integer call () {
3523+ return 1 ;
3524+ }
3525+ },
3526+ new Func1 <Integer , Completable >() {
3527+ @ Override
3528+ public Completable call (Integer t ) {
3529+ return null ;
3530+ }
3531+ }, onDispose ).subscribe (ts );
3532+
3533+ ts .assertNoValues ();
3534+ ts .assertNotCompleted ();
3535+ ts .assertError (CompositeException .class );
3536+
3537+ CompositeException ex = (CompositeException )ts .getOnErrorEvents ().get (0 );
3538+
3539+ List <Throwable > listEx = ex .getExceptions ();
3540+
3541+ assertEquals (2 , listEx .size ());
3542+
3543+ assertTrue (listEx .get (0 ).toString (), listEx .get (0 ) instanceof NullPointerException );
3544+ assertTrue (listEx .get (1 ).toString (), listEx .get (1 ) instanceof TestException );
3545+ }
3546+
34133547}
0 commit comments