Index: src/test/org/apache/james/JamesTest.java =================================================================== --- src/test/org/apache/james/JamesTest.java (revision 366206) +++ src/test/org/apache/james/JamesTest.java (working copy) @@ -18,14 +18,15 @@ package org.apache.james; +import org.apache.james.services.MailServer; import org.apache.james.services.MailServerTestAllImplementations; -import org.apache.james.services.MailServer; +import org.apache.james.test.mock.avalon.MockContext; +import org.apache.james.test.mock.avalon.MockLogger; import org.apache.james.test.mock.avalon.MockServiceManager; -import org.apache.james.test.mock.avalon.MockLogger; import org.apache.james.test.mock.avalon.MockStore; -import org.apache.james.test.mock.avalon.MockContext; import org.apache.james.test.mock.james.MockUsersRepository; import org.apache.james.test.mock.james.MockUsersStore; +import org.apache.james.test.mock.james.MockMailRepository; import java.io.File; @@ -55,7 +56,9 @@ MockUsersRepository mockUsersRepository = new MockUsersRepository(); serviceManager.put("org.apache.james.services.UsersRepository", mockUsersRepository); serviceManager.put("org.apache.james.services.UsersStore", new MockUsersStore(mockUsersRepository)); - serviceManager.put("org.apache.avalon.cornerstone.services.store.Store", new MockStore()); + MockStore mockStore = new MockStore(); + mockStore.add(EXISTING_USER_NAME, new MockMailRepository()); + serviceManager.put("org.apache.avalon.cornerstone.services.store.Store", mockStore); return serviceManager; } Index: src/test/org/apache/james/test/mock/james/MockMailServer.java =================================================================== --- src/test/org/apache/james/test/mock/james/MockMailServer.java (revision 366206) +++ src/test/org/apache/james/test/mock/james/MockMailServer.java (working copy) @@ -18,6 +18,7 @@ import org.apache.james.services.MailRepository; import org.apache.james.services.MailServer; +import org.apache.james.services.UsersRepository; import org.apache.james.smtpserver.MessageSizeException; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -31,7 +32,7 @@ public class MockMailServer implements MailServer { - private final HashMap m_users = new HashMap(); + private final MockUsersRepository m_users = new MockUsersRepository(); private int m_counter = 0; private int m_maxMessageSizeBytes = 0; @@ -39,6 +40,10 @@ private final ArrayList mails = new ArrayList(); private HashMap inboxes; + + public MockUsersRepository getUsersRepository() { + return m_users; + } public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg) throws MessagingException { Object[] mailObjects = new Object[]{sender, recipients, msg}; @@ -103,7 +108,7 @@ } public boolean addUser(String userName, String password) { - m_users.put(userName, password); + m_users.addUser(userName, password); return true; } Index: src/test/org/apache/james/test/mock/james/MockUsersRepository.java =================================================================== --- src/test/org/apache/james/test/mock/james/MockUsersRepository.java (revision 366206) +++ src/test/org/apache/james/test/mock/james/MockUsersRepository.java (working copy) @@ -64,7 +64,7 @@ } public void removeUser(String name) { - // trivial implementation + m_users.remove(name); } public boolean contains(String name) { Index: src/test/org/apache/james/test/mock/avalon/MockStore.java =================================================================== --- src/test/org/apache/james/test/mock/avalon/MockStore.java (revision 366206) +++ src/test/org/apache/james/test/mock/avalon/MockStore.java (working copy) @@ -35,20 +35,33 @@ } public Object select(Object object) throws ServiceException { + Object result = get(object); + return result; + } + + private Object get(Object object) { + return m_storedObjectMap.get(extractKeyObject(object)); + } + + private Object extractKeyObject(Object object) { if (object instanceof Configuration) { Configuration repConf = (Configuration) object; try { - object = repConf.getAttribute("destinationURL"); + String attribute = repConf.getAttribute("destinationURL"); + String[] strings = attribute.split("/"); + if (strings.length > 0) { + object = strings[strings.length-1]; + } } catch (ConfigurationException e) { - throw new RuntimeException("test failed"); + throw new RuntimeException("test configuration setup failed"); } + } - Object result = m_storedObjectMap.get(object); - return result; + return object; } public boolean isSelectable(Object object) { - return m_storedObjectMap.get(object) != null; + return get(object) != null; } public void release(Object object) { Index: src/test/org/apache/james/test/util/Util.java =================================================================== --- src/test/org/apache/james/test/util/Util.java (revision 366206) +++ src/test/org/apache/james/test/util/Util.java (working copy) @@ -18,16 +18,41 @@ import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.DefaultConfiguration; +import org.apache.james.smtpserver.*; public class Util { - + public static int getRandomNonPrivilegedPort() { return ((int)( Math.random() * 1000) + 3000); } - + public static Configuration getValuedConfiguration(String name, String value) { DefaultConfiguration defaultConfiguration = new DefaultConfiguration(name); defaultConfiguration.setValue(value); return defaultConfiguration; } + + public static DefaultConfiguration createRemoteManagerHandlerChainConfiguration() { + DefaultConfiguration handlerChainConfig = new DefaultConfiguration("handlerchain"); + handlerChainConfig.addChild(createCommandHandlerConfiguration("HELO", HeloCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("EHLO", EhloCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("AUTH", AuthCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("VRFY", VrfyCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("EXPN", ExpnCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("MAIL", MailCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("RCPT", RcptCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("DATA", DataCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("RSET", RsetCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("HELP", HelpCmdHandler.class)); + handlerChainConfig.addChild(createCommandHandlerConfiguration("QUIT", QuitCmdHandler.class)); + return handlerChainConfig; + } + + private static DefaultConfiguration createCommandHandlerConfiguration(String command, Class commandClass) { + DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration("handler"); + cmdHandlerConfig.setAttribute("command", command); + String classname = commandClass.getName(); + cmdHandlerConfig.setAttribute("class", classname); + return cmdHandlerConfig; + } } Index: src/test/org/apache/james/services/MailServerTestAllImplementations.java =================================================================== --- src/test/org/apache/james/services/MailServerTestAllImplementations.java (revision 366206) +++ src/test/org/apache/james/services/MailServerTestAllImplementations.java (working copy) @@ -24,6 +24,8 @@ * tests all implementations for interface MailServer */ abstract public class MailServerTestAllImplementations extends TestCase { + + protected static final String EXISTING_USER_NAME = "testExistingUserName"; abstract public MailServer createMailServer(); abstract public boolean allowsPasswordlessUser(); @@ -93,7 +95,7 @@ } - public void testGetNonexisitingUserInbox() { + public void testGetNonexistingUserInbox() { // // TODO fix test (or James) -- test WILL FAIL @@ -116,30 +118,9 @@ } public void testGetExisitingUserInbox() { - - // - // TODO fix test (or James) -- test WILL FAIL - // - MailServer mailServer = createMailServer(); - String userName = "testUserName"; - MailRepository userInbox = null; - - // getUserInbox acts on field mailboxes for class org.apache.james.James - // thus, it is unrelated to addUser() for the only known implementation of MailServer - // TODO clarify this - mailServer.addUser(userName, "password"); // !! is not retrievable via getUserInbox !! - - - try { - userInbox = mailServer.getUserInbox(userName); - assertEquals("test user does not exist", null, userInbox); - fail("found inbox which should be unexistent"); - } catch (NullPointerException e) { - // this is what org.apache.james.James returns - // is this behavior compatible with other implementations? - // shouldn't James behave more gracefully? - } + MailRepository userInbox = mailServer.getUserInbox(EXISTING_USER_NAME); + assertNotNull("existing user exists", userInbox); } } Index: src/test/org/apache/james/remotemanager/RemoteManagerTest.java =================================================================== --- src/test/org/apache/james/remotemanager/RemoteManagerTest.java (revision 0) +++ src/test/org/apache/james/remotemanager/RemoteManagerTest.java (revision 0) @@ -0,0 +1,276 @@ +/*********************************************************************** + * Copyright (c) 2000-2005 The Apache Software Foundation. * + * All rights reserved. * + * ------------------------------------------------------------------- * + * Licensed under the Apache License, Version 2.0 (the "License"); you * + * may not use this file except in compliance with the License. You * + * may obtain a copy of the License at: * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * + * implied. See the License for the specific language governing * + * permissions and limitations under the License. * + ***********************************************************************/ + + +package org.apache.james.remotemanager; + +import junit.framework.TestCase; +import org.apache.avalon.cornerstone.services.sockets.SocketManager; +import org.apache.avalon.cornerstone.services.threads.ThreadManager; +import org.apache.commons.net.telnet.TelnetClient; +import org.apache.james.services.JamesConnectionManager; +import org.apache.james.test.mock.avalon.MockLogger; +import org.apache.james.test.mock.avalon.MockServiceManager; +import org.apache.james.test.mock.avalon.MockSocketManager; +import org.apache.james.test.mock.avalon.MockThreadManager; +import org.apache.james.test.mock.james.MockMailServer; +import org.apache.james.test.mock.james.MockUsersRepository; +import org.apache.james.test.mock.james.MockUsersStore; +import org.apache.james.test.util.Util; +import org.apache.james.util.connection.SimpleConnectionManager; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Arrays; + +/** + * Tests the org.apache.james.remotemanager.RemoteManager + */ +public class RemoteManagerTest extends TestCase { + + protected int m_remoteManagerListenerPort = Util.getRandomNonPrivilegedPort(); + protected RemoteManager m_remoteManager; + protected RemoteManagerTestConfiguration m_testConfiguration; + protected String m_host = "127.0.0.1"; + protected BufferedReader m_reader; + protected OutputStreamWriter m_writer; + protected TelnetClient m_telnetClient; + private MockUsersRepository m_mockUsersRepository; + + protected void setUp() throws Exception { + m_remoteManager = new RemoteManager(); + m_remoteManager.enableLogging(new MockLogger()); + + m_remoteManager.service(setUpServiceManager()); + m_testConfiguration = new RemoteManagerTestConfiguration(m_remoteManagerListenerPort); + } + + protected void finishSetUp(RemoteManagerTestConfiguration testConfiguration) { + testConfiguration.init(); + try { + m_remoteManager.configure(testConfiguration); + m_remoteManager.initialize(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected void login() throws IOException { + sendCommand(m_testConfiguration.getLoginName()); + sendCommand(m_testConfiguration.getLoginPassword()); + + List answers = readAnswer(); + String last = getLastLine(answers); + assertTrue(last.startsWith("Welcome")); + } + + protected String getLastLine(List list) { + if (list == null || list.isEmpty()) return null; + return (String)list.get(list.size()-1); + } + + protected List readAnswer() { + try { + while (!m_reader.ready()) { ; } + } catch (IOException e) { + return null; + } + + StringBuffer stringBuffer = new StringBuffer(); + char[] charBuffer = new char[100]; + List allAnswerLines = new ArrayList(); + try { + int readCount; + while ((m_reader.ready() && (readCount = m_reader.read(charBuffer)) > 0)) { + stringBuffer.append(charBuffer, 0, readCount); + } + } catch (IOException e) { + fail("reading remote manager answer failed"); + } + + allAnswerLines.addAll(Arrays.asList(stringBuffer.toString().split("\n"))); + if ("".equals(getLastLine(allAnswerLines))) allAnswerLines.remove(allAnswerLines.size()-1); + return allAnswerLines; + } + + protected void sendCommand(String command) throws IOException { + m_writer.write(command + "\n"); + m_writer.flush(); + } + + protected void connect() throws IOException { + m_telnetClient = new TelnetClient(); + m_telnetClient.connect(m_host, m_remoteManagerListenerPort); + + m_reader = new BufferedReader(new InputStreamReader(m_telnetClient.getInputStream())); + m_writer = new OutputStreamWriter(m_telnetClient.getOutputStream()); + } + + private MockServiceManager setUpServiceManager() { + MockServiceManager serviceManager = new MockServiceManager(); + SimpleConnectionManager connectionManager = new SimpleConnectionManager(); + connectionManager.enableLogging(new MockLogger()); + serviceManager.put(JamesConnectionManager.ROLE, connectionManager); + MockMailServer mailServer = new MockMailServer(); + serviceManager.put("org.apache.james.services.MailServer", mailServer); + m_mockUsersRepository = mailServer.getUsersRepository(); + serviceManager.put("org.apache.james.services.UsersRepository", m_mockUsersRepository); + serviceManager.put("org.apache.james.services.UsersStore", new MockUsersStore(m_mockUsersRepository)); + serviceManager.put(SocketManager.ROLE, new MockSocketManager(m_remoteManagerListenerPort)); + serviceManager.put(ThreadManager.ROLE, new MockThreadManager()); + return serviceManager; + } + + public void testLogin() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + + login(); + } + + public void testWrongLoginUser() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + + sendCommand("sindbad"); + sendCommand(m_testConfiguration.getLoginPassword()); + + List answers = readAnswer(); + String last = getLastLine(answers); + assertTrue(last.startsWith("Login id:")); // login failed, getting new login prompt + } + + public void testWrongLoginPassword() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + + sendCommand(m_testConfiguration.getLoginName()); + sendCommand("getmethru"); + + List answers = readAnswer(); + String last = getLastLine(answers); + assertTrue(last.startsWith("Login id:")); // login failed, getting new login prompt + } + + public void testUserCount() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + login(); + + sendCommand("countusers"); + assertTrue(getLastLine(readAnswer()).endsWith(" 0")); // no user yet + + sendCommand("adduser testCount1 testCount"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("countusers"); + assertTrue(getLastLine(readAnswer()).endsWith(" 1")); // 1 total + + sendCommand("adduser testCount2 testCount"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("countusers"); + assertTrue(getLastLine(readAnswer()).endsWith(" 2")); // 2 total + + m_mockUsersRepository.removeUser("testCount1"); + + sendCommand("countusers"); + assertTrue(getLastLine(readAnswer()).endsWith(" 1")); // 1 total + } + + public void testAddUserAndVerify() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + login(); + + sendCommand("adduser testAdd test"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("verify testNotAdded"); + assertTrue(getLastLine(readAnswer()).endsWith(" does not exist")); + + sendCommand("verify testAdd"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + + sendCommand("deluser testAdd"); + readAnswer(); + + sendCommand("verify testAdd"); + assertTrue(getLastLine(readAnswer()).endsWith(" does not exist")); + } + + public void testDelUser() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + login(); + + sendCommand("adduser testDel test"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("deluser testNotDeletable"); + assertTrue(getLastLine(readAnswer()).endsWith(" doesn't exist")); + + sendCommand("verify testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + + sendCommand("deluser testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" deleted")); + + sendCommand("verify testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" does not exist")); + } + + public void testCommandCaseInsensitive() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + login(); + + sendCommand("adduser testDel test"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("verify testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + + sendCommand("VERIFY testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + + sendCommand("vErIfY testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + } + + public void testParameterCaseSensitive() throws IOException { + finishSetUp(m_testConfiguration); + connect(); + login(); + + sendCommand("adduser testDel test"); + assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success + + sendCommand("verify testDel"); + assertTrue(getLastLine(readAnswer()).endsWith(" exists")); + + sendCommand("verify TESTDEL"); + assertTrue(getLastLine(readAnswer()).endsWith(" does not exist")); + + sendCommand("verify testdel"); + assertTrue(getLastLine(readAnswer()).endsWith(" does not exist")); + } +} Index: src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java =================================================================== --- src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java (revision 0) +++ src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java (revision 0) @@ -0,0 +1,84 @@ +/*********************************************************************** + * Copyright (c) 2000-2006 The Apache Software Foundation. * + * All rights reserved. * + * ------------------------------------------------------------------- * + * Licensed under the Apache License, Version 2.0 (the "License"); you * + * may not use this file except in compliance with the License. You * + * may obtain a copy of the License at: * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * + * implied. See the License for the specific language governing * + * permissions and limitations under the License. * + ***********************************************************************/ + + +package org.apache.james.remotemanager; + +import org.apache.avalon.framework.configuration.DefaultConfiguration; +import org.apache.james.test.util.Util; + +public class RemoteManagerTestConfiguration extends DefaultConfiguration { + + private int m_remoteManagerListenerPort; + private Integer m_connectionLimit = null; + private String m_loginName = "testLogin"; + private String m_loginPassword = "testPassword"; + + public RemoteManagerTestConfiguration(int smtpListenerPort) { + super("smptserver"); + + m_remoteManagerListenerPort = smtpListenerPort; + } + + public void setConnectionLimit(int iConnectionLimit) { + m_connectionLimit = new Integer(iConnectionLimit); + } + + public String getLoginName() { + return m_loginName; + } + + public void setLoginName(String loginName) { + m_loginName = loginName; + } + + public String getLoginPassword() { + return m_loginPassword; + } + + public void setLoginPassword(String loginPassword) { + m_loginPassword = loginPassword; + } + + public void init() { + + setAttribute("enabled", true); + + addChild(Util.getValuedConfiguration("port", "" + m_remoteManagerListenerPort)); + if (m_connectionLimit != null) addChild(Util.getValuedConfiguration("connectionLimit", "" + m_connectionLimit.intValue())); + + DefaultConfiguration handlerConfig = new DefaultConfiguration("handler"); + handlerConfig.addChild(Util.getValuedConfiguration("helloName", "myMailServer")); + handlerConfig.addChild(Util.getValuedConfiguration("connectiontimeout", "360000")); + + DefaultConfiguration adminAccounts = new DefaultConfiguration("administrator_accounts"); + + DefaultConfiguration account = new DefaultConfiguration("account"); + + account.setAttribute("login", m_loginName); + account.setAttribute("password", m_loginPassword); + + adminAccounts.addChild(account); + handlerConfig.addChild(adminAccounts); + + // handlerConfig.addChild(Util.getValuedConfiguration("prompt", ">")); + + handlerConfig.addChild(Util.createRemoteManagerHandlerChainConfiguration()); + addChild(handlerConfig); + } + +} Index: src/test/org/apache/james/core/MailImplTest.java =================================================================== --- src/test/org/apache/james/core/MailImplTest.java (revision 366206) +++ src/test/org/apache/james/core/MailImplTest.java (working copy) @@ -69,7 +69,7 @@ helperTestInitialState(mail); helperTestMessageSize(mail, mimeMessage.getSize()); // MockMimeMessage default is -1 (accord. to javax.mail javadoc) - assertEquals("initial message", mimeMessage, mail.getMessage()); + assertEquals("initial message", mimeMessage.getMessageID(), mail.getMessage().getMessageID()); assertEquals("sender", sender, mail.getSender().toString()); assertEquals("name", name, mail.getName()); } Index: src/test/org/apache/james/pop3server/POP3ServerTest.java =================================================================== --- src/test/org/apache/james/pop3server/POP3ServerTest.java (revision 366206) +++ src/test/org/apache/james/pop3server/POP3ServerTest.java (working copy) @@ -142,7 +142,7 @@ pop3Protocol.quit(); } - public void testknownUserEmptyInbox() throws Exception, POP3Exception { + public void testKnownUserEmptyInbox() throws Exception, POP3Exception { finishSetUp(m_testConfiguration); POP3Protocol pop3Protocol = new POP3Protocol("127.0.0.1", @@ -197,7 +197,7 @@ pop3Protocol.quit(); } - public void testknownUserInboxWithMessages() throws Exception, + public void testKnownUserInboxWithMessages() throws Exception, POP3Exception { finishSetUp(m_testConfiguration); Index: src/test/org/apache/james/smtpserver/SMTPServerTest.java =================================================================== --- src/test/org/apache/james/smtpserver/SMTPServerTest.java (revision 366206) +++ src/test/org/apache/james/smtpserver/SMTPServerTest.java (working copy) @@ -29,6 +29,7 @@ import org.apache.james.test.util.Util; import org.apache.james.util.Base64; import org.apache.james.util.connection.SimpleConnectionManager; +import org.apache.mailet.MailAddress; import org.columba.ristretto.composer.MimeTreeRenderer; import org.columba.ristretto.io.CharSequenceSource; import org.columba.ristretto.message.Address; @@ -43,12 +44,14 @@ import com.sun.mail.util.SharedByteArrayInputStream; import javax.mail.internet.MimeMessage; +import javax.mail.internet.ParseException; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.util.Arrays; import java.util.List; +import java.util.Collection; import junit.framework.TestCase; @@ -66,6 +69,17 @@ super("SMTPServerTest"); } + public void verifyLastMail(String sender, String recipient, MimeMessage msg) throws ParseException { + Object[] mailData = m_mailServer.getLastMail(); + assertNotNull("mail received by mail server", mailData); + + if (sender == null && recipient == null && msg == null) fail("no verification can be done with all arguments null"); + + if (sender != null) assertEquals("sender verfication", sender, ((MailAddress)mailData[0]).toString()); + if (recipient != null) assertTrue("recipient verfication", ((Collection) mailData[1]).contains(new MailAddress(recipient))); + if (msg != null) assertEquals("message verification", msg, ((MimeMessage) mailData[2])); + } + protected void setUp() throws Exception { m_smtpServer = new SMTPServer(); m_smtpServer.enableLogging(new MockLogger()); @@ -95,6 +109,17 @@ return serviceManager; } + private LocalMimePart createMail() { + MimeHeader mimeHeader = new MimeHeader(new Header()); + mimeHeader.set("Mime-Version", "1.0"); + LocalMimePart mail = new LocalMimePart(mimeHeader); + MimeHeader header = mail.getHeader(); + header.setMimeType(new MimeType("text", "plain")); + + mail.setBody(new CharSequenceSource("James Unit Test Body")); + return mail; + } + public void testSimpleMailSendWithEHLO() throws Exception, SMTPException { finishSetUp(m_testConfiguration); @@ -121,7 +146,7 @@ // mail was propagated by SMTPServer assertNotNull("mail received by mail server", m_mailServer.getLastMail()); } - + public void testEmptyMessage() throws Exception { InputStream mSource = new SharedByteArrayInputStream(("").getBytes()); finishSetUp(m_testConfiguration); @@ -144,11 +169,9 @@ int size = ((MimeMessage) m_mailServer.getLastMail()[2]).getSize(); - assertEquals(size,2); + assertEquals(size, 2); } - - public void testSimpleMailSendWithHELO() throws Exception, SMTPException { finishSetUp(m_testConfiguration); @@ -171,15 +194,40 @@ assertNotNull("mail received by mail server", m_mailServer.getLastMail()); } - private LocalMimePart createMail() { - MimeHeader mimeHeader = new MimeHeader(new Header()); - mimeHeader.set("Mime-Version", "1.0"); - LocalMimePart mail = new LocalMimePart(mimeHeader); - MimeHeader header = mail.getHeader(); - header.setMimeType(new MimeType("text", "plain")); + public void testTwoSimultaneousMails() throws Exception, SMTPException { + finishSetUp(m_testConfiguration); - mail.setBody(new CharSequenceSource("James Unit Test Body")); - return mail; + SMTPProtocol smtpProtocol1 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort); + SMTPProtocol smtpProtocol2 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort); + smtpProtocol1.openPort(); + smtpProtocol2.openPort(); + + assertEquals("first connection taken", 1, smtpProtocol1.getState()); + assertEquals("second connection taken", 1, smtpProtocol2.getState()); + + // no message there, yet + assertNull("no mail received by mail server", m_mailServer.getLastMail()); + + smtpProtocol1.helo(InetAddress.getLocalHost()); + + String sender1 = "mail_sender1@localhost"; + String recipient1 = "mail_recipient1@localhost"; + smtpProtocol1.mail(new Address(sender1)); + smtpProtocol1.rcpt(new Address(recipient1)); + + String sender2 = "mail_sender2@localhost"; + String recipient2 = "mail_recipient2@localhost"; + smtpProtocol2.mail(new Address(sender2)); + smtpProtocol2.rcpt(new Address(recipient2)); + + smtpProtocol1.data(MimeTreeRenderer.getInstance().renderMimePart(createMail())); + verifyLastMail(sender1, recipient1, null); + + smtpProtocol2.data(MimeTreeRenderer.getInstance().renderMimePart(createMail())); + verifyLastMail(sender2, recipient2, null); + + smtpProtocol1.quit(); + smtpProtocol2.quit(); } public void testAuth() throws Exception, SMTPException { @@ -340,7 +388,6 @@ body.append("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); body.append("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); body.append("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); - body.append("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); body.append("1234567810123456782012345"); // 1025 chars mail.setBody(new CharSequenceSource(body.toString())); @@ -353,6 +400,19 @@ } } + + public void testConnectionLimitExceeded() throws Exception, SMTPException { + m_testConfiguration.setConnectionLimit(1); // allow no more than one connection at a time + finishSetUp(m_testConfiguration); + + SMTPProtocol smtpProtocol1 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort); + SMTPProtocol smtpProtocol2 = new SMTPProtocol("127.0.0.1", m_smtpListenerPort); + smtpProtocol1.openPort(); + assertEquals("first connection taken", 1, smtpProtocol1.getState()); + + smtpProtocol2.openPort(); + assertEquals("second connection not taken", SMTPProtocol.NOT_CONNECTED, smtpProtocol2.getState()); + } } class MySMTPProtocol extends SMTPProtocol Index: src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java =================================================================== --- src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java (revision 366206) +++ src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java (working copy) @@ -28,6 +28,7 @@ private String m_authorizedAddresses = "127.0.0.0/8"; private String m_authorizingMode = "false"; private boolean m_verifyIdentity = false; + private Integer m_connectionLimit = null; public SMTPTestConfiguration(int smtpListenerPort) { super("smptserver"); @@ -67,43 +68,27 @@ m_verifyIdentity = true; } + public void setConnectionLimit(int iConnectionLimit) { + m_connectionLimit = new Integer(iConnectionLimit); + } + public void init() { setAttribute("enabled", true); addChild(Util.getValuedConfiguration("port", "" + m_smtpListenerPort)); - + if (m_connectionLimit != null) addChild(Util.getValuedConfiguration("connectionLimit", "" + m_connectionLimit.intValue())); + DefaultConfiguration handlerConfig = new DefaultConfiguration("handler"); handlerConfig.addChild(Util.getValuedConfiguration("helloName", "myMailServer")); handlerConfig.addChild(Util.getValuedConfiguration("connectiontimeout", "360000")); handlerConfig.addChild(Util.getValuedConfiguration("authorizedAddresses", m_authorizedAddresses)); handlerConfig.addChild(Util.getValuedConfiguration("maxmessagesize", "" + m_maxMessageSize)); handlerConfig.addChild(Util.getValuedConfiguration("authRequired", m_authorizingMode)); - if (m_verifyIdentity) handlerConfig.addChild(Util.getValuedConfiguration("verifyIdentity", "" + m_verifyIdentity)); + if (m_verifyIdentity) handlerConfig.addChild(Util.getValuedConfiguration("verifyIdentity", "" + m_verifyIdentity)); - DefaultConfiguration handlerChainConfig = new DefaultConfiguration("handlerchain"); - handlerChainConfig.addChild(createCommandHandlerConfiguration("HELO", HeloCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("EHLO", EhloCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("AUTH", AuthCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("VRFY", VrfyCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("EXPN", ExpnCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("MAIL", MailCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("RCPT", RcptCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("DATA", DataCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("RSET", RsetCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("HELP", HelpCmdHandler.class)); - handlerChainConfig.addChild(createCommandHandlerConfiguration("QUIT", QuitCmdHandler.class)); - - handlerConfig.addChild(handlerChainConfig); + handlerConfig.addChild(Util.createRemoteManagerHandlerChainConfiguration()); addChild(handlerConfig); } - private DefaultConfiguration createCommandHandlerConfiguration(String command, Class commandClass) { - DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration("handler"); - cmdHandlerConfig.setAttribute("command", command); - String classname = commandClass.getName(); - cmdHandlerConfig.setAttribute("class", classname); - return cmdHandlerConfig; - } - } Index: build.xml =================================================================== --- build.xml (revision 366206) +++ build.xml (working copy) @@ -788,7 +788,7 @@ - +