content: /**
   * Copyright (c) 2016-present, RxJava Contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License is
   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
   * the License for the specific language governing permissions and limitations under the License.
   */
  
  package io.reactivex.observable.internal.operators;
  
  import static org.junit.Assert.*;
  
  import org.junit.Test;
  
  import io.reactivex.common.*;
  import io.reactivex.common.exceptions.TestException;
  import io.reactivex.common.functions.Function;
  import io.reactivex.observable.*;
  import io.reactivex.observable.observers.TestObserver;
  import io.reactivex.observable.subjects.PublishSubject;
  
  public class MaybeSwitchIfEmptyTest {
  
      @Test
      public void nonEmpty() {
          Maybe.just(1).switchIfEmpty(Maybe.just(2)).test().assertResult(1);
      }
  
      @Test
      public void empty() {
          Maybe.<Integer>empty().switchIfEmpty(Maybe.just(2)).test().assertResult(2);
      }
  
      @Test
      public void defaultIfEmptyNonEmpty() {
          Maybe.just(1).defaultIfEmpty(2).test().assertResult(1);
      }
  
      @Test
      public void defaultIfEmptyEmpty() {
          Maybe.<Integer>empty().defaultIfEmpty(2).test().assertResult(2);
      }
  
      @Test
      public void error() {
          Maybe.<Integer>error(new TestException()).switchIfEmpty(Maybe.just(2))
          .test().assertFailure(TestException.class);
      }
  
      @Test
      public void errorOther() {
          Maybe.empty().switchIfEmpty(Maybe.<Integer>error(new TestException()))
          .test().assertFailure(TestException.class);
      }
  
      @Test
      public void emptyOtherToo() {
          Maybe.empty().switchIfEmpty(Maybe.empty())
          .test().assertResult();
      }
  
      @Test
      public void dispose() {
          PublishSubject<Integer> pp = PublishSubject.create();
  
          TestObserver<Integer> ts = pp.singleElement().switchIfEmpty(Maybe.just(2)).test();
  
          assertTrue(pp.hasObservers());
  
          ts.cancel();
  
          assertFalse(pp.hasObservers());
      }
  
  
      @Test
      public void isDisposed() {
          PublishSubject<Integer> pp = PublishSubject.create();
  
          TestHelper.checkDisposed(pp.singleElement().switchIfEmpty(Maybe.just(2)));
      }
  
      @Test
      public void doubleOnSubscribe() {
          TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, Maybe<Integer>>() {
              @Override
              public Maybe<Integer> apply(Maybe<Integer> f) throws Exception {
                  return f.switchIfEmpty(Maybe.just(2));
              }
          });
      }
  
      @Test
      public void emptyCancelRace() {
          for (int i = 0; i < 500; i++) {
              final PublishSubject<Integer> pp = PublishSubject.create();
  
              final TestObserver<Integer> ts = pp.singleElement().switchIfEmpty(Maybe.just(2)).test();
  
              Runnable r1 = new Runnable() {
                  @Override
                  public void run() {
                      pp.onComplete();
                  }
              };
  
              Runnable r2 = new Runnable() {
                  @Override
                  public void run() {
                      ts.cancel();
                  }
              };
  
              TestCommonHelper.race(r1, r2, Schedulers.single());
          }
      }
  }
  