@Test
 public void fromArray() {
   String[] items = new String[] {"one", "two", "three"};
   assertEquals((Long) 3L, Observable.fromArray(items).count().toBlocking().single());
   assertEquals("two", Observable.fromArray(items).skip(1).take(1).toBlocking().single());
   assertEquals("three", Observable.fromArray(items).takeLast(1).toBlocking().single());
 }
 @Override
 public void update(Observable o, Object arg) {
   if (o.getClass().equals(CopyPMod.class) || o.getClass().equals(BooksPMod.class)) {
     Preconditions.checkNotNull(arg);
     if (getReferenceObject().equals(arg)) {
       lblMessage.setText(Texts.get("BookDetailMainView.optimisticlock.error"));
       lblMessage.setIcon(IconUtil.loadIcon("warning.png"));
       logger.info("Book changed in the background, disabling view...");
       disableComponents(
           btnSave,
           btnAdd,
           btnRemove,
           btnSetCondition,
           listCopies,
           txtFieldAuthor,
           txtFieldPublisher,
           txtFieldTitle,
           comboShelf);
     } else {
       logger.info("A different book changed in the background. Doing nothing...");
     }
   } else {
     super.update(o, arg);
   }
 }
  public void addHiddenConversation(Conversation conversation) {
    if (conversation instanceof ConversationData) {
      if (!this.hiddenConversations.contains(conversation)) {
        this.hiddenConversations.add((ConversationData) conversation);
        if (this.conversations.contains(conversation)) {
          this.conversations.remove(conversation);
          if (this.conversations.size() < 1) {
            this.activeConversation = null;
          }
        }
      }
    } else if (conversation instanceof MultiConversationData) {
      if (!this.hiddenMultiConversations.contains(conversation)) {
        this.hiddenMultiConversations.add((MultiConversationData) conversation);
        if (this.multiConversations.contains(conversation)) {
          this.multiConversations.remove(conversation);
          if (multiConversations.size() < 1) {
            this.activeConversation = null;
          }
        }
      }
    }

    super.setChanged();
    super.notifyObservers();

    return;
  }
  /**
   * The error from the user provided Observable is handled by the subscribe try/catch because this
   * is synchronous
   *
   * <p>Result: Passes
   */
  @Test
  public void testCustomObservableWithErrorInObservableSynchronous() {
    final AtomicInteger count = new AtomicInteger();
    final AtomicReference<Throwable> error = new AtomicReference<>();
    // FIXME custom built???
    Observable.just("1", "2")
        .concatWith(Observable.error(() -> new NumberFormatException()))
        .subscribe(
            new Observer<String>() {

              @Override
              public void onComplete() {
                System.out.println("completed");
              }

              @Override
              public void onError(Throwable e) {
                error.set(e);
                System.out.println("error");
                e.printStackTrace();
              }

              @Override
              public void onNext(String v) {
                System.out.println(v);
                count.incrementAndGet();
              }
            });
    assertEquals(2, count.get());
    assertNotNull(error.get());
    if (!(error.get() instanceof NumberFormatException)) {
      fail("It should be a NumberFormatException");
    }
  }
  /**
   * A reduce on an empty Observable and a seed should just pass the seed through.
   *
   * <p>This is confirmed at https://github.com/ReactiveX/RxJava/issues/423#issuecomment-27642456
   */
  @Test
  public void testReduceWithEmptyObservableAndSeed() {
    Observable<Integer> observable = Observable.range(1, 0);
    int value = observable.reduce(1, (t1, t2) -> t1 + t2).toBlocking().last();

    assertEquals(1, value);
  }
  @Test
  public void fromArityArgs1() {
    Observable<String> items = Observable.just("one");

    assertEquals((Long) 1L, items.count().toBlocking().single());
    assertEquals("one", items.takeLast(1).toBlocking().single());
  }
  public boolean removeConversation(Conversation conversation) {
    boolean removed = false;

    if (conversation instanceof ConversationData) {
      if (this.conversations.contains(conversation)) {
        this.conversations.remove(conversation);
        this.hiddenConversations.add((ConversationData) conversation);
        removed = true;
        this.activeConversation = this.conversations.isEmpty() ? null : this.conversations.get(0);
      }
    } else if (conversation instanceof MultiConversationData) {
      if (this.multiConversations.contains(conversation)) {
        this.multiConversations.remove(conversation);
        this.hiddenMultiConversations.add((MultiConversationData) conversation);
        removed = true;
        this.activeConversation =
            this.multiConversations.isEmpty() ? null : this.multiConversations.get(0);
      }
    }

    super.setChanged();
    super.notifyObservers();

    return removed;
  }
 @Test
 public void testReduceWithInitialValue() {
   Observable<Integer> observable = Observable.just(1, 2, 3, 4);
   observable.reduce(50, (t1, t2) -> t1 + t2).subscribe(w);
   // we should be called only once
   verify(w, times(1)).onNext(anyInt());
   verify(w).onNext(60);
 }
 @Test
 public void testFirstWithPredicateOfNoneMatchingThePredicate() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
   observable.filter(IS_EVEN).first().subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, never()).onComplete();
   verify(w, times(1)).onError(isA(NoSuchElementException.class));
 }
