2010-06-17

GAE/J + Slim3 : メール受信アプリを作ってみる(3)

今回はMailServiceTestを作成する。
まずは受信メールを表すモデルクラス MailItemを作成しておく。
  • メール受信のサービス: "gen-model"で /src/../model/MailItem.java と /test/../model/MailItem.java を生成する。
  • 属性はfrom, to, subject, bodyを作成する。型は全てStringとする。

では、MailService#receive(String pAddress, InputStream pInputStream)のテストを作成する。
文字コードは、現時点では余計なことは考えたくないので"UTF-8"としておく。またマルチパートも現時点では考慮しない。
最初は、単純で基本的な処理から考えていくのが"吉"です。(^_^)

テストメソッドは以下のようになった。
public class MailServiceTest extends AppEngineTestCase {
    private MailService service = new MailService();

    /**
     * シングルパート(text/plain)を入力し{@link MailItem}オブジェクトが生成されること
     * @throws UnsupportedEncodingException
     */
    @Test
    public void receive_plainText_UTF8() throws UnsupportedEncodingException {
        String mailFormat = "Received: by 10.115.25.19 with SMTP id c19mr2897164waj.34.1275464064977;\n"
            + "Wed, 02 Jun 2010 00:12:34 -0700 (PDT)\n"
            + "Return-Path: <%1$s>\n"
            + "Received: from smtp.hoge.org (ml.hoge.org [128.1.2.3])\n"
            + "by gmr-mx.google.com with ESMTP id u10si11332059wak.6.2010.06.02.00.34.24;\n"
            + "Wed, 02 Jun 2010 00:12:34 -0700 (PDT)\n"
            + "Received-SPF: pass (google.com: best guess record for domain of sender@hoge.org designates 128.1.2.3 as permitted sender) client-ip=128.1.2.3;\n"
            + "Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of sender@hoge.org designates 128.1.2.3 as permitted sender) smtp.mail=sender@hoge.org\n"
            + "Received: from [128.1.2.3] (unknown [128.1.2.3])\n"
            + "by smtp.hoge.org (Postfix) with ESMTPA id 8F1C716838E\n"
            + "for <%2$s>; Wed,  2 Jun 2010 00:12:34 -0700 (PDT)\n"
            + "Message-ID: <4C060983.1090109@hoge.org>\n"
            + "Date: Wed, 02 Jun 2010 00:12:34 -0700\n"
            + "From: SENDER <%1$s>\n"
            + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ja; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4\n"
            + "MIME-Version: 1.0\n"
            + "To: RECEIVER <%2$s>\n"
            + "Subject: %3$s\n"
            + "Content-Type: text/plain; charset=UTF-8\n"
            + "\n"
            + "%4$s\n";
        String encodedSubject = MimeUtility.encodeText("これは件名", "UTF-8", "B");
        String mailStr = String.format(mailFormat, "sender@hoge.org", "reciever@fuga.org", encodedSubject, "本文1行目\n本文2行目");

        byte[] bytes = mailStr.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);
        MailItem mailItem = service.receive("sender@hoge.org", in);
        assertThat(mailItem.getFrom(), is("SENDER <sender@hoge.org>"));
        assertThat(mailItem.getTo(), is("RECEIVER <reciever@fuga.org>"));
        assertThat(mailItem.getBody(), is("本文1行目\n本文2行目\n"));
        assertThat(mailItem.getSubject(), is("これは件名"));
    }
}
内容は、
  • encodedSubjectに、件名を"UTF-8"でエンコードした文字列をセットする(=?UTF-8?B?44GT44KM44Gv5Lu25ZCN?= となる)
  • mailStrにヘッダーを含めた受信メールの文字列を作成する
  • 次の2行でこのmailStrをInputStramに変換する
    byte[] bytes = mailStr.getBytes();
    InputStream in = new ByteArrayInputStream(bytes);
    
  • MailService#receive()メソッドを実行する
  • 生成されたmailItemの内容を検証する
となっている。

この時点ではコンパイルエラー(MailService#receive()が未定義)となっているはず。
次回はこのテストをパスするようにMailService#receive()を実装していく。

0 件のコメント: