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 hb...@apache.org on 2002/10/15 06:36:31 UTC
cvs commit: jakarta-james/tests/src/java/org/apache/james/testing SMTPTest.java BaseTest.java
hbedi 2002/10/14 21:36:31
Modified: tests/src/java/org/apache/james/testing BaseTest.java
Added: tests/src/java/org/apache/james/testing SMTPTest.java
Log:
tests for smtp server. first cut. needs more work.
Revision Changes Path
1.3 +15 -0 jakarta-james/tests/src/java/org/apache/james/testing/BaseTest.java
Index: BaseTest.java
===================================================================
RCS file: /home/cvs/jakarta-james/tests/src/java/org/apache/james/testing/BaseTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BaseTest.java 14 Oct 2002 06:44:22 -0000 1.2
+++ BaseTest.java 15 Oct 2002 04:36:31 -0000 1.3
@@ -72,4 +72,19 @@
}
}
}
+
+ // ------------ helper methods ----------------------
+
+ /**
+ * @param conf Test configuration
+ * @param name Child name
+ * @return String values of child elements
+ */
+ protected String[] getChildrenValues(Configuration conf,String name) throws ConfigurationException {
+ Configuration[] childconf = conf.getChildren(name);
+ String[] val = new String[childconf.length];
+ for ( int i = 0 ; i < childconf.length ; i++ )
+ val[i] = childconf[i].getValue();
+ return val;
+ }
}
1.1 jakarta-james/tests/src/java/org/apache/james/testing/SMTPTest.java
Index: SMTPTest.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.james.testing;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.smtp.SMTP;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.james.util.RFC822DateFormat;
/**
* Send email. Can be configured and extended to test specific SMTP
* operations.
*/
public class SMTPTest extends BaseTest {
/**
* The SMTP host to be tested.
*/
private String host;
/**
* The name of the user account being used to send the test emails.
*/
private String username;
/**
* The password of the user account being used to send the test emails.
*/
private String password;
/** mail from */
private String from;
/** mail to */
private String[] to;
/** send mail msg copy to */
private String[] cc;
/** send mail msg blind copy to */
private String[] bcc;
/** mail message */
private String mailMsg;
/**
* Sole constructor for the test case.
*
* @param name the name of the test case
*/
public SMTPTest(String name) {
super(name);
}
/**
* @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
*/
public void configure(Configuration configuration)
throws ConfigurationException {
this.host = configuration.getChild("host").getValue();
this.username = configuration.getChild("username").getValue(null);
this.password = configuration.getChild("password").getValue(null);
this.from = configuration.getChild("from").getValue();
this.to = getChildrenValues(configuration,"to");
this.cc = getChildrenValues(configuration,"cc");
this.bcc = getChildrenValues(configuration,"bcc");
int msgSize = configuration.getChild("msgsize").getValueAsInteger(1000);
String subject = configuration.getChild("subject").getValue("");
this.mailMsg = createMailMsg(subject,msgSize);
super.configure(configuration);
}
/**
* SMTP client that provides a simple interface to the SMTP interaction
* from the client side.
*/
private SMTPClient client;
/**
* Sets up the test case by creating the SMTP client and connecting
* to the host.
*/
protected void setUp() throws Exception {
client = new SMTPClient();
}
/**
* The number of messages sent
*/
private static int msgSentCounter;
/**
* The number of failed msgs
*/
private static int failureCounter;
/**
* Send Mail message
*/
public void sendMsg() throws Exception {
try {
client.connect(host);
// HBNOTE: extend this to do to, cc, bcc.
client.sendSimpleMessage("postmaster@localhost", to[0], mailMsg);
System.out.println("msgs sent="+(++msgSentCounter));
} catch( Throwable t) {
System.out.println("msg send failures="+(++failureCounter));
} finally {
try {
client.disconnect();
} catch(Throwable t) { }
}
}
// ------ helper methods ------
/**
* @param arr of strings
* @param sep separator character.
* @return concatenates a an array of strings.
*/
private String toString(String[] arr,char sep) {
StringBuffer buf = new StringBuffer();
for ( int i = 0 ; i < arr.length ; i++ ) {
if ( i > 0 )
buf.append(sep);
buf.append(arr[i]);
}
return buf.toString();
}
private String createMailMsg(String subject,int msgSize) {
StringWriter str = new StringWriter();
final char[] CRLF = new char[] { '\r', '\n' };
PrintWriter prt = new PrintWriter(str) {
public void println() {
write(CRLF,0,CRLF.length);
flush();
}
};
prt.println("From: "+from);
String to = toString(this.to,';');
if ( to.length() > 0 )
prt.println("To: "+to);
String cc = toString(this.cc,';');
if ( cc.length() > 0 )
prt.println("CC: "+cc);
prt.println("Subject: "+subject);
prt.println("Date: "+RFC822DateFormat.toString(new Date()));
prt.println("MIME-Version: 1.0");
prt.println("Content-Type: text/plain; charset=\"iso-8859-1\"");
prt.println();
char[] ca = new char[msgSize];
for (int i = 0; i < msgSize; i++)
ca[i] = 'm';
prt.print(new String(ca));
prt.flush();
return str.toString();
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>