@Before public void setUp() throws Exception { System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestContextFactory.class.getName()); QueueConnectionFactory queueConnectionFactory = Mockito.mock(QueueConnectionFactory.class); Queue queue = Mockito.mock(Queue.class); Context context = Mockito.mock(Context.class); TestContextFactory.context = context; when(context.lookup(eq("jms/queueConnectionFactory"))).thenReturn(queueConnectionFactory); when(context.lookup(eq("jms/mailQueue"))).thenReturn(queue); queueSender = Mockito.mock(QueueSender.class); QueueConnection queueConnection = Mockito.mock(QueueConnection.class); when(queueConnectionFactory.createQueueConnection()).thenReturn(queueConnection); when(queueConnectionFactory.createQueueConnection(anyString(), anyString())).thenReturn(queueConnection); QueueSession queueSession = Mockito.mock(QueueSession.class); bytesMessage = Mockito.mock(BytesMessage.class); when(queueSession.createBytesMessage()).thenReturn(bytesMessage); when(queueConnection.createQueueSession(anyBoolean(), anyInt())).thenReturn(queueSession); when(queueSession.createSender(eq(queue))).thenReturn(queueSender); transport = new SmtpJmsTransport(Session.getDefaultInstance(new Properties()), new URLName("jms")); transportListener = Mockito.mock(TransportListener.class); transport.addTransportListener(transportListener); }
/** * Return a {@link Transport} object from the session registering the passed in transport listener * for delivery notifications. */ private Transport getEmailTransport(Session session, TransportListener listener) { try { Transport transport = session.getTransport(); transport.addTransportListener(listener); if (!transport.isConnected()) { transport.connect(); } LOG.debug("Email transport {}, transport listener {}", transport, listener); return transport; } catch (MessagingException ex) { LOG.error("Got exception while initializing transport", ex); throw new NotifierRuntimeException("Got exception while initializing transport", ex); } }
@Test public void transportNOPTest() throws Exception { TransportListener transportListener = Mockito.mock(TransportListener.class); Message message = generateMessage(); Transport transport = session.getTransport("nop"); transport.addTransportListener(transportListener); transport.sendMessage(message, toAddress); waitForListeners(); ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class); verify(transportListener).messageDelivered(transportEventArgumentCaptor.capture()); TransportEvent event = transportEventArgumentCaptor.getValue(); assertEquals(message, event.getMessage()); assertEquals(TransportEvent.MESSAGE_DELIVERED, event.getType()); assertArrayEquals(toAddress, event.getValidSentAddresses()); }
public SmtpConnectionFactoryBuilder defaultTransportListeners(TransportListener... listeners) { defaultTransportListeners = Arrays.asList(requireNonNull(listeners)); return this; }
public SmtpConnectionFactory(Session session, TransportStrategy transportStrategy, ConnectionStrategy connectionStrategy, Collection<TransportListener> defaultTransportListeners) { this.session = session; this.transportFactory = transportStrategy; this.connectionStrategy = connectionStrategy; this.defaultTransportListeners = new ArrayList<>(defaultTransportListeners); }
public SmtpConnectionFactory(Session session, TransportStrategy transportFactory, ConnectionStrategy connectionStrategy) { this(session, transportFactory, connectionStrategy, Collections.<TransportListener>emptyList()); }
public void setDefaultListeners(Collection<TransportListener> listeners) { defaultTransportListeners = new CopyOnWriteArrayList<>(Objects.requireNonNull(listeners)); }
public List<TransportListener> getDefaultListeners() { return new ArrayList<>(defaultTransportListeners); }
private void initDefaultListeners(ClosableSmtpConnection smtpTransport) { for (TransportListener transportListener : defaultTransportListeners) { smtpTransport.addTransportListener(transportListener); } }
public void addTransportListener(TransportListener l) { transportListeners.add(l); delegate.addTransportListener(l); }
public void removeTransportListener(TransportListener l) { transportListeners.remove(l); delegate.removeTransportListener(l); }
public void clearListeners() { for (TransportListener transportListener : transportListeners) { delegate.removeTransportListener(transportListener); } transportListeners.clear(); }
/** * Add a new {@link TransportListener} * * @param l */ void addTransportListener(TransportListener l);
/** * Remove the provided {@link TransportListener} * * @param l */ void removeTransportListener(TransportListener l);