    1   package com.ozacc.mail.impl;
    2
    3   import java.io.File;
    4
    5   import javax.mail.internet.InternetAddress;
    6
    7   import junit.framework.TestCase;
    8
    9   import org.apache.log4j.BasicConfigurator;
    10
    11  import com.ozacc.mail.Mail;
    12  import com.ozacc.mail.MailBuildException;
    13  import com.ozacc.mail.MultipleMailBuilder;
    14
    15  /***
    16   *
    17   * @since 1.0.1
    18   * @author Tomohiro Otsuka
    19   * @version $Id: XMLMailBuilderImplTest.java,v 1.6.2.1 2005/01/21 22:17:27 otsuka Exp $
    20   */
    21  public class XMLMailBuilderImplTest extends TestCase {
    22
    23  	private MultipleMailBuilder builder;
    24
    25  	/*
    26  	 * @see TestCase#setUp()
    27  	 */
    28  	protected void setUp() throws Exception {
    29  		super.setUp();
    30  		BasicConfigurator.configure();
    31
    32  		builder = new XMLMailBuilderImpl();
    33  	}
    34
    35  	/***
    36  	 * @see junit.framework.TestCase#tearDown()
    37  	 */
    38  	protected void tearDown() throws Exception {
    39  		super.tearDown();
    40  		BasicConfigurator.resetConfiguration();
    41  	}
    42
    43  	public final void testBuildMailCDATA() throws Exception {
    44  		String classPath = "/com/ozacc/mail/test-mail6-cdata.xml";
    45
    46  		String expectedBody = "¤³¤?¤ÏCDATA¤Î¥Æ¥¥¹¥È¤Ç¤¹¡£";
    47
    48  		Mail result = builder.buildMail(classPath);
    49
    50  		assertEquals(expectedBody, result.getText());
    51  	}
    52
    53  	public final void testBuildMultipartMailFromFile() throws Exception {
    54  		String path = "src/test/com/ozacc/mail/test-mail1.xml";
    55  		File file = new File(path);
    56
    57  		String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
    58  		String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
    59  		String htmlText = "<html><body>¤³¤?¤ÏHTML¥Æ¥¥¹¥È</body></html>";
    60
    61  		InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
    62  		InternetAddress returnPath = new InternetAddress("return@example.com");
    63  		InternetAddress replyTo = new InternetAddress("reply@example.com");
    64
    65  		InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
    66  		InternetAddress to2 = new InternetAddress("to2@example.com");
    67
    68  		InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
    69  		InternetAddress cc2 = new InternetAddress("cc2@example.com");
    70
    71  		InternetAddress bcc = new InternetAddress("bcc@example.com");
    72
    73  		Mail result = builder.buildMail(file);
    74
    75  		System.out.println(result);
    76
    77  		assertEquals(subject, result.getSubject());
    78  		assertEquals(text, result.getText());
    79
    80  		assertTrue(result.isMultipartMail());
    81  		assertTrue(result.isHtmlMail());
    82  		assertEquals(htmlText, result.getHtmlText());
    83
    84  		assertEquals(from, result.getFrom());
    85  		assertEquals(returnPath, result.getReturnPath());
    86  		assertEquals(replyTo, result.getReplyTo());
    87
    88  		InternetAddress[] tos = result.getTo();
    89  		assertEquals(2, tos.length);
    90  		assertEquals(to1, tos[0]);
    91  		assertEquals(to2, tos[1]);
    92
    93  		InternetAddress[] ccs = result.getCc();
    94  		assertEquals(2, ccs.length);
    95  		assertEquals(cc1, ccs[0]);
    96  		assertEquals(cc2, ccs[1]);
    97
    98  		InternetAddress[] bccs = result.getBcc();
    99  		assertEquals(1, bccs.length);
    100 		assertEquals(bcc, bccs[0]);
    101 	}
    102
    103 	/*
    104 	 * Class under test for Mail buildMail(String)
    105 	 */
    106 	public final void testBuildMailFromClassPath() throws Exception {
    107 		String classPath = "/com/ozacc/mail/test-mail1.xml";
    108
    109 		String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
    110 		String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
    111
    112 		InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
    113 		InternetAddress returnPath = new InternetAddress("return@example.com");
    114 		InternetAddress replyTo = new InternetAddress("reply@example.com");
    115
    116 		InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
    117 		InternetAddress to2 = new InternetAddress("to2@example.com");
    118
    119 		InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
    120 		InternetAddress cc2 = new InternetAddress("cc2@example.com");
    121
    122 		InternetAddress bcc = new InternetAddress("bcc@example.com");
    123
    124 		Mail result = builder.buildMail(classPath);
    125
    126 		assertEquals(subject, result.getSubject());
    127 		assertEquals(text, result.getText());
    128
    129 		assertEquals(from, result.getFrom());
    130 		assertEquals(returnPath, result.getReturnPath());
    131 		assertEquals(replyTo, result.getReplyTo());
    132
    133 		InternetAddress[] tos = result.getTo();
    134 		assertEquals(2, tos.length);
    135 		assertEquals(to1, tos[0]);
    136 		assertEquals(to2, tos[1]);
    137
    138 		InternetAddress[] ccs = result.getCc();
    139 		assertEquals(2, ccs.length);
    140 		assertEquals(cc1, ccs[0]);
    141 		assertEquals(cc2, ccs[1]);
    142
    143 		InternetAddress[] bccs = result.getBcc();
    144 		assertEquals(1, bccs.length);
    145 		assertEquals(bcc, bccs[0]);
    146 	}
    147
    148 	/*
    149 	 * Class under test for Mail buildMail(File)
    150 	 */
    151 	public final void testBuildMailFile() throws Exception {
    152 		String path = "src/test/com/ozacc/mail/test-mail1.xml";
    153 		File file = new File(path);
    154
    155 		String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
    156 		String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
    157
    158 		InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
    159 		InternetAddress returnPath = new InternetAddress("return@example.com");
    160 		InternetAddress replyTo = new InternetAddress("reply@example.com");
    161
    162 		InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
    163 		InternetAddress to2 = new InternetAddress("to2@example.com");
    164
    165 		InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
    166 		InternetAddress cc2 = new InternetAddress("cc2@example.com");
    167
    168 		InternetAddress bcc = new InternetAddress("bcc@example.com");
    169
    170 		Mail result = builder.buildMail(file);
    171
    172 		assertEquals(subject, result.getSubject());
    173 		assertEquals(text, result.getText());
    174
    175 		assertEquals(from, result.getFrom());
    176 		assertEquals(returnPath, result.getReturnPath());
    177 		assertEquals(replyTo, result.getReplyTo());
    178
    179 		InternetAddress[] tos = result.getTo();
    180 		assertEquals(2, tos.length);
    181 		assertEquals(to1, tos[0]);
    182 		assertEquals(to2, tos[1]);
    183
    184 		InternetAddress[] ccs = result.getCc();
    185 		assertEquals(2, ccs.length);
    186 		assertEquals(cc1, ccs[0]);
    187 		assertEquals(cc2, ccs[1]);
    188
    189 		InternetAddress[] bccs = result.getBcc();
    190 		assertEquals(1, bccs.length);
    191 		assertEquals(bcc, bccs[0]);
    192 	}
    193
    194 	public void testBuildMailFromMultipleMailsTemplate() throws Exception {
    195 		String path = "src/test/com/ozacc/mail/test-mail7-multiple.xml";
    196 		File file = new File(path);
    197
    198 		Mail result1 = builder.buildMail(file, "first");
    199 		assertEquals("1ÄÌÌÜ", result1.getText());
    200
    201 		Mail result2 = builder.buildMail(file, "second");
    202 		assertEquals("2ÄÌÌÜ", result2.getText());
    203
    204 		try {
    205 			Mail result3 = builder.buildMail(file, "¥µ¡¼¥É");
    206 		} catch (MailBuildException expected) {
    207
    208 		}
    209 	}
    210 }