Exemplo n.º 1
0
  /**
   * Tests normal mailer behaviour with non-default locale.
   *
   * @throws Exception when fails
   */
  public void testSendLocalized() throws Exception {
    InvocationsCollector inits = new InvocationsCollector();
    InvocationsCollector sends = new InvocationsCollector();
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString());
    doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString());

    when(preferences.getString(eq(KEY_PREF_EMAIL_LOCALE), anyString())).thenReturn("ru_RU");

    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),
            true,
            null));

    assertTrue(errors.isEmpty());
    assertArrayEquals(
        new Object[] {"*****@*****.**", "decrypted password", "smtp.mail.com", "111"},
        inits.get(0));
    assertEquals("[SMailer] Исходящий звонок на +12345678901", sends.get(0)[0]);
  }
Exemplo n.º 2
0
  /**
   * Tests normal mailer behaviour.
   *
   * @throws Exception when fails
   */
  public void testSend() throws Exception {
    InvocationsCollector inits = new InvocationsCollector();
    InvocationsCollector sends = new InvocationsCollector();
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString());
    doAnswer(sends).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),
            true,
            null));

    assertTrue(errors.isEmpty());
    assertArrayEquals(
        new Object[] {"*****@*****.**", "decrypted password", "smtp.mail.com", "111"},
        inits.get(0));
    assertEquals("[SMailer] Outgoing call to +12345678901", sends.get(0)[0]);
  }
Exemplo n.º 3
0
  private void mail(JobExecutionContext jec, Set emails, String subject, String mail) {
    try {
      log.info("Sending mail to service admins: " + Arrays.toString(emails.toArray()));

      for (String email : (Set<String>) emails) {
        if (isInterrupted()) {
          log.info("Interrupted, ending monitoring job");
          return;
        }

        try {
          Mailer.sendMail(
              (String) jec.getJobDetail().getJobDataMap().get("from.name"),
              (String) jec.getJobDetail().getJobDataMap().get("from.email"),
              email,
              subject,
              mail);
        } catch (Exception e) {
          log.error("Error sending mail to service admin " + email, e);
        }
      }
    } catch (Exception e) {
      log.error("Error sending mail to service admins", e);
    }
  }
Exemplo n.º 4
0
  /**
   * Check that mailer produces notification without internet connection.
   *
   * @throws Exception when fails
   */
  public void testErrorNotConnected() throws Exception {
    InvocationsCollector inits = new InvocationsCollector();
    InvocationsCollector sends = new InvocationsCollector();
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString());
    doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString());
    when(networkInfo.isConnected()).thenReturn(false);

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

    assertTrue(inits.isEmpty());
    assertTrue(sends.isEmpty());
    assertEquals(R.string.notification_error_no_connection, errors.get(0)[0]);
  }
Exemplo n.º 5
0
  /**
   * Check that mailer produces notification when port parameter is empty.
   *
   * @throws Exception when fails
   */
  public void testErrorEmptyPort() throws Exception {
    InvocationsCollector inits = new InvocationsCollector();
    InvocationsCollector sends = new InvocationsCollector();
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString());
    doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString());

    when(preferences.getString(eq(KEY_PREF_EMAIL_PORT), anyString())).thenReturn(null);

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

    assertTrue(inits.isEmpty());
    assertTrue(sends.isEmpty());
    assertEquals(R.string.notification_error_no_port, errors.get(0)[0]);
  }
Exemplo n.º 6
0
  /**
   * Check that mailer produces notification on other transport exceptions.
   *
   * @throws Exception when fails
   */
  public void testErrorOtherExceptions() throws Exception {
    InvocationsCollector inits = new InvocationsCollector();
    InvocationsCollector sends = new InvocationsCollector();
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString());
    doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString());
    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, null, true, null));

    assertFalse(inits.isEmpty());
    assertTrue(sends.isEmpty());
    assertEquals(R.string.notification_error_mail_general, errors.get(0)[0]);
  }
Exemplo n.º 7
0
  /**
   * When mailer parameters goes back to normal last notification should be removed.
   *
   * @throws Exception when fails
   */
  public void testClearNotificationExceptions() throws Exception {
    InvocationsCollector errors = new InvocationsCollector();
    InvocationsCollector clears = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(clears).when(notifications).hideMailError();
    doAnswer(
            new Answer() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                String subject = invocation.getArgumentAt(0, String.class);
                if (subject.equals("[SMailer] Outgoing call to bad_phone")) {
                  throw new MessagingException("bad_phone");
                }
                return null;
              }
            })
        .when(transport)
        .send(anyString(), anyString(), anyString(), anyString());

    Mailer mailer = new Mailer(context, transport, cryptor, notifications, database);

    /* bad_phone produces notification */

    mailer.send(
        new MailMessage("bad_phone", false, null, null, false, false, null, null, true, null));
    assertEquals(R.string.notification_error_mail_general, errors.get(0)[0]);
    assertTrue(clears.isEmpty());

    /* good_phone removes it */

    errors.clear();
    clears.clear();

    mailer.send(
        new MailMessage("good_phone", false, null, null, false, false, null, null, true, null));

    assertTrue(errors.isEmpty());
    assertFalse(clears.isEmpty());
  }
