]> WPIA git - gigi.git/blob - util-testing/club/wpia/gigi/email/TestEmailProvider.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / util-testing / club / wpia / gigi / email / TestEmailProvider.java
1 package club.wpia.gigi.email;
2
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.net.SocketTimeoutException;
10 import java.util.Properties;
11
12 /**
13  * This class intercepts emails so that the test cases can evaluate them
14  * automatically.
15  */
16 public class TestEmailProvider extends DelegateMailProvider {
17
18     private ServerSocket servs;
19
20     private Socket client;
21
22     private DataOutputStream out;
23
24     private DataInputStream in;
25
26     protected TestEmailProvider(Properties props) {
27         super(props, props.getProperty("emailProvider.test.target"));
28         try {
29             servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33     }
34
35     @Override
36     public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
37         while (true) {
38             if ( !ensureLocalConnection() && getTarget() != null) {
39                 super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
40                 return;
41             }
42             try {
43                 if (out == null) {
44                     continue;
45                 }
46                 out.writeUTF("mail");
47                 write(to);
48                 write(subject);
49                 write(message);
50                 write(replyto);
51                 out.flush();
52                 return;
53             } catch (IOException e) {
54                 client = null;
55             }
56         }
57     }
58
59     private boolean ensureLocalConnection() throws IOException {
60         if (out != null) {
61             try {
62                 out.writeUTF("ping");
63             } catch (IOException e) {
64                 client = null;
65             }
66         }
67         if (client == null || client.isClosed()) {
68             servs.setSoTimeout(2000);
69             try {
70                 client = servs.accept();
71             } catch (SocketTimeoutException e) {
72                 return false;
73             }
74             out = new DataOutputStream(client.getOutputStream());
75             in = new DataInputStream(client.getInputStream());
76         }
77         return true;
78     }
79
80     @Override
81     public synchronized String checkEmailServer(int forUid, String address) throws IOException {
82         while (true) {
83             if ( !ensureLocalConnection() && getTarget() != null) {
84                 return super.checkEmailServer(forUid, address);
85             }
86             try {
87                 out.writeUTF("challengeAddrBox");
88                 out.writeUTF(address);
89                 return in.readUTF();
90             } catch (IOException e) {
91                 client = null;
92             }
93         }
94     }
95
96     private void write(String to) throws IOException {
97         if (to == null) {
98             out.writeUTF("<null>");
99         } else {
100             out.writeUTF(to);
101         }
102     }
103 }