001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util.test;
016    
017    import com.dumbster.smtp.MailMessage;
018    import com.dumbster.smtp.SmtpServer;
019    import com.dumbster.smtp.SmtpServerFactory;
020    import com.dumbster.smtp.mailstores.RollingMailStore;
021    
022    import com.liferay.portal.kernel.test.ReflectionTestUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.PropsUtil;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author Manuel de la Pe??a
032     * @author Jos?? Manuel Navarro
033     */
034    public class MailServiceTestUtil {
035    
036            public static void clearMessages() {
037                    _smtpServer.clearMessages();
038            }
039    
040            public static int getInboxSize() {
041                    return _smtpServer.getEmailCount();
042            }
043    
044            public static MailMessage getLastMailMessage() {
045                    if (_smtpServer.getEmailCount() > 0) {
046                            return _smtpServer.getMessage(_smtpServer.getEmailCount() - 1);
047                    }
048    
049                    throw new IndexOutOfBoundsException(
050                            "There are no messages in the inbox");
051            }
052    
053            public static List<MailMessage> getMailMessages(
054                    String headerName, String headerValue) {
055    
056                    List<MailMessage> mailMessages = new ArrayList<MailMessage>();
057    
058                    for (int i = 0; i < _smtpServer.getEmailCount(); ++i) {
059                            MailMessage message = _smtpServer.getMessage(i);
060    
061                            if (headerName.equals("Body")) {
062                                    String body = message.getBody();
063    
064                                    if (body.equals(headerValue)) {
065                                            mailMessages.add(message);
066                                    }
067                            }
068                            else {
069                                    String messageHeaderValue = message.getFirstHeaderValue(
070                                            headerName);
071    
072                                    if (messageHeaderValue.equals(headerValue)) {
073                                            mailMessages.add(message);
074                                    }
075                            }
076                    }
077    
078                    return mailMessages;
079            }
080    
081            public static boolean lastMailMessageContains(String text) {
082                    MailMessage mailMessage = getLastMailMessage();
083    
084                    String bodyMailMessage = mailMessage.getBody();
085    
086                    return bodyMailMessage.contains(text);
087            }
088    
089            public static void start() {
090                    if (_smtpServer != null) {
091                            throw new IllegalStateException("Server is already running");
092                    }
093    
094                    _smtpServer = new SmtpServer();
095    
096                    _smtpServer.setMailStore(
097                            new RollingMailStore() {
098    
099                                    @Override
100                                    public void addMessage(MailMessage message) {
101                                            try {
102                                                    List<MailMessage> receivedMail =
103                                                            ReflectionTestUtil.getFieldValue(
104                                                                    this, "receivedMail");
105    
106                                                    receivedMail.add(message);
107    
108                                                    if (getEmailCount() > 100) {
109                                                            receivedMail.remove(0);
110                                                    }
111                                            }
112                                            catch (Exception e) {
113                                                    throw new RuntimeException(e);
114                                            }
115                                    }
116    
117                            });
118                    _smtpServer.setPort(
119                            GetterUtil.getInteger(
120                                    PropsUtil.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT)));
121                    _smtpServer.setThreaded(false);
122    
123                    try {
124                            ReflectionTestUtil.invoke(
125                                    SmtpServerFactory.class, "startServerThread",
126                                    new Class<?>[] {SmtpServer.class}, _smtpServer);
127                    }
128                    catch (Exception e) {
129                            throw new RuntimeException(e);
130                    }
131            }
132    
133            public static void stop() {
134                    if ((_smtpServer != null) && _smtpServer.isStopped()) {
135                            throw new IllegalStateException("Server is already stopped");
136                    }
137    
138                    _smtpServer.stop();
139    
140                    _smtpServer = null;
141            }
142    
143            private static SmtpServer _smtpServer;
144    
145    }