001: package test;
002:
003: import dalma.ReplyIterator;
004: import dalma.endpoints.email.EmailEndPoint;
005: import dalma.endpoints.email.NewMailHandler;
006: import dalma.endpoints.email.MimeMessageEx;
007: import dalma.test.WorkflowTestProgram;
008: import junit.textui.TestRunner;
009:
010: import javax.mail.Message;
011: import javax.mail.MessagingException;
012: import javax.mail.internet.InternetAddress;
013: import javax.mail.internet.MimeMessage;
014: import java.io.Serializable;
015: import java.util.UUID;
016:
017: /**
018: * @author Kohsuke Kawaguchi
019: */
020: public class EmailTest extends WorkflowTestProgram {
021:
022: public EmailTest(String name) {
023: super (name);
024: }
025:
026: public static void main(String[] args) throws Exception {
027: TestRunner.run(EmailTest.class);
028: }
029:
030: EmailEndPoint ep1;
031: EmailEndPoint ep2;
032:
033: protected void setupEndPoints() throws Exception {
034: // passive side
035: ep1 = (EmailEndPoint) engine.addEndPoint("email1",
036: getProperty("email.endpoint1"));
037: ep1.setNewMailHandler(new NewMailHandler() {
038: public void onNewMail(MimeMessage mail) throws Exception {
039: System.out.println("new e-mail");
040: createConversation(Bob.class, ep1, mail);
041: }
042: });
043:
044: // active side
045: ep2 = (EmailEndPoint) engine.addEndPoint("email2",
046: getProperty("email.endpoint2"));
047: }
048:
049: public void test() throws Throwable {
050: for (int i = 0; i < 10; i++)
051: createConversation(Alice.class, ep2, ep1.getAddress());
052: engine.waitForCompletion();
053: }
054:
055: public static final class Alice implements Runnable, Serializable {
056: private final EmailEndPoint ep;
057: private final InternetAddress adrs;
058:
059: public Alice(EmailEndPoint ep, InternetAddress adrs) {
060: this .ep = ep;
061: this .adrs = adrs;
062: }
063:
064: public void run() {
065: try {
066: UUID uuid = UUID.randomUUID();
067:
068: System.out.println("A: initiating conversation. UUID="
069: + uuid);
070:
071: // ep2 -> ep1 to iniciate a conversation
072: MimeMessage msg = new MimeMessageEx(ep.getSession());
073: msg.setRecipient(Message.RecipientType.TO, adrs);
074: msg.setSubject("new conv");
075: msg.setText(uuid.toString());
076: msg = ep.waitForReply(msg);
077: System.out.println("A: got a reply");
078:
079: // send multiple replies
080: for (int i = 0; i < 10; i++) {
081: MimeMessage reply = (MimeMessage) msg.reply(false);
082: reply.setText(uuid.toString());
083: System.out.println("A: sending a reply");
084: ep.send(reply);
085: }
086:
087: // and finally say bye
088: System.out.println("A: sending bye");
089: MimeMessage reply = (MimeMessage) msg.reply(false);
090: reply.setText("bye");
091: ep.send(reply);
092:
093: System.out.println("A: exiting");
094: } catch (MessagingException e) {
095: e.printStackTrace();
096: }
097: }
098: }
099:
100: public static final class Bob implements Runnable, Serializable {
101: private final EmailEndPoint ep;
102:
103: // initial e-mail
104: private MimeMessage email;
105:
106: public Bob(EmailEndPoint ep, MimeMessage email) {
107: this .ep = ep;
108: this .email = email;
109: }
110:
111: public void run() {
112: try {
113: MimeMessage msg = email;
114:
115: UUID uuid = UUID.fromString(msg.getContent().toString()
116: .trim());
117: System.out.println("B: started " + uuid);
118:
119: msg = (MimeMessage) msg.reply(false);
120: msg.setText("welcome");
121:
122: ReplyIterator<MimeMessage> itr = ep
123: .waitForMultipleReplies(msg);
124: while (itr.hasNext()) {
125: MimeMessage in = itr.next();
126: if (in.getContent().toString().contains("bye"))
127: break;
128:
129: // make sure that the UUID matches
130: assertEquals(uuid, UUID.fromString(in.getContent()
131: .toString().trim()));
132:
133: System.out.println("B: got a reply.");
134: }
135:
136: System.out.println("B: done");
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140: }
141:
142: private static final long serialVersionUID = 1L;
143: }
144: }
|