Beispiel #1
0
  /**
   * Test resending unsent messages.
   *
   * @throws Exception when fails
   */
  public void testSendUnsent() throws Exception {
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doThrow(MessagingException.class)
        .when(transport)
        .send(anyString(), anyString(), anyString(), anyString());

    Mailer mailer = new Mailer(context, transport, cryptor, notifications, database);
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));

    assertEquals(3, database.getMessages().getCount());
    assertEquals(3, database.getUnsentMessages().getCount());
    assertEquals(3, errors.size());

    /* try resend with transport still disabled */
    errors.clear();

    mailer.sendAllUnsent();

    assertEquals(3, database.getMessages().getCount());
    assertEquals(3, database.getUnsentMessages().getCount());
    assertTrue(errors.isEmpty()); /* no error notifications should be shown */

    /* enable transport an try again */
    doNothing().when(transport).send(anyString(), anyString(), anyString(), anyString());
    errors.clear();

    mailer.sendAllUnsent();

    assertEquals(3, database.getMessages().getCount());
    assertEquals(0, database.getUnsentMessages().getCount());
    assertTrue(errors.isEmpty());
  }