]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Adding automated system bootup tests.
[gigi.git] / tests / org / cacert / gigi / testUtils / TestEmailReciever.java
1 package org.cacert.gigi.testUtils;
2
3 import java.io.DataInputStream;
4 import java.io.IOException;
5 import java.net.SocketAddress;
6 import java.net.Socket;
7 import java.util.concurrent.LinkedBlockingQueue;
8 import java.util.concurrent.TimeUnit;
9
10 public class TestEmailReciever implements Runnable {
11         public class TestMail {
12                 String to;
13                 String subject;
14                 String message;
15                 String from;
16                 String replyto;
17                 public TestMail(String to, String subject, String message, String from,
18                                 String replyto) {
19                         this.to = to;
20                         this.subject = subject;
21                         this.message = message;
22                         this.from = from;
23                         this.replyto = replyto;
24                 }
25                 public String getTo() {
26                         return to;
27                 }
28                 public String getSubject() {
29                         return subject;
30                 }
31                 public String getMessage() {
32                         return message;
33                 }
34                 public String getFrom() {
35                         return from;
36                 }
37                 public String getReplyto() {
38                         return replyto;
39                 }
40
41         }
42         private Socket s;
43         private DataInputStream dis;
44
45         public TestEmailReciever(SocketAddress target) throws IOException {
46                 s = new Socket();
47                 s.connect(target);
48                 s.setKeepAlive(true);
49                 s.setSoTimeout(1000 * 60 * 60);
50                 dis = new DataInputStream(s.getInputStream());
51                 new Thread(this).start();
52         }
53         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
54
55         public TestMail recieve() throws InterruptedException {
56                 return mails.poll(5, TimeUnit.SECONDS);
57         }
58         @Override
59         public void run() {
60                 try {
61                         while (true) {
62                                 String to = dis.readUTF();
63                                 String subject = dis.readUTF();
64                                 String message = dis.readUTF();
65                                 String from = dis.readUTF();
66                                 String replyto = dis.readUTF();
67                                 mails.add(new TestMail(to, subject, message, from, replyto));
68                         }
69                 } catch (IOException e) {
70                         e.printStackTrace();
71                 }
72
73         }
74
75         public void clearMails() {
76                 mails.clear();
77         }
78
79 }