Exemple #10
0
 @Test
 public void testFirstOfNone() {
   Observable<Integer> observable = Observable.empty();
   observable.first().subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, never()).onComplete();
   verify(w, times(1)).onError(isA(NoSuchElementException.class));
 }
Exemple #11
0
 @Test
 public void testTakeFirstOfNone() {
   Observable<Integer> observable = Observable.empty();
   observable.take(1).subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
Exemple #12
0
 @Test
 public void testTakeFirstWithPredicateOfNoneMatchingThePredicate() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
   observable.takeFirst(IS_EVEN).subscribe(w);
   verify(w, never()).onNext(anyInt());
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
Exemple #13
0
 public void testTakeFirstWithPredicateOfSome() {
   Observable<Integer> observable = Observable.just(1, 3, 5, 4, 6, 3);
   observable.takeFirst(IS_EVEN).subscribe(w);
   verify(w, times(1)).onNext(anyInt());
   verify(w).onNext(4);
   verify(w, times(1)).onComplete();
   verify(w, never()).onError(any(Throwable.class));
 }
Exemple #14
0
  @Test
  public void testCountError() {
    Observable<String> o = Observable.error(() -> new RuntimeException());

    o.count().subscribe(w);
    verify(w, never()).onNext(anyInt());
    verify(w, never()).onComplete();
    verify(w, times(1)).onError(any(RuntimeException.class));
  }
Exemple #15
0
 @Test
 public void testCountZeroItems() {
   Observable<String> observable = Observable.empty();
   observable.count().subscribe(w);
   // we should be called only once
   verify(w, times(1)).onNext(anyLong());
   verify(w).onNext(0L);
   verify(w, never()).onError(any(Throwable.class));
   verify(w, times(1)).onComplete();
 }
  public void run(double duration) {
    double endtime = _currentTime + duration;

    while ((!empty()) && (_head.next.waketime <= endtime)) {
      _currentTime = _head.next.waketime;
      dequeue().run();
      super.setChanged();
      super.notifyObservers();
    }
    _currentTime = endtime;
  }
Exemple #17
0
  @Test
  public void fromIterable() {
    ArrayList<String> items = new ArrayList<>();
    items.add("one");
    items.add("two");
    items.add("three");

    assertEquals((Long) 3L, Observable.fromIterable(items).count().toBlocking().single());
    assertEquals("two", Observable.fromIterable(items).skip(1).take(1).toBlocking().single());
    assertEquals("three", Observable.fromIterable(items).takeLast(1).toBlocking().single());
  }
Exemple #18
0
 /**
  * Met à jour la gui active lorsque l'objet obsevable remarque un changement dans l'état de la gui
  * active
  *
  * @param observable Objet observer
  * @param o
  */
 @Override
 public void update(Observable observable, Object o) {
   if (observable.toString().equals("image")) {
     ImageConcrete image = (ImageConcrete) observable;
     this.setImage(image);
     this.repaint();
   }
   if (observable.toString().equals("perspective")) {
     this.perspective = (Perspective) observable;
     this.repaint();
   }
 }
Exemple #19
0
  @Test
  public void testCountAFewItems() {
    Observable<String> observable = Observable.just("a", "b", "c", "d");

    observable.count().subscribe(w);

    // we should be called only once
    verify(w, times(1)).onNext(anyLong());
    verify(w).onNext(4L);
    verify(w, never()).onError(any(Throwable.class));
    verify(w, times(1)).onComplete();
  }
  public void removeAllConversations() {
    this.conversations.clear();
    this.hiddenConversations.clear();
    this.hiddenMultiConversations.clear();
    this.multiConversations.clear();
    this.activeConversation = null;

    super.setChanged();
    super.notifyObservers();

    return;
  }
Exemple #21
0
  @Test
  public void testIgnoreElements() {
    Observable<Integer> observable = Observable.just(1, 2, 3).ignoreElements();

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, never()).onNext(any(Integer.class));
    verify(observer, never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }
Exemple #22
0
  /** Shuffle the observers list */
  protected void shuffleObservers() {

    logger.info("Shuffling observers list...");
    logger.debug("#Observers before shuffle = " + super.countObservers());
    logger.debug("Deleting observers...");
    super.deleteObservers();
    logger.debug("#Observers = " + super.countObservers());
    Collections.shuffle(obs, prng);
    for (Observer o : obs) {
      super.addObserver(o);
    }
    logger.debug("Observers shuffled and re-added: #Observers = " + super.countObservers());
  }
  public void setActiveConversation(Conversation activeConversation) {
    this.activeConversation = activeConversation;
    if (this.isHidden(this.activeConversation)) {
      this.activateConversation(this.activeConversation);
    } else {
      // Only notify if not calling activeConversation because
      // it also notifies
      super.setChanged();
      super.notifyObservers();
    }

    return;
  }
Exemple #24
0
  /** A reduce should fail with an NoSuchElementException if done on an empty Observable. */
  @Test(expected = NoSuchElementException.class)
  public void testReduceWithEmptyObservable() {
    Observable<Integer> observable = Observable.range(1, 0);
    observable
        .reduce((t1, t2) -> t1 + t2)
        .toBlocking()
        .forEach(
            t1 -> {
              // do nothing ... we expect an exception instead
            });

    fail("Expected an exception to be thrown");
  }
Exemple #25
0
  @Test
  public void testMaterializeDematerializeChaining() {
    Observable<Integer> obs = Observable.just(1);
    Observable<Integer> chained = obs.materialize().dematerialize();

    Subscriber<Integer> observer = TestHelper.mockSubscriber();

    chained.subscribe(observer);

    verify(observer, times(1)).onNext(1);
    verify(observer, times(1)).onComplete();
    verify(observer, times(0)).onError(any(Throwable.class));
  }
 /**
  * Defines the constraint which is applied to the list model elements
  *
  * @param constraint the constraint to set
  * @throws IllegalArgumentException if constraint is null
  */
 public final void setConstraint(Constraint constraint) {
   Assert.notNull(constraint);
   if (!constraint.equals(this.constraint)) {
     if (this.constraint instanceof Observable) {
       ((Observable) constraint).deleteObserver(this);
     }
     this.constraint = constraint;
     if (constraint instanceof Observable) {
       ((Observable) constraint).addObserver(this);
     }
     fireContentsChanged(this, -1, -1);
   }
 }
Exemple #27
0
  @Test
  public void testContainsWithEmptyObservable() {
    Observable<Boolean> observable = Observable.<String>empty().contains("a");

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, times(1)).onNext(false);
    verify(observer, never()).onNext(true);
    verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }
 protected void disableEndObjectObserving(/*ShapeGraphicalRepresentation<?> anEndObject*/ ) {
   if (
   /*anEndObject != null &&*/ enabledEndObjectObserving) {
     /*anEndObject.deleteObserver(this);
     if (!isDeserializing()) {
     	for (Object o : anEndObject.getAncestors())
     		if (getGraphicalRepresentation(o) != null) getGraphicalRepresentation(o).deleteObserver(this);
     }*/
     for (Observable o : observedEndObjects) {
       o.deleteObserver(this);
     }
     enabledEndObjectObserving = false;
   }
 }
Exemple #29
0
  @Test
  public void testContainsWithInexistence() {
    Observable<Boolean> observable =
        Observable.just("a", "b").contains("c"); // FIXME null values are not allowed, removed

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, times(1)).onNext(false);
    verify(observer, never()).onNext(true);
    verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }
Exemple #30
0
  @Test
  @Ignore("null values are not allowed")
  public void testContainsWithNull() {
    Observable<Boolean> observable = Observable.just("a", "b", null).contains(null);

    Subscriber<Object> observer = TestHelper.mockSubscriber();

    observable.subscribe(observer);

    verify(observer, times(1)).onNext(true);
    verify(observer, never()).onNext(false);
    verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class));
    verify(observer, times(1)).onComplete();
  }