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.test.rule.callback;
016    
017    import com.liferay.portal.kernel.messaging.DestinationNames;
018    import com.liferay.portal.kernel.test.rule.callback.SynchronousDestinationTestCallback;
019    import com.liferay.portal.util.test.MailServiceTestUtil;
020    
021    import org.junit.runner.Description;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class SynchronousMailTestCallback
027            extends SynchronousDestinationTestCallback {
028    
029            public static final SynchronousMailTestCallback INSTANCE =
030                    new SynchronousMailTestCallback();
031    
032            @Override
033            public void afterClass(Description description, SyncHandler syncHandler)
034                    throws Exception {
035    
036                    super.afterClass(description, syncHandler);
037    
038                    MailServiceTestUtil.stop();
039            }
040    
041            @Override
042            public void afterMethod(
043                    Description description, SyncHandler syncHandler, Object target) {
044    
045                    super.afterMethod(description, syncHandler, target);
046    
047                    MailServiceTestUtil.clearMessages();
048            }
049    
050            @Override
051            public SyncHandler beforeClass(Description description) throws Throwable {
052                    MailServiceTestUtil.start();
053    
054                    SyncHandler syncHandler = super.beforeClass(description);
055    
056                    syncHandler.replaceDestination(DestinationNames.MAIL);
057    
058                    return syncHandler;
059            }
060    
061            private SynchronousMailTestCallback() {
062            }
063    
064    }