You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/01/07 16:45:57 UTC
svn commit: r366800 - in /james/server/trunk: ./ src/java/org/apache/james/
src/test/org/apache/james/ src/test/org/apache/james/core/
src/test/org/apache/james/pop3server/
src/test/org/apache/james/remotemanager/
src/test/org/apache/james/services/ sr...
Author: bago
Date: Sat Jan 7 07:45:40 2006
New Revision: 366800
URL: http://svn.apache.org/viewcvs?rev=366800&view=rev
Log:
Removed an NPE on getUserInbox for an unknown user: now we simply return null.
Upgraded unit tests to 3.0, thanks to Bernd Fondermann again (JAMES-427).
Added:
james/server/trunk/src/test/org/apache/james/remotemanager/
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java
james/server/trunk/src/test/org/apache/james/services/MailServerTestAllImplementations.java
Modified:
james/server/trunk/build.xml
james/server/trunk/src/java/org/apache/james/James.java
james/server/trunk/src/test/org/apache/james/JamesTest.java
james/server/trunk/src/test/org/apache/james/core/MailImplTest.java
james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java
james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java
james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java
james/server/trunk/src/test/org/apache/james/test/mock/james/MockMailServer.java
james/server/trunk/src/test/org/apache/james/test/mock/james/MockUsersRepository.java
james/server/trunk/src/test/org/apache/james/test/util/Util.java
Modified: james/server/trunk/build.xml
URL: http://svn.apache.org/viewcvs/james/server/trunk/build.xml?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/build.xml (original)
+++ james/server/trunk/build.xml Sat Jan 7 07:45:40 2006
@@ -788,7 +788,7 @@
<echo message="Running James Unit Tests"/>
<mkdir dir="${build.test}"/>
<mkdir dir="${build.test.reports}"/>
- <junit printsummary="yes" haltonfailure="yes" >
+ <junit printsummary="yes" haltonfailure="no" >
<classpath refid="unittest.class.path"/>
<classpath refid="project.class.path"/>
<classpath>
Modified: james/server/trunk/src/java/org/apache/james/James.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/James.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/James.java (original)
+++ james/server/trunk/src/java/org/apache/james/James.java Sat Jan 7 07:45:40 2006
@@ -537,7 +537,9 @@
mboxConf.setAttribute("type", "MAIL");
try {
userInbox = (MailRepository) store.select(mboxConf);
- mailboxes.put(userName, userInbox);
+ if (userInbox!=null) {
+ mailboxes.put(userName, userInbox);
+ }
} catch (Exception e) {
if (getLogger().isErrorEnabled())
{
Modified: james/server/trunk/src/test/org/apache/james/JamesTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/JamesTest.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/JamesTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/JamesTest.java Sat Jan 7 07:45:40 2006
@@ -18,14 +18,15 @@
package org.apache.james;
-import org.apache.james.services.MailServerTestAllImplementations;
import org.apache.james.services.MailServer;
-import org.apache.james.test.mock.avalon.MockServiceManager;
+import org.apache.james.services.MailServerTestAllImplementations;
+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.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;
}
Modified: james/server/trunk/src/test/org/apache/james/core/MailImplTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/core/MailImplTest.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/core/MailImplTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/core/MailImplTest.java Sat Jan 7 07:45:40 2006
@@ -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());
}
Modified: james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/pop3server/POP3ServerTest.java Sat Jan 7 07:45:40 2006
@@ -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);
Added: james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java?rev=366800&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTest.java Sat Jan 7 07:45:40 2006
@@ -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"));
+ }
+}
Added: james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java?rev=366800&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java (added)
+++ james/server/trunk/src/test/org/apache/james/remotemanager/RemoteManagerTestConfiguration.java Sat Jan 7 07:45:40 2006
@@ -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);
+ }
+
+}
Added: james/server/trunk/src/test/org/apache/james/services/MailServerTestAllImplementations.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/services/MailServerTestAllImplementations.java?rev=366800&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/services/MailServerTestAllImplementations.java (added)
+++ james/server/trunk/src/test/org/apache/james/services/MailServerTestAllImplementations.java Sat Jan 7 07:45:40 2006
@@ -0,0 +1,114 @@
+/***********************************************************************
+ * 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.services;
+
+import junit.framework.TestCase;
+
+/**
+ * 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();
+
+ /**
+ * while addUser() is part of MailServer interface, a user cannot be tested for afterwards
+ * at the same time, James allows to do exactly this via isLocalUser(), other implementations
+ * might vary.
+ */
+ abstract public boolean canTestUserExists();
+ abstract public boolean isUserExisting(MailServer mailServerImpl, String username);
+
+ public void testId() {
+ MailServer mailServer = createMailServer();
+
+ String id = mailServer.getId();
+ assertNotNull("mail id not null", id);
+ assertFalse("mail id not empty", "".equals(id));
+ }
+
+ public void testIdIncrement() {
+ MailServer mailServer = createMailServer();
+
+ String id1 = mailServer.getId();
+ String id2 = mailServer.getId();
+ assertFalse("next id is different", id1.equals(id2));
+ }
+
+ public void testAddUser() {
+
+ // addUser acts on field localUsers for class org.apache.james.James
+ // thus, it is unrelated to getUserInbox() for the only known implementation of MailServer
+ // TODO clarify this
+
+ MailServer mailServer = createMailServer();
+
+ String userName = "testUserName";
+
+ if (canTestUserExists())
+ {
+ assertFalse("this is a fresh user", isUserExisting(mailServer, userName));
+ }
+
+ boolean allowsPasswordlessUser = allowsPasswordlessUser();
+ try {
+ boolean success = mailServer.addUser(userName, null);
+ if (!allowsPasswordlessUser) fail("null pwd was accepted unexpectedly");
+ if (!success) fail("null pwd was not accepted unexpectedly");
+ } catch (Exception e) {
+ if (allowsPasswordlessUser) fail("null pwd not accepted unexpectedly (with exception)");
+ }
+
+ userName = userName + "_next";
+ String password = "password";
+
+ boolean success = mailServer.addUser(userName, password);
+ if (!success) fail("user has not been added");
+
+ if (canTestUserExists())
+ {
+ assertTrue("user is present now", isUserExisting(mailServer, userName));
+ }
+
+ boolean successAgain = mailServer.addUser(userName, password);
+ if (successAgain) fail("user has been added two times");
+
+ }
+
+ public void testGetNonexistingUserInbox() {
+
+ MailServer mailServer = createMailServer();
+
+ String userName = "testNonexisitingUserName";
+ MailRepository userInbox = null;
+
+ userInbox = mailServer.getUserInbox(userName);
+ assertEquals("test user does not exist", null, userInbox);
+ }
+
+ public void testGetExisitingUserInbox() {
+ MailServer mailServer = createMailServer();
+
+ MailRepository userInbox = mailServer.getUserInbox(EXISTING_USER_NAME);
+ assertNotNull("existing user exists", userInbox);
+ }
+}
Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java Sat Jan 7 07:45:40 2006
@@ -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()));
@@ -352,6 +399,19 @@
assertEquals("expected 552 error", 552, e.getCode());
}
+ }
+
+ 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());
}
}
Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SMTPTestConfiguration.java Sat Jan 7 07:45:40 2006
@@ -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;
}
}
Modified: james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java Sat Jan 7 07:45:40 2006
@@ -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) {
Modified: james/server/trunk/src/test/org/apache/james/test/mock/james/MockMailServer.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/test/mock/james/MockMailServer.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/james/MockMailServer.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/james/MockMailServer.java Sat Jan 7 07:45:40 2006
@@ -31,7 +31,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 +39,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 +107,7 @@
}
public boolean addUser(String userName, String password) {
- m_users.put(userName, password);
+ m_users.addUser(userName, password);
return true;
}
Modified: james/server/trunk/src/test/org/apache/james/test/mock/james/MockUsersRepository.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/test/mock/james/MockUsersRepository.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/james/MockUsersRepository.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/james/MockUsersRepository.java Sat Jan 7 07:45:40 2006
@@ -64,7 +64,7 @@
}
public void removeUser(String name) {
- // trivial implementation
+ m_users.remove(name);
}
public boolean contains(String name) {
Modified: james/server/trunk/src/test/org/apache/james/test/util/Util.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/test/util/Util.java?rev=366800&r1=366799&r2=366800&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/util/Util.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/util/Util.java Sat Jan 7 07:45:40 2006
@@ -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;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org