protected MockNotification createMockNotification(long expectedTime, String subject, String textMsg, String email) { MockNotification notification; notification = new MockNotification(); notification.setExpectedTime(expectedTime); notification.setSubject(subject); notification.setTextMsg(textMsg); notification.setEmail(email); return notification; }
private static String listNotifications(String prefix, Collection<?> notifications) { final StringBuilder b = new StringBuilder(); for (Object o : notifications) { MockNotification notification; MockNotification received = null; if (o instanceof MockNotification[]) { notification = ((MockNotification[]) o)[0]; received = ((MockNotification[]) o)[1]; } else { notification = (MockNotification) o; } b.append(prefix); b.append(notification); if (received != null) { b.append(" (received: "); b.append(received.getExpectedTime()); b.append(")"); } b.append("\n"); } return b.toString(); } }
@Override public int send(List<Argument> arguments) { MockUtil.println("Message sent with arguments:"+arguments); MockNotification notification = new MockNotification(); Iterator<Argument> it = arguments.iterator(); while (it.hasNext()) { Argument arg = it.next(); if (arg.getSwitch().equals(NotificationManager.PARAM_SUBJECT)) { notification.setSubject(arg.getValue()); } else if (arg.getSwitch().equals(NotificationManager.PARAM_EMAIL)) { notification.setEmail(arg.getValue()); } else if (arg.getSwitch().equals(NotificationManager.PARAM_TEXT_MSG)) { notification.setTextMsg(arg.getValue()); } } notification.setExpectedTime(System.currentTimeMillis()); NotificationAnticipator anticipator = getAnticipator(); if (anticipator != null) { anticipator.notificationReceived(notification); } else { throw new NullPointerException("anticipator is null"); } return 0; }
/** * @param event */ public synchronized void notificationReceived(MockNotification mn) { int i = m_anticipated.indexOf(mn); if (i != -1) { MockNotification notification = m_anticipated.get(i); long receivedTime = mn.getExpectedTime(); long expectedTime = notification.getExpectedTime(); long difference = expectedTime - receivedTime; if (Math.abs(difference) < m_expectedDiff) { MockUtil.println("Received expected notification: " + mn); m_anticipated.remove(mn); notifyAll(); } else { MockNotification[] n = new MockNotification[] { notification, mn }; if (difference > 0) { MockUtil.println("Received early notification: " + mn); m_earlyArrival.add(n); } else { MockUtil.println("Received late notification: " + mn); m_lateBloomers.add(n); } } } else { MockUtil.println("Received unexpected notification: " + mn); m_unanticipated.add(mn); } }