Exemplo n.º 8
0
  /**
   * When {@link Settings#KEY_PREF_NOTIFY_SEND_SUCCESS} set to true success notification should be
   * shown.
   *
   * @throws Exception when fails
   */
  public void testSuccessNotification() throws Exception {
    InvocationsCollector errors = new InvocationsCollector();
    InvocationsCollector successes = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(successes).when(notifications).showMailSuccess(anyLong());

    Mailer mailer = new Mailer(context, transport, cryptor, notifications, database);

    /* settings is off */
    when(preferences.getBoolean(eq(KEY_PREF_NOTIFY_SEND_SUCCESS), anyBoolean())).thenReturn(false);

    mailer.send(new MailMessage("1", false, null, null, false, false, null, null, true, null));

    assertTrue(errors.isEmpty());
    assertTrue(successes.isEmpty());

    /* settings is on */
    when(preferences.getBoolean(eq(KEY_PREF_NOTIFY_SEND_SUCCESS), anyBoolean())).thenReturn(true);
    mailer.send(new MailMessage("1", false, null, null, false, false, null, null, true, null));

    assertTrue(errors.isEmpty());
    assertFalse(successes.isEmpty());
  }
Exemplo n.º 9
0
 @Override
 public void publish(final LogRecord record) {
   if (record.getLevel().intValue() < this.getLevel().intValue()
       || record.getLoggerName().equals(getClass().getName())) {
     return;
   }
   try {
     Mailer.instance()
         .send(
             (message) -> {
               message.addRecipient(
                   Message.RecipientType.TO,
                   new InternetAddress(
                       "clash.nanobot+log."
                           + record.getLevel().getName().toLowerCase()
                           + "@gmail.com"));
               message.setSubject(record.getLevel() + ": " + record.getMessage());
               String emailBody = "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">";
               emailBody +=
                   "<tr><td>Version</td><td>" + BuildInfo.instance().getVersion() + "</td></tr>";
               emailBody +=
                   "<tr><td>BuildTime</td><td>"
                       + BuildInfo.instance().getTimestamp()
                       + "</td></tr>";
               emailBody += "<tr><td>Time</td><td>" + record.getMillis() + "</td></tr>";
               emailBody += "<tr><td>Thread</td><td>" + record.getThreadID() + "</td></tr>";
               emailBody += "<tr><td>Logger</td><td>" + record.getLoggerName() + "</td></tr>";
               emailBody += "<tr><td>Class</td><td>" + record.getSourceClassName() + "</td></tr>";
               emailBody +=
                   "<tr><td>Method</td><td>" + record.getSourceMethodName() + "</td></tr>";
               emailBody += "<tr><td>Level</td><td>" + record.getLevel() + "</td></tr>";
               emailBody += "<tr><td>Message</td><td>" + record.getMessage() + "</td></tr>";
               final Throwable e = record.getThrown();
               if (e != null) {
                 final StringWriter writer = new StringWriter();
                 e.printStackTrace(new PrintWriter(writer));
                 emailBody +=
                     "<tr><td>Exception</td><td>"
                         + writer.toString().replace("\n", "<br/>")
                         + "</td></tr>";
               }
               emailBody += "</table>";
               message.setContent(emailBody, "text/html");
             });
   } catch (final Exception e) {
     logger.log(Level.SEVERE, e.getMessage(), e);
   }
 }
Exemplo n.º 10
0
  public Object getValueAt(int row, int column) {
    try {
      Message message = getMessage(row);
      switch (column) {
        case 0:
          // Answered?
          return (message.isSet(Flags.Flag.ANSWERED) ? "R" : "");

        case 1:
          // From.
          Address[] addresses = message.getFrom();
          if (addresses == null || addresses.length == 0) {
            return "(No sender)";
          }
          // Given "Personal Name <*****@*****.**>", choose "Personal Name" if it's available, and
          // "*****@*****.**" if not.
          InternetAddress address = (InternetAddress) addresses[0];
          String name = address.getPersonal();
          return (name != null) ? name : address.getAddress();

        case 2:
          // Subject.
          String subject = message.getSubject();
          return (subject != null) ? subject : "(No subject)";

        case 3:
          // Date.
          Date date = message.getReceivedDate();
          return (date != null) ? Mailer.dateToIsoString(date) : "Unknown";

        default:
          return "<no-column-" + column + ">";
      }
    } catch (MessagingException ex) {
      ex.printStackTrace();
      return "<error>";
    }
  }
Exemplo n.º 11
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());
  }