--- /dev/null
+package net.jaekl.qd.util;
+
+import java.nio.charset.Charset;
+
+public class SmtpConversationMock {
+ private InputStreamMock m_is;
+ private OutputStreamMock m_os;
+
+ String[][] m_responses;
+
+ private class Listener implements OutputStreamMock.Listener {
+ @Override
+ public void onWrite(int b) {
+ String stream = m_os.mock_getContent(Charset.forName(FileMock.UTF_8));
+ if (stream.endsWith("\r\n")) {
+ int pos = stream.lastIndexOf("\r\n", stream.length() - 3);
+ if (pos < 0) {
+ pos = -2;
+ }
+ String cmd = stream.substring(pos + 2);
+ String reply = replyFor(cmd);
+ if (null != reply) {
+ m_is.mock_append("\r\n" + reply + "\r\n", Charset.forName(FileMock.UTF_8));
+ }
+ }
+ }
+
+ private String replyFor(String cmd) {
+ for (int idx = 0; idx < m_responses.length; ++idx) {
+ if (cmd.startsWith(m_responses[idx][0])) {
+ return m_responses[idx][1];
+ }
+ }
+ return null; // no canned response for this input
+ }
+ }
+
+ // responses: array of 2-tuples of Strings { {input, output}, {input, output}, ... }
+ public SmtpConversationMock(String init, String[][] responses) {
+ assert(null != responses);
+
+ m_is = new InputStreamMock();
+ m_os = new OutputStreamMock();
+
+ m_responses = responses.clone();
+
+ Listener listener = new Listener();
+ m_os.mock_setListener(listener);
+
+ m_is.mock_reset(init.getBytes(Charset.forName(FileMock.UTF_8)));
+ }
+
+ public InputStreamMock getInputStream() { return m_is; }
+ public OutputStreamMock getOutputStream() { return m_os; }
+
+}