    1   package com.ozacc.mail.xml.impl;
    2
    3   import java.io.File;
    4
    5   import junit.framework.TestCase;
    6
    7   import org.apache.log4j.BasicConfigurator;
    8   import org.jdom.Document;
    9   import org.jdom.input.DOMBuilder;
    10  import org.jdom.output.XMLOutputter;
    11
    12  import com.ozacc.mail.Mail;
    13
    14  /***
    15   *
    16   * @author Tomohiro Otsuka
    17   * @version $Id: JDomXMLBuilderTest.java,v 1.2.2.1 2004/10/24 10:28:09 otsuka Exp $
    18   */
    19  public class JDomXMLBuilderTest extends TestCase {
    20
    21  	private JDomXMLBuilder builder;
    22
    23  	/***
    24  	 * @see TestCase#setUp()
    25  	 */
    26  	protected void setUp() throws Exception {
    27  		super.setUp();
    28
    29  		BasicConfigurator.configure();
    30
    31  		builder = new JDomXMLBuilder();
    32  	}
    33
    34  	/***
    35  	 * @see junit.framework.TestCase#tearDown()
    36  	 */
    37  	protected void tearDown() throws Exception {
    38  		BasicConfigurator.resetConfiguration();
    39  	}
    40
    41  	public final void testCreateDocument() throws Exception {
    42  		Mail mail = getMailForTest();
    43
    44  		org.w3c.dom.Document doc = builder.buildDocument(mail);
    45
    46  		DOMBuilder builder = new DOMBuilder();
    47  		Document jdomDoc = builder.build(doc);
    48
    49  		System.out.println(jdomDoc);
    50
    51  		XMLOutputter outputter = new XMLOutputter();
    52  		String document = outputter.outputString(jdomDoc);
    53  		System.out.println(document);
    54
    55  	}
    56
    57  	/*
    58  	 * Class under test for void saveDocument(Mail, File)
    59  	 */
    60  	public final void testSaveDocumentMailFile() throws Exception {
    61  		Mail mail = getMailForTest();
    62
    63  		String filePath = "target/test/data/mail.xml";
    64  		File file = new File(filePath);
    65  		file.getParentFile().mkdirs();
    66
    67  		builder.saveDocument(mail, file);
    68  	}
    69
    70  	public final void testSaveDocumentHtml() throws Exception {
    71  		Mail mail = getMailForTest();
    72  		mail.setHtmlText("<html><body>¥Æ¥¹¥ÈÀ®¸?</body></html>");
    73
    74  		String filePath = "target/test/data/mail-jdk-html.xml";
    75  		File file = new File(filePath);
    76  		file.getParentFile().mkdirs();
    77
    78  		builder.saveDocument(mail, file);
    79  	}
    80
    81  	/***
    82  	 * @return
    83  	 */
    84  	private Mail getMailForTest() {
    85  		String from = "from@example.com";
    86  		String fromName = "º¹½Ð¿Í";
    87  		String to = "info@example.com";
    88  		String subject = "·?Ì¾";
    89  		String text = "¥Æ¥¹¥ÈÀ®¸ù\n&<>";
    90
    91  		Mail mail = new Mail();
    92  		mail.setFrom(from, fromName);
    93  		mail.addTo(to);
    94  		mail.setSubject(subject);
    95  		mail.setText(text);
    96  		return mail;
    97  	}
    98
    99  }