1 package net.jaekl.qd.util;
3 import java.nio.charset.Charset;
5 public class SmtpConversationMock {
6 private InputStreamMock m_is;
7 private OutputStreamMock m_os;
9 String[][] m_responses;
11 private class Listener implements OutputStreamMock.Listener {
13 public void onWrite(int b) {
14 String stream = m_os.mock_getContent(Charset.forName(FileMock.UTF_8));
15 if (stream.endsWith("\r\n")) {
16 int pos = stream.lastIndexOf("\r\n", stream.length() - 3);
20 String cmd = stream.substring(pos + 2);
21 String reply = replyFor(cmd);
23 m_is.mock_append("\r\n" + reply + "\r\n", Charset.forName(FileMock.UTF_8));
28 private String replyFor(String cmd) {
29 for (int idx = 0; idx < m_responses.length; ++idx) {
30 if (cmd.startsWith(m_responses[idx][0])) {
31 return m_responses[idx][1];
34 return null; // no canned response for this input
38 // responses: array of 2-tuples of Strings { {input, output}, {input, output}, ... }
39 public SmtpConversationMock(String init, String[][] responses) {
40 assert(null != responses);
42 m_is = new InputStreamMock();
43 m_os = new OutputStreamMock();
45 m_responses = responses.clone();
47 Listener listener = new Listener();
48 m_os.mock_setListener(listener);
50 m_is.mock_reset(init.getBytes(Charset.forName(FileMock.UTF_8)));
53 public InputStreamMock getInputStream() { return m_is; }
54 public OutputStreamMock getOutputStream() { return m_os; }