1 package club.wpia.gigi.email;
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;
13 * This class intercepts emails so that the test cases can evaluate them
16 public class TestEmailProvider extends DelegateMailProvider {
18 private ServerSocket servs;
20 private Socket client;
22 private DataOutputStream out;
24 private DataInputStream in;
26 protected TestEmailProvider(Properties props) {
27 super(props, props.getProperty("emailProvider.test.target"));
29 servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
30 } catch (IOException e) {
36 public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
38 if ( !ensureLocalConnection() && getTarget() != null) {
39 super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
53 } catch (IOException e) {
59 private boolean ensureLocalConnection() throws IOException {
63 } catch (IOException e) {
67 if (client == null || client.isClosed()) {
68 servs.setSoTimeout(2000);
70 client = servs.accept();
71 } catch (SocketTimeoutException e) {
74 out = new DataOutputStream(client.getOutputStream());
75 in = new DataInputStream(client.getInputStream());
81 public synchronized String checkEmailServer(int forUid, String address) throws IOException {
83 if ( !ensureLocalConnection() && getTarget() != null) {
84 return super.checkEmailServer(forUid, address);
87 out.writeUTF("challengeAddrBox");
88 out.writeUTF(address);
90 } catch (IOException e) {
96 private void write(String to) throws IOException {
98 out.writeUTF("<null>");