001/* 002 * (C) Copyright 2013-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * [email protected] 018 */ 019package org.nuxeo.ecm.core.test; 020 021import java.io.File; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.commons.io.FileUtils; 026import org.nuxeo.common.Environment; 027import org.nuxeo.runtime.api.Framework; 028import org.nuxeo.runtime.test.runner.FeaturesRunner; 029import org.nuxeo.runtime.test.runner.RunnerFeature; 030 031import com.dumbster.smtp.SimpleSmtpServer; 032 033/** 034 * @since 5.7 035 */ 036public class FakeSmtpMailServerFeature implements RunnerFeature { 037 038 public static final int SERVER_PORT = 2525; 039 040 public static final String SERVER_HOST = "127.0.0.1"; 041 042 public static SimpleSmtpServer server; 043 044 @Override 045 public void beforeSetup(FeaturesRunner runner) throws Exception { 046 server = SimpleSmtpServer.start(SERVER_PORT); 047 if (Framework.isInitialized()) { 048 File file = new File(Environment.getDefault().getConfig(), "mail.properties"); 049 List<String> mailProperties = new ArrayList<>(); 050 mailProperties.add(String.format("mail.smtp.host = %s", SERVER_HOST)); 051 mailProperties.add(String.format("mail.smtp.port = %s", SERVER_PORT)); 052 FileUtils.writeLines(file, mailProperties); 053 054 Framework.getProperties().put("mail.transport.host", SERVER_HOST); 055 Framework.getProperties().put("mail.transport.port", String.valueOf(SERVER_PORT)); 056 } 057 } 058 059 @Override 060 public void afterTeardown(FeaturesRunner runner) { 061 if (server != null) { 062 server.stop(); 063 } 064 } 065}