001: package org.contineo.core.communication.dao;
002:
003: import java.util.Collection;
004:
005: import org.contineo.core.AbstractCoreTestCase;
006: import org.contineo.core.communication.Attachment;
007: import org.contineo.core.communication.EMail;
008: import org.contineo.core.communication.Recipient;
009: import org.contineo.core.communication.dao.EMailDAO;
010:
011: public class HibernateEMailDAOTest extends AbstractCoreTestCase {
012:
013: // Instance under test
014: private EMailDAO dao;
015:
016: public HibernateEMailDAOTest(String name) {
017: super (name);
018: }
019:
020: protected void setUp() throws Exception {
021: super .setUp();
022:
023: // Retrieve the instance under test from spring context.
024: // Make sure that it is an HibernateEMailDAO
025: dao = (EMailDAO) context.getBean("EMailDAO");
026: }
027:
028: public void testStore() {
029: EMail message = new EMail();
030: message.setMessageText("text");
031: message.setAuthor("myself");
032: message
033: .setSubject("[Contineo-devel] Join Contineo developer community");
034: message.setSentDate("Tue, 28 Nov 2006 15:44:02 +0100");
035: message.setRead(1);
036: message.setAuthorAddress("a.gasparini@logicalobjects.it");
037: message.setUserName("author");
038: message.setFolder("Contineo-devel");
039: message.setAccountId(1);
040: message.setEmailId("xxx");
041:
042: // Add some recipients
043: Recipient recipient = new Recipient();
044: recipient.setAddress("m.meschieri@logicalobjects.it");
045: recipient.setName("Marco Meschieri");
046: Recipient recipient2 = new Recipient();
047: recipient2.setAddress("author@contineo.sf.net");
048: recipient2.setName("Contineo Author");
049: message.addRecipient(recipient);
050: message.addRecipient(recipient2);
051:
052: // Add some attachments
053: Attachment attachment = new Attachment();
054: attachment.setFilename("magnolia30_log4j.xml");
055: attachment.setIcon("icon");
056: attachment.setMimeType("text/xml");
057: message.addAttachment(4, attachment);
058:
059: assertTrue(dao.store(message));
060: assertEquals(20, message.getMessageId());
061:
062: message = dao.findByPrimaryKey(20);
063: assertNotNull(message);
064: assertFalse(message.getRecipients().isEmpty());
065: assertEquals(2, message.getRecipients().size());
066: assertEquals(1, message.getAttachments().size());
067:
068: // Update an already existing message
069: message = dao.findByPrimaryKey(18);
070: assertNotNull(message);
071: assertEquals("messageText", message.getMessageText());
072: message.setMessageText("xxxx");
073:
074: dao.store(message);
075: message = dao.findByPrimaryKey(18);
076: assertNotNull(message);
077: assertEquals("xxxx", message.getMessageText());
078:
079: Attachment xxx = message.getAttachments().values().iterator()
080: .next();
081: assertEquals("hibernate.log", xxx.getFilename());
082: }
083:
084: public void testDelete() {
085: assertTrue(dao.delete(18));
086: EMail message = dao.findByPrimaryKey(18);
087: assertNull(message);
088: }
089:
090: public void testFindByPrimaryKey() {
091: EMail message = dao.findByPrimaryKey(17);
092: assertNotNull(message);
093:
094: assertEquals("messageText", message.getMessageText());
095: assertEquals("Morven Macauley", message.getAuthor());
096: assertEquals("Re: maintenanc", message.getSubject());
097: assertEquals("12/14/2006 04:49 AM", message.getSentDate());
098: assertEquals("sprou@l2r9f8.varberg.net", message
099: .getAuthorAddress());
100:
101: assertNotNull(message.getAttachments());
102: assertEquals(1, message.getAttachmentCount());
103: assertNotNull(message.getAttachment(54));
104:
105: // Try with unexisting document
106: message = dao.findByPrimaryKey(99);
107: assertNull(message);
108: }
109:
110: public void testFindByUserNameString() {
111: Collection<EMail> emails = dao.findByUserName("admin");
112: assertNotNull(emails);
113: assertEquals(1, emails.size());
114: assertEquals(17, emails.iterator().next().getMessageId());
115:
116: // Try with a user without documents
117: emails = dao.findByUserName("test");
118: assertNotNull(emails);
119: assertEquals(0, emails.size());
120: }
121:
122: public void testFindByUserNameStringString() {
123: Collection<EMail> emails = dao.findByUserName("author", "Junk");
124: assertNotNull(emails);
125: assertEquals(2, emails.size());
126: assertEquals(18, emails.iterator().next().getMessageId());
127:
128: // Try with a user without documents
129: emails = dao.findByUserName("author", "Trash");
130: assertNotNull(emails);
131: assertEquals(0, emails.size());
132: }
133:
134: public void testFindByAccountId() {
135: Collection<EMail> emails = dao.findByAccountId(1);
136: assertNotNull(emails);
137: assertEquals(2, emails.size());
138: assertEquals(17, emails.iterator().next().getMessageId());
139:
140: dao.findByAccountId(2);
141: assertNotNull(emails);
142: assertEquals(2, emails.size());
143: assertEquals(17, emails.iterator().next().getMessageId());
144: }
145:
146: public void testCollectEmailIds() {
147: Collection<String> emails = dao.collectEmailIds(1);
148: assertNotNull(emails);
149: assertEquals(2, emails.size());
150: assertTrue(emails.contains("id1"));
151: assertTrue(emails.contains("id2"));
152:
153: emails = dao.collectEmailIds(2);
154: assertNotNull(emails);
155: assertEquals(1, emails.size());
156: assertTrue(emails.contains("id1"));
157: }
158: }